简体   繁体   English

networkX 中最大的弱连通分量

[英]Largest weakly connected component in networkX

I have two questions.我有两个问题。

  1. In undirected graph, I want to find the largest connected component.在无向图中,我想找到最大的连通分量。 And I read the API documents of networkX, finding this function nx.connected_component_subgraphs() .我阅读了 networkX 的 API 文档,找到了这个函数nx.connected_component_subgraphs() But I don't know how to use it, since its return value is a generator and I cant derive a subgraph of largest connected component.但我不知道如何使用它,因为它的返回值是一个生成器,我无法导出最大连通分量的子图。

  2. It is same as one.它与一个相同。 But the graph is directed.但图形是有向的。 And I want to find the largest weakly connected component of directed graph.我想找到有向图的最大弱连通分量。 Therefore, I use nx.weakly_connected_component_subgraphs() , this function.因此,我使用nx.weakly_connected_component_subgraphs()这个函数。 There has a same problem in question 1.问题 1 也有同样的问题。

How can I use the built-in function in networkX to find the largest connected component in undirected graph and the largest weakly connected component in directed graph?如何使用networkX中的内置函数找到无向图中最大的连通分量和有向图中最大的弱连通分量?

I use NetworkX 1.9.1.我使用 NetworkX 1.9.1。

The NetworkX component functions return Python generators. NetworkX组件函数返回Python生成器。 You can create a list of items in the generator using the Python list function. 您可以使用Python list函数在生成器中创建项list Here is an example showing that and also finding the largest weakly connected component. 这是一个示例,显示并找到最大的弱连接组件。

In [1]: import networkx as nx

In [2]: G = nx.DiGraph()

In [3]: G.add_path([1,2,3,4])

In [4]: G.add_path([10,11,12])

You can use eg list to turn the generator into a list of subgraphs: 您可以使用eg list将生成器转换为子图列表:

In [5]: list(nx.weakly_connected_component_subgraphs(G))
Out[5]: 
[<networkx.classes.digraph.DiGraph at 0x278bc10>,
 <networkx.classes.digraph.DiGraph at 0x278ba90>]

The max operator takes a key argument which you can set to the Python function len which calls len(g) on each subgraph to compute the number of nodes. max运算符采用一个键参数,您可以将其设置为Python函数len ,它在每个子图上调用len(g)来计算节点数。 So to get the component with the largest number of nodes you can write 因此,要获得具有最大节点数的组件,您可以编写

In [6]: largest = max(nx.weakly_connected_component_subgraphs(G),key=len)

In [7]: largest.nodes()
Out[7]: [1, 2, 3, 4]

In [8]: largest.edges()
Out[8]: [(1, 2), (2, 3), (3, 4)]

Currently, the solution offered no longer works since weakly_connected_component_subgraphs has been deprecated on networkx.目前,提供的解决方案不再有效,因为weakly_connected_component_subgraphs在networkx 上已被弃用。

TLDR: use subgraph(original_graph, weakly_connected_component_set) TLDR:使用subgraph(original_graph, weakly_connected_component_set)

Instead of using an older version of networkx, it's possible to get the largest weakly connected component by doing the following:不使用旧版本的 networkx,可以通过执行以下操作来获得最大的弱连接组件:

#create an example graph
import networkx as nx
G = nx.DiGraph()
G.add_path([2,4,6,8])
G.add_path([11,13,17])

You can collect all of the weakly connected subgraphs or you can select the maximum subgraph by doing the following:您可以收集所有弱连接子图,也可以通过执行以下操作来选择最大子图:

#list of all weakly connected subgraphs, each one is a set. 
#If you don't list them, you'll get a generator.
list_of_subgraphs = list(nx.weakly_connected_component_subgraphs(G))

list_of_digraphs = []

for subgraph in list_of_subgraphs:
    list_of_digraphs.append(nx.subgraph(G, subgraph)) #this is the relevant part.

Each one of those subgraphs will now be an nx digraph stored in list_of_digraphs .这些子图中的每一个现在都将是一个存储在list_of_digraphs的 nx 有向图。

If you want the max weakly connected subgraph,如果你想要最大弱连通子图,

max_wcc = max(nx.weakly_connected_component_subgraphs(G), key=len)
max_wcc = subgraph(G, max_wcc)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM