Ignore non-connected nodes in NetworkX Graph
Question:
I have a network diagram where a node is connected to another node which in-turn is connected to another node, worked through a logic to ignore when there is only one neighbour but that is still leaving few bits back. The attached diagram does a better job explaining it
I need to only keep where a red-node is connected to a blue-node or another red-node through a green node. Data used
for u, v in CA_new.edges:
if len(list(CA_new.neighbors(v))) == 1 and len(list(CA_new.neighbors(u))) == 1:
removeNodeslist.append(v)
removeNodeslist.append(u)
else:
keepNodeslist.append(v)
keepNodeslist.append(u)
CA_new.remove_nodes_from(removeNodeslist)
Answers:
From your comments, it sounds like you’re looking for the connected components that contain at least two parent nodes, and at least one of those parents has a colored edge. With that in mind, you can do something like the following:
import numpy as np
import networkx as nx
import pandas as pd
data = {
'Parent': list("EEABEHDILGKCDBLLFBCCJ"),
'Child': ["X1","X2","Y1","Y1","Y1","M1","N3","N4","N5","N7","N8","M1","M2",
"M3","M4","M5","M6","M7","M8","M9","P7"],
'Colour': list("NNNNYYNYNNNYNNNNYNNNN")
}
df = pd.DataFrame(data)
G = nx.from_pandas_edgelist(df, source = 'Parent', target = 'Child')
parent_set = set(df['Parent'])
colored_parent_set = set(df.loc[df['Colour']=='Y','Parent'])
node_set = set()
for comp in nx.connected_components(G):
if (len(comp & parent_set) >= 2 and
comp & colored_parent_set):
node_set |= comp
H = G.subgraph(node_set)
colors = ['limegreen']*len(H)
for i,n in enumerate(H.nodes):
if n in colored_parent_set:
colors[i] = "red"
elif n in parent_set:
colors[i] = "deepskyblue"
nx.draw(H, node_color = colors, with_labels = True)
Here’s the result that I get:
I have a network diagram where a node is connected to another node which in-turn is connected to another node, worked through a logic to ignore when there is only one neighbour but that is still leaving few bits back. The attached diagram does a better job explaining it
I need to only keep where a red-node is connected to a blue-node or another red-node through a green node. Data used
for u, v in CA_new.edges:
if len(list(CA_new.neighbors(v))) == 1 and len(list(CA_new.neighbors(u))) == 1:
removeNodeslist.append(v)
removeNodeslist.append(u)
else:
keepNodeslist.append(v)
keepNodeslist.append(u)
CA_new.remove_nodes_from(removeNodeslist)
From your comments, it sounds like you’re looking for the connected components that contain at least two parent nodes, and at least one of those parents has a colored edge. With that in mind, you can do something like the following:
import numpy as np
import networkx as nx
import pandas as pd
data = {
'Parent': list("EEABEHDILGKCDBLLFBCCJ"),
'Child': ["X1","X2","Y1","Y1","Y1","M1","N3","N4","N5","N7","N8","M1","M2",
"M3","M4","M5","M6","M7","M8","M9","P7"],
'Colour': list("NNNNYYNYNNNYNNNNYNNNN")
}
df = pd.DataFrame(data)
G = nx.from_pandas_edgelist(df, source = 'Parent', target = 'Child')
parent_set = set(df['Parent'])
colored_parent_set = set(df.loc[df['Colour']=='Y','Parent'])
node_set = set()
for comp in nx.connected_components(G):
if (len(comp & parent_set) >= 2 and
comp & colored_parent_set):
node_set |= comp
H = G.subgraph(node_set)
colors = ['limegreen']*len(H)
for i,n in enumerate(H.nodes):
if n in colored_parent_set:
colors[i] = "red"
elif n in parent_set:
colors[i] = "deepskyblue"
nx.draw(H, node_color = colors, with_labels = True)
Here’s the result that I get: