[英]completely connected subgraphs from a larger graph in networkx
I have tried not to repost here, but I think my request is very simple and I am just inexperienced with network graphs. 我试着不要在这里重新发布,但我认为我的请求非常简单,我对网络图表缺乏经验。 When using the networkx module in python, I would like to recover, from a connected graph, the subgraphs where all nodes are connected to each other (where the number of nodes is greater than 2). 在python中使用networkx模块时,我想从连接图中恢复所有节点相互连接的子图(节点数大于2的子图)。 Is there a simple way to do this? 有一个简单的方法吗?
Here is my example: 这是我的例子:
A simple graph with seven nodes. 一个包含七个节点的简单图表。 Nodes 1,2,3 are shared connections, nodes 1,2,4 all share connections, and nodes 5,6,7 all share connections. 节点1,2,3是共享连接,节点1,2,4都共享连接,节点5,6,7都共享连接。
import networkx as nx
G=nx.Graph() #Make the graph
G.add_nodes_from([1,2,3,4,5,6,7]) #Add nodes, although redundant because of the line below
G.add_edges_from([(1,2),(1,3),(2,3),(1,4),(2,4),(1,5),(5,6),(5,7),(6,7)]) # Adding the edges
My desired output would be: ([1,2,3],[1,2,4],[5,6,7]) 我想要的输出是:([1,2,3],[1,2,4],[5,6,7])
I can think of slightly laborious methods for writing this but was wondering if there was a simple inbuilt function for it. 我可以想到写一些稍微费力的方法,但是想知道是否有一个简单的内置函数。
It sounds like you want to discover the cliques in your graph. 听起来您想要发现图表中的派系。 For this you could use nx.clique.find_cliques()
: 为此,您可以使用nx.clique.find_cliques()
:
>>> list(nx.clique.find_cliques(G))
[[1, 2, 3], [1, 2, 4], [1, 5], [6, 5, 7]]
nx.clique.find_cliques()
returns a generator which will yield all cliques in the graph. nx.clique.find_cliques()
返回一个生成器,它将生成图中的所有派系。 You can filter out the cliques with fewer than three nodes using list comprehension: 您可以使用列表推导过滤掉少于三个节点的派系:
>>> [g for g in nx.clique.find_cliques(G) if len(g) > 2]
[[1, 2, 3], [1, 2, 4], [6, 5, 7]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.