当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python NetworkX strongly_connected_components用法及代码示例


本文简要介绍 networkx.algorithms.components.strongly_connected_components 的用法。

用法:

strongly_connected_components(G)

在图的强连通分量中生成节点。

参数

GNetworkX 图表

有向图。

返回

comp集合生成器

一组节点的生成器,一个用于 G 的每个强连通分量。

抛出

NetworkXNotImplemented

如果 G 是无向的。

注意

使用 Tarjan 的算法[R827335e01166-1]_ 和 Nuutila 的修改[R827335e01166-2]_。算法的非递归版本。

参考

1

Depth-first search and linear graph algorithms, R. Tarjan SIAM Journal of Computing 1(2):146-160, (1972).

2

On finding the strongly connected components in a directed graph. E. Nuutila and E. Soisalon-Soinen Information Processing Letters 49(1): 9-14, (1994)..

例子

生成强连接组件的排序列表,最大的在前。

>>> G = nx.cycle_graph(4, create_using=nx.DiGraph())
>>> nx.add_cycle(G, [10, 11, 12])
>>> [
...     len(c)
...     for c in sorted(nx.strongly_connected_components(G), key=len, reverse=True)
... ]
[4, 3]

如果你只想要最大的组件,使用 max 而不是 sort 更有效。

>>> largest = max(nx.strongly_connected_components(G), key=len)

相关用法


注:本文由纯净天空筛选整理自networkx.org大神的英文原创作品 networkx.algorithms.components.strongly_connected_components。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。