
    \Mh7                         d Z ddlmZ ddlZddlmZmZmZ dgZ	 ed           ed           ej
        d	
          d	efd                                    ZdS )z%
Stoer-Wagner minimum cut algorithm.
    )isliceN   )
BinaryHeaparbitrary_elementnot_implemented_forstoer_wagnerdirected
multigraphweight)
edge_attrsc           
         t          |           }|dk     rt          j        d          t          j        |           st          j        d          t          j        fd|                     d          D                       } d| _        |                     d          D ]&\  }}}|d         d	k     rt          j        d
          't          d          }t          |           }g }	t          |dz
            D ]}
t          |           }|h} |            }| |                                         D ]"\  }}|                    ||d                     #t          ||
z
  dz
            D ]}|                                d	         }|                    |           | |                                         D ]<\  }}||vr3|                    ||                    |d	          |d         z
             =|                                \  }}| }||k     r|}|
}|	                    ||f           | |                                         D ]V\  }}||k    rK|| |         vr|                     |||d                    4| |         |         dxx         |d         z  cc<   W|                     |           t          j        t)          |	|                    } |	|         d         }|                     |           t          t          j        | |                    }t/          |          t/          ||z
            f}||fS )a	  Returns the weighted minimum edge cut using the Stoer-Wagner algorithm.

    Determine the minimum edge cut of a connected graph using the
    Stoer-Wagner algorithm. In weighted cases, all weights must be
    nonnegative.

    The running time of the algorithm depends on the type of heaps used:

    ============== =============================================
    Type of heap   Running time
    ============== =============================================
    Binary heap    $O(n (m + n) \log n)$
    Fibonacci heap $O(nm + n^2 \log n)$
    Pairing heap   $O(2^{2 \sqrt{\log \log n}} nm + n^2 \log n)$
    ============== =============================================

    Parameters
    ----------
    G : NetworkX graph
        Edges of the graph are expected to have an attribute named by the
        weight parameter below. If this attribute is not present, the edge is
        considered to have unit weight.

    weight : string
        Name of the weight attribute of the edges. If the attribute is not
        present, unit weight is assumed. Default value: 'weight'.

    heap : class
        Type of heap to be used in the algorithm. It should be a subclass of
        :class:`MinHeap` or implement a compatible interface.

        If a stock heap implementation is to be used, :class:`BinaryHeap` is
        recommended over :class:`PairingHeap` for Python implementations without
        optimized attribute accesses (e.g., CPython) despite a slower
        asymptotic running time. For Python implementations with optimized
        attribute accesses (e.g., PyPy), :class:`PairingHeap` provides better
        performance. Default value: :class:`BinaryHeap`.

    Returns
    -------
    cut_value : integer or float
        The sum of weights of edges in a minimum cut.

    partition : pair of node lists
        A partitioning of the nodes that defines a minimum cut.

    Raises
    ------
    NetworkXNotImplemented
        If the graph is directed or a multigraph.

    NetworkXError
        If the graph has less than two nodes, is not connected or has a
        negative-weighted edge.

    Examples
    --------
    >>> G = nx.Graph()
    >>> G.add_edge("x", "a", weight=3)
    >>> G.add_edge("x", "b", weight=1)
    >>> G.add_edge("a", "c", weight=3)
    >>> G.add_edge("b", "c", weight=5)
    >>> G.add_edge("b", "d", weight=4)
    >>> G.add_edge("d", "e", weight=2)
    >>> G.add_edge("c", "y", weight=2)
    >>> G.add_edge("e", "y", weight=3)
    >>> cut_value, partition = nx.stoer_wagner(G)
    >>> cut_value
    4
       zgraph has less than two nodes.zgraph is not connected.c              3   b   K   | ])\  }}}||k    ||d |                     d          ifV  *dS )r      N)get).0uver   s       l/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/networkx/algorithms/connectivity/stoerwagner.py	<genexpr>zstoer_wagner.<locals>.<genexpr>_   sX        18AqRSWXRXRXA!%%**+,RXRXRXRX     T)dataNr   r   z#graph has a negative-weighted edge.infr   )r   )lennxNetworkXErroris_connectedGraphedges__networkx_cache__floatsetranger   itemsinsertpopaddr   minappendadd_edgeremove_noder   add_node"single_source_shortest_path_lengthlist)Gr   heapnr   r   r   	cut_valuenodescontractionsiAhjw
best_phase	reachable	partitions    `                r   r   r      sg   T 	AA1uu?@@@?1 :8999 	    <=GGG<N<N   	 	A  A777%% J J1aX;??"#HIII  eIFFEL 1q5\\ " "a  C DFFaDJJLL 	& 	&DAqHHQ8%%%%q1uqy!! 	; 	;A
AEE!HHH!

 ; ;1A::HHQaak 9:::; uuww1By==IJQF###aDJJLL 	5 	5DAqAvvAaD==JJq!AhKJ8888aDGH%%%84%%%	a 	j1122AZ #AJJqMMMB9!Q??@@Ii$uy'8"9"9:Iir   )__doc__	itertoolsr   networkxr   utilsr   r   r   __all___dispatchabler    r   r   <module>rE      s               G G G G G G G G G G
 Z  \""X&&&#* G  G  G  '& #" ! G  G  G r   