Re: [networkx-discuss] splitting and merging a networkx graph

264 views
Skip to first unread message

Dan Schult

unread,
Jan 23, 2021, 11:56:12 AM1/23/21
to networkx...@googlegroups.com
It looks like you are moving the Network to a matrix, only to find a diagonal submatrix and then create a new Network from that.
Why not forego the matrix and just construct the subgraph...

sub0=G.subgraph(range(0,10))
sub1=G.subgraph(range(10,30)
H=nx.compose(sub0, sub1)  # this will look like G with edges between the two subgraphs removed.

On Fri, Jan 22, 2021 at 9:46 PM deepama...@gmail.com <deepama...@gmail.com> wrote:
Hello Everyone,

I'm trying to split a networkx graph and merge the subgraphs again in the following code

     ed_ls = [(13, 20), (13, 18), (13, 22), (13, 10), (13, 7), (20, 19), (20, 22), (20, 12), (20, 16), (18, 10), (18, 17),(10, 19), (10, 9), (10, 11), (10, 5), (10, 8), (7, 4), (7, 12), (7, 11), (7, 2), (29, 21), (21, 19), (21, 27), (21, 17), (21, 12), (21, 14), (26, 17), (17, 9), (17, 27), (19, 28), (19, 24), (28, 27), (24, 15), (25, 16),(16, 9), (16, 14), (15, 9), (3, 8), (3, 1), (3, 5), (8, 6), (8, 14), (1, 0), (1, 2), (1, 4), (1, 5), (5, 9), (5, 6), (9, 4), (4, 6), (27, 23), (23, 14), (6, 11), (6, 2), (11, 12)]
     H = nx.OrderedGraph()
     H.add_edges_from(ed_ls)
     H_adj = nx.adjacency_matrix(H, nodelist=range(30))
     m = H_adj.toarray()
     
     # Split into subgraphs by creating block matrices of adjacency matrix
     sl = [(0, 20), (10, 30)]
     blocks = [m[i, i] for i in map(lambda x: slice(*x), sl)]
     blocks_csr = list(map(csr_matrix, blocks))


Subgraphs are generated from the blocks of adjacency matrix split from the original graph's adjacency matrix.

    sub0 = nx.from_scipy_sparse_matrix(blocks_csr[0])
    nx.draw(sub0, with_labels=True)
    plt.show()
    print(sub0.nodes())

    sub1 = nx.from_scipy_sparse_matrix(blocks_csr[1])
    nx.draw(sub1, with_labels=True)
    plt.show()
    print(sub1.nodes())
    nx.relabel_nodes(G=sub1, mapping=dict(zip(sub1.nodes(), range(15,30)))) 

I'm not sure how to retain the labels of the nodes present in the actual graph, so the nodes have been relabelled after generating the second subgraph `    nx.relabel_nodes(G=sub1, mapping=dict(zip(sub1.nodes(), range(15,30)))) 
`. Following which I tried to merge the subgraphs using `compose`.
Unfortunately, when I try to merge the subgraphs stored in `sub0` and `sub1`, the original graph H isn't obtained.

    G = nx.compose(sub0, sub1)
    print(nx.is_isomorphic(H, G))

Could someone please have a look? 

Thanks a lot,
Deepa


--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/networkx-discuss/0a80d4bc-3e83-4526-bf77-597108e8bed5n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages