I have a very large undirected network loaded into a NetworkX graph()
that is composed of many disconnected components. I also have a set of nodes of interest loaded into a set. I would like to look through all of the extract all of the components have contain at least one of the nodes of interest.
# create empty graph
g = nx.Graph()
# add edges to the graph
g.add_edges_from([['a','b'],['a','c'],['b','c'],['d','e'],['e','f'],['d','f'],['g','h'],['g','i'],['h','i']])
# load nodes of interest into a set
interest_nodes = set(['a', 'b', 'f'])
# number of connected components
nx.number_connected_components(g)
# loop through each connected component and add all of the edges for that component to a list if a node in that component is found in the interest_nodes
interest_edges = []
for i in nx.connected_component_subgraph(g):
for u in i.edges():
if u in interest_nodes:
interest_edges.append(u)
However, I get back an empty list.
Ideally, I would want back a list with all of the edges in any connected component that contains at least one of the nodes in the interest_nodes
set. What I should get back below, but instead I don't get back anything.
interest_edges = [('a', 'c'),
('a', 'b'),
('c', 'b'),
('e', 'd'),
('e', 'f'),
('d', 'f')]
You are close. The simplest way is to check each component to see if the node sets overlap by checking the length of the set intersection.
import networkx as nx
g = nx.Graph([['a','b'],['a','c'],['b','c'],['d','e'],['e','f'],['d','f'],['g','h'],['g','i'],['h','i']])
interest_nodes = set(['a', 'b', 'f'])
interest_edges = []
for component in nx.connected_component_subgraphs(g):
if len(set(component) & interest_nodes) > 0:
interest_edges.extend(component.edges())
print interest_edges
# output
# [('a', 'c'), ('a', 'b'), ('c', 'b'), ('e', 'd'), ('e', 'f'), ('d', 'f')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With