识别连接组件中的边 - Python

时间:2018-05-31 19:54:34

标签: python-3.x tree networkx connected-components

我有一组从树T获得的连通组件,如下所示。

enter image description here

要获取所有连接的组件,请使用以下代码示例:

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

这就是我得到的;

[{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}]

我需要获得每个组件的边缘。例如,对于第一个组件{66, 98, 68, 37, 7, 8, 73, 75, 44, 47, 81, 51, 19, 23, 55, 56, 58, 62, 63},我需要一个单独的边列表[(37,47),(47,7),(7,62),...]

我尝试了如下:

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))

但没有工作!

有人可以帮我这个吗?

0 个答案:

没有答案