pajamas
pajamas

Reputation: 1266

How to get largest (unconnected) network / cluster using networkx in Python?

I have the following example data:

my_network_data = [(39, 118), (179, 14), (35, 118), (225, 14), (64, 118), (6, 14), (187, 14), (161, 14), (42, 14), (53, 14), (47, 1), (127, 14), (14, 118), (3, 1), (175, 14), (21, 118), (5, 14), (18, 14), (122, 1), (137, 14), (157, 14), (19, 14), (19, 118), (118, 14), (30, 118), (159, 14), (124, 118), (56, 14), (161, 118), (100, 14), (53, 118), (136, 118), (41, 14), (4, 14), (217, 14), (32, 14), (175, 118), (104, 14), (82, 118), (4, 118), (222, 14), (201, 118), (136, 14), (86, 1), (153, 14), (195, 14)]

If I plot it using networkx, I do the following:

import networkx as nx
g = nx.Graph()
g.add_edges_from(my_network_data)   
print(nx.info(g))

Output:

Name: 
Type: Graph
Number of nodes: 41
Number of edges: 45
Average degree:   2.1951

The graph looks like this:

nx.draw(g, with_labels=True)

enter image description here

How do I get the information using networkx that there are 2 distinctive clusters and which items are in those clusters?

Suggested output :

[[1, 86, 47, 3, 122], [14, 118, 136, 53, 179, 30, 100, 41, 35, 4, 19, 82, 104, 159, 64, 32, 124, 14, 39, 4, 137, 136, 187, 217, 153, 5, 53, 19, 42, 175, 18, 21, 222, 175, 6, 195, 56, 157, 201, 161, 161, 127,  225]]

I'm not sure if networkx is the best possible library for this task. If you have a better suggestion (using Python), I'm open to using it.

Upvotes: 0

Views: 755

Answers (1)

AKX
AKX

Reputation: 169338

Looks like nx.connected_components() is what you need:

for c in nx.connected_components(g):
    print(c)

{4, 5, 6, 136, 137, 14, 18, 19, 21, 153, 157, 30, 159, 32, 161, 35, 39, 41, 42, 175, 179, 53, 56, 187, 64, 195, 201, 82, 217, 222, 225, 100, 104, 118, 124, 127}
{1, 3, 47, 86, 122}

Upvotes: 1

Related Questions