1

I've been busting my head on this one, searching through the other topics but I couldn't find anything useful. So I have a randomly generated graph with 15 nodes and I need to find connected components without using NetworkX's function nx.connected_components in python. Finding them is kind of easy in my case I've used the following:

for n in g.nodes(): 
   print n, g.neighbors(n)

So I get an an output like this: 1 [4,6,7]. My question is how to group each node and its neighbours so I get a list of groups of nodes that are connected or have no connection. Something like this: lis=[[1,4,6,7],[2,3],[5],[8,9,10,11],[0,12,13],[14]]. The code should work for a randomly created graph of any size.

BLaZZeD
  • 11
  • 1
  • 3

0 Answers0