ó žÃÒYc@s›dZddlZddlmZddlZdjdgƒZdddgZe d ƒZ dd „Z ddd „Z ddd „Zd „Zd„ZdS(s* Fast approximation for node connectivity iÿÿÿÿN(t itemgetters s%Jordi Torrents tlocal_node_connectivitytnode_connectivitytall_pairs_node_connectivitytinfc Cs||krtjdƒ‚n|jƒrNt|j|ƒ|j|ƒƒ}n!t|j|ƒ|j|ƒƒ}d}|s|S|dkr”t}nt ƒ}xkt t||ƒƒD]T}y6t ||||ƒ}|j t |ƒƒ|d7}Wq³tj k rPq³Xq³W|S(s Compute node connectivity between source and target. Pairwise or local node connectivity between two distinct and nonadjacent nodes is the minimum number of nodes that must be removed (minimum separating cutset) to disconnect them. By Menger's theorem, this is equal to the number of node independent paths (paths that share no nodes other than source and target). Which is what we compute in this function. This algorithm is a fast approximation that gives an strict lower bound on the actual number of node independent paths between two nodes [1]_. It works for both directed and undirected graphs. Parameters ---------- G : NetworkX graph source : node Starting node for node connectivity target : node Ending node for node connectivity cutoff : integer Maximum node connectivity to consider. If None, the minimum degree of source or target is used as a cutoff. Default value None. Returns ------- k: integer pairwise node connectivity Examples -------- >>> # Platonic octahedral graph has node connectivity 4 >>> # for each non adjacent node pair >>> from networkx.algorithms import approximation as approx >>> G = nx.octahedral_graph() >>> approx.local_node_connectivity(G, 0, 5) 4 Notes ----- This algorithm [1]_ finds node independents paths between two nodes by computing their shortest path using BFS, marking the nodes of the path found as 'used' and then searching other shortest paths excluding the nodes marked as used until no more paths exist. It is not exact because a shortest path could use nodes that, if the path were longer, may belong to two different node independent paths. Thus it only guarantees an strict lower bound on node connectivity. Note that the authors propose a further refinement, losing accuracy and gaining speed, which is not implemented yet. See also -------- all_pairs_node_connectivity node_connectivity References ---------- .. [1] White, Douglas R., and Mark Newman. 2001 A Fast Algorithm for Node-Independent Paths. Santa Fe Institute Working Paper #01-07-035 http://eclectic.ss.uci.edu/~drwhite/working.pdf s-source and target have to be different nodes.iiN(tnxt NetworkXErrort is_directedtmint out_degreet in_degreetdegreetNonetINFtsettranget_bidirectional_shortest_pathtupdatetNetworkXNoPath( tGtsourcettargettcutofftpossibletKtexcludetitpath((s„/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/approximation/connectivity.pyRs&C  $!    c sò|d k r|d ks0|d krB|d k rBtjdƒ‚n|d k r®|d k r®|ˆkr|tjd|ƒ‚n|ˆkržtjd|ƒ‚ntˆ||ƒSˆjƒrÞtj}tj}‡fd†}ntj}tj }ˆj }|ˆƒs dSt ˆj ƒdt dƒƒ\}}|}xRtˆƒt||ƒƒt|gƒD]'} t |tˆ|| d|ƒƒ}q]Wxc|||ƒdƒD]L\} } | ˆ| krž| | kržt |tˆ| | d|ƒƒ}qžqžW|S( s’Returns an approximation for node connectivity for a graph or digraph G. Node connectivity is equal to the minimum number of nodes that must be removed to disconnect G or render it trivial. By Menger's theorem, this is equal to the number of node independent paths (paths that share no nodes other than source and target). If source and target nodes are provided, this function returns the local node connectivity: the minimum number of nodes that must be removed to break all paths from source to target in G. This algorithm is based on a fast approximation that gives an strict lower bound on the actual number of node independent paths between two nodes [1]_. It works for both directed and undirected graphs. Parameters ---------- G : NetworkX graph Undirected graph s : node Source node. Optional. Default value: None. t : node Target node. Optional. Default value: None. Returns ------- K : integer Node connectivity of G, or local node connectivity if source and target are provided. Examples -------- >>> # Platonic octahedral graph is 4-node-connected >>> from networkx.algorithms import approximation as approx >>> G = nx.octahedral_graph() >>> approx.node_connectivity(G) 4 Notes ----- This algorithm [1]_ finds node independents paths between two nodes by computing their shortest path using BFS, marking the nodes of the path found as 'used' and then searching other shortest paths excluding the nodes marked as used until no more paths exist. It is not exact because a shortest path could use nodes that, if the path were longer, may belong to two different node independent paths. Thus it only guarantees an strict lower bound on node connectivity. See also -------- all_pairs_node_connectivity local_node_connectivity References ---------- .. [1] White, Douglas R., and Mark Newman. 2001 A Fast Algorithm for Node-Independent Paths. Santa Fe Institute Working Paper #01-07-035 http://eclectic.ss.uci.edu/~drwhite/working.pdf s)Both source and target must be specified.snode %s not in graphcs"tjˆj|ƒˆj|ƒƒS(N(t itertoolstchaint predecessorst successors(tv(R(s„/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/approximation/connectivity.pyt neighborsÂsitkeyiRiN(R RRRRtis_weakly_connectedRt permutationst is_connectedt combinationsR!RR RR( Rtstttconnected_funct iter_funcR!R tminimum_degreeRtwtxty((Rs„/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/approximation/connectivity.pyRts2?0         $0%"(c Cs¹|dkr|}n t|ƒ}|jƒ}|r?tj}n tj}d„|Dƒ}xZ||dƒD]I\}}t|||d|ƒ}||||<|sh||||s iRN(R RRRR$R&R( RtnbunchRtdirectedR*t all_pairstuR tk((s„/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/approximation/connectivity.pyRÛs+      c Csšt||||ƒ}|\}}}g}x'|dk rS|j|ƒ||}q-W|jƒ||d}x'|dk r•|j|ƒ||}qoW|S(skReturn shortest path between source and target ignoring nodes in the container 'exclude'. Parameters ---------- G : NetworkX graph source : node Starting node for path target : node Ending node for path exclude: container Container for nodes to exclude from the search for shortest paths Returns ------- path: list Shortest path between source and target ignoring nodes in 'exclude' Raises ------ NetworkXNoPath: exception If there is no path or if nodes are adjacent and have only one path between them Notes ----- This function and its helper are originaly from networkx.algorithms.shortest_paths.unweighted and are modified to accept the extra parameter 'exclude', which is a container for nodes already used in other paths that should be ignored. References ---------- .. [1] White, Douglas R., and Mark Newman. 2001 A Fast Algorithm for Node-Independent Paths. Santa Fe Institute Working Paper #01-07-035 http://eclectic.ss.uci.edu/~drwhite/working.pdf iÿÿÿÿN(t_bidirectional_pred_succR tappendtreverse( RRRRtresultstpredtsuccR,R((s„/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/approximation/connectivity.pyRs,   cCs |dks|dkr*tjdƒ‚n||krQid|6id|6|fS|jƒrr|j}|j}n|j}|j}id|6}id|6}|g}|g} d} x7|rï| rï| d7} | ddksg|} g}xþ| D]n} xe|| ƒD]W} | |krqn| |krC|j| ƒ| || s     _gA ?