为方法创建循环,该方法将提取所有具有三角关系的子图

时间:2019-03-05 09:20:27

标签: python networking graph networkx subgraph

我正在尝试创建一个for循环,该循环将在python中针对图G中将返回子图的集合/列表的所有节点循环使用此方法。

H = G.subgraph(nodes_in_triangle(G,n))

谢谢。

1 个答案:

答案 0 :(得分:0)

要查找图形中的所有三角形,可以使用函数enumerate_all_cliques(),该函数返回图形中的所有组。您可以按集团中的节点数过滤掉所有三角形。

import networkx as nx

G = nx.house_x_graph()

%matplotlib inline # jupyter notebook
nx.draw(G, with_labels = True, node_color='pink', node_size=1000)

tri = filter(lambda x: len(x) == 3, nx.enumerate_all_cliques(G))
tri_subraphs = [G.subgraph(nodes) for nodes in tri]

for graph in tri_subraphs:
    print(graph.nodes())

输出:

[0, 1, 2]
[0, 1, 3]
[0, 2, 3]
[1, 2, 3]
[2, 3, 4]

enter image description here