0

I have a set of connected components obtained from a tree Tas follows.

enter image description here

To get all the connected components, I'm using the following code sample:

connectedcomponents = sorted(nx.connected_components(T), key = len,reverse=True)

This is what I'm getting;

[{66, 98, 68, 37, 7, 8, 73, 75, 44, 47, 81, 51, 19, 23, 55, 56, 58, 62, 63}, {97, 3, 6, 71, 39, 11, 77, 17, 60, 95}, {99, 5, 10, 43, 15, 20, 90, 92, 93}, {96, 4, 76, 80, 84, 53, 52}, {34, 74, 46, 18, 24, 61, 30}, {36, 9, 41, 83, 88, 57}, {65, 69, 40, 78, 21, 54}, {1, 2, 12, 13}, {89, 26, 70, 31}, {0, 42, 28, 79}, {32, 85, 86}, {59, 45, 94}, {82, 50, 22}, {64, 72}, {33, 14}, {16}, {87}, {48}, {91}, {49}, {67}, {29}, {35}, {25}, {38}, {27}]

I need to get the edges in each of these components. For an example, for the first component {66, 98, 68, 37, 7, 8, 73, 75, 44, 47, 81, 51, 19, 23, 55, 56, 58, 62, 63} I need a separate list of edges as [(37,47),(47,7),(7,62),...].

I tried it as follows:

def nodes_connected(u, v):
if u in T.neighbors(v):
    return True
else:
    return False


for i in connectedcomponents:
    print(i)
    for u,v in i:
        nodes_connected("u", "v")
        edges.append((u,v))

But didn't work!!!

Can someone please help me with this?

ccc
  • 574
  • 2
  • 9
  • 22
  • 1
    Take a look at `connected_component_subgraphs` for a good starting point – Joel May 31 '18 at 20:04
  • @Joel, I tried it as `connectedcomponents = list(sorted(nx.connected_component_subgraphs(T), key = len, reverse=True))`. but I didn't get any output. Istead, it prints something like this; `[, ,... ]` – ccc May 31 '18 at 20:55
  • 1
    It did give output. It's produced a list of graphs, each of which is one of the connected components. Try `for G in connectedcomponents: print(G.order())`. What do you have? Can you figure out the rest? – Joel May 31 '18 at 21:09
  • @Joel, thanks, I tried, but here's what I got; `22 16 15 6 6 4 3 3 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1` How can I identify the edges of each component? I need to iterate through edges and some special nodes in each component. – ccc May 31 '18 at 21:11
  • @Joel, Thanks, Joel, I got it!!! nicely done :) – ccc May 31 '18 at 21:20

0 Answers0