简体   繁体   English

如何在.networkx 中找到一个图的所有连通子图?

[英]How to find all connected subgraph of a graph in networkx?

I'm developing a python application, and i want to list all possible connected subgraph of any size and starting from every node using NetworkX.我正在开发一个 python 应用程序,我想列出所有可能的任意大小的连接子图,并从使用 NetworkX 的每个节点开始。

I just tried using combinations() from itertools library to find all possible combination of nodes but it is very too slow because it searchs also for not connected nodes:我只是尝试使用 itertools 库中的 combinations() 来查找所有可能的节点组合,但速度太慢,因为它还会搜索未连接的节点:

for r in range(0,NumberOfNodes)
for SG in (G.subgraph(s) for s in combinations(G,r):
    if (nx.is_connected(SG)):
        nx.draw(SG,with_labels=True)
        plt.show()

The actual output is correct.实际的 output 是正确的。 But i need another way faster to do this, because all combinations of nodes with a graph of 50 nodes and 8 as LenghtTupleToFind are up to 1 billion (n. / r, / (nr)?) but only a minimal part of them are connected subgraph so are what i am interested in. So, it's possible to have a function for do this?但是我需要另一种更快的方法来做到这一点,因为节点的所有组合与 50 个节点和 8 个节点的图作为 LenghtTupleToFind 高达 10 亿(n。/ r,/(nr)?)但其中只有一小部分是连接的子图也是我感兴趣的。所以,有可能有一个 function 来做这个吗?

Sorry for my english, thank you in advance对不起我的英语,提前谢谢你

EDIT :编辑

As an example:举个例子:

起始图示例

so, the results i would like to have:所以,我想要的结果是:

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

and all combination that generates a connected graph以及生成连通图的所有组合

I had the same requirements and ended up using this code, super close to what you were doing.我有相同的要求,最终使用了这段代码,非常接近你正在做的事情。 This code yields exactly the input you asked for.这段代码产生的正是您要求的输入。

import networkx as nx
import itertools

G = you_graph
all_connected_subgraphs = []

# here we ask for all connected subgraphs that have at least 2 nodes AND have less nodes than the input graph
for nb_nodes in range(2, G.number_of_nodes()):
    for SG in (G.subgraph(selected_nodes) for selected_nodes in itertools.combinations(G, nb_nodes)):
        if nx.is_connected(SG):
            print(SG.nodes)
            all_connected_subgraphs.append(SG)

You can find all the connected components in O(n) time and memory complexity.您可以在 O(n) 时间和内存复杂度中找到所有连接的组件。 Keep a seen boolean array, and run Depth First Search (DFS) or Bread First Search (BFS), to find the connected components.保留一个可见的布尔数组,并运行深度优先搜索 (DFS) 或面包优先搜索 (BFS),以查找连接的组件。
In my code, I used DFS to find the connected components.在我的代码中,我使用 DFS 来查找连接的组件。

seen = [False] * num_nodes
def search(node):
    component.append(node)
    seen[node] = True
    for neigh in G.neighbors(node):
        if not seen[neigh]:
            dfs(neigh)

all_subgraphs = []    

# Assuming nodes are numbered 0, 1, ..., num_nodes - 1
for node in range(num_nodes):
    component = []
    dfs(node)
    # Here `component` contains nodes in a connected component of G
    plot_graph(component)  # or do anything
    all_subgraphs.append(component)

I have modified Charly Empereur-mot's answer by using ego graph to make it faster:我通过使用自我图修改了 Charly Empereur-mot 的答案以使其更快:

import networkx as nx
import itertools

G = you_graph.copy()
all_connected_subgraphs = []

# here we ask for all connected subgraphs that have nb_nodes
for n in you_graph.nodes():
    egoG = nx.generators.ego_graph(G,n,radius=nb_nodes-1)
    for SG in (G.subgraph(sn+(n,) for sn in itertools.combinations(egoG, nb_nodes-1)):
        if nx.is_connected(SG):
            all_connected_subgraphs.append(SG)
    G.remove_node(n)

You might want to look into connected_components function. It will return you all connected nodes, which you can then filter by size and node.您可能想查看connected_components function。它将返回所有连接的节点,然后您可以按大小和节点过滤这些节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何测试一个图是否是 Networkx 中另一个图的子图? - How to test if one graph is a subgraph of another in Networkx? Python NetworkX从节点作为根在有向图中找到子图 - Python NetworkX find a subgraph in a Directed Graph from a node as root 如何在networkx中创建连接图 - How to create a connected graph in networkx 如何将 a.networkx 多部分图的 plot 子图作为具有刷新位置的新图 - How to plot subgraph of a networkx multipartite graph as a new graph with refreshed positions 使用networkx计算加权无向图中连接到一个节点的每个子图中的节点和边数 - Count the number of nodes and edges in each subgraph connected to one node in a weighted undirected graph with networkx 连接到子图网络的节点或节点集x - Nodes or set of nodes connected to a subgraph networkx 检查networkx图是否是另一个的子图 - Check whether a networkx graph is a subgraph of another one 如何使用networkx绘制子图 - How to draw subgraph using networkx 用于化学的 NetworkX:如何检查较小的分子图 A 是否是较大分子图 B 的有效子图? - NetworkX for chemistry: how to check if a smaller molecular graph A is a valid subgraph of a larger molecular graph B? 如何使用python的networkx模块从节点列表生成完全连接的子图 - How to generate a fully connected subgraph from node list using python's networkx module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM