
    h8                         S r S/rSSKJ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 r\R                  " S\" S5      SS.S9SSS\	4S j5       rg)z/
Capacity scaling minimum cost flow algorithm.
capacity_scaling    )chain)logN   )
BinaryHeaparbitrary_elementnot_implemented_forc                    [         R                  " 5       nUR                  U 5        U R                  S   n[	        S5      nU  Hi  nX   R                  5        HP  u  pVUnUR                  5        H  u  pUS   U:X  d  M  [        XvS   5      nM      Xs:w  d  M@  UR                  XEUS9  MR     Mk     [         R                  " U5      (       a  [         R                  " S5      eg)z)Detect infinite-capacity negative cycles.infcapacityweight)r   UNegative cost cycle of infinite capacity found. Min cost flow may be unbounded below.N)
nxDiGraphadd_nodes_fromgraphfloatitemsminadd_edgenegative_edge_cycleNetworkXUnbounded)	RGr   f_infuvewks	            Z/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/flow/capacityscaling.py_detect_unboundednessr"      s    


AQ ''%.C%LEDJJLDAA	Z=C'A{+A " z

1
* !  
a  ""4
 	
 !    
undirectedc           
        ^ ^^^^ [        U U4S jT  5       5      S:w  a  [        R                  " S5      e[        R                  " 5       mTR	                  U U4S jT  5       5        [        S5      m[        R                  " T SS9 HJ  u  pEnUR                  US5      S:  d  M  UR                  TT5      T:X  d  M6  [        R                  " S5      e   T R                  5       (       aK  T R                  SSS	9 VVVVs/ s H)  u  pEpvXE:w  d  M  UR                  TT5      S:  d  M%  XEXv4PM+     nnnnnOHT R                  SS9 VVVs/ s H*  u  pEnXE:w  d  M  UR                  TT5      S:  d  M%  XESU4PM,     nnnn[        [        U4S
 jT 5       5      S[        UU4S jU 5       5      -  5      =(       d    SmU H^  u  pEpv[        UR                  TT5      T5      n	UR                  US5      n
TR                  XEUS4XSS9  TR                  XTUS4SU
* SS9  M`     TTR                  S'   [        T5        T$ s  snnnnf s  snnnf )z4Build a residual network and initialize a zero flow.c              3   b   >#    U  H$  nTR                   U   R                  TS 5      v   M&     g7fr   Nnodesget.0r   r   demands     r!   	<genexpr>*_build_residual_network.<locals>.<genexpr>,   s'     
0a1771:>>&!$$as   ,/r   zSum of the demands should be 0.c              3   l   >#    U  H)  oTR                   U   R                  TS 5      * S S.4v   M+     g7f)r   )excess	potentialNr(   r+   s     r!   r.   r/   0   s2      MN
vq11BCQs   14r   Tdatar   )r4   keysc              3   Z   >#    U  H   n[        TR                  U   S    5      v   M"     g7f)r1   N)absr)   r,   r   r   s     r!   r.   r/   Q   s&     51aAGGAJx())1s   (+   c              3   Z   >#    U  H   u  pp4TU;   d  M  UT   T:w  d  M  UT   v   M"     g 7f)N )r,   r   r   r    r   r   r   s        r!   r.   r/   S   s:      "+JA!q= %&x[C%7 ("+s   +++   )keyr   r   flowF)sumr   NetworkXUnfeasibleMultiDiGraphr   r   selfloop_edgesr*   r   is_multigraphedgesmaxr   r   r   r"   )r   r-   r   r   r   r   r   r    	edge_listrr   r   r   s   ```        @@r!   _build_residual_networkrH   )   sI    
0a
00A5##$EFF
A MN  ,C$$QT2a55aAEE(C$8C$?&&8  3 	  gg4dg;
;
av %%#.2 Q1L; 	 
	 777-
-av %%#.2 Q1aL- 	 
 	5155 "+ 	
 		    
ah$c*EE&! 	


1aY1
E	

1aZ!QBQ
G   AGGEN!HU

s$   ?H<
H<
'H<
II0	Ic                 6   [        S5      n0 nU R                  5       (       a  U  H  n0 XV'   X   R                  5        Hd  u  pxUR                  5        V	V
s0 s H<  u  pU	Xg:w  d+  U
R                  X$5      S::  d  U
R                  US5      S:  a  SOX   _M>     sn
n	XV   U'   Mf     X   R                  5        H:  u  pxXuU   ;   d  M  XV   U   R	                  S UR                  5        5       5        M<     M     U$ U  H  nX   R                  5        VV
s0 s H<  u  pzUXg:w  d+  U
R                  X$5      S::  d  U
R                  US5      S:  a  SOX   _M>     sn
nXV'   XV   R	                  S X   R                  5        5       5        M     U$ s  sn
n	f s  sn
nf )z0Build a flow dictionary from a residual network.r   r   c              3   P   #    U  H  u  pUS    S:  d  M  US   US    4v   M     g7fr>   r   Nr;   )r,   r    r   s      r!   r.   #_build_flow_dict.<locals>.<genexpr>   s0      +6@daAfIPQM)1qy)js   &&c              3   x   #    U  H0  u  pUR                  5         H  nUS    S:  d  M  XS    4v   M     M2     g7frK   )values)r,   r   esr   s       r!   r.   rL      s?       )EAAV9q= fI$ )s   #::)r   rC   r   r*   update)r   r   r   r   r   	flow_dictr   r   rO   r    r   s              r!   _build_flow_dictrR   l   s   
,CIAIL !#

# !+  FaeeH&:a&?155QRCSWXCX  [) !+
#	Q & !$LO** +68hhj+  & H ! A DJJL )DA !%%"6!";quuVQ?OST?T %
 )IL L  TZZ\     C
#$s   AF
AFr-   r   )r   r   )
node_attrs
edge_attrsr   r   c           
      	  ^^^&^' [        XTT5      m&[        S5      m'[        UU'U4S j[        R                  " U SS9 5       5      n[        [        T'* /S T&R                  SS9 5       5      5      nUT'* :X  a  U[        U T&TT5      4$ T&R                  nT&R                  nS[        [        US5      5      -  n	U	S:  Ga;  T& H  n
Xz   S   nX   R                  5        H  u  pUR                  5        H  u  pUS	   US
   -
  nUS   U-
  X|   S   -   S:  d  M&  US	   US
   -
  nUU	:  d  M9  US
==   U-  ss'   X   U
   US   US   (       + 4   S
==   U-  ss'   Xz   S==   U-  ss'   X|   S==   U-  ss'   M     M     M     [        5       n[        5       nUR                  nUR                   nUR                  nUR                   nT& H+  n
Xz   S   nUU	:  a
  U" U
5        M  UU	* ::  d  M#  U" U
5        M-     U(       Ga  U(       Ga  [#        U5      nSn0 nUS0nU" 5       nUR$                  nUR&                  nU" US5        U(       a  UR)                  5       u  n
nUUU
'   U
U;   a  U
nOXz   S   nX   R                  5        H|  u  pUU;   a  M  T'n UR                  5        H)  u  pUS	   US
   -
  U	:  d  M  US   n!U!U :  d  M#  U!n Un"Un#M+     U T':X  a  MT  UU -   U-
  X|   S   -   n$U" UU$5      (       d  Mt  U
W"W#4UU'   M~     U(       a  M  Ub  W
U:w  aA  U
nUU   u  pnUS
==   U	-  ss'   X   U
   US   US   (       + 4   S
==   U	-  ss'   U
U:w  a  MA  UU   S==   U	-  ss'   UU   S==   U	-  ss'   UU   S   U	:  a  U" U5        UU   S   U	* :  a  U" U5        UU   n%UR                  5        H  u  n
nXz   S==   UU%-
  -  ss'   M     OU" U5        U(       a
  U(       a  GM  U	S-  n	U	S:  a  GM;  [+        U&4S jT& 5       5      (       a  [        R,                  " S5      eT& HM  n
X   R                  5        H4  u  pUR/                  5        H  nUS
   nUS:  d  M  UUUS   -  -  nM     M6     MO     U[        U T&TT5      4$ )aP  Find a minimum cost flow satisfying all demands in digraph G.

This is a capacity scaling successive shortest augmenting path algorithm.

G is a digraph with edge costs and capacities and in which nodes
have demand, i.e., they want to send or receive some amount of
flow. A negative demand means that the node wants to send flow, a
positive demand means that the node want to receive flow. A flow on
the digraph G satisfies all demand if the net flow into each node
is equal to the demand of that node.

Parameters
----------
G : NetworkX graph
    DiGraph or MultiDiGraph on which a minimum cost flow satisfying all
    demands is to be found.

demand : string
    Nodes of the graph G are expected to have an attribute demand
    that indicates how much flow a node wants to send (negative
    demand) or receive (positive demand). Note that the sum of the
    demands should be 0 otherwise the problem in not feasible. If
    this attribute is not present, a node is considered to have 0
    demand. Default value: 'demand'.

capacity : string
    Edges of the graph G are expected to have an attribute capacity
    that indicates how much flow the edge can support. If this
    attribute is not present, the edge is considered to have
    infinite capacity. Default value: 'capacity'.

weight : string
    Edges of the graph G are expected to have an attribute weight
    that indicates the cost incurred by sending one unit of flow on
    that edge. If not present, the weight is considered to be 0.
    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
-------
flowCost : integer
    Cost of a minimum cost flow satisfying all demands.

flowDict : dictionary
    If G is a digraph, a dict-of-dicts keyed by nodes such that
    flowDict[u][v] is the flow on edge (u, v).
    If G is a MultiDiGraph, a dict-of-dicts-of-dicts keyed by nodes
    so that flowDict[u][v][key] is the flow on edge (u, v, key).

Raises
------
NetworkXError
    This exception is raised if the input graph is not directed,
    not connected.

NetworkXUnfeasible
    This exception is raised in the following situations:

        * The sum of the demands is not zero. Then, there is no
          flow satisfying all demands.
        * There is no flow satisfying all demand.

NetworkXUnbounded
    This exception is raised if the digraph G has a cycle of
    negative cost and infinite capacity. Then, the cost of a flow
    satisfying all demands is unbounded below.

Notes
-----
This algorithm does not work if edge weights are floating-point numbers.

See also
--------
:meth:`network_simplex`

Examples
--------
A simple example of a min cost flow problem.

>>> G = nx.DiGraph()
>>> G.add_node("a", demand=-5)
>>> G.add_node("d", demand=5)
>>> G.add_edge("a", "b", weight=3, capacity=4)
>>> G.add_edge("a", "c", weight=6, capacity=10)
>>> G.add_edge("b", "d", weight=1, capacity=9)
>>> G.add_edge("c", "d", weight=2, capacity=5)
>>> flowCost, flowDict = nx.capacity_scaling(G)
>>> flowCost
24
>>> flowDict
{'a': {'b': 4, 'c': 1}, 'd': {}, 'b': {'d': 4}, 'c': {'d': 1}}

It is possible to change the name of the attributes used for the
algorithm.

>>> G = nx.DiGraph()
>>> G.add_node("p", spam=-4)
>>> G.add_node("q", spam=2)
>>> G.add_node("a", spam=-2)
>>> G.add_node("d", spam=-1)
>>> G.add_node("t", spam=2)
>>> G.add_node("w", spam=3)
>>> G.add_edge("p", "q", cost=7, vacancies=5)
>>> G.add_edge("p", "a", cost=1, vacancies=4)
>>> G.add_edge("q", "d", cost=2, vacancies=3)
>>> G.add_edge("t", "q", cost=1, vacancies=2)
>>> G.add_edge("a", "t", cost=2, vacancies=4)
>>> G.add_edge("d", "w", cost=3, vacancies=4)
>>> G.add_edge("t", "w", cost=4, vacancies=1)
>>> flowCost, flowDict = nx.capacity_scaling(
...     G, demand="spam", capacity="vacancies", weight="cost"
... )
>>> flowCost
37
>>> flowDict
{'p': {'q': 2, 'a': 2}, 'q': {'d': 1}, 'a': {'t': 4}, 'd': {'w': 2}, 't': {'q': 1, 'w': 1}, 'w': {}}
r   c              3      >#    U  HA  u  pnUR                  TT5      S ::  d  UR                  TS 5      S :  a  S O
UT   UT   -  v   MC     g7fr'   )r*   )r,   r   r   r   r   r   r   s       r!   r.   #capacity_scaling.<locals>.<genexpr>!  s\       7GA! 5531$fa(8A(= 	
x[1V9$	% 7s   A	ATr3   c              3   0   #    U  H  u  po3S    v   M     g7f)r   Nr;   )r,   r   r   r   s       r!   r.   rW   )  s     N;Ma
m;Ms   r9   r<   r2   r   r>   r   r   r1   Nc              3   N   >#    U  H  nTR                   U   S    S:g  v   M     g7f)r1   r   N)r)   r8   s     r!   r.   rW     s#     
0a1771:h1$as   "%zNo flow satisfying all demands.)rH   r   r?   r   rB   rE   r   rD   rR   r)   succintr   r   setaddremover   insertr*   popanyr@   rN   )(r   r-   r   r   heap	flow_costwmaxR_nodesR_succdeltar   p_ur   rO   r    r   r>   STS_addS_removeT_addT_remover1   stdpredhh_inserth_getd_uwminr   kminemind_vd_tr   r   s(     ``                                  @@r!   r   r      s   J 	 8V<A
,C  ((6	 I usdVN1777;MNOPDt|*1a6BBBggGVVFSq\""E
1* A*[)C*HHJDAZ=1V94D{S(7:k+BBQF }qy85=fI-I"IaL!A$AaD)9:6BdJB#Jx0D80#Jx0D80 ' +  EE8888AZ)FaE6!a  A!!$AA At9DAxxHEEEQN3!6Aj-#Y__.EAAv D "
Z=1V94= !(A 4x'('('( !+ s{ *s*WZ-DDC3''#$dD/Q# / !4 }1fA"1gGA!fI&IIaL!A$AaD!126:eC:	 1f 
8$-$
8$-$1:h'%/QK1:h'5&0QKdggiFAsJ{+sSy8+ ( s AAt 	!q 1*t 
0a
000##$EFF Y__&EAYY[y!8(!33I ! '  &q!Xv>>>r#   )__doc____all__	itertoolsr   mathr   networkxr   utilsr   r   r	   r"   rH   rR   _dispatchabler   r   r;   r#   r!   <module>r      s    
    G G
4 \"? #?D)X u$K H:|?|?r#   