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


Python NetworkX gomory_hu_tree用法及代码示例


本文简要介绍 networkx.algorithms.flow.gomory_hu_tree 的用法。

用法:

gomory_hu_tree(G, capacity='capacity', flow_func=None)

返回无向图 G 的 Gomory-Hu 树。

具有容量的无向图的 Gomory-Hu 树是加权树,它表示图中所有 s-t 对的最小 s-t 割。

它只需要 n-1 最小切割计算,而不是明显的 n(n-1)/2 。该树表示所有s-t割,因为任何一对节点之间的最小割值是Gomory-Hu树中两个节点之间的最短路径中的最小边权重。

Gomory-Hu 树还具有这样的特性,即在任意两个节点之间的最短路径中移除具有最小权重的边会留下两个连通分量,这些分量形成 G 中节点的划分,定义了最小 s-t 割。

有关详细信息,请参阅下面的示例部分。

参数

GNetworkX 图

无向图

capacitystring

图 G 的边应该有一个属性容量,表示边可以支持多少流量。如果此属性不存在,则认为边具有无限容量。默认值:‘capacity’。

flow_func函数

执行底层流计算的函数。默认值 edmonds_karp() 。此函数在具有右尾度分布的稀疏图中表现更好。 shortest_augmenting_path() 在更密集的图中表现更好。

返回

TreeNetworkX 图

NetworkX 图表示输入图的 Gomory-Hu 树。

抛出

NetworkXNotImplemented

如果输入图是有向的,则引发。

NetworkXError

如果输入图是空图,则引发。

注意

该实现基于Gusfield方法[1]来计算Comory-Hu树,该方法不需要节点收缩并且具有与原始方法相同的计算复杂度。

参考

1

Gusfield D: Very simple methods for all pairs network flow analysis. SIAM J Comput 19(1):143-155, 1990.

例子

>>> G = nx.karate_club_graph()
>>> nx.set_edge_attributes(G, 1, "capacity")
>>> T = nx.gomory_hu_tree(G)
>>> # The value of the minimum cut between any pair
... # of nodes in G is the minimum edge weight in the
... # shortest path between the two nodes in the
... # Gomory-Hu tree.
... def minimum_edge_weight_in_shortest_path(T, u, v):
...     path = nx.shortest_path(T, u, v, weight="weight")
...     return min((T[u][v]["weight"], (u, v)) for (u, v) in zip(path, path[1:]))
>>> u, v = 0, 33
>>> cut_value, edge = minimum_edge_weight_in_shortest_path(T, u, v)
>>> cut_value
10
>>> nx.minimum_cut_value(G, u, v)
10
>>> # The Comory-Hu tree also has the property that removing the
... # edge with the minimum weight in the shortest path between
... # any two nodes leaves two connected components that form
... # a partition of the nodes in G that defines the minimum s-t
... # cut.
... cut_value, edge = minimum_edge_weight_in_shortest_path(T, u, v)
>>> T.remove_edge(*edge)
>>> U, V = list(nx.connected_components(T))
>>> # Thus U and V form a partition that defines a minimum cut
... # between u and v in G. You can compute the edge cut set,
... # that is, the set of edges that if removed from G will
... # disconnect u from v in G, with this information:
... cutset = set()
>>> for x, nbrs in ((n, G[n]) for n in U):
...     cutset.update((x, y) for y in nbrs if y in V)
>>> # Because we have set the capacities of all edges to 1
... # the cutset contains ten edges
... len(cutset)
10
>>> # You can use any maximum flow algorithm for the underlying
... # flow computations using the argument flow_func
... from networkx.algorithms import flow
>>> T = nx.gomory_hu_tree(G, flow_func=flow.boykov_kolmogorov)
>>> cut_value, edge = minimum_edge_weight_in_shortest_path(T, u, v)
>>> cut_value
10
>>> nx.minimum_cut_value(G, u, v, flow_func=flow.boykov_kolmogorov)
10

相关用法


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