ó žĆŅYc@spdZddlZddlmZddlmZddlmZeZ dgZ edƒd dd „ƒZ dS( s& Gomory-Hu tree of undirected Graphs. i’’’’N(tnot_implemented_fori(t edmonds_karp(tbuild_residual_networktgomory_hu_treetdirectedtcapacityc s‘|dkrt}nt|ƒdkr?d}tj|ƒ‚ni}i‰t|ƒ}t|ƒ}x|D]}|||>> 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 Notes ----- This implementation is based on Gusfield approach [1]_ to compute Comory-Hu trees, which does not require node contractions and has the same computational complexity than the original method. See also -------- :func:`minimum_cut` :func:`maximum_flow` References ---------- .. [1] Gusfield D: Very simple methods for all pairs network flow analysis. SIAM J Comput 19(1):143-155, 1990. is9Empty Graph does not have a Gomory-Hu tree representationRt flow_functresidualc3s.|]$\}}||ˆ||ffVqdS(N((t.0tutv(tlabels(sx/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/gomory_hu.pys ³sN(tNonetdefault_flow_functlentnxt NetworkXErrortitertnextRt minimum_cuttgettGraphtadd_nodes_fromtadd_weighted_edges_fromtitems(tGRRtmsgttreet iter_nodestroottntRtsourcettargett cut_valuet partitiontnodetT((R sx/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/gomory_hu.pyRs4u        ( -  #( t__doc__tnetworkxRtnetworkx.utilsRt edmondskarpRtutilsRR t__all__R R(((sx/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/gomory_hu.pyts