简体   繁体   English

完全连接来自networkx中较大图形的子图

[英]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.

相关问题 从 NetworkX 图中查找连接组件内的子图 - Find Subgraphs inside a Connected Component from a NetworkX Graph 继承networkx图并使用nx.connected_component_subgraphs - inheriting networkx Graph and using nx.connected_component_subgraphs 如何在NetworkX中仅绘制前50-100个连接的组件子图; 一次绘制多个子图 - How to graph only top 50-100 connected component subgraphs in NetworkX; drawing multiple subgraphs at once 如何使用networkx从给定图形中提取所有可能的诱导子图 - How can I extract all possible induced subgraphs from a given graph with networkx AttributeError:模块“networkx”没有属性“connected_component_subgraphs” - AttributeError: module 'networkx' has no attribute 'connected_component_subgraphs' 如何使用.networkx 查找强连通分量的子图 - How to find subgraphs of strongly connected components using networkx Networkx - 从检测到的社区生成的子图的熵 - Networkx - entropy of subgraphs generated from detected communities 获取 NetworkX 图中的连接节点 - Fetch connected nodes in a NetworkX graph 如何在networkx中创建连接图 - How to create a connected graph in networkx 将图拆分为&lt;= N个节点的断开连接的子图的算法,挑战是获得最大数量的子图(networkx,python) - Algorithm to split a graph into disconnected subgraphs of <=N nodes, challenge is to obtain the maximum number of subgraphs (networkx, python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM