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


Python NetworkX EdgeComponentAuxGraph用法及代码示例

本文简要介绍 networkx.algorithms.connectivity.edge_kcomponents.EdgeComponentAuxGraph 的用法。

用法:

class EdgeComponentAuxGraph

在图中查找所有k-edge-connected 组件的简单算法。

构造AuxillaryGraph(可能需要一些时间)允许在任意k 的线性时间内找到k-edge-ccs。

注意

该实现基于[1]。这个想法是构建一个辅助图,可以在线性时间内提取k-edge-ccs。辅助图是在 操作中构造的,其中F是最大流的复杂度。查询组件需要额外的 操作。该算法对于大型图来说可能很慢,但它可以处理任意 k 并且适用于有向和无向输入。

k=1 的无向情况是完全连通的分量。 k=2 的无向情况正好是桥接组件。 k=1 的有向情况是完全强连通分量。

参考

1

Wang, Tianhao, et al. (2015) A simple algorithm for finding all k-edge-connected components. http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0136264

例子

>>> import itertools as it
>>> from networkx.utils import pairwise
>>> from networkx.algorithms.connectivity import EdgeComponentAuxGraph
>>> # Build an interesting graph with multiple levels of k-edge-ccs
>>> paths = [
...     (1, 2, 3, 4, 1, 3, 4, 2),  # a 3-edge-cc (a 4 clique)
...     (5, 6, 7, 5),  # a 2-edge-cc (a 3 clique)
...     (1, 5),  # combine first two ccs into a 1-edge-cc
...     (0,),  # add an additional disconnected 1-edge-cc
... ]
>>> G = nx.Graph()
>>> G.add_nodes_from(it.chain(*paths))
>>> G.add_edges_from(it.chain(*[pairwise(path) for path in paths]))
>>> # Constructing the AuxGraph takes about O(n ** 4)
>>> aux_graph = EdgeComponentAuxGraph.construct(G)
>>> # Once constructed, querying takes O(n)
>>> sorted(map(sorted, aux_graph.k_edge_components(k=1)))
[[0], [1, 2, 3, 4, 5, 6, 7]]
>>> sorted(map(sorted, aux_graph.k_edge_components(k=2)))
[[0], [1, 2, 3, 4], [5, 6, 7]]
>>> sorted(map(sorted, aux_graph.k_edge_components(k=3)))
[[0], [1, 2, 3, 4], [5], [6], [7]]
>>> sorted(map(sorted, aux_graph.k_edge_components(k=4)))
[[0], [1], [2], [3], [4], [5], [6], [7]]

辅助图主要用于k-edge-ccs,但它也可以通过优化搜索空间来加速k-edge-subgraphs的查询。

>>> import itertools as it
>>> from networkx.utils import pairwise
>>> from networkx.algorithms.connectivity import EdgeComponentAuxGraph
>>> paths = [
...     (1, 2, 4, 3, 1, 4),
... ]
>>> G = nx.Graph()
>>> G.add_nodes_from(it.chain(*paths))
>>> G.add_edges_from(it.chain(*[pairwise(path) for path in paths]))
>>> aux_graph = EdgeComponentAuxGraph.construct(G)
>>> sorted(map(sorted, aux_graph.k_edge_subgraphs(k=3)))
[[1], [2], [3], [4]]
>>> sorted(map(sorted, aux_graph.k_edge_components(k=3)))
[[1, 4], [2], [3]]

相关用法


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