
    wg>3                         d Z ddlZddlmZ ddlZg dZ ej                  d      dd       Z ej                  d      dd	       Z	 ej                  d
      dd       Z
d Zd Zy)z(Fast approximation for node connectivity    N)
itemgetter)local_node_connectivitynode_connectivityall_pairs_node_connectivity#approximate_local_node_connectivity)namec                    ||k(  rt        j                  d      | j                         r+t        | j	                  |      | j                  |            }n*t        | j                  |      | j                  |            }d}|s|S |t        d      }t               }t        t        ||            D ]0  }	 t        | |||      }|j                  t        |             |dz  }2 |S # t         j                  $ r Y  |S w xY w)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	            s/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/approximation/connectivity.pyr   r      s    H NOO 	}}q||F+Q[[-@Aqxx'&)9:	A~ueG3x() 	/667KDNN3t9%FA	 H    	H	s   6-C''C?>C?approximate_node_connectivityc           
          ||||t        j                  d      |I|G| vrt        j                  d| d      | vrt        j                  d| d      t         ||      S  j                         r&t         j                  }t
        j                  } fd}n,t         j                  }t
        j                  } j                  } |       syt         j                         t        d            \  }}|}t               t         ||            z
  |hz
  D ]  }	t        |t         ||	|            }  | ||      d	      D ],  \  }
}| |
   vs|
|k7  st        |t         |
||            }. |S )
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                 l    t        j                  j                  |       j                  |             S N)	itertoolschainpredecessors
successors)vr   s    r"   	neighborsz$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   s   @ 	
!)q}JKK 	}A:""U1#]#;<<A:""U1#]#;<<&q!Q// 	}}//**		G **	KK	! AHHJJqM:A~A Vc)A,''1#- ?*1a1=>? )A,* C1AaD=Q!VA.q!QqABAC Hr-   'approximate_all_pairs_node_connectivityc                    || }nt        |      }| j                         }|rt        j                  }nt        j                  }|D ci c]  }|i  }} ||d      D ]'  \  }}t        | |||      }	|	||   |<   |r |	||   |<   ) |S c c}w )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
    r0   r/   )r   r   r'   r2   r4   r   )
r   nbunchr   directedr8   n	all_pairsur+   ks
             r"   r   r      s    t ~V}}H**	**	 &'1B'I'&!$  1#Aq!F;	!QIaLO	   (s   
B	c                     t        | |||      }|\  }}}g }||j                  |       ||   }||j                          ||d      }||j                  |       ||   }||S )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   )  s    X 'q&&'BGMD$ D
-AG - 	LLNT"XA
-AG - Kr-   c                    | j                         r| j                  }| j                  }n| j                  }| j                  }|d i}|d i}|g}|g}	d}
|r|	r|
dz  }
|
dz  dk7  rE|}g }|D ];  } ||      D ].  }||v r||vr|j	                  |       |||<   ||v s'|||fc c S  = nD|	}g }	|D ];  } ||      D ].  }||v r||vr|||<   |	j	                  |       ||v s'|||fc c S  = |r|	rt        j                  d| d| d      )Nr   r   r0   zNo path between z and .)r   r)   r*   r,   rH   r   r   )r   r   r   r   GpredGsuccrK   rL   forward_fringereverse_fringelevel
this_levelr+   r:   s                 r"   rG   rG   h  so   
 	}} D>DD>D XNXNE
^ 	
19>'JN -q -AG| }&--a0"#QDy#T1},-- (JN -q -AG| }"#Q&--a0Dy#T1},--) ^< 

.vheF81E
FFr-   r&   )NN)__doc__r'   operatorr   networkxr   __all___dispatchabler   r   r   r   rG    r-   r"   <module>r[      s    .    <=\ >\~ 67f 8fR @AL BL^<~4Gr-   