
    h7                         S r SSKJr  SSKrSSKJrJrJr  S/r	\" S5      \" S5      \R                  " S	S
9S	\4S j5       5       5       rg)z%
Stoer-Wagner minimum cut algorithm.
    )isliceN   )
BinaryHeaparbitrary_elementnot_implemented_forstoer_wagnerdirected
multigraphweight)
edge_attrsc           
        ^ [        U 5      nUS:  a  [        R                  " S5      e[        R                  " U 5      (       d  [        R                  " S5      e[        R                  " U4S jU R                  SS9 5       5      n SU l        U R                  SS9 H%  u  pEnUS   S	:  d  M  [        R                  " S
5      e   [        S5      n[        U 5      n/ n	[        US-
  5       GHb  n
[        U 5      nU1nU" 5       nX   R                  5        H  u  pVUR                  XVS   * 5        M     [        X:-
  S-
  5       Hp  nUR                  5       S	   nUR                  U5        X   R                  5        H3  u  pVX[;  d  M  UR                  X\R                  US	5      US   -
  5        M5     Mr     UR!                  5       u  p^U* nX:  a  UnU
nU	R#                  XE45        X   R                  5        H>  u  pX:w  d  M  XU   ;  a  U R%                  XNUS   S9  M)  X   U   S==   US   -  ss'   M@     U R'                  U5        GMe     [        R                  " [)        U	W5      5      n X   S   nU R+                  U5        [        [        R,                  " X5      5      n[/        U5      [/        UU-
  5      4nUU4$ )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   d   >#    U  H%  u  po1U:w  d  M  XS UR                  TS5      04v   M'     g7f)r      N)get).0uver   s       ^/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/connectivity/stoerwagner.py	<genexpr>stoer_wagner.<locals>.<genexpr>_   s7      <NqWXRX,!%%*+,<Ns   00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      s   T 	AA1u?@@??1899 	 <=GGG<N 	A  A777%aX;?""#HII & eIFEL 1q5\a C FDJJLDAHHQ8% ! quqy!A
AEE!H

:HHQaak 9: % " uuwB=IJQF#DJJLDAvaD=JJqAhKJ8DGH%84% ! 	
aE J 	j12A #AJJqMB99!?@Ii$uy'8"9:Ii    )__doc__	itertoolsr   networkxr   utilsr   r   r   __all___dispatchabler    r>   r   <module>rF      s`      G G
 Z \"X&#* G  ' # !G r>   