ó
    …~i
b  ã                   ó´   • S r S/rSSKJrJrJr  SSKJrJr  SSK	r
SSKJr   " S S5      r\" S	5      \
R                  " S
\" S5      SS.S9SS j5       5       rg)z<
Minimum cost flow algorithms on directed connected graphs.
Únetwork_simplexé    )ÚchainÚisliceÚrepeat)ÚceilÚsqrtN)Únot_implemented_forc                   ót   • \ rS rSr SS jrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rSrg)Ú_DataEssentialsAndFunctionsé   c                 ó|  ^^
• [        U5      U l        [        U R                  5       VVs0 s H  u  pgXv_M	     snnU l        U R                   Vs/ s H!  oqR                  U   R                  US5      PM#     snU l        / U l        / U l        U(       a  / U l	        0 U l
        / U l        / U l        U(       d  UR                  SS9nOUR                  SSS9n[        S5      m
UU
4S jU 5       n[        U5       Hí  u  piU R                  R                  U R                  U	S      5        U R                  R                  U R                  U	S      5        U(       a  U R                  R                  U	S   5        X`R                  U	S S	 '   U R                  R                  U	S	   R                  TT
5      5        U R                  R                  U	S	   R                  US5      5        Mï     S U l        S U l        S U l        S U l        S U l        S U l        S U l        S U l        S U l        S
U l        g s  snnf s  snf )Nr   T©Údata©r   ÚkeysÚinfc              3   óx   >#   • U  H/  oS    US   :w  d  M  US   R                  TT5      S :w  d  M+  Uv •  M1     g7f)r   é   éÿÿÿÿN©Úget)Ú.0ÚeÚcapacityr   s     €€Úd/home/mande/repo/quber/.venv/lib/python3.13/site-packages/networkx/algorithms/flow/networksimplex.pyÚ	<genexpr>Ú7_DataEssentialsAndFunctions.__init__.<locals>.<genexpr>'   s8   øé € ÐTšE�q q¡T¨Q¨q©T¡\“°a¸±e·i±iÀÈ#Ó6NÐRSÑ6S—‘šEùs   ƒ:–:±	:r   é   r   F)ÚlistÚ	node_listÚ	enumerateÚnode_indicesÚnodesr   Únode_demandsÚedge_sourcesÚedge_targetsÚ	edge_keysÚedge_indicesÚedge_capacitiesÚedge_weightsÚedgesÚfloatÚappendÚ
edge_countÚ	edge_flowÚnode_potentialsÚparentÚparent_edgeÚsubtree_sizeÚnext_node_dftÚprev_node_dftÚlast_descendent_dftÚ_spanning_tree_initialized)ÚselfÚGÚ
multigraphÚdemandr   ÚweightÚiÚur+   r   r   s       `     @r   Ú__init__Ú$_DataEssentialsAndFunctions.__init__   sû  ù€ ô ˜a›ˆŒÜ.7¸¿¹Ô.GÔHÒ.G¡d a˜QšTÑ.GÒHˆÔà/3¯~ª~ó
Ú/=¨!�G‰G�A‰J�N‰N˜6 1Ö%©~ñ
ˆÔð ˆÔØˆÔÞØˆDŒNØˆÔØ!ˆÔØˆÔæØ—G‘G �GÐ&‰Eà—G‘G ¨D�GÐ1ˆEä�E‹lˆÝT™EÓTˆÜ˜eÖ$‰DˆAØ×Ñ×$Ñ$ T×%6Ñ%6°q¸±tÑ%<Ô=Ø×Ñ×$Ñ$ T×%6Ñ%6°q¸±tÑ%<Ô=ÞØ—‘×%Ñ% a¨¡dÔ+Ø()×Ñ˜a  ˜fÑ%Ø× Ñ ×'Ñ'¨¨"©¯	©	°(¸CÓ(@ÔAØ×Ñ×$Ñ$ Q r¡U§Y¡Y¨v°qÓ%9Ö:ñ %ð ˆŒØˆŒØ#ˆÔØˆŒØˆÔØ ˆÔØ!ˆÔØ!ˆÔØ#'ˆÔ àð 	Õ'ùóQ Iùò
s   «H3Á(H9c                 óì  • [        U R                  5      U l        [        [	        [        SU R                  5      S U R                   5       5      5      U l        U R                   Vs/ s H  o3S::  a  UOU* PM     snU l        [        [	        [        SU5      S /5      5      U l	        [        [        U R                  U R                  U-   5      5      U l        [        [	        [        SU5      US-   /5      5      U l        [        [	        [        SU5      SS/5      5      U l        [        [        SU5      5      U l        [        [	        [        U5      US-
  /5      5      U l        SU l        g s  snf )Nr   c              3   ó8   #   • U  H  n[        U5      v •  M     g 7f©N©Úabs©r   Úds     r   r   ÚG_DataEssentialsAndFunctions.initialize_spanning_tree.<locals>.<genexpr>C   s   é € Ð.QÒ?P¸!¬s°1¯v¨vÒ?Pùó   ‚r   r   T)Úlenr(   r.   r   r   r   r$   r/   r0   r1   Úranger2   r3   r4   r5   r6   r7   )r8   ÚnÚfaux_infrG   s       r   Úinitialize_spanning_treeÚ4_DataEssentialsAndFunctions.initialize_spanning_tree@   s:  € Ü˜d×/Ñ/Ó0ˆŒÜÜ”&˜˜DŸO™OÓ,Ñ.Q¸t×?PÒ?PÓ.QÓRó
ˆŒð 8<×7HÒ7Hó 
Ú7H°!˜Q›‰H X IÒ-Ñ7Hñ 
ˆÔô œ5¤¨¨A£°°Ó7Ó8ˆŒÜÜ�$—/‘/ 4§?¡?°QÑ#6Ó7ó
ˆÔô !¤¤v¨a°£|°a¸!±e°WÓ!=Ó>ˆÔÜ!Ü”%˜˜1“+  A˜wÓ'ó
ˆÔô "¤%¨¨A£,Ó/ˆÔÜ#'Ü”%˜“(˜Q ™U˜GÓ$ó$
ˆÔ ð +/ˆÕ'ùò 
s   Á(E1c                 ó~  • U R                   U   nU R                   U   n X4:  a%  U R                  U   nU R                   U   nX4:  a  M%  X4:”  a%  U R                  U   nU R                   U   nX4:”  a  M%  X4:X  aD  X:w  a=  U R                  U   nU R                   U   nU R                  U   nU R                   U   nOU$ MŸ  )zH
Find the lowest common ancestor of nodes p and q in the spanning tree.
)r3   r1   )r8   ÚpÚqÚsize_pÚsize_qs        r   Ú	find_apexÚ%_DataEssentialsAndFunctions.find_apexV   sÌ   € ð ×"Ñ" 1Ñ%ˆØ×"Ñ" 1Ñ%ˆØØ“/Ø—K‘K ‘N�Ø×*Ñ*¨1Ñ-�ð •/ð “/Ø—K‘K ‘N�Ø×*Ñ*¨1Ñ-�ð •/ð ÓØ“6ØŸ™ A™�AØ!×.Ñ.¨qÑ1�FØŸ™ A™�AØ!×.Ñ.¨qÑ1‘Fà�Hñ ó    c                 ó¦   • U/n/ nX:w  aE  UR                  U R                  U   5        U R                  U   nUR                  U5        X:w  a  ME  X44$ )zH
Returns the nodes and edges on the path from node p to its ancestor w.
)r-   r2   r1   )r8   rQ   ÚwÚWnÚWes        r   Ú
trace_pathÚ&_DataEssentialsAndFunctions.trace_pathl   sT   € ð ˆSˆØˆØ‹fØ�I‰I�d×&Ñ& qÑ)Ô*Ø—‘˜A‘ˆAØ�I‰I�aŒLð �fð ˆvˆrW   c                 óú   • U R                  X#5      nU R                  X$5      u  pVUR                  5         UR                  5         Xa/:w  a  UR                  U5        U R                  X45      u  pxUS	 XW-  nXh-  nXV4$ )zª
Returns the nodes and edges on the cycle containing edge i == (p, q)
when the latter is added to the spanning tree.

The cycle is oriented in the direction from p to q.
r   )rU   r\   Úreverser-   )	r8   r=   rQ   rR   rY   rZ   r[   ÚWnRÚWeRs	            r   Ú
find_cycleÚ&_DataEssentialsAndFunctions.find_cyclex   sr   € ð �N‰N˜1Ó ˆØ—‘ Ó&‰ˆØ
�
‰
ŒØ
�
‰
ŒØ�‹9Ø�I‰I�aŒLØ—?‘? 1Ó(‰ˆØ�ˆGØ
‰	ˆØ
‰	ˆØˆvˆrW   c                 ó²   • [        X!5       HH  u  pEU R                  U   U:X  a  U R                  U==   U-  ss'   M1  U R                  U==   U-  ss'   MJ     g)zA
Augment f units of flow along a cycle represented by Wn and We.
N)Úzipr%   r/   )r8   rZ   r[   Úfr=   rQ   s         r   Úaugment_flowÚ(_DataEssentialsAndFunctions.augment_flow‹   sO   € ô ˜–K‰DˆAØ× Ñ  Ñ# qÓ(Ø—‘˜qÓ! QÑ&Õ!à—‘˜qÓ! QÑ&Õ!ò	  rW   c              #   ór   #   • Uv •  U R                   U   nX:w  a  U R                  U   nUv •  X:w  a  M  gg7f)z4
Yield the nodes in the subtree rooted at a node p.
N)r6   r4   )r8   rQ   Úls      r   Útrace_subtreeÚ)_DataEssentialsAndFunctions.trace_subtree•   s=   é € ð ŠØ×$Ñ$ QÑ'ˆØ‹fØ×"Ñ" 1Ñ%ˆAØŠGð �fùs   ‚17µ7c                 óÈ  • U R                   U   nU R                  U   nU R                  U   nU R                  U   nSU R                  U'   SU R
                  U'   X`R                  U'   X@R                  U'   X R                  U'   XPR                  U'   UbM  U R                   U==   U-  ss'   U R                  U   U:X  a  X@R                  U'   U R                  U   nUb  ML  gg)zD
Remove an edge (s, t) where parent[t] == s from the spanning tree.
N)r3   r5   r6   r4   r1   r2   )r8   ÚsÚtÚsize_tÚprev_tÚlast_tÚnext_last_ts          r   Úremove_edgeÚ'_DataEssentialsAndFunctions.remove_edgeŸ   së   € ð ×"Ñ" 1Ñ%ˆØ×#Ñ# AÑ&ˆØ×)Ñ)¨!Ñ,ˆØ×(Ñ(¨Ñ0ˆàˆ�‰�A‰Ø"ˆ×Ñ˜Ñà%0×Ñ˜6Ñ"Ø*0×Ñ˜;Ñ'Ø%&×Ñ˜6Ñ"Ø &×Ñ˜1Ñð ‰mØ×Ñ˜aÓ  FÑ*Ó Ø×'Ñ'¨Ñ*¨fÓ4Ø.4×(Ñ(¨Ñ+Ø—‘˜A‘ˆAð	 �mrW   c                 ó`  • / nUb%  UR                  U5        U R                  U   nUb  M%  UR                  5         [        U[	        USS5      5       GHX  u  p1U R
                  U   nU R                  U   nU R                  U   nU R                  U   nU R                  U   nXR                  U'   SU R                  U'   U R                  U   U R                  U'   SU R                  U'   X@R
                  U   -
  U R
                  U'   X@R
                  U'   X€R                  U'   X`R                  U'   XR                  U'   XpR                  U'   XW:X  a  X`R                  U'   UnXpR                  U'   X0R                  U'   XR                  U'   XPR                  U'   XPR                  U'   GM[     g)z3
Make a node q the root of its containing subtree.
Nr   )
r-   r1   r_   re   r   r3   r6   r5   r4   r2   )	r8   rR   Ú	ancestorsrQ   rS   Úlast_pÚprev_qÚlast_qÚnext_last_qs	            r   Ú	make_rootÚ%_DataEssentialsAndFunctions.make_root·   s²  € ð ˆ	Ø‰mØ×Ñ˜QÔØ—‘˜A‘ˆAð ‹mð 	×ÑÔÜ˜	¤6¨)°Q¸Ó#=×>‰DˆAØ×&Ñ& qÑ)ˆFØ×-Ñ-¨aÑ0ˆFØ×'Ñ'¨Ñ*ˆFØ×-Ñ-¨aÑ0ˆFØ×,Ñ,¨VÑ4ˆKà�K‰K˜‰NØ!ˆD�K‰K˜‰NØ"&×"2Ñ"2°1Ñ"5ˆD×Ñ˜QÑØ"&ˆD×Ñ˜QÑØ#)×,=Ñ,=¸aÑ,@Ñ#@ˆD×Ñ˜aÑ Ø#)×Ñ˜aÑ à)4×Ñ˜vÑ&Ø.4×Ñ˜{Ñ+Ø)*×Ñ˜vÑ&Ø$*×Ñ˜qÑ!ØÓØ.4×(Ñ(¨Ñ+Ø�ð %+×Ñ˜qÑ!Ø)*×Ñ˜vÑ&Ø)*×Ñ˜vÑ&Ø$*×Ñ˜qÑ!Ø*0×$Ñ$ QÔ'ò7 ?rW   c                 óÄ  • U R                   U   nU R                  U   nU R                  U   nU R                   U   nX R                  U'   XR                  U'   X0R                  U'   X@R
                  U'   XpR
                  U'   XPR                  U'   UbM  U R                  U==   U-  ss'   U R                   U   U:X  a  XpR                   U'   U R                  U   nUb  ML  gg)zK
Add an edge (p, q) to the spanning tree where q is the root of a subtree.
N)r6   r4   r3   r1   r2   r5   )r8   r=   rQ   rR   rx   Únext_last_prT   rz   s           r   Úadd_edgeÚ$_DataEssentialsAndFunctions.add_edgeÝ   sç   € ð ×)Ñ)¨!Ñ,ˆØ×(Ñ(¨Ñ0ˆØ×"Ñ" 1Ñ%ˆØ×)Ñ)¨!Ñ,ˆà�‰�A‰Ø×Ñ˜Ñà%&×Ñ˜6Ñ"Ø &×Ñ˜1ÑØ*0×Ñ˜;Ñ'Ø%0×Ñ˜6Ñ"ð ‰mØ×Ñ˜aÓ  FÑ*Ó Ø×'Ñ'¨Ñ*¨fÓ4Ø.4×(Ñ(¨Ñ+Ø—‘˜A‘ˆAð	 �mrW   c                 óD  • X0R                   U   :X  a0  U R                  U   U R                  U   -
  U R                  U   -
  nO/U R                  U   U R                  U   -   U R                  U   -
  nU R                  U5       H  nU R                  U==   U-  ss'   M     g)zn
Update the potentials of the nodes in the subtree rooted at a node
q connected to its parent p by an edge i.
N)r&   r0   r*   rk   )r8   r=   rQ   rR   rG   s        r   Úupdate_potentialsÚ-_DataEssentialsAndFunctions.update_potentialsõ   s¢   € ð
 ×!Ñ! !Ñ$Ó$Ø×$Ñ$ QÑ'¨$×*;Ñ*;¸AÑ*>Ñ>À×AUÑAUÐVWÑAXÑX‰Aà×$Ñ$ QÑ'¨$×*;Ñ*;¸AÑ*>Ñ>À×AUÑAUÐVWÑAXÑXˆAØ×#Ñ# AÖ&ˆAØ× Ñ  Ó# qÑ(Õ#ò 'rW   c                 óÄ   • U R                   U   U R                  U R                  U      -
  U R                  U R                  U      -   nU R                  U   S:X  a  U$ U* $ )z&Returns the reduced cost of an edge i.r   )r*   r0   r%   r&   r/   )r8   r=   Úcs      r   Úreduced_costÚ(_DataEssentialsAndFunctions.reduced_cost  sr   € ð ×Ñ˜aÑ Ø×"Ñ" 4×#4Ñ#4°QÑ#7Ñ8ñ9à×"Ñ" 4×#4Ñ#4°QÑ#7Ñ8ñ9ð 	
ð
 —N‘N 1Ñ%¨Ó*ˆqÐ2°°Ð2rW   c              #   ó†  #   • U R                   S:X  a  g[        [        [        U R                   5      5      5      nU R                   U-   S-
  U-  nSnSnX2:  aç  XA-   nXPR                   ::  a  [	        XE5      nO6XPR                   -  n[        [	        X@R                   5      [	        U5      5      nUn[        X`R                  S9nU R                  U5      nUS:¼  a  US-  nOXU R                  U   S:X  a  U R                  U   n	U R                  U   n
OU R                  U   n	U R                  U   n
XyU
4v •  SnX2:  a  Mæ  gg7f)z-Yield entering edges until none can be found.r   Nr   ©Úkey)r.   Úintr   r   rK   r   Úminr‡   r/   r%   r&   )r8   ÚBÚMÚmrf   rj   r+   r=   r†   rQ   rR   s              r   Úfind_entering_edgesÚ/_DataEssentialsAndFunctions.find_entering_edges
  s1  é € à�?‰?˜aÓØô ””T˜$Ÿ/™/Ó*Ó+Ó,ˆØ�_‰_˜qÑ  1Ñ$¨Ñ*ˆØˆàˆØ‹eà‘ˆAØ—O‘OÓ#Ü˜a›‘à—_‘_Ñ$�Üœe A§¡Ó7¼¸q»ÓB�ØˆAä�E×0Ñ0Ñ1ˆAØ×!Ñ! !Ó$ˆAØ�A‹và�Q‘‘ð —>‘> !Ñ$¨Ó)Ø×)Ñ)¨!Ñ,�AØ×)Ñ)¨!Ñ,‘Aà×)Ñ)¨!Ñ,�AØ×)Ñ)¨!Ñ,�AØ˜A�g’Ø�ð1 �eùs   ‚D;EÄ?Ec                 ó„   • U R                   U   U:X  a  U R                  U   U R                  U   -
  $ U R                  U   $ )zVReturns the residual capacity of an edge i in the direction away
from its endpoint p.
)r%   r)   r/   )r8   r=   rQ   s      r   Úresidual_capacityÚ-_DataEssentialsAndFunctions.residual_capacity4  sM   € ð × Ñ  Ñ# qÓ(ð × Ñ  Ñ# d§n¡n°QÑ&7Ñ7ð	
ð —‘ Ñ"ð	
rW   c                 óÆ   ^ • [        [        [        U5      [        U5      5      U 4S jS9u  p4T R                  U   U:X  a  T R                  U   OT R                  U   nX4U4$ )z=Returns the leaving edge in a cycle represented by Wn and We.c                 ó"   >• TR                   " U 6 $ rC   )r”   )Úi_pr8   s    €r   Ú<lambda>Ú?_DataEssentialsAndFunctions.find_leaving_edge.<locals>.<lambda>B  s   ø€ ˜D×2Ò2°CÑ8rW   rŠ   )r�   re   Úreversedr%   r&   )r8   rZ   r[   Újrn   ro   s   `     r   Úfind_leaving_edgeÚ-_DataEssentialsAndFunctions.find_leaving_edge>  s_   ø€ äÜ”˜“œh r›lÓ+Ü8ñ
‰ˆð %)×$5Ñ$5°aÑ$8¸AÓ$=ˆD×Ñ˜aÒ À4×CTÑCTÐUVÑCWˆØ�QˆwˆrW   )r7   r)   r.   r/   r(   r'   r%   r&   r*   r6   r4   r$   r"   r    r0   r1   r2   r5   r3   N©r;   r   r<   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r?   rN   rU   r\   rb   rg   rk   rt   r|   r€   rƒ   r‡   r‘   r”   r�   Ú__static_attributes__© rW   r   r   r      sS   † àJRô/
òb/ò,ò,
òò&'òòò0$1òLò0
)ò3ò&òT
õrW   r   Ú
undirectedr;   r   )r   r<   )Ú
node_attrsÚ
edge_attrsc           	      ó”  ^^^^^^• [        U 5      S:X  a  [        R                  " S5      eU R                  5       n[	        XUTTS9m[        S5      m[        TR                  TR                  5       H/  u  pV[        U5      T:X  d  M  [        R                  " SU< S35      e   [        TR                  TR                  5       H/  u  px[        U5      T:X  d  M  [        R                  " SU< S35      e   U(       d  [        R                  " U S	S
9n	O[        R                  " U S	S	S9n	U	 HC  n[        US   R                  TS5      5      T:X  d  M'  [        R                  " SUSS < S35      e   [        TR                  5      S:w  a  [        R                  " S5      e[        TR                  TR                   5       H&  u  pzU
S:  d  M  [        R                  " SU< S35      e   U(       d  [        R                  " U S	S
9n	O[        R                  " U S	S	S9n	U	 H:  nUS   R                  TT5      S:  d  M  [        R                  " SUSS < S35      e   [#        TR                  5       Hy  u  p¶US:”  a8  TR$                  R'                  S5        TR(                  R'                  U5        MC  TR$                  R'                  U5        TR(                  R'                  S5        M{     S[+        [        U4S jTR                    5       5      [        S TR                   5       5      [        S TR                   5       5      5      -  =(       d    Sm[        TR                  5      nTR                  R-                  [/        TU5      5        TR                   R-                  [/        TU5      5        TR1                  UT5        TR3                  5        Hà  u  p½nTR5                  X½U5      u  nnTR7                  UU5      u  nnnTR9                  UUTR;                  UU5      5        UU:w  d  M\  TR<                  U   U:w  a  UUnnUR?                  U5      UR?                  U5      :”  a  XípíTRA                  UU5        TRC                  U5        TRE                  X½U5        TRG                  X½U5        Mâ     [I        U4S j[K        U* S5       5       5      (       a  [        R                  " S5      e[I        UU4S j[K        TRL                  5       5       5      (       d/  [I        UUU4S j[        R                  " U S	S
9 5       5      (       a  [        RN                  " S5      eTRP                  TRL                  S2	 [        S [        TR                  TRP                  5       5       5      nTR                   Vs0 s H  oÌ0 _M     snmU4S jnU4S jTR$                   5       Tl        U4S jTR(                   5       Tl        U(       dJ  [        TR$                  TR(                  TRP                  5       H  nU" U5        M     U RS                  S	S
9n	OU[        TR$                  TR(                  TRT                  TRP                  5       H  nU" U5        M     U RS                  S	S	S9n	U	 Hƒ  nUS   US   :w  a+  US   R                  TT5      S:X  a  U" USS S-   5        M8  M:  US   R                  TS5      nUS:¼  a  U" USS S-   5        Me  US   T   n
UXŠ-  -  nU" USS U
4-   5        M…     UT4$ s  snf )a8  Find a minimum cost flow satisfying all demands in digraph G.

This is a primal network simplex algorithm that uses the leaving
arc rule to prevent cycling.

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 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'.

Returns
-------
flowCost : integer, float
    Cost of a minimum cost flow satisfying all demands.

flowDict : dictionary
    Dictionary of dictionaries keyed by nodes such that
    flowDict[u][v] is the flow edge (u, v).

Raises
------
NetworkXError
    This exception is raised if the input graph is not directed or
    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 is not guaranteed to work if edge weights or demands
are floating point numbers (overflows and roundoff errors can
cause problems). As a workaround you can use integer numbers by
multiplying the relevant edge attributes by a convenient
constant factor (eg 100).

See also
--------
cost_of_flow, max_flow_min_cost, min_cost_flow, min_cost_flow_cost

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.network_simplex(G)
>>> flowCost
24
>>> flowDict
{'a': {'b': 4, 'c': 1}, 'd': {}, 'b': {'d': 4}, 'c': {'d': 1}}

The mincost flow algorithm can also be used to solve shortest path
problems. To find the shortest path between two nodes u and v,
give all edges an infinite capacity, give node u a demand of -1 and
node v a demand a 1. Then run the network simplex. The value of a
min cost flow will be the distance between u and v and edges
carrying positive flow will indicate the path.

>>> G = nx.DiGraph()
>>> G.add_weighted_edges_from(
...     [
...         ("s", "u", 10),
...         ("s", "x", 5),
...         ("u", "v", 1),
...         ("u", "x", 2),
...         ("v", "y", 1),
...         ("x", "u", 3),
...         ("x", "v", 5),
...         ("x", "y", 2),
...         ("y", "s", 7),
...         ("y", "v", 6),
...     ]
... )
>>> G.add_node("s", demand=-1)
>>> G.add_node("v", demand=1)
>>> flowCost, flowDict = nx.network_simplex(G)
>>> flowCost == nx.shortest_path_length(G, "s", "v", weight="weight")
True
>>> sorted([(u, v) for u in flowDict for v in flowDict[u] if flowDict[u][v] > 0])
[('s', 'x'), ('u', 'v'), ('x', 'u')]
>>> nx.shortest_path(G, "s", "v", weight="weight")
['s', 'x', 'u', 'v']

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.network_simplex(
...     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': {}}

References
----------
.. [1] Z. Kiraly, P. Kovacs.
       Efficient implementation of minimum-cost flow algorithms.
       Acta Universitatis Sapientiae, Informatica 4(1):67--118. 2012.
.. [2] R. Barr, F. Glover, D. Klingman.
       Enhancement of spanning tree labeling procedures for network
       optimization.
       INFOR 17(1):16--34. 1979.
r   zgraph has no nodesrŸ   r   znode z has infinite demandzedge z has infinite weightTr   r   r   Nztotal node demand is not zeroz has negative capacityé   c              3   ó6   >#   • U  H  oT:  d  M
  Uv •  M     g 7frC   r¥   )r   r†   r   s     €r   r   Ú"network_simplex.<locals>.<genexpr>5  s   øé € Ð;Ò/�a°s±7—‘Ò/ùs   ƒ	�	c              3   ó8   #   • U  H  n[        U5      v •  M     g 7frC   rD   )r   rY   s     r   r   r¬   6  ó   é € Ð2Ò 1˜1”�A—�Ò 1ùrI   c              3   ó8   #   • U  H  n[        U5      v •  M     g 7frC   rD   rF   s     r   r   r¬   7  r®   rI   r   c              3   óH   >#   • U  H  nTR                   U   S :g  v •  M     g7f)r   N©r/   )r   r=   ÚDEAFs     €r   r   r¬   \  s   øé € Ð
8ª< aˆ4�>‰>˜!Ñ Ö!ª<ùs   ƒ"z"no flow satisfies all node demandsc              3   óN   >#   • U  H  nTR                   U   S -  T:¬  v •  M     g7f)r   Nr±   )r   r=   r²   rM   s     €€r   r   r¬   _  s&   øé € Ð
MÒ6L°ˆ4�>‰>˜!Ñ˜qÑ  HÖ,Ò6Lùs   ƒ"%c              3   ó’   >#   • U  H<  nUS    R                  TT5      T:H  =(       a    US    R                  TS5      S:  v •  M>     g7f)r   r   Nr   )r   r   r   r   r<   s     €€€r   r   r¬   _  sI   øé € ð Uâ0ˆAð 	
ˆ"‰�	‰	�(˜CÓ  CÑ'×D¨A¨b©E¯I©I°f¸aÓ,@À1Ñ,DÔDÚ0ùs   ƒAAz+negative cycle with infinite capacity foundc              3   ó.   #   • U  H  u  pX-  v •  M     g 7frC   r¥   )r   rY   Úxs      r   r   r¬   j  s   é € ÐMÒ&L™d˜a�A–EÒ&Lùs   ‚c                 ó~   >• TU S      nU SS  H  n X   nM
     U S   XS   '   g! [          a    0 nX1U'   Un M,  f = f)zAdd a flow dict entry.r   r   éþÿÿÿr   N)ÚKeyError)r   rG   Úkro   Ú	flow_dicts       €r   Ú	add_entryÚ"network_simplex.<locals>.add_entrym  s_   ø€ à�a˜‘d‰OˆØ�1�R“ˆAðØ‘D’ñ ð �R‘5ˆˆB‰%Šøô	 ó Ø�Ø�!‘Ø“ðús   “&¦<»<c              3   óB   >#   • U  H  nTR                   U   v •  M     g 7frC   ©r    )r   rn   r²   s     €r   r   r¬   y  ó   øé € ð Ú#4˜aˆ�‰�qÖÒ#4ùó   ƒc              3   óB   >#   • U  H  nTR                   U   v •  M     g 7frC   r¿   )r   ro   r²   s     €r   r   r¬   |  rÀ   rÁ   )r   )+rJ   ÚnxÚNetworkXErrorÚis_multigraphr   r,   re   r    r$   rE   r(   r*   Úselfloop_edgesr   ÚsumÚNetworkXUnfeasibler)   r!   r%   r-   r&   ÚmaxÚextendr   rN   r‘   rb   r�   rg   r”   r1   Úindexrt   r|   r€   rƒ   ÚanyrK   r.   ÚNetworkXUnboundedr/   r+   r'   )r9   r;   r   r<   r:   r>   rG   r   rY   r+   r†   r=   rL   rQ   rR   rZ   r[   rœ   rn   ro   Ú	flow_costr¼   r²   rM   r»   r   s     ``                  @@@@r   r   r   H  sl  ý€ ôT ˆ1ƒv�ƒ{Ü×ÒÐ3Ó4Ð4à—‘Ó"€Jô 'Ø	˜f¨xÀñ€Dô �‹,€CÜ�D—N‘N D×$5Ñ$5Ö6‰ˆÜˆq‹6�S�=Ü×"Ò" U¨1©%Ð/CÐ#DÓEÐEñ 7ô �D×%Ñ% t×'8Ñ'8Ö9‰ˆÜˆq‹6�S�=Ü×"Ò" U¨1©%Ð/CÐ#DÓEÐEñ :ö Ü×!Ò! !¨$Ñ/‰ä×!Ò! !¨$°TÑ:ˆÛˆÜˆq�‰u�y‰y˜ Ó#Ó$¨Õ+Ü×"Ò" U¨1¨S¨b¨6©*Ð4HÐ#IÓJÐJñ ô ˆ4×ÑÓ Ó"Ü×#Ò#Ð$CÓDÐDÜ�D×%Ñ% t×';Ñ';Ö<‰ˆØˆq�5Ü×'Ò'¨%°©uÐ4JÐ(KÓLÐLñ =ö Ü×!Ò! !¨$Ñ/‰ä×!Ò! !¨$°TÑ:ˆÛˆØˆR‰5�9‰9�X˜sÓ# aÕ'Ü×'Ò'¨%°°#°2°©zÐ9OÐ(PÓQÐQñ ô ˜$×+Ñ+Ö,‰ˆð ˆq‹5Ø×Ñ×$Ñ$ RÔ(Ø×Ñ×$Ñ$ QÖ'à×Ñ×$Ñ$ QÔ'Ø×Ñ×$Ñ$ RÖ(ñ -ð 	
Ü
ÜÔ;˜4×/Ò/Ó;Ó;ÜÑ2 × 1Ò 1Ó2Ó2ÜÑ2 × 1Ò 1Ó2Ó2ó
ñ	
÷ 	ð ð ô 	ˆD�N‰NÓ€AØ×Ñ×ÑœV H¨aÓ0Ô1Ø×Ñ×Ñ¤ x°Ó 3Ô4ð 	×!Ñ! ! XÔ.ð ×+Ñ+Ö-‰ˆˆaØ—‘  qÓ)‰ˆˆBØ×(Ñ(¨¨RÓ0‰ˆˆ1ˆaØ×Ñ˜"˜b $×"8Ñ"8¸¸AÓ">Ô?à��6Ø�{‰{˜1‰~ Ó"à˜!�1�Ø�x‰x˜‹{˜RŸX™X a›[Ó(à�1Ø×Ñ˜Q Ô"Ø�N‰N˜1ÔØ�M‰M˜! Ô"Ø×"Ñ" 1¨Ö+ñ .ô* Ô
8¬5°!°°Q¬<Ó
8×8Ñ8Ü×#Ò#Ð$HÓIÐIä
Õ
M´e¸D¿O¹OÔ6LÓ
M×MÑMÔQTö Uä×"Ò" 1¨4Ò0óU÷ Rñ Rô ×"Ò"Ð#PÓQÐQð 	�‰�t—‘Ñ(Ð)ÜÑM¤c¨$×*;Ñ*;¸T¿^¹^Ô&LÓMÓM€IØ $§¢Ó/¢˜1�B’¡Ñ/€Iõ
ôØ#'×#4Ò#4ó€DÔôØ#'×#4Ò#4ó€DÔö Ü�T×&Ñ&¨×(9Ñ(9¸4¿>¹>ÖJˆAÙ�aŽLñ Kà—‘˜T�Ð"‰äØ×Ñ˜t×0Ñ0°$·.±.À$Ç.Á.ö
ˆAñ �aŽLñ
ð —‘˜T¨�Ð-ˆÛˆØˆQ‰4�1�Q‘4‹<Ø�‰u�y‰y˜ 3Ó'¨1Ó,Ù˜!˜C˜R˜& 4™-Ö(ñ -ð �"‘—	‘	˜& !Ó$ˆAØ�A‹vÙ˜!˜C˜R˜& 4™-Ö(à�b‘E˜(‘O�Ø˜Q™UÑ"�	Ù˜!˜C˜R˜& A 4™-Ö(ñ ð �iÐÐùòW 0s   ×]rŸ   )Ú__doc__Ú__all__Ú	itertoolsr   r   r   Úmathr   r   ÚnetworkxrÃ   Únetworkx.utilsr	   r   Ú_dispatchabler,   r   r¥   rW   r   Ú<module>rÖ      sl   ðñð Ð
€ç +Ñ +ß ã Ý .÷wñ wñt	 �\Ó"Ø×ÒØ±°u³ÈÑ$KñóJ óó #ñJ rW   