Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetworkX - Finding the natural clusters of points on a graph

Still kinda new to NetworkX here, but I wanted to be able to query a NetworkX graph to find all nodes within a cluster of nodes. Here is an example graph I had generated: Network of Nodes

As you can see, there are clusters of nodes. Within each cluster, every node is connected to every other node. You can see that in a zoom-up of five such clusters below: Zoom in on 5 clusters

I'd like to be able to find out how I can extract out each individual cluster of nodes. Each node is named with a long name ("A/Vietnam/2009/8/431", for example), and I know how to fish out an individual node using NetworkX's functions, but I don't know how to get all connected nodes within that cluster. Language of preference is Python (2.7), and I have been using the NetworkX package in conjunction.

Thanks everybody!

like image 572
ericmjl Avatar asked Jul 03 '13 14:07

ericmjl


1 Answers

     import networkx as nx

     #G is the networkx graph 
     sub_graphs = nx.connected_component_subgraphs(G)

     #n gives the number of sub graphs
     n = len(sub_graphs)

     # you can now loop through all nodes in each sub graph
     for i in range(n):
         print "Subgraph:", i, "consists of ",sub_graphs[i].nodes()
like image 102
Vikram Avatar answered Sep 28 '22 13:09

Vikram