I am trying to extract from a big graph the sub-graph of all connected nodes containing a specific node.
Is there a solution in the Networkx library?
[EDIT]
My graph is a DiGraph
[EDIT]
Rephrased simply:
I want the part of my graph that contain my specific node N_i and and all the nodes that are connected directly or indirectly (passing by other nodes) using any incoming or outcoming edges.
Example:
>>> g = nx.DiGraph()
>>> g.add_path(['A','B','C',])
>>> g.add_path(['X','Y','Z',])
>>> g.edges()
[('A', 'B'), ('B', 'C'), ('Y', 'Z'), ('X', 'Y')]
My desired result would be:
>>> g2 = getSubGraph(g, 'B')
>>> g2.nodes()
['A', 'B', 'C']
>>> g2.edges()
[('A', 'B'), ('B', 'C')]
A strongly connected component (SCC) of a directed graph is a maximal strongly connected subgraph. For example, there are 3 SCCs in the following graph. We can find all strongly connected components in O(V+E) time using Kosaraju's algorithm.
Test directed graph for strong connectivity. A directed graph is strongly connected if and only if every vertex in the graph is reachable from every other vertex. Parameters: GNetworkX Graph.
One way to check whether two nodes are connected with NetworkX is to check whether a node u is a neighbor of another node v .
You can use shortest_path() to find all of the nodes reachable from a given node. In your case you need to first convert the graph to an undirected representation so both in- and out-edges are followed.
In [1]: import networkx as nx
In [2]: >>> g = nx.DiGraph()
In [3]: >>> g.add_path(['A','B','C',])
In [4]: >>> g.add_path(['X','Y','Z',])
In [5]: u = g.to_undirected()
In [6]: nodes = nx.shortest_path(u,'B').keys()
In [7]: nodes
Out[7]: ['A', 'C', 'B']
In [8]: s = g.subgraph(nodes)
In [9]: s.edges()
Out[9]: [('A', 'B'), ('B', 'C')]
Or in one line
In [10]: s = g.subgraph(nx.shortest_path(g.to_undirected(),'B'))
In [11]: s.edges()
Out[11]: [('A', 'B'), ('B', 'C')]
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