Ë
    D^(hab  ã                   ó¸   — d Z dgZddlmZmZmZ ddlmZmZ ddl	Z
ddlmZ  G d„ d«      Z ed	«       e
j                  d
 ed«      ddœ¬«      dd„«       «       Zy)z<
Minimum cost flow algorithms on directed connected graphs.
Únetwork_simplexé    )ÚchainÚisliceÚrepeat)ÚceilÚsqrtN)Únot_implemented_forc                   ój   — e Zd Z	 dd„Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Z	d„ Z
d	„ Zd
„ Zd„ Zd„ Zd„ Zd„ Zd„ Zy)Ú_DataEssentialsAndFunctionsc                 ób  ‡‡
— t        |«      | _        t        | j                  «      D ��ci c]  \  }}||“Œ
 c}}| _        | j                  D �cg c]!  }|j                  |   j                  |d«      ‘Œ# c}| _        g | _        g | _        |rg | _	        i | _
        g | _        g | _        |s|j                  d¬«      }n|j                  dd¬«      }t        d«      Š
ˆˆ
fd„|D «       }t        |«      D ]é  \  }}	| j                  j                  | j                  |	d      «       | j                  j                  | j                  |	d      «       |r| j                  j                  |	d   «       || j                  |	d d	 <   | j                  j                  |	d	   j                  ‰‰
«      «       | j                  j                  |	d	   j                  |d«      «       Œë d | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        d
| _        y c c}}w c c}w )Nr   T©Údata©r   ÚkeysÚinfc              3   ól   •K  — | ]+  }|d    |d   k7  sŒ|d   j                  ‰‰«      d k7  sŒ(|–— Œ- y­w)r   é   éÿÿÿÿN©Úget)Ú.0ÚeÚcapacityr   s     €€úe/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/networkx/algorithms/flow/networksimplex.pyú	<genexpr>z7_DataEssentialsAndFunctions.__init__.<locals>.<genexpr>'   s6   øè ø€ ÒT�q Q q¡T¨Q¨q©T£\°a¸±e·i±iÀÈ#Ó6NÐRSÓ6S”ÑTùs   ƒ4”4­4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__z$_DataEssentialsAndFunctions.__init__   s   ù€ ô ˜a›ˆŒÜ.7¸¿¹Ó.G×H¡d a¨˜Q ™TÓHˆÔà/3¯~©~ö
Ø*+ˆA�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ˆqØ×Ñ×$Ñ$ T×%6Ñ%6°q¸±tÑ%<Ô=Ø×Ñ×$Ñ$ T×%6Ñ%6°q¸±tÑ%<Ô=ÙØ—‘×%Ñ% a¨¡dÔ+Ø()ˆD×Ñ˜a  ˜fÑ%Ø× Ñ ×'Ñ'¨¨"©¯	©	°(¸CÓ(@ÔAØ×Ñ×$Ñ$ Q r¡U§Y¡Y¨v°qÓ%9Õ:ð	;ð ˆŒØˆŒØ#ˆÔØˆŒØˆÔØ ˆÔØ!ˆÔØ!ˆÔØ#'ˆÔ àð 	Õ'ùóQ Iùò
s   «H&Á&H,c                 óæ  — t        | j                  «      | _        t        t	        t        d| j                  «      d„ | j                  D «       «      «      | _        | j                  D �cg c]  }|dk  r|n| ‘Œ c}| _        t        t	        t        d|«      d g«      «      | _	        t        t        | j                  | j                  |z   «      «      | _        t        t	        t        d|«      |dz   g«      «      | _        t        t	        t        d|«      ddg«      «      | _        t        t        d|«      «      | _        t        t	        t        |«      |dz
  g«      «      | _        d| _        y c c}w )Nr   c              3   ó2   K  — | ]  }t        |«      –— Œ y ­w©N©Úabs©r   Úds     r   r   zG_DataEssentialsAndFunctions.initialize_spanning_tree.<locals>.<genexpr>C   s   è ø€ Ò.Q¸!¬s°1¯vÑ.Qùó   ‚r   r   T)Úlenr&   r,   r   r   r   r"   r-   r.   r/   Úranger0   r1   r2   r3   r4   r5   )r6   ÚnÚfaux_infrD   s       r   Úinitialize_spanning_treez4_DataEssentialsAndFunctions.initialize_spanning_tree@   s7  € Ü˜d×/Ñ/Ó0ˆŒÜÜ”&˜˜DŸO™OÓ,Ñ.Q¸t×?PÑ?PÔ.QÓRó
ˆŒð 8<×7HÑ7Hö 
Ø23˜˜Qš‰H X IÑ-ò 
ˆÔô œ5¤¨¨A£°°Ó7Ó8ˆŒÜÜ�$—/‘/ 4§?¡?°QÑ#6Ó7ó
ˆÔô !¤¤v¨a°£|°a¸!±e°WÓ!=Ó>ˆÔÜ!Ü”%˜˜1“+  A˜wÓ'ó
ˆÔô "¤%¨¨A£,Ó/ˆÔÜ#'Ü”%˜“(˜Q ™U˜GÓ$ó$
ˆÔ ð +/ˆÕ'ùò 
s   Á(E.c                 óx  — | j                   |   }| j                   |   }	 ||k  r$| j                  |   }| j                   |   }||k  rŒ$||kD  r$| j                  |   }| j                   |   }||kD  rŒ$||k(  rD||k7  r=| j                  |   }| j                   |   }| j                  |   }| j                   |   }n|S Œœ)zX
        Find the lowest common ancestor of nodes p and q in the spanning tree.
        )r1   r/   )r6   ÚpÚqÚsize_pÚsize_qs        r   Ú	find_apexz%_DataEssentialsAndFunctions.find_apexV   sØ   € ð ×"Ñ" 1Ñ%ˆØ×"Ñ" 1Ñ%ˆØØ˜6’/Ø—K‘K ‘N�Ø×*Ñ*¨1Ñ-�ð ˜6“/ð ˜6’/Ø—K‘K ‘N�Ø×*Ñ*¨1Ñ-�ð ˜6“/ð ˜ÒØ˜’6ØŸ™ A™�AØ!×.Ñ.¨qÑ1�FØŸ™ A™�AØ!×.Ñ.¨qÑ1‘Fà�Hð ó    c                 ó¦   — |g}g }||k7  rD|j                  | j                  |   «       | j                  |   }|j                  |«       ||k7  rŒD||fS )zX
        Returns the nodes and edges on the path from node p to its ancestor w.
        )r+   r0   r/   )r6   rL   ÚwÚWnÚWes        r   Ú
trace_pathz&_DataEssentialsAndFunctions.trace_pathl   sZ   € ð ˆSˆØˆØ�1ŠfØ�I‰I�d×&Ñ& qÑ)Ô*Ø—‘˜A‘ˆAØ�I‰I�aŒLð �1‹fð �2ˆvˆrQ   c                 ó
  — | j                  ||«      }| j                  ||«      \  }}|j                  «        |j                  «        ||gk7  r|j                  |«       | j                  ||«      \  }}|d= ||z  }||z  }||fS )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   )rP   rV   Úreverser+   )	r6   r;   rL   rM   rS   rT   rU   ÚWnRÚWeRs	            r   Ú
find_cyclez&_DataEssentialsAndFunctions.find_cyclex   s„   € ð �N‰N˜1˜aÓ ˆØ—‘  AÓ&‰ˆˆBØ
�
‰
ŒØ
�
‰
ŒØ�!�Š9Ø�I‰I�aŒLØ—?‘? 1 aÓ(‰ˆˆSØ�ˆGØ
ˆc‰	ˆØ
ˆc‰	ˆØ�2ˆvˆrQ   c                 ó®   — t        ||«      D ]F  \  }}| j                  |   |k(  r| j                  |xx   |z  cc<   Œ0| j                  |xx   |z  cc<   ŒH y)zQ
        Augment f units of flow along a cycle represented by Wn and We.
        N)Úzipr#   r-   )r6   rT   rU   Úfr;   rL   s         r   Úaugment_flowz(_DataEssentialsAndFunctions.augment_flow‹   sX   € ô ˜˜B“Kò 	'‰DˆAˆqØ× Ñ  Ñ# qÒ(Ø—‘˜qÓ! QÑ&Ô!à—‘˜qÓ! QÑ&Ô!ñ		'rQ   c              #   óp   K  — |–— | j                   |   }||k7  r| j                  |   }|–— ||k7  rŒyy­w)zD
        Yield the nodes in the subtree rooted at a node p.
        N)r4   r2   )r6   rL   Úls      r   Útrace_subtreez)_DataEssentialsAndFunctions.trace_subtree•   sB   è ø€ ð ŠØ×$Ñ$ QÑ'ˆØ�1ŠfØ×"Ñ" 1Ñ%ˆAØŠGð �1�fùs   ‚16´6c                 óÊ  — | j                   |   }| j                  |   }| j                  |   }| j                  |   }d| j                  |<   d| j
                  |<   || j                  |<   || j                  |<   || j                  |<   || j                  |<   |�K| j                   |xx   |z  cc<   | j                  |   |k(  r|| j                  |<   | j                  |   }|�ŒJyy)zT
        Remove an edge (s, t) where parent[t] == s from the spanning tree.
        N)r1   r3   r4   r2   r/   r0   )r6   ÚsÚtÚsize_tÚprev_tÚlast_tÚnext_last_ts          r   Úremove_edgez'_DataEssentialsAndFunctions.remove_edgeŸ   sõ   € ð ×"Ñ" 1Ñ%ˆØ×#Ñ# AÑ&ˆØ×)Ñ)¨!Ñ,ˆØ×(Ñ(¨Ñ0ˆàˆ�‰�A‰Ø"ˆ×Ñ˜Ñà%0ˆ×Ñ˜6Ñ"Ø*0ˆ×Ñ˜;Ñ'Ø%&ˆ×Ñ˜6Ñ"Ø &ˆ×Ñ˜1Ñð ˆmØ×Ñ˜aÓ  FÑ*Ó Ø×'Ñ'¨Ñ*¨fÒ4Ø.4�×(Ñ(¨Ñ+Ø—‘˜A‘ˆAð	 ‹mrQ   c                 ór  — g }|�#|j                  |«       | j                  |   }|�Œ#|j                  «        t        |t	        |dd«      «      D �]e  \  }}| j
                  |   }| j                  |   }| j                  |   }| j                  |   }| j                  |   }|| j                  |<   d| j                  |<   | j                  |   | j                  |<   d| j                  |<   || j
                  |   z
  | j
                  |<   || j
                  |<   || j                  |<   || j                  |<   || j                  |<   || j                  |<   ||k(  r|| j                  |<   |}|| j                  |<   || j                  |<   || j                  |<   || j                  |<   || j                  |<   �Œh y)zC
        Make a node q the root of its containing subtree.
        Nr   )
r+   r/   rX   r]   r   r1   r4   r3   r2   r0   )	r6   rM   Ú	ancestorsrL   rN   Úlast_pÚprev_qÚlast_qÚnext_last_qs	            r   Ú	make_rootz%_DataEssentialsAndFunctions.make_root·   sÕ  € ð ˆ	ØˆmØ×Ñ˜QÔØ—‘˜A‘ˆAð ‰mð 	×ÑÔÜ˜	¤6¨)°Q¸Ó#=Ó>ó 	1‰DˆAˆqØ×&Ñ& qÑ)ˆFØ×-Ñ-¨aÑ0ˆFØ×'Ñ'¨Ñ*ˆFØ×-Ñ-¨aÑ0ˆFØ×,Ñ,¨VÑ4ˆKàˆD�K‰K˜‰NØ!ˆD�K‰K˜‰NØ"&×"2Ñ"2°1Ñ"5ˆD×Ñ˜QÑØ"&ˆD×Ñ˜QÑØ#)¨D×,=Ñ,=¸aÑ,@Ñ#@ˆD×Ñ˜aÑ Ø#)ˆD×Ñ˜aÑ à)4ˆD×Ñ˜vÑ&Ø.4ˆD×Ñ˜{Ñ+Ø)*ˆD×Ñ˜vÑ&Ø$*ˆD×Ñ˜qÑ!Ø˜ÒØ.4�×(Ñ(¨Ñ+Ø�ð %+ˆD×Ñ˜qÑ!Ø)*ˆD×Ñ˜vÑ&Ø)*ˆD×Ñ˜vÑ&Ø$*ˆD×Ñ˜qÑ!Ø*0ˆD×$Ñ$ QÓ'ñ7	1rQ   c                 óÊ  — | j                   |   }| j                  |   }| j                  |   }| j                   |   }|| j                  |<   || j                  |<   || j                  |<   || j
                  |<   || j
                  |<   || j                  |<   |�K| j                  |xx   |z  cc<   | j                   |   |k(  r|| j                   |<   | j                  |   }|�ŒJyy)z[
        Add an edge (p, q) to the spanning tree where q is the root of a subtree.
        N)r4   r2   r1   r/   r0   r3   )r6   r;   rL   rM   rm   Únext_last_prO   ro   s           r   Úadd_edgez$_DataEssentialsAndFunctions.add_edgeÝ   sõ   € ð ×)Ñ)¨!Ñ,ˆØ×(Ñ(¨Ñ0ˆØ×"Ñ" 1Ñ%ˆØ×)Ñ)¨!Ñ,ˆàˆ�‰�A‰Øˆ×Ñ˜Ñà%&ˆ×Ñ˜6Ñ"Ø &ˆ×Ñ˜1ÑØ*0ˆ×Ñ˜;Ñ'Ø%0ˆ×Ñ˜6Ñ"ð ˆmØ×Ñ˜aÓ  FÑ*Ó Ø×'Ñ'¨Ñ*¨fÒ4Ø.4�×(Ñ(¨Ñ+Ø—‘˜A‘ˆAð	 ‹mrQ   c                 ó@  — || j                   |   k(  r0| j                  |   | j                  |   z
  | j                  |   z
  }n/| j                  |   | j                  |   z   | j                  |   z
  }| j                  |«      D ]  }| j                  |xx   |z  cc<   Œ y)z†
        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$   r.   r(   rb   )r6   r;   rL   rM   rD   s        r   Úupdate_potentialsz-_DataEssentialsAndFunctions.update_potentialsõ   s©   € ð
 �×!Ñ! !Ñ$Ò$Ø×$Ñ$ QÑ'¨$×*;Ñ*;¸AÑ*>Ñ>À×AUÑAUÐVWÑAXÑX‰Aà×$Ñ$ QÑ'¨$×*;Ñ*;¸AÑ*>Ñ>À×AUÑAUÐVWÑAXÑXˆAØ×#Ñ# AÓ&ò 	)ˆAØ× Ñ  Ó# qÑ(Ô#ñ	)rQ   c                 óÂ   — | j                   |   | j                  | j                  |      z
  | j                  | j                  |      z   }| j                  |   dk(  r|S | S )z&Returns the reduced cost of an edge i.r   )r(   r.   r#   r$   r-   )r6   r;   Úcs      r   Úreduced_costz(_DataEssentialsAndFunctions.reduced_cost  sr   € ð ×Ñ˜aÑ Ø×"Ñ" 4×#4Ñ#4°QÑ#7Ñ8ñ9à×"Ñ" 4×#4Ñ#4°QÑ#7Ñ8ñ9ð 	
ð
 —N‘N 1Ñ%¨Ò*ˆqÐ2°°Ð2rQ   c              #   ó�  K  — | j                   dk(  ryt        t        t        | j                   «      «      «      }| j                   |z   dz
  |z  }d}d}||k  rí||z   }|| j                   k  rt	        ||«      }n8|| j                   z  }t        t	        || j                   «      t	        |«      «      }|}t        || j                  ¬«      }| j                  |«      }|dk\  r|dz  }nX| j                  |   dk(  r| j                  |   }	| j                  |   }
n| j                  |   }	| j                  |   }
||	|
f–— d}||k  rŒìyy­w)z-Yield entering edges until none can be found.r   Nr   ©Úkey)r,   Úintr   r   rG   r   Úminry   r-   r#   r$   )r6   ÚBÚMÚmr^   ra   r)   r;   rx   rL   rM   s              r   Úfind_entering_edgesz/_DataEssentialsAndFunctions.find_entering_edges
  sD  è ø€ à�?‰?˜aÒØô ””T˜$Ÿ/™/Ó*Ó+Ó,ˆØ�_‰_˜qÑ  1Ñ$¨Ñ*ˆØˆàˆØ�!Šeà�A‘ˆAØ�D—O‘OÒ#Ü˜a ›‘à�T—_‘_Ñ$�Üœe A t§¡Ó7¼¸q»ÓB�ØˆAä�E˜t×0Ñ0Ô1ˆAØ×!Ñ! !Ó$ˆAØ�AŠvà�Q‘‘ð —>‘> !Ñ$¨Ò)Ø×)Ñ)¨!Ñ,�AØ×)Ñ)¨!Ñ,‘Aà×)Ñ)¨!Ñ,�AØ×)Ñ)¨!Ñ,�AØ˜˜A�g’Ø�ð1 �!�eùs   ‚EEÅEc                 ó‚   — | j                   |   |k(  r| j                  |   | j                  |   z
  S | j                  |   S )zfReturns the residual capacity of an edge i in the direction away
        from its endpoint p.
        )r#   r'   r-   )r6   r;   rL   s      r   Úresidual_capacityz-_DataEssentialsAndFunctions.residual_capacity4  sM   € ð × Ñ  Ñ# qÒ(ð × Ñ  Ñ# d§n¡n°QÑ&7Ñ7ð	
ð —‘ Ñ"ð	
rQ   c                 óÌ   ‡ — t        t        t        |«      t        |«      «      ˆ fd„¬«      \  }}‰ j                  |   |k(  r‰ j                  |   n‰ j                  |   }|||fS )z=Returns the leaving edge in a cycle represented by Wn and We.c                 ó"   •—  ‰j                   | Ž S r@   )r„   )Úi_pr6   s    €r   ú<lambda>z?_DataEssentialsAndFunctions.find_leaving_edge.<locals>.<lambda>B  s   ø€ Ð2˜D×2Ñ2°CÐ8€ rQ   r{   )r~   r]   Úreversedr#   r$   )r6   rT   rU   Újrd   re   s   `     r   Úfind_leaving_edgez-_DataEssentialsAndFunctions.find_leaving_edge>  sc   ø€ äÜ”˜“œh r›lÓ+Û8ô
‰ˆˆ1ð %)×$5Ñ$5°aÑ$8¸AÒ$=ˆD×Ñ˜aÒ À4×CTÑCTÐUVÑCWˆØ�!�QˆwˆrQ   N©r9   r   r:   )Ú__name__Ú
__module__Ú__qualname__r=   rJ   rP   rV   r[   r_   rb   rj   rq   rt   rv   ry   r‚   r„   r‹   © rQ   r   r   r      sS   „ àJRó/
òb/ò,ò,
òò&'òòò0$1òLò0
)ò3ò&òT
órQ   r   Ú
undirectedr9   r   )r   r:   )Ú
node_attrsÚ
edge_attrsc           
      ó  ‡‡‡‡‡‡— t        | «      dk(  rt        j                  d«      ‚| j                  «       }t	        | ||‰‰¬«      Št        d«      Št        ‰j                  ‰j                  «      D ],  \  }}t        |«      ‰k(  sŒt        j                  d|›d�«      ‚ t        ‰j                  ‰j                  «      D ],  \  }}t        |«      ‰k(  sŒt        j                  d|›d�«      ‚ |st        j                  | d	¬
«      }	nt        j                  | d	d	¬«      }	|	D ]?  }t        |d   j                  ‰d«      «      ‰k(  sŒ%t        j                  d|dd ›d�«      ‚ t        ‰j                  «      dk7  rt        j                  d«      ‚t        ‰j                  ‰j                   «      D ]#  \  }}
|
dk  sŒt        j                  d|›d�«      ‚ |st        j                  | d	¬
«      }	nt        j                  | d	d	¬«      }	|	D ]6  }|d   j                  ‰‰«      dk  sŒt        j                  d|dd ›d�«      ‚ t#        ‰j                  «      D ]w  \  }}|dkD  r7‰j$                  j'                  d«       ‰j(                  j'                  |«       ŒB‰j$                  j'                  |«       ‰j(                  j'                  d«       Œy dt+        t-        t        ˆfd„‰j                   D «       «      t        d„ ‰j                  D «       «      gd„ ‰j                  D «       «      «      z  xs dŠt        ‰j                  «      }‰j                  j/                  t1        ‰|«      «       ‰j                   j/                  t1        ‰|«      «       ‰j3                  |‰«       ‰j5                  «       D ]á  \  }}}‰j7                  |||«      \  }}‰j9                  ||«      \  }}}‰j;                  ||‰j=                  ||«      «       ||k7  sŒ\‰j>                  |   |k7  r||}}|jA                  |«      |jA                  |«      kD  r||}}‰jC                  ||«       ‰jE                  |«       ‰jG                  |||«       ‰jI                  |||«       Œã tK        ˆfd„tM        | d«      D «       «      rt        j                  d«      ‚tK        ˆˆfd„tM        ‰jN                  «      D «       «      s+tK        ˆˆˆfd„t        j                  | d	¬
«      D «       «      rt        jP                  d«      ‚‰jR                  ‰jN                  d…= t        d„ t        ‰j                  ‰jR                  «      D «       «      }‰j                  D �ci c]  }|i “Œ c}Šˆfd„}ˆfd„‰j$                  D «       ‰_        ˆfd„‰j(                  D «       ‰_        |sKt        ‰j$                  ‰j(                  ‰jR                  «      D ]
  } ||«       Œ | jU                  d	¬
«      }	nVt        ‰j$                  ‰j(                  ‰jV                  ‰jR                  «      D ]
  } ||«       Œ | jU                  d	d	¬«      }	|	D ]}  }|d   |d   k7  r(|d   j                  ‰‰«      dk(  sŒ' ||dd dz   «       Œ6|d   j                  ‰d«      }|dk\  r ||dd dz   «       Œ_|d   ‰   }
|||
z  z  } ||dd |
fz   «       Œ |‰fS c c}w )ad  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   ó.   •K  — | ]  }|‰k  sŒ	|–— Œ y ­wr@   r�   )r   rx   r   s     €r   r   z"network_simplex.<locals>.<genexpr>7  s   øè ø€ ÒC˜a¸1¸s»7œÑCùs   ƒ
Žc              3   ó2   K  — | ]  }t        |«      –— Œ y ­wr@   rA   )r   rS   s     r   r   z"network_simplex.<locals>.<genexpr>8  s   è ø€ Ò: 1œ˜AŸÑ:ùrE   c              3   ó2   K  — | ]  }t        |«      –— Œ y ­wr@   rA   rC   s     r   r   z"network_simplex.<locals>.<genexpr>:  s   è ø€ Ò3˜A”�Q—Ñ3ùrE   r   c              3   óB   •K  — | ]  }‰j                   |   d k7  –— Œ y­w)r   N©r-   )r   r;   ÚDEAFs     €r   r   z"network_simplex.<locals>.<genexpr>`  s   øè ø€ Ò
8 aˆ4�>‰>˜!Ñ Õ!Ñ
8ùs   ƒz"no flow satisfies all node demandsc              3   óH   •K  — | ]  }‰j                   |   d z  ‰k\  –— Œ y­w)r   Nrš   )r   r;   r›   rI   s     €€r   r   z"network_simplex.<locals>.<genexpr>c  s$   øè ø€ Ò
M°ˆ4�>‰>˜!Ñ˜qÑ  HÕ,Ñ
Mùs   ƒ"c              3   ó‚   •K  — | ]6  }|d    j                  ‰‰«      ‰k(  xr |d    j                  ‰d«      dk  –— Œ8 y­w)r   r   Nr   )r   r   r   r   r:   s     €€€r   r   z"network_simplex.<locals>.<genexpr>c  sK   øè ø€ ò Uàð 	
ˆ"‰�	‰	�(˜CÓ  CÑ'ÒD¨A¨b©E¯I©I°f¸aÓ,@À1Ñ,DÓDñUùs   ƒ<?z+negative cycle with infinite capacity foundc              3   ó,   K  — | ]  \  }}||z  –— Œ y ­wr@   r�   )r   rS   Úxs      r   r   z"network_simplex.<locals>.<genexpr>n  s   è ø€ ÒM™d˜a �A˜•EÑMùs   ‚c                 ó|   •— ‰| d      }| dd D ]  }	 ||   }Œ
 | d   || d   <   y# t         $ r i }|||<   |}Y Œ+w xY w)zAdd a flow dict entry.r   r   éþÿÿÿr   N)ÚKeyError)r   rD   Úkre   Ú	flow_dicts       €r   Ú	add_entryz"network_simplex.<locals>.add_entryq  sj   ø€ à�a˜‘d‰OˆØ�1�R�ò 	ˆAðØ�a‘D‘ð	ð �R‘5ˆˆ!ˆB‰%Šøô	 ò Ø�Ø��!‘Ø’ðús   “&¦;º;c              3   ó<   •K  — | ]  }‰j                   |   –— Œ y ­wr@   ©r   )r   rd   r›   s     €r   r   z"network_simplex.<locals>.<genexpr>}  ó   øè ø€ ò Øˆ�‰�qÕñùó   ƒc              3   ó<   •K  — | ]  }‰j                   |   –— Œ y ­wr@   r§   )r   re   r›   s     €r   r   z"network_simplex.<locals>.<genexpr>€  r¨   r©   )r   ),rF   ÚnxÚNetworkXErrorÚis_multigraphr   r*   r]   r   r"   rB   r&   r(   Úselfloop_edgesr   ÚsumÚNetworkXUnfeasibler'   r   r#   r+   r$   Úmaxr   Úextendr   rJ   r‚   r[   r‹   r_   r„   r/   Úindexrj   rq   rt   rv   ÚanyrG   r,   ÚNetworkXUnboundedr-   r)   r%   )r7   r9   r   r:   r8   r<   rD   r   rS   r)   rx   r;   rH   rL   rM   rT   rU   rŠ   rd   re   Ú	flow_costr¥   r›   rI   r¤   r   s     ``                  @@@@r   r   r   H  s³  ý€ ôT ˆ1ƒv�‚{Ü×ÑÐ3Ó4Ð4à—‘Ó"€Jô 'Ø	ˆ:˜f¨xÀô€Dô �‹,€CÜ�D—N‘N D×$5Ñ$5Ó6ò F‰ˆˆ1Üˆq‹6�S‹=Ü×"Ñ" U¨1¨%Ð/CÐ#DÓEÐEðFô �D×%Ñ% t×'8Ñ'8Ó9ò F‰ˆˆ1Üˆq‹6�S‹=Ü×"Ñ" U¨1¨%Ð/CÐ#DÓEÐEðFñ Ü×!Ñ! !¨$Ô/‰ä×!Ñ! !¨$°TÔ:ˆØò KˆÜˆq�‰u�y‰y˜ Ó#Ó$¨Ó+Ü×"Ñ" U¨1¨S¨b¨6¨*Ð4HÐ#IÓJÐJðKô ˆ4×ÑÓ Ò"Ü×#Ñ#Ð$CÓDÐDÜ�D×%Ñ% t×';Ñ';Ó<ò M‰ˆˆ1Øˆq‹5Ü×'Ñ'¨%°¨uÐ4JÐ(KÓLÐLðMñ Ü×!Ñ! !¨$Ô/‰ä×!Ñ! !¨$°TÔ:ˆØò RˆØˆR‰5�9‰9�X˜sÓ# aÓ'Ü×'Ñ'¨%°°#°2°¨zÐ9OÐ(PÓQÐQðRô ˜$×+Ñ+Ó,ò )‰ˆˆ1ð ˆqŠ5Ø×Ñ×$Ñ$ RÔ(Ø×Ñ×$Ñ$ QÕ'à×Ñ×$Ñ$ QÔ'Ø×Ñ×$Ñ$ RÕ(ð)ð 	
Ü
ÜäÓC 4×#7Ñ#7ÔCÓCÜÑ:¨×(9Ñ(9Ô:Ó:ðñ 4 ×!2Ñ!2Ô3óó
ñ		
ò 
	ð ð ô 	ˆD�N‰NÓ€AØ×Ñ×ÑœV H¨aÓ0Ô1Ø×Ñ×Ñ¤ x°Ó 3Ô4ð 	×!Ñ! ! XÔ.ð ×+Ñ+Ó-ò ,‰ˆˆ1ˆaØ—‘  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˜!˜Q Ô"Ø×"Ñ" 1 a¨Õ+ð,ô* Ó
8¬5°!°°Q«<Ô
8Ô8Ü×#Ñ#Ð$HÓIÐIä
Ô
M´e¸D¿O¹OÓ6LÔ
MÔMÔQTõ Uä×"Ñ" 1¨4Ô0ôUô 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ð	à—‘˜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�   rQ   r   ú<module>r¾      sn   ðñð Ð
€ç +Ñ +ß ã Ý .÷wñ wñt	 �\Ó"Ø€×ÑØ±°u³ÈÑ$KôòN óó #ñN rQ   