user13476360
user13476360

Reputation: 15

How to find edges between nodes more efficiently?

I have an undirected Graph G=(V,E) and its adjacency matrix x in DataFrame format and a list of nodes NODE subset of V. I want to find out all the edges b/w the nodes of NODE.

I have tried the following code. Is there any other efficient way of doing it in terms of time complexity? Please help me.

    import networkx as nx
    import random

    G=nx.erdos_renyi_graph(100000,0.4)
    x =nx.to_pandas_adjacency(G)
    NODE=[]
    [NODE.append(random.randint(1,99999)) for i in range(1000)]
    edges=[]
    [edges.append((NODE[i],NODE[j])) for i in range(len(NODE)-1) for j in range(i+1,len(NODE)) if(x.loc[NODE[i],NODE[j]]==1)]


    print(edges)

Upvotes: 0

Views: 203

Answers (1)

Joel
Joel

Reputation: 23827

If you want it as a list, then a reasonably efficient (and simple to interpret) code is:

edges = list(G.subgraph(NODE).edges())

Upvotes: 1

Related Questions