Detecting Clusters in Graphs using NetworkX

Clusters of all connected nodes inside a graph is commonly known as “connected components”

Let’s recreate the above graph in NetworkX:

import networkx as nx

G = nx.Graph()

G.add_nodes_from(["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N"])

G.add_edges_from([("B", "A"), ("E", "F"), ("E", "D"), ("G", "H"), ("I", "H"), ("J", "I"), ("J", "K"), ("M", "L")])

We then call the connected_components function of NetworkX.

list(nx.connected_components(G))

Which will give the list of all clusters or connected components of this graph:

[{'A', 'B'},
 {'C'},
 {'D', 'E', 'F'},
 {'G', 'H', 'I', 'J', 'K'},
 {'L', 'M'},
 {'N'}]