
    h>3                         S r SSKrSSKJr  SSKr/ SQr\R                  " SS9SS j5       r\R                  " SS9SS	 j5       r	\R                  " S
S9SS j5       r
S rS rg)z(Fast approximation for node connectivity    N)
itemgetter)local_node_connectivitynode_connectivityall_pairs_node_connectivity#approximate_local_node_connectivity)namec                    X!:X  a  [         R                  " S5      eU R                  5       (       a+  [        U R	                  U5      U R                  U5      5      nO*[        U R                  U5      U R                  U5      5      nSnU(       d  U$ Uc  [        S5      n[        5       n[        [        XC5      5       H/  n [        XX&5      nUR                  [        U5      5        US-  nM1     U$ ! [         R                   a       U$ f = f)a/  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

z-source and target have to be different nodes.r   inf   )nxNetworkXErroris_directedmin
out_degree	in_degreedegreefloatsetrange_bidirectional_shortest_pathupdateNetworkXNoPath)	GsourcetargetcutoffpossibleKexcludeipaths	            `/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/approximation/connectivity.pyr   r      s    H NOO 	}}q||F+Q[[-@Aqxx'&)9:	A~ueG3x()	/6KDNN3t9%FA	 * H    	H	s   +C22D
Dapproximate_node_connectivityc                   ^  Ub  Ub  Uc  Ub  [         R                  " S5      eUbO  UbL  UT ;  a  [         R                  " SU S35      eUT ;  a  [         R                  " SU S35      e[        T X5      $ T R                  5       (       a'  [         R                  n[
        R                  nU 4S jnO,[         R                  n[
        R                  nT R                  nU" T 5      (       d  g[        T R                  5       [        S5      S9u  pgUn[        T 5      [        U" U5      5      -
  U1-
   H  n	[        U[        T XiUS95      nM     U" U" U5      S	5       H,  u  pUT U
   ;  d  M  X:w  d  M  [        U[        T XUS95      nM.     U$ )
a  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

z)Both source and target must be specified.znode z not in graphc                 n   > [         R                  " TR                  U 5      TR                  U 5      5      $ N)	itertoolschainpredecessors
successors)vr   s    r"   	neighbors$node_connectivity.<locals>.neighbors   s%    ??1>>!#4all1oFF    r   r   )keyr      )r   r   r   r   is_weakly_connectedr'   permutationsis_connectedcombinationsr,   r   r   r   r   )r   stconnected_func	iter_funcr,   r+   minimum_degreer   wxys   `           r"   r   r   o   sy   @ 	
!)q}JKK 	}A:""U1#]#;<<A:""U1#]#;<<&q!// 	}}//**		G **	KK	! AHHJJqM:AA Vc)A,''1#-*1a1=> . )A,*AaD=QVA.q!qABA + Hr.   'approximate_all_pairs_node_connectivityc                 ,   Uc  U nO[        U5      nU R                  5       nU(       a  [        R                  nO[        R                  nU Vs0 s H  oU0 _M     nnU" US5       H&  u  px[        XXS9n	XU   U'   U(       a  M  XU   U'   M(     U$ s  snf )a  Compute node connectivity between all pairs of nodes.

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

nbunch: container
    Container of nodes. If provided node connectivity will be computed
    only over pairs of nodes in nbunch.

cutoff : integer
    Maximum node connectivity to consider. If None, the minimum degree
    of source or target is used as a cutoff in each pair of nodes.
    Default value None.

Returns
-------
K : dictionary
    Dictionary, keyed by source and target, of pairwise node connectivity

Examples
--------
A 3 node cycle with one extra node attached has connectivity 2 between all
nodes in the cycle and connectivity 1 between the extra node and the rest:

>>> G = nx.cycle_graph(3)
>>> G.add_edge(2, 3)
>>> import pprint  # for nice dictionary formatting
>>> pprint.pprint(nx.all_pairs_node_connectivity(G))
{0: {1: 2, 2: 2, 3: 1},
 1: {0: 2, 2: 2, 3: 1},
 2: {0: 2, 1: 2, 3: 1},
 3: {0: 1, 1: 1, 2: 1}}

See Also
--------
local_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
r1   r0   )r   r   r'   r3   r5   r   )
r   nbunchr   directedr9   n	all_pairsur+   ks
             r"   r   r      s    t ~V}}H**	**	 &'1BI'&!$#A!;!QxaLO	 %  (s   Bc                     [        XX#5      nUu  pVn/ nUb  UR                  U5        XW   nUb  M  UR                  5         XhS      nUb  UR                  U5        Xg   nUb  M  U$ )a  Returns 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
    If there is no path or if nodes are adjacent and have only one path
    between them

Notes
-----
This function and its helper are originally 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

)_bidirectional_pred_succappendreverse)	r   r   r   r   resultspredsuccr;   r!   s	            r"   r   r   )  sz    X 'q&BGMD D
-AG - 	LLN"XA
-AG - Kr.   c                 j   U R                  5       (       a  U R                  nU R                  nOU R                  nU R                  nUS 0nUS 0nU/nU/n	Sn
U(       a  U	(       a  U
S-  n
U
S-  S:w  aK  Un/ nU H@  nU" U5       H1  nX;   a  M
  X;  a  UR	                  U5        XU'   X;   d  M+  XgU4s  s  $    MB     OJU	n/ n	U H@  nU" U5       H1  nX;   a  M
  X;  a  XU'   U	R	                  U5        X;   d  M+  XgU4s  s  $    MB     U(       a	  U	(       a  M  [
        R                  " SU SU S35      e)Nr   r   r1   zNo path between z and .)r   r)   r*   r,   rI   r   r   )r   r   r   r   GpredGsuccrL   rM   forward_fringereverse_fringelevel
this_levelr+   r;   s                 r"   rH   rH   h  sK   
 	}} D>DD>D XNXNE
^ 	
19>'JNqA| }&--a0"#Qy#1}, "   (JNqA| }"#Q&--a0y#1}, "  ) ^^< 

.vheF81E
FFr.   r&   )NN)__doc__r'   operatorr   networkxr   __all___dispatchabler   r   r   r   rH    r.   r"   <module>r\      s    .    <=\ >\~ 67f 8fR @AL BL^<~4Gr.   