Reputation: 47
I have a networkx graph with many nodes and edges. Some of the nodes share only one edge with another node. How do I extract a list of the isolated 2 node graphs?
The table of data I am working with looks like this
>|Name1|Name2|distance|
>|----|----|----|
>|AAA|BBB|2.315544|
>|AAB|BBB|2.576293|
>|AAC|BBB|2.967239|
>|AAD|BBB|2.779942|
>|...|...|...|
The graph looks like this:
Alternatively, is there a way to generate a list of clusters?
Upvotes: 0
Views: 280
Reputation: 13021
Your "clusters" are typically called components in network analysis.
In networkx, you can compute them using the function nx.connected_components
, which returns an iterator over components. Each component returned by the iterator simply a list of nodes.
import networkx as nx
g = nx.Graph()
... # add nodes & edges
components = nx.connected_components(g)
for component in components:
if len(components) == 2:
# do something special with components of size 2
If you are only interested in the largest component (often called "giant component"), it is worth noting that you can then sort the components or find the largest group by post-processing this output as detailed in the documentation:
Generate a sorted list of connected components, largest first.
>>> G = nx.path_graph(4)
>>> nx.add_path(G, [10, 11, 12])
>>> [len(c) for c in sorted(nx.connected_components(G), key=len, reverse=True)]
[4, 3]
If you only want the largest connected component, it's more
efficient to use max instead of sort.
>>> largest_cc = max(nx.connected_components(G), key=len)
Upvotes: 1