0

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:

enter image description here

Alternatively, is there a way to generate a list of clusters?

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you! This is not my area of expertise and I also appreciate you pointing out the right vocabulary.
Did you mean nx.connected_components(g)? It doesn't yield the connected components in order of size though...
@mozway It does with networkx version 2.5.1.
@Paul can you provide a link to the doc of nx.components? I'm unable to reproduce your code, for me its the module and not a callable. With connected_components on networkx 2.8.6 this yields the component in the found order, not by size: list(nx.connected_components(nx.Graph([('A', 'B'), ('C', 'D'), ('D', 'E')]))) -> [{'A', 'B'}, {'C', 'D', 'E'}]
@mozway Sorry, I should have been more explicit. You were correct that the function is called connected_components. I was correct that the returned result is ordered (at least in the version I am working with).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.