ó žÃÒYc@ sAdZddlmZddlZddlmZddlZddlm Z ddlm Z ddlm Z ddlm Z dd lm Z e Zd d lmZmZd jd gƒZddddddgZddddd„Zdddd„Zdd„Zddd„Zddddd„Zdddd„ZdS(s$ Flow based connectivity algorithms iÿÿÿÿ(tdivisionN(t itemgetter(tboykov_kolmogorov(tdinitz(t edmonds_karp(tshortest_augmenting_path(tbuild_residual_networki(t!build_auxiliary_node_connectivityt!build_auxiliary_edge_connectivitys s%Jordi Torrents taverage_node_connectivitytlocal_node_connectivitytnode_connectivitytlocal_edge_connectivitytedge_connectivitytall_pairs_node_connectivityc C s|d krt}n|d kr0t|ƒ}n|}|jjdd ƒ}|d kritjdƒ‚ntd|d|ƒ} |tkr¡|| d>> from networkx.algorithms.connectivity import local_node_connectivity We use in this example the platonic icosahedral graph, which has node connectivity 5. >>> G = nx.icosahedral_graph() >>> local_node_connectivity(G, 0, 6) 5 If you need to compute local connectivity on several pairs of nodes in the same graph, it is recommended that you reuse the data structures that NetworkX uses in the computation: the auxiliary digraph for node connectivity, and the residual network for the underlying maximum flow computation. Example of how to compute local node connectivity among all pairs of nodes of the platonic icosahedral graph reusing the data structures. >>> import itertools >>> # You also have to explicitly import the function for >>> # building the auxiliary digraph from the connectivity package >>> from networkx.algorithms.connectivity import ( ... build_auxiliary_node_connectivity) ... >>> H = build_auxiliary_node_connectivity(G) >>> # And the function for building the residual network from the >>> # flow package >>> from networkx.algorithms.flow import build_residual_network >>> # Note that the auxiliary digraph has an edge attribute named capacity >>> R = build_residual_network(H, 'capacity') >>> result = dict.fromkeys(G, dict()) >>> # Reuse the auxiliary digraph and the residual network by passing them >>> # as parameters >>> for u, v in itertools.combinations(G, 2): ... k = local_node_connectivity(G, u, v, auxiliary=H, residual=R) ... result[u][v] = k ... >>> all(result[u][v] == 5 for u, v in itertools.combinations(G, 2)) True You can also use alternative flow algorithms for computing node connectivity. For instance, in dense networks the algorithm :meth:`shortest_augmenting_path` will usually perform better than the default :meth:`edmonds_karp` which is faster for sparse networks with highly skewed degree distributions. Alternative flow functions have to be explicitly imported from the flow package. >>> from networkx.algorithms.flow import shortest_augmenting_path >>> local_node_connectivity(G, 0, 6, flow_func=shortest_augmenting_path) 5 Notes ----- This is a flow based implementation of node connectivity. We compute the maximum flow using, by default, the :meth:`edmonds_karp` algorithm (see: :meth:`maximum_flow`) on an auxiliary digraph build from the original input graph: For an undirected graph G having `n` nodes and `m` edges we derive a directed graph H with `2n` nodes and `2m+n` arcs by replacing each original node `v` with two nodes `v_A`, `v_B` linked by an (internal) arc in H. Then for each edge (`u`, `v`) in G we add two arcs (`u_B`, `v_A`) and (`v_B`, `u_A`) in H. Finally we set the attribute capacity = 1 for each arc in H [1]_ . For a directed graph G having `n` nodes and `m` arcs we derive a directed graph H with `2n` nodes and `m+n` arcs by replacing each original node `v` with two nodes `v_A`, `v_B` linked by an (internal) arc (`v_A`, `v_B`) in H. Then for each arc (`u`, `v`) in G we add one arc (`u_B`, `v_A`) in H. Finally we set the attribute capacity = 1 for each arc in H. This is equal to the local node connectivity because the value of a maximum s-t-flow is equal to the capacity of a minimum s-t-cut. See also -------- :meth:`local_edge_connectivity` :meth:`node_connectivity` :meth:`minimum_node_cut` :meth:`maximum_flow` :meth:`edmonds_karp` :meth:`preflow_push` :meth:`shortest_augmenting_path` References ---------- .. [1] Kammer, Frank and Hanjo Taubig. Graph Connectivity. in Brandes and Erlebach, 'Network Analysis: Methodological Foundations', Lecture Notes in Computer Science, Volume 3418, Springer-Verlag, 2005. http://www.informatik.uni-augsburg.de/thi/personen/kammer/Graph_Connectivity.pdf tmappingsInvalid auxiliary digraph.t flow_functresidualtcutofft two_phases%sBs%sAN(tNonetdefault_flow_funcRtgraphtgettnxt NetworkXErrortdictRtTrueRRRtmaximum_flow_value( tGtsttRt auxiliaryRRtHRtkwargs((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/connectivity.pyR !s&š             c s1|d k r|d ks0|d krB|d k rBtjdƒ‚n|d k r´|d k r´|ˆkr|tjd|ƒ‚n|ˆkržtjd|ƒ‚ntˆ||d|ƒSˆjƒrîtjˆƒsÓdStj}‡fd†}n%tjˆƒsdStj }ˆj }t ˆƒ}t |dƒ}t d|d|d|ƒ}tˆjƒd td ƒƒ\} } xYtˆƒt|| ƒƒt| gƒD].} | |d >> # Platonic icosahedral graph is 5-node-connected >>> G = nx.icosahedral_graph() >>> nx.node_connectivity(G) 5 You can use alternative flow algorithms for the underlying maximum flow computation. In dense networks the algorithm :meth:`shortest_augmenting_path` will usually perform better than the default :meth:`edmonds_karp`, which is faster for sparse networks with highly skewed degree distributions. Alternative flow functions have to be explicitly imported from the flow package. >>> from networkx.algorithms.flow import shortest_augmenting_path >>> nx.node_connectivity(G, flow_func=shortest_augmenting_path) 5 If you specify a pair of nodes (source and target) as parameters, this function returns the value of local node connectivity. >>> nx.node_connectivity(G, 3, 7) 5 If you need to perform several local computations among different pairs of nodes on the same graph, it is recommended that you reuse the data structures used in the maximum flow computations. See :meth:`local_node_connectivity` for details. Notes ----- This is a flow based implementation of node connectivity. The algorithm works by solving $O((n-\delta-1+\delta(\delta-1)/2))$ maximum flow problems on an auxiliary digraph. Where $\delta$ is the minimum degree of G. For details about the auxiliary digraph and the computation of local node connectivity see :meth:`local_node_connectivity`. This implementation is based on algorithm 11 in [1]_. See also -------- :meth:`local_node_connectivity` :meth:`edge_connectivity` :meth:`maximum_flow` :meth:`edmonds_karp` :meth:`preflow_push` :meth:`shortest_augmenting_path` References ---------- .. [1] Abdol-Hossein Esfahanian. Connectivity Algorithms. http://www.cse.msu.edu/~cse835/Papers/Graph_connectivity_revised.pdf s)Both source and target must be specified.snode %s not in graphRic s(tjjˆj|ƒˆj|ƒgƒS(N(t itertoolstchaint from_iterablet predecessorst successors(tv(R(sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/connectivity.pyt neighborsBstcapacityR RtkeyiRiN(RRRR t is_directedtis_weakly_connectedR#t permutationst is_connectedt combinationsR)RRRtmintdegreeRtset(RRRRt iter_funcR)R!tRR"R(tKtwtxty((Rsƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/connectivity.pyR Õs<Z0       $0 "" "c C s¾|jƒrtj}n tj}t|ƒ}t|dƒ}td|d|d|ƒ}d\}}x@||dƒD]/\}} |t||| |7}|d7}qsW|dkr¶dS||S( s Returns the average connectivity of a graph G. The average connectivity `\bar{\kappa}` of a graph G is the average of local node connectivity over all pairs of nodes of G [1]_ . .. math:: \bar{\kappa}(G) = \frac{\sum_{u,v} \kappa_{G}(u,v)}{{n \choose 2}} Parameters ---------- G : NetworkX graph Undirected graph flow_func : function A function for computing the maximum flow among a pair of nodes. The function has to accept at least three parameters: a Digraph, a source node, and a target node. And return a residual network that follows NetworkX conventions (see :meth:`maximum_flow` for details). If flow_func is None, the default maximum flow function (:meth:`edmonds_karp`) is used. See :meth:`local_node_connectivity` for details. The choice of the default function may change from version to version and should not be relied on. Default value: None. Returns ------- K : float Average node connectivity See also -------- :meth:`local_node_connectivity` :meth:`node_connectivity` :meth:`edge_connectivity` :meth:`maximum_flow` :meth:`edmonds_karp` :meth:`preflow_push` :meth:`shortest_augmenting_path` References ---------- .. [1] Beineke, L., O. Oellermann, and R. Pippert (2002). The average connectivity of a graph. Discrete mathematics 252(1-3), 31-45. http://www.sciencedirect.com/science/article/pii/S0012365X01001807 R*RR Riii(ii(R,R#R.R0RRRR ( RRR4R!R5R"tnumtdentuR(((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/connectivity.pyR as0      c C sù|dkr|}n t|ƒ}|jƒ}|r?tj}n tj}d„|Dƒ}t|ƒ}|jd}t|dƒ}t d|d|d|ƒ} xW||dƒD]F\} } t || | | } | || | <|s«| || | ×s RR*RR RiN( RR3R,R#R.R0RRRRR ( RtnbunchRtdirectedR4t all_pairsR!RR5R"R<R(R6((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/connectivity.pyR¥s$'        c C sÏ|dkrt}n|dkr0t|ƒ}n|}td|d|ƒ}|tkrn||d>> from networkx.algorithms.connectivity import local_edge_connectivity We use in this example the platonic icosahedral graph, which has edge connectivity 5. >>> G = nx.icosahedral_graph() >>> local_edge_connectivity(G, 0, 6) 5 If you need to compute local connectivity on several pairs of nodes in the same graph, it is recommended that you reuse the data structures that NetworkX uses in the computation: the auxiliary digraph for edge connectivity, and the residual network for the underlying maximum flow computation. Example of how to compute local edge connectivity among all pairs of nodes of the platonic icosahedral graph reusing the data structures. >>> import itertools >>> # You also have to explicitly import the function for >>> # building the auxiliary digraph from the connectivity package >>> from networkx.algorithms.connectivity import ( ... build_auxiliary_edge_connectivity) >>> H = build_auxiliary_edge_connectivity(G) >>> # And the function for building the residual network from the >>> # flow package >>> from networkx.algorithms.flow import build_residual_network >>> # Note that the auxiliary digraph has an edge attribute named capacity >>> R = build_residual_network(H, 'capacity') >>> result = dict.fromkeys(G, dict()) >>> # Reuse the auxiliary digraph and the residual network by passing them >>> # as parameters >>> for u, v in itertools.combinations(G, 2): ... k = local_edge_connectivity(G, u, v, auxiliary=H, residual=R) ... result[u][v] = k >>> all(result[u][v] == 5 for u, v in itertools.combinations(G, 2)) True You can also use alternative flow algorithms for computing edge connectivity. For instance, in dense networks the algorithm :meth:`shortest_augmenting_path` will usually perform better than the default :meth:`edmonds_karp` which is faster for sparse networks with highly skewed degree distributions. Alternative flow functions have to be explicitly imported from the flow package. >>> from networkx.algorithms.flow import shortest_augmenting_path >>> local_edge_connectivity(G, 0, 6, flow_func=shortest_augmenting_path) 5 Notes ----- This is a flow based implementation of edge connectivity. We compute the maximum flow using, by default, the :meth:`edmonds_karp` algorithm on an auxiliary digraph build from the original input graph: If the input graph is undirected, we replace each edge (`u`,`v`) with two reciprocal arcs (`u`, `v`) and (`v`, `u`) and then we set the attribute 'capacity' for each arc to 1. If the input graph is directed we simply add the 'capacity' attribute. This is an implementation of algorithm 1 in [1]_. The maximum flow in the auxiliary network is equal to the local edge connectivity because the value of a maximum s-t-flow is equal to the capacity of a minimum s-t-cut (Ford and Fulkerson theorem). See also -------- :meth:`edge_connectivity` :meth:`local_node_connectivity` :meth:`node_connectivity` :meth:`maximum_flow` :meth:`edmonds_karp` :meth:`preflow_push` :meth:`shortest_augmenting_path` References ---------- .. [1] Abdol-Hossein Esfahanian. Connectivity Algorithms. http://www.cse.msu.edu/~cse835/Papers/Graph_connectivity_revised.pdf RRRRN( RRRRRRRRRRR( RRRRR RRR!R"((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/connectivity.pyR ès            c C sz|d k r|d ks0|d krB|d k rBtjdƒ‚n|d k r´|d k r´||kr|tjd|ƒ‚n||kržtjd|ƒ‚nt|||d|ƒSt|ƒ}t|dƒ}td|d|d|ƒ}|jƒrÊtj|ƒs dSt d„|j ƒDƒƒ}t |ƒ}t |ƒ} x†t | ƒD]x} ||d >> # Platonic icosahedral graph is 5-edge-connected >>> G = nx.icosahedral_graph() >>> nx.edge_connectivity(G) 5 You can use alternative flow algorithms for the underlying maximum flow computation. In dense networks the algorithm :meth:`shortest_augmenting_path` will usually perform better than the default :meth:`edmonds_karp`, which is faster for sparse networks with highly skewed degree distributions. Alternative flow functions have to be explicitly imported from the flow package. >>> from networkx.algorithms.flow import shortest_augmenting_path >>> nx.edge_connectivity(G, flow_func=shortest_augmenting_path) 5 If you specify a pair of nodes (source and target) as parameters, this function returns the value of local edge connectivity. >>> nx.edge_connectivity(G, 3, 7) 5 If you need to perform several local computations among different pairs of nodes on the same graph, it is recommended that you reuse the data structures used in the maximum flow computations. See :meth:`local_edge_connectivity` for details. Notes ----- This is a flow based implementation of global edge connectivity. For undirected graphs the algorithm works by finding a 'small' dominating set of nodes of G (see algorithm 7 in [1]_ ) and computing local maximum flow (see :meth:`local_edge_connectivity`) between an arbitrary node in the dominating set and the rest of nodes in it. This is an implementation of algorithm 6 in [1]_ . For directed graphs, the algorithm does n calls to the maximum flow function. This is an implementation of algorithm 8 in [1]_ . See also -------- :meth:`local_edge_connectivity` :meth:`local_node_connectivity` :meth:`node_connectivity` :meth:`maximum_flow` :meth:`edmonds_karp` :meth:`preflow_push` :meth:`shortest_augmenting_path` :meth:`k_edge_components` :meth:`k_edge_subgraphs` References ---------- .. [1] Abdol-Hossein Esfahanian. Connectivity Algorithms. http://www.cse.msu.edu/~cse835/Papers/Graph_connectivity_revised.pdf s)Both source and target must be specified.snode %s not in graphRR*R Rics s|]\}}|VqdS(N((R=R>td((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/connectivity.pys sRics s|]\}}|VqdS(N((R=R>RB((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/connectivity.pys st start_withN(RRRR RRRR,R-R1R2tlisttlentranget IndexErrorR/tdominating_settpop(RRRRR!R5R"tLtnodesR>titnodetDR(R7((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/connectivity.pyR ‹sN_0            "(t__doc__t __future__RR#toperatorRtnetworkxRtnetworkx.algorithms.flowRRRRRRtutilsRRtjoint __author__t__all__RR R R RR R (((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/connectivity.pyts4   ³Œ DC¢