简体   繁体   中英

extracting connected nodes from weighted undirected graph based on the weight of the edges

Is there a way to extract connected nodes from a weighted undirected networkx graph based on some weight threshold? For example get lists of connected nodes, where weight > 0.5.

So basically something like this for weighted undirected graphs: http://networkx.lanl.gov/reference/generated/networkx.algorithms.components.connected.connected_components.html#networkx.algorithms.components.connected.connected_components

You can run the connected components algorithm that you mention. But first either create a new graph with only the edges you want, or remove the edges you don't want from the original graph. For example

In [1]: import networkx as nx

In [2]: G = nx.Graph()

In [3]: G.add_edge(1,2,weight=1)

In [4]: G.add_edge(2,3,weight=0.25)

In [5]: H = nx.Graph([(u,v,d) for (u,v,d) in  G.edges(data=True) if d['weight']>0.5])

In [6]: H.edges()
Out[6]: [(1, 2)]

In [7]: G.remove_edges_from([(u,v,d) for (u,v,d) in  G.edges(data=True) if d['weight']<0.5])

In [8]: G.edges()
Out[8]: [(1, 2)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM