Ë
    D^(h#†  ã                   óP  — d Z ddlZddlmZmZ ddlmZ ddlmZ ddl	Z
ddlmZ ddlmZmZ g d	¢Zd
dhZddddœZ ed«      Z ed«      d.d„«       Zd„ Zd„ Z e
j0                  ddi¬«      d/d„«       Z ed«       e
j0                  ddid¬«      d0d„«       «       Z e
j0                  dd¬«      	 	 	 	 d1d„«       Z e
j0                  ddd¬«      	 d1d „«       Z e
j0                  ddd¬«      ddddd!œd"„«       Z e
j0                  ddd¬«      	 d1d#„«       Z e
j0                  ddd¬«      	 d1d$„«       Zd%Z e d&z   Z!e jE                  d'd¬(«      e_         e jE                  d)d¬(«      d*z   e_         e!jE                  d'd+¬(«      e_         e!jE                  d)d+¬(«      e_          G d,„ d-«      Z#y)2uö   
Algorithms for finding optimum branchings and spanning arborescences.

This implementation is based on:

    J. Edmonds, Optimum branchings, J. Res. Natl. Bur. Standards 71B (1967),
    233â€“240. URL: http://archive.org/details/jresv71Bn4p233

é    N)Ú	dataclassÚfield)Ú
itemgetter)ÚPriorityQueue)Úpy_random_stateé   )Úis_arborescenceÚis_branching)Úbranching_weightÚgreedy_branchingÚmaximum_branchingÚminimum_branchingÚminimal_branchingÚmaximum_spanning_arborescenceÚminimum_spanning_arborescenceÚArborescenceIteratorÚmaxÚminÚ	branchingÚarborescence)r   r   úspanning arborescenceÚinfc                 ó–   — dj                  t        | «      D �cg c]!  }|j                  t        j                  «      ‘Œ# c}«      S c c}w )NÚ )ÚjoinÚrangeÚchoiceÚstringÚascii_letters)ÚLÚseedÚns      úa/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/networkx/algorithms/tree/branchings.pyÚrandom_stringr$   >   s2   € à�7‰7¼uÀQ»xÖH¸!�D—K‘K¤× 4Ñ 4Õ5ÒHÓIÐIùÒHs   ™&Ac                 ó   — |  S ©N© ©Úweights    r#   Ú_min_weightr*   C   s	   € Øˆ7€Nó    c                 ó   — | S r&   r'   r(   s    r#   Ú_max_weightr-   G   s   € Ø€Mr+   ÚattrÚdefault)Ú
edge_attrsr)   c                 óP   ‡‡— t        ˆˆfd„| j                  d¬«      D «       «      S )aø  
    Returns the total weight of a branching.

    You must access this function through the networkx.algorithms.tree module.

    Parameters
    ----------
    G : DiGraph
        The directed graph.
    attr : str
        The attribute to use as weights. If None, then each edge will be
        treated equally with a weight of 1.
    default : float
        When `attr` is not None, then if an edge does not have that attribute,
        `default` specifies what value it should take.

    Returns
    -------
    weight: int or float
        The total weight of the branching.

    Examples
    --------
    >>> G = nx.DiGraph()
    >>> G.add_weighted_edges_from([(0, 1, 2), (1, 2, 4), (2, 3, 3), (3, 4, 2)])
    >>> nx.tree.branching_weight(G)
    11

    c              3   óH   •K  — | ]  }|d    j                  ‰‰«      –— Œ y­w)é   N)Úget)Ú.0Úedger.   r/   s     €€r#   ú	<genexpr>z#branching_weight.<locals>.<genexpr>j   s    øè ø€ ÒI¨dˆt�A‰w�{‰{˜4 ×)ÑIùs   ƒ"T©Údata)ÚsumÚedges)ÚGr.   r/   s    ``r#   r   r   K   s!   ù€ ô> ÔI°a·g±gÀ4°gÓ6HÔIÓIÐIr+   é   T)r0   Úreturns_graphc                 óÚ  — |t         vrt        j                  d«      ‚|dk(  rd}nd}|€t        |¬«      }| j	                  d¬«      D ���cg c]  \  }}}|||j                  ||«      f‘Œ }	}}}	 |	j                  t        ddd	«      |¬
«       t        j                  «       }
|
j                  | «       t        j                  j                  «       }t        |	«      D ]Y  \  }\  }}}||   ||   k(  rŒ|
j                  |«      d	k(  rŒ+i }|�|||<    |
j                  ||fi |¤Ž |j!                  ||«       Œ[ |
S c c}}}w # t        $ r |	j                  t        d«      |¬
«       Y ŒÚw xY w)a7  
    Returns a branching obtained through a greedy algorithm.

    This algorithm is wrong, and cannot give a proper optimal branching.
    However, we include it for pedagogical reasons, as it can be helpful to
    see what its outputs are.

    The output is a branching, and possibly, a spanning arborescence. However,
    it is not guaranteed to be optimal in either case.

    Parameters
    ----------
    G : DiGraph
        The directed graph to scan.
    attr : str
        The attribute to use as weights. If None, then each edge will be
        treated equally with a weight of 1.
    default : float
        When `attr` is not None, then if an edge does not have that attribute,
        `default` specifies what value it should take.
    kind : str
        The type of optimum to search for: 'min' or 'max' greedy branching.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    B : directed graph
        The greedily obtained branching.

    zUnknown value for `kind`.r   FT)r!   r8   r3   r   r   )ÚkeyÚreverse)ÚKINDSÚnxÚNetworkXExceptionr$   r;   r4   Úsortr   Ú	TypeErrorÚDiGraphÚadd_nodes_fromÚutilsÚ	UnionFindÚ	enumerateÚ	in_degreeÚadd_edgeÚunion)r<   r.   r/   Úkindr!   rA   ÚuÚvr9   r;   ÚBÚufÚiÚws                 r#   r   r   m   st  € ðF ”5ÑÜ×"Ñ"Ð#>Ó?Ð?àˆu‚}Ø‰àˆà€|ä $Ô'ˆàABÇÁÈdÀÓAS×TÐT±°!°Q¸ˆa��D—H‘H˜T 7Ó+Ò,ÐT€EÒTð7Ø�
‰
”z ! Q¨Ó*°Gˆ
Ô<ô 	�
‰
‹€AØ×Ñ�QÔô 
�‰×	Ñ	Ó	€BÜ! %Ó(ò ‰ˆ‰9ˆAˆq�!Øˆa‰5�B�q‘EŠ>àØ�[‰[˜‹^˜qÒ àð ˆDØÐØ��T‘
ØˆA�J‰J�q˜!Ñ$˜tÒ$Ø�H‰H�Q˜�Nðð €Hùô? Uøô
 ò 7ð 	�
‰
”z !“}¨gˆ
Ö6ð7ús   Á D;Á1E Å%E*Å)E*)Úpreserve_edge_attrsr>   Fc                 ój  ‡ ‡‡‡‡‡ ‡!‡"‡#‡$‡%‡&‡'‡(‡)‡*— d„ Š$d„ Š%dŠ"dŠ(‰ }t        j                  «       Š d ‰ _        i Š t        |j	                  d¬«      «      D ]t  \  }\  }}}	‰|	j                  ‰|«      i}
|	j                  ‰«      �|	j                  ‰«      |
‰<   |r#|	j                  «       D ]  \  }}|‰k7  sŒ||
|<   Œ  ‰$‰ ‰ |||fi |
¤Ž Œv d}t        j                  «       Ši Šg Š&g Š!t        «       Š)t         j                  j                  «       Š*g Š#g Š'ˆ ˆˆfd„}ˆˆˆ ˆ ˆˆ!ˆ"ˆ#ˆ$ˆ%ˆ&ˆ'ˆ(ˆˆ)ˆ*fd	„}d
„ }t        t        ‰ j                  «      «      }	 	 t        |«      }|‰)v rŒ‰)j'                  |«       ‰j)                  |«        ||«      \  }}|�ª|dkD  r¥|d   }‰*|   ‰*|   k(  }‰|i}|d   j                  ‰«      �|d   j                  ‰«      |‰<    ‰$‰‰|||d   fi |¤Ž d‰ |   |   |d      ‰"<   ‰*j+                  ||«       |r1 ||||«       t        t        ‰ j                  «       «      «      }|dz  }Œë# t        $ r´ t        ‰ «      t        ‰«      k(  sJ ‚t        ‰«      rt!        ‰«      sJ ‚‰&j#                  ‰ j%                  «       ‰ j%                  «       f«       ‰!j#                  ‰j%                  «       ‰j%                  «       f«       ‰#j#                  g «       ‰'j#                  d «       Y nw xY w|j-                  «       }t        ‰!|   d   «      }|dkD  r©|dz  }‰(t/        |«      z   }‰#|   } |‰&|dz      d   ||«      \  }}|j1                  |«       |r‰'|   }|€t2        ‚|j5                  |«       nC‰&|   \  Š Š ‰ |   d   }|D ]  }‰ |   \  }}}	||k(  sŒ n t3        d«      ‚|j5                  |«       |dkD  rŒ©|j7                  |«       |D ]R  }‰&d   d   |   \  }}}
‰|
‰   i}|r$|
j                  «       D ]  \  }}|‰‰"fvsŒ|||<   Œ  |j8                  ||fi |¤Ž ŒT |S )Nc                 ó°   — ||v r"||   \  }}}||k7  s||k7  rt        d|›d�«      ‚ | j                  |||fi |¤Ž ||| j                  |   |   |   f||<   y)aS  
        Adds an edge to `G` while also updating the edge index.

        This algorithm requires the use of an external dictionary to track
        the edge keys since it is possible that the source or destination
        node of an edge will be changed and the default key-handling
        capabilities of the MultiDiGraph class do not account for this.

        Parameters
        ----------
        G : MultiDiGraph
            The graph to insert an edge into.
        edge_index : dict
            A mapping from integers to the edges of the graph.
        u : node
            The source node of the new edge.
        v : node
            The destination node of the new edge.
        key : int
            The key to use from `edge_index`.
        d : keyword arguments, optional
            Other attributes to store on the new edge.
        zKey z is already in use.N)Ú	ExceptionrM   Úsucc)	r<   Ú
edge_indexrP   rQ   r@   ÚdÚuuÚvvÚ_s	            r#   Úedmonds_add_edgez+maximum_branching.<locals>.edmonds_add_edgeÊ   su   € ð2 �*ÑØ" 3™‰IˆB��AØ�R’˜Q "šWÜ $ s gÐ-@Ð AÓBÐBàˆ�
‰
�1�a˜Ñ" Ò"Ø˜a §¡¨¡¨1¡¨cÑ!2Ð3ˆ
�3Šr+   c                 ó  — t        «       }| j                  |   j                  «       D ]  }|j                  |«       Œ | j                  |   j                  «       D ]  }|j                  |«       Œ |D ]  }||= Œ | j                  |«       y)aR  
        Remove a node from the graph, updating the edge index to match.

        Parameters
        ----------
        G : MultiDiGraph
            The graph to remove an edge from.
        edge_index : dict
            A mapping from integers to the edges of the graph.
        n : node
            The node to remove from `G`.
        N)ÚsetÚpredÚvaluesÚupdaterZ   Úremove_node)r<   r[   r"   ÚkeysÚkeydictr@   s         r#   Úedmonds_remove_nodez.maximum_branching.<locals>.edmonds_remove_nodeë   s‰   € ô ‹uˆØ—v‘v˜a‘y×'Ñ'Ó)ò 	!ˆGØ�K‰K˜Õ ð	!à—v‘v˜a‘y×'Ñ'Ó)ò 	!ˆGØ�K‰K˜Õ ð	!ð ò 	 ˆCØ˜3‘ð	 ð 	
�‰�aÕr+   z#edmonds' secret candidate attributezedmonds new node base name Tr8   r   c                 óP  •— d}t          }‰j                  | dd¬«      D ]‚  \  }}}}|j                  ‰
«      t        j                  j
                  k(  rŒ5|‰	   }|j                  ‰
«      t        j                  j                  k(  r|}|| |||f} ||fS ||kD  sŒz|}|| |||f}Œ„ ||fS )a¼  
        Find the edge directed towards v with maximal weight.

        If an edge partition exists in this graph, return the included
        edge if it exists and never return any excluded edge.

        Note: There can only be one included edge for each vertex otherwise
        the edge partition is empty.

        Parameters
        ----------
        v : node
            The node to search for the maximal weight incoming edge.
        NT©r9   rg   )ÚINFÚin_edgesr4   rC   ÚEdgePartitionÚEXCLUDEDÚINCLUDED)rQ   r6   Ú
max_weightrP   r_   r@   r9   Ú
new_weightr<   r.   Ú	partitions           €€€r#   Úedmonds_find_desired_edgez4maximum_branching.<locals>.edmonds_find_desired_edgeO  sÐ   ø€ ð ˆÜ�Tˆ
Ø Ÿz™z¨!°$¸T˜zÓBò 	5‰OˆAˆq�#�tà�x‰x˜	Ó"¤b×&6Ñ&6×&?Ñ&?Ò?Øà˜d™ˆJð �x‰x˜	Ó"¤b×&6Ñ&6×&?Ñ&?Ò?Ø'�
Ø˜1˜c :¨tÐ4�Øð �ZÐÐð	 ˜JÓ&Ø'�
Ø˜1˜c :¨tÐ4‘ð!	5ð$ �ZÐÐr+   c           	      óð  •— |d   }t        j                  ‰| |«      }t        |dd «      D ��cg c]*  \  }}t        ‰||      |   j	                  «       «      d   ‘Œ, }}}|j                  |d   «       t        }d}	i }
|D ]L  }‰|   \  }} }|‰   }||
| <   |j                  ‰ «      t         j                  j                  k(  rŒC||k  sŒI|}|}	ŒN ‰j                  |«       ‰j                  |	«       ‰j                  ‰j                  «       ‰j                  «       f«       ‰j                  ‰j                  «       ‰j                  «       f«       ‰t        |«      z   }‰j                  |«       g }‰j                  dd¬«      D ]u  \  }} }}||
v r+| |
v rŒ|j                  «       }|j                  || ||f«       Œ7| |
v r;|‰   }|||
|    z
  z  }|j                  «       }||‰<   |j                  ||||f«       ŒvŒw |D ]  } ‰‰‰|«        ‰‰‰|«       Œ ‰!j                  t        |«      «       |D ];  \  }} }} ‰‰‰|| |fi |¤Ž ‰|v sŒ|‰=  ‰‰‰|| |fi |¤Ž ‰"j!                  || «       Œ= yc c}}w )aü  
        Perform step I2 from Edmonds' paper

        First, check if the last step I1 created a cycle. If it did not, do nothing.
        If it did, store the cycle for later reference and contract it.

        Parameters
        ----------
        v : node
            The current node to consider
        desired_edge : edge
            The minimum desired edge to remove from the cycle.
        level : int
            The current level, i.e. the number of cycles that have already been removed.
        r   r   Nr3   Trk   )rC   Úshortest_pathrK   Úlistrg   Úappendrl   r4   rn   rp   ÚcopyÚstrÚadd_noder;   Údifference_updaterb   rN   )#rQ   Údesired_edgeÚlevelrP   ÚQ_nodesrT   r^   ÚQ_edgesÚ	minweightÚminedgeÚQ_incoming_weightÚedge_keyr9   rU   Únew_nodeÚ	new_edgesr@   ÚddÚnoderR   ÚB_edge_indexr<   ÚG_edge_indexr.   Ú
branchingsÚcandidate_attrÚcircuitsr`   ri   ÚgraphsÚminedge_circuitÚnew_node_base_namers   Úselected_nodesrS   s#                      €€€€€€€€€€€€€€€€r#   Úedmonds_step_I2z*maximum_branching.<locals>.edmonds_step_I2t  sÍ  ø€ ð  ˜‰Oˆä×"Ñ" 1 a¨Ó+ˆä;DÀWÈQÈRÀ[Ó;Q÷
Ù27°!°RŒD��7˜1‘:‘˜rÑ"×'Ñ'Ó)Ó*¨1Ó-ð
ˆñ 
ð 	�‰�| A‘Ô'ô ˆ	ØˆØÐØò 
	#ˆHØ% hÑ/‰JˆAˆq�$Ø�T‘
ˆAð $%Ð˜aÑ Ø�x‰x˜	Ó"¤b×&6Ñ&6×&?Ñ&?Ò?ØØ�9‹}Ø�	Ø"‘ð
	#ð 	�‰˜Ô Ø×Ñ˜wÔ'Ø�‰�q—v‘v“x ×!2Ñ!2Ó!4Ð5Ô6Ø×Ñ˜1Ÿ6™6›8 \×%6Ñ%6Ó%8Ð9Ô:ð &¬¨E«
Ñ2ˆØ	�
‰
�8ÔØˆ	Ø Ÿw™w¨D°t˜wÓ<ò 	‰OˆAˆq�#�tØÐ%Ñ%ØÐ)Ñ)ð ð Ÿ™›�BØ×$Ñ$ h°°3¸Ð%;Õ<àÐ)Ñ)ð ˜T™
�AØ˜Ð%6°qÑ%9Ñ9Ñ9�AØŸ™›�BØ �B�t‘HØ×$Ñ$ a¨°3¸Ð%;Õ<ð ð-	ð0 ò 	7ˆDÙ  <°Ô6Ù  <°Õ6ð	7ð 	×(Ñ(¬¨W«Ô6à(ò 	‰OˆAˆq�#�tÙ˜Q ¨a°°CÑ@¸4Ò@Ø Ò%Ø˜Ð(Ù   L°!°Q¸ÑD¸tÒDØ—‘˜˜A•ñ	ùó{
s   ¯/I2c                 ó–   — || vrt        |›d�«      ‚| j                  |   D ]%  }| j                  |   |   D ]  }||v sŒd|fc c S  Œ' y)a�  
        Returns True if `u` is a root node in G.

        Node `u` is a root node if its in-degree over the specified edges is zero.

        Parameters
        ----------
        G : Graph
            The current graph.
        u : node
            The node in `G` to check if it is a root.
        edgekeys : iterable of edges
            The edges for which to check if `u` is a root of.
        z	 not in GF)TN)rY   rc   )r<   rP   ÚedgekeysrQ   Úedgekeys        r#   Úis_rootz"maximum_branching.<locals>.is_rootË  sf   € ð �A‰:Ü˜q˜e 9Ð-Ó.Ð.à—‘˜‘ò 	ˆAØŸ6™6 !™9 Q™<ò *�Ø˜hÒ&Ø  '˜>Ô)ñ*ð	ð
 r+   r=   r3   r   z+Couldn't find edge incoming to merged node.)rC   ÚMultiDiGraphÚ__networkx_cache__rK   r;   r4   Úitemsrb   rI   rJ   Úiterrw   ÚnodesÚnextÚStopIterationÚlenr
   rx   ry   Úaddr{   rN   Ú	__class__rz   re   rY   ÚremoverH   rM   )+r<   r.   r/   Úpreserve_attrsrs   Ú
G_originalr@   rP   rQ   r9   r\   Úd_kÚd_vr~   rt   r’   r–   r›   r}   Údesired_edge_weightÚcircuitr‡   ÚHr;   Úmerged_nodeÚisrootr•   r‚   ÚtargetÚvaluerR   r‰   rŠ   r‹   rŒ   r�   r`   ri   rŽ   r�   r�   r‘   rS   s+   ``  `                         @@@@@@@@@@@@@r#   r   r   ¾   s½  ÿÿ€ ò4òBð: ;€NØ6Ðà€JÜ
�‰Ó€AØ€AÔð €Lä& z×'7Ñ'7¸TÐ'7Ó'BÓCò :Ñˆ‰\ˆa��DØ�4—8‘8˜D 'Ó*Ð+ˆà�8‰8�IÓÐ*ØŸ8™8 IÓ.ˆAˆi‰LáØ ŸJ™J›Lò !‘��SØ˜$“;Ø �A�c’Fð!ñ 	˜˜L¨!¨Q°Ñ9°qÓ9ð:ð €Eô 	�‰Ó€AØ€LØ€FØ€JÜ“U€NÜ	�‰×	Ñ	Ó	€Bð €Hð €Oö(# ÷JU÷ Uó Uònô2 ”�a—g‘g“Ó€EØ
ð	Ü�U“ˆAð* �NÑ"Øà×Ñ˜1ÔØ	�
‰
�1ŒÙ,EÀaÓ,HÑ)ˆÐ)ð Ð#Ð(;¸aÒ(?Ø˜Q‘ˆAð ˜‘e˜r !™u‘nˆGØÐ+Ð,ˆBØ˜A‰×"Ñ" 9Ó-Ð9Ø ,¨Q¡× 3Ñ 3°IÓ >��9‘á˜Q ¨a°°LÀ±OÑJÀrÒJØ7;ˆAˆa‰D�‰G�L ‘OÑ$ ^Ñ4Ø�H‰H�Q˜ŒNñ Ù  <°Ô7ÜœT !§'¡'£)›_Ó-�Ø˜‘
�ðq øô ò 	ô �q“6œS ›VÒ#Ð#Ð#Ü�1ŒvÜ# A”Ð&�à�M‰M˜1Ÿ6™6›8 \×%6Ñ%6Ó%8Ð9Ô:Ø×Ñ˜qŸv™v›x¨×):Ñ):Ó)<Ð=Ô>Ø�O‰O˜BÔØ×"Ñ" 4Ô(áð	úð@ 	×ÑÓ€Aô �
˜5Ñ! !Ñ$Ó%€EØ
�!Š)Ø�‰
ˆð )¬3¨u«:Ñ5ˆØ˜5‘/ˆÙ! &¨°©Ñ"3°AÑ"6¸ÀUÓK‰ˆ�Ø�‰�WÔáØ% eÑ,ˆGØˆÜ�ð �L‰L˜Õ!ð % U™m‰OˆAˆ|Ø! 'Ñ*¨1Ñ-ˆFØ"ò O�Ø)¨'Ñ2‘
��1�dØ˜“;ÙðOô
  Ð MÓNÐNà�L‰L˜Ô!ðQ �!‹)ðT ×Ñ�ZÔ Øò 	ˆØ˜‘)˜A‘,˜wÑ'‰ˆˆ1ˆaØ�A�d‘Gˆ_ˆáØŸg™g›iò $‘
��UØ˜t ^Ð4Ò4Ø#�B�s’Gð$ð 	ˆ�
‰
�1�aÑ˜2Óð	ð €Hs   ÅH< È<B:K9Ë8K9)rV   Úmutates_inputr>   c                 óÈ  — | j                  d¬«      D ]  \  }}}|j                  ||«       ||<   Œ t        j                  | «       t	        | ||||«      }| j                  d¬«      D ]  \  }}}|j                  ||«       ||<   Œ t        j                  | «       |j                  d¬«      D ]  \  }}}|j                  ||«       ||<   Œ t        j                  |«       |S )NTr8   )r;   r4   rC   Ú_clear_cacher   )r<   r.   r/   r¢   rs   r_   r\   rR   s           r#   r   r   i  sä   € ð —7‘7 �7Ó%ò (‰ˆˆ1ˆaØ—5‘5˜˜wÓ'Ð'ˆˆ$Šð(ä‡O�O�AÔä˜!˜T 7¨N¸IÓF€Aà—7‘7 �7Ó%ò (‰ˆˆ1ˆaØ—5‘5˜˜wÓ'Ð'ˆˆ$Šð(ä‡O�O�AÔà—7‘7 �7Ó%ò (‰ˆˆ1ˆaØ—5‘5˜˜wÓ'Ð'ˆˆ$Šð(ä‡O�O�AÔà€Hr+   ©r.   r/   r¢   rs   c               óz  — t          }t         }| j                  ||¬«      D ]  \  }}}||kD  r|}||k  sŒ|}Œ | j                  d¬«      D ]'  \  }}}	|dz   ||z
  z   |	j                  ||«      z
  |	|<   Œ) t        j                  | «       t        | ||||«      }
| j                  d¬«      D ]'  \  }}}	|dz   ||z
  z   |	j                  ||«      z
  |	|<   Œ) t        j                  | «       |
j                  d¬«      D ]'  \  }}}	|dz   ||z
  z   |	j                  ||«      z
  |	|<   Œ) t        j                  |
«       |
S )a  
    Returns a minimal branching from `G`.

    A minimal branching is a branching similar to a minimal arborescence but
    without the requirement that the result is actually a spanning arborescence.
    This allows minimal branchinges to be computed over graphs which may not
    have arborescence (such as multiple components).

    Parameters
    ----------
    G : (multi)digraph-like
        The graph to be searched.
    attr : str
        The edge attribute used in determining optimality.
    default : float
        The value of the edge attribute used if an edge does not have
        the attribute `attr`.
    preserve_attrs : bool
        If True, preserve the other attributes of the original graph (that are not
        passed to `attr`)
    partition : str
        The key for the edge attribute containing the partition
        data on the graph. Edges can be included, excluded or open using the
        `EdgePartition` enum.

    Returns
    -------
    B : (multi)digraph-like
        A minimal branching.
    ©r9   r/   Tr8   r   )rl   r;   r4   rC   r¯   r   )r<   r.   r/   r¢   rs   rq   Ú
min_weightr_   rU   r\   rR   s              r#   r   r   ~  si  € ôD �€JÜ€JØ—7‘7 ¨g�7Ó6ò ‰ˆˆ1ˆaØˆzŠ>ØˆJØˆz‹>Ø‰Jð	ð —7‘7 �7Ó%ò T‰ˆˆ1ˆað
 ˜q‘. J°Ñ$;Ñ<¸q¿u¹uÀTÈ7Ó?SÑSˆˆ$ŠðTô ‡O�O�AÔä˜!˜T 7¨N¸IÓF€Að —7‘7 �7Ó%ò T‰ˆˆ1ˆaØ˜q‘. J°Ñ$;Ñ<¸q¿u¹uÀTÈ7Ó?SÑSˆˆ$ŠðTä‡O�O�AÔà—7‘7 �7Ó%ò T‰ˆˆ1ˆaØ˜q‘. J°Ñ$;Ñ<¸q¿u¹uÀTÈ7Ó?SÑSˆˆ$ŠðTä‡O�O�AÔà€Hr+   c                 óÎ  — t         }t          }| j                  ||¬«      D ]  \  }}}||k  r|}||kD  sŒ|}Œ | j                  d¬«      D ]'  \  }}}	|	j                  ||«      |z
  dz   ||z
  z
  |	|<   Œ) t        j                  | «       t        | ||||«      }
| j                  d¬«      D ]'  \  }}}	|	j                  ||«      |z   dz
  ||z
  z   |	|<   Œ) t        j                  | «       |
j                  d¬«      D ]'  \  }}}	|	j                  ||«      |z   dz
  ||z
  z   |	|<   Œ) t        j                  |
«       t        |
«      st        j                  j                  d«      ‚|
S )Nr²   Tr8   r   z&No maximum spanning arborescence in G.)	rl   r;   r4   rC   r¯   r   r	   Ú	exceptionrD   )r<   r.   r/   r¢   rs   r³   rq   r_   rU   r\   rR   s              r#   r   r   ¾  sƒ  € ô €JÜ�€JØ—7‘7 ¨g�7Ó6ò ‰ˆˆ1ˆaØˆzŠ>ØˆJØˆz‹>Ø‰Jð	ð —7‘7 �7Ó%ò T‰ˆˆ1ˆaØ—%‘%˜˜gÓ&¨Ñ3°aÑ7¸:È
Ñ;RÑSˆˆ$ŠðTä‡O�O�AÔä˜!˜T 7¨N¸IÓF€Aà—7‘7 �7Ó%ò T‰ˆˆ1ˆaØ—%‘%˜˜gÓ&¨Ñ3°aÑ7¸:È
Ñ;RÑSˆˆ$ŠðTä‡O�O�AÔà—7‘7 �7Ó%ò T‰ˆˆ1ˆaØ—%‘%˜˜gÓ&¨Ñ3°aÑ7¸:È
Ñ;RÑSˆˆ$ŠðTä‡O�O�AÔä˜1ÔÜ�l‰l×,Ñ,Ð-UÓVÐVà€Hr+   c                 óz   — t        | ||||¬«      }t        |«      st        j                  j	                  d«      ‚|S )Nr°   z&No minimum spanning arborescence in G.)r   r	   rC   rµ   rD   )r<   r.   r/   r¢   rs   rR   s         r#   r   r   è  sB   € ô 	Ø	ØØØ%Øô	€Aô ˜1ÔÜ�l‰l×,Ñ,Ð-UÓVÐVà€Hr+   a‰  
Returns a {kind} {style} from G.

Parameters
----------
G : (multi)digraph-like
    The graph to be searched.
attr : str
    The edge attribute used to in determining optimality.
default : float
    The value of the edge attribute used if an edge does not have
    the attribute `attr`.
preserve_attrs : bool
    If True, preserve the other attributes of the original graph (that are not
    passed to `attr`)
partition : str
    The key for the edge attribute containing the partition
    data on the graph. Edges can be included, excluded or open using the
    `EdgePartition` enum.

Returns
-------
B : (multi)digraph-like
    A {kind} {style}.
zV
Raises
------
NetworkXException
    If the graph does not contain a {kind} {style}.

Úmaximum)rO   ÚstyleÚminimumz)
See Also
--------
    minimal_branching
r   c                   ób   — e Zd ZdZ ed¬«       G d„ d«      «       Zdd„Zd„ Zd	„ Zd
„ Z	d„ Z
d„ Zy)r   uÚ  
    Iterate over all spanning arborescences of a graph in either increasing or
    decreasing cost.

    Notes
    -----
    This iterator uses the partition scheme from [1]_ (included edges,
    excluded edges and open edges). It generates minimum spanning
    arborescences using a modified Edmonds' Algorithm which respects the
    partition of edges. For arborescences with the same weight, ties are
    broken arbitrarily.

    References
    ----------
    .. [1] G.K. Janssens, K. SÃ¶rensen, An algorithm to generate all spanning
           trees in order of increasing cost, Pesquisa Operacional, 2005-08,
           Vol. 25 (2), p. 219-229,
           https://www.scielo.br/j/pope/a/XHswBwRwJyrfL88dmMwYNWp/?lang=en
    T)Úorderc                   ó>   — e Zd ZU dZeed<    ed¬«      Zeed<   d„ Z	y)úArborescenceIterator.Partitionz·
        This dataclass represents a partition and stores a dict with the edge
        data and the weight of the minimum spanning arborescence of the
        partition dict.
        Ú
mst_weightF)ÚcompareÚpartition_dictc                 ór   — t         j                  | j                  | j                  j	                  «       «      S r&   )r   Ú	Partitionr¾   rÀ   ry   )Úselfs    r#   Ú__copy__z'ArborescenceIterator.Partition.__copy__U  s-   € Ü'×1Ñ1Ø—‘ ×!4Ñ!4×!9Ñ!9Ó!;óð r+   N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚfloatÚ__annotations__r   rÀ   ÚdictrÄ   r'   r+   r#   rÂ   r½   J  s#   … ñ	ð ÓÙ$¨UÔ3ˆ˜Ó3ó	r+   rÂ   Nc                 óh  — |j                  «       | _        || _        || _        |rt        nt
        | _        d| _        |�li }|d   D ]  }t        j                  j                  ||<   Œ! |d   D ]  }t        j                  j                  ||<   Œ! t        j                  d|«      | _        yd| _        y)aþ  
        Initialize the iterator

        Parameters
        ----------
        G : nx.DiGraph
            The directed graph which we need to iterate trees over

        weight : String, default = "weight"
            The edge attribute used to store the weight of the edge

        minimum : bool, default = True
            Return the trees in increasing order while true and decreasing order
            while false.

        init_partition : tuple, default = None
            In the case that certain edges have to be included or excluded from
            the arborescences, `init_partition` should be in the form
            `(included_edges, excluded_edges)` where each edges is a
            `(u, v)`-tuple inside an iterable such as a list or set.

        z;ArborescenceIterators super secret partition attribute nameNr   r   )ry   r<   r)   r¹   r   r   ÚmethodÚpartition_keyrC   rn   rp   ro   r   rÂ   Úinit_partition)rÃ   r<   r)   r¹   rÏ   rÀ   Úes          r#   Ú__init__zArborescenceIterator.__init__Z  s»   € ð. —‘“ˆŒØˆŒØˆŒá-4Õ)Ô:Wð 	Œð
 Jð 	Ôð Ð%ØˆNØ# AÑ&ò >�Ü$&×$4Ñ$4×$=Ñ$=�˜qÒ!ð>à# AÑ&ò >�Ü$&×$4Ñ$4×$=Ñ$=�˜qÒ!ð>ä"6×"@Ñ"@ÀÀNÓ"SˆDÕà"&ˆDÕr+   c                 óú  — t        «       | _        | j                  | j                  «       | j                  �| j                  | j                  «       | j                  | j                  | j                  | j                  d¬«      j                  | j                  ¬«      }| j                  j                  | j                  | j                  r|n| | j                  €i n| j                  j                  «      «       | S )zu
        Returns
        -------
        ArborescenceIterator
            The iterator object for this graph
        T©rs   r¢   r(   )r   Úpartition_queueÚ_clear_partitionr<   rÏ   Ú_write_partitionrÍ   r)   rÎ   ÚsizeÚputrÂ   r¹   rÀ   )rÃ   r¾   s     r#   Ú__iter__zArborescenceIterator.__iter__…  sÜ   € ô  -›ˆÔØ×Ñ˜dŸf™fÔ%ð ×ÑÐ*Ø×!Ñ! $×"5Ñ"5Ô6à—[‘[Ø�F‰FØ�K‰KØ×(Ñ(Øð	 !ó 
÷
 ‰$�d—k‘kˆ$Ó
"ð 	ð 	×Ñ× Ñ Ø�N‰NØ"Ÿlšl‘
°°ð ×*Ñ*Ð2ñ à×,Ñ,×;Ñ;óô		
ð ˆr+   c                 óP  — | j                   j                  «       r
| `| ` t        ‚| j                   j	                  «       }| j                  |«       | j                  | j                  | j                  | j                  d¬«      }| j                  ||«       | j                  |«       |S )z 
        Returns
        -------
        (multi)Graph
            The spanning tree of next greatest weight, which ties broken
            arbitrarily.
        TrÓ   )rÔ   Úemptyr<   r�   r4   rÖ   rÍ   r)   rÎ   Ú
_partitionrÕ   )rÃ   rs   Únext_arborescences      r#   Ú__next__zArborescenceIterator.__next__§  sœ   € ð ×Ñ×%Ñ%Ô'Ø�˜Ð,ÜÐà×(Ñ(×,Ñ,Ó.ˆ	Ø×Ñ˜iÔ(Ø ŸK™KØ�F‰FØ�K‰KØ×(Ñ(Øð	 (ó 
Ðð 	�‰˜	Ð#4Ô5à×ÑÐ/Ô0Ø Ð r+   c                 ó<  — | j                  d|j                  j                  «       «      }| j                  d|j                  j                  «       «      }|j                  D �]  }||j                  vsŒt        j
                  j                  |j                  |<   t        j
                  j                  |j                  |<   | j                  |«       	 | j                  | j                  | j                  | j                  d¬«      }|j                  | j                  ¬«      }| j                  r|n| |_        | j                   j#                  |j%                  «       «       |j                  j                  «       |_        �Œ" y# t        j&                  $ r Y Œ8w xY w)aŒ  
        Create new partitions based of the minimum spanning tree of the
        current minimum partition.

        Parameters
        ----------
        partition : Partition
            The Partition instance used to generate the current minimum spanning
            tree.
        partition_arborescence : nx.Graph
            The minimum spanning arborescence of the input partition.
        r   TrÓ   r(   N)rÂ   rÀ   ry   r;   rC   rn   ro   rp   rÖ   rÍ   r<   r)   rÎ   r×   r¹   r¾   rÔ   rØ   rÄ   rD   )rÃ   rs   Úpartition_arborescenceÚp1Úp2rÐ   Úp1_mstÚp1_mst_weights           r#   rÜ   zArborescenceIterator._partitionÀ  sQ  € ð �^‰^˜A˜y×7Ñ7×<Ñ<Ó>Ó?ˆØ�^‰^˜A˜y×7Ñ7×<Ñ<Ó>Ó?ˆØ'×-Ñ-ó 	=ˆAà˜	×0Ñ0Ò0ä')×'7Ñ'7×'@Ñ'@�×!Ñ! !Ñ$Ü')×'7Ñ'7×'@Ñ'@�×!Ñ! !Ñ$à×%Ñ% bÔ)ðØ!Ÿ[™[ØŸ™ØŸ™Ø"&×"4Ñ"4Ø'+ð	 )ó �Fð %+§K¡K°t·{±{ KÓ$C�MØ59·\²\¡MÈÀ~�B”MØ×(Ñ(×,Ñ,¨R¯[©[«]Ô;ð %'×$5Ñ$5×$:Ñ$:Ó$<�Ö!ñ-	=øô& ×+Ñ+ò Ùðús   ÃBFÆFÆFc                 óú  — | j                   j                  d¬«      D ]\  \  }}}||f|j                  v r|j                  ||f   || j                  <   Œ6t        j
                  j                  || j                  <   Œ^ t	        j                  | j                   «       | j                   D �]Q  }d}d}| j                   j                  |d¬«      D ]~  \  }}}|j                  | j                  «      t        j
                  j                  k(  r|dz  }ŒC|j                  | j                  «      t        j
                  j                  k(  sŒz|dz  }Œ€ |dk(  sŒ¬|| j                   j                  |«      dz
  k7  sŒÎ| j                   j                  |d¬«      D ]d  \  }}}|j                  | j                  «      t        j
                  j                  k7  sŒ>t        j
                  j                  || j                  <   Œf �ŒT y)aß  
        Writes the desired partition into the graph to calculate the minimum
        spanning tree. Also, if one incoming edge is included, mark all others
        as excluded so that if that vertex is merged during Edmonds' algorithm
        we cannot still pick another of that vertex's included edges.

        Parameters
        ----------
        partition : Partition
            A Partition dataclass describing a partition on the edges of the
            graph.
        Tr8   r   )Únbunchr9   r   N)r<   r;   rÀ   rÎ   rC   rn   ÚOPENr¯   rm   r4   rp   ro   rL   )rÃ   rs   rP   rQ   r\   r"   Úincluded_countÚexcluded_counts           r#   rÖ   z%ArborescenceIterator._write_partitionè  s²  € ð —v‘v—|‘|¨�|Ó.ò 	>‰GˆAˆq�!Ø�1ˆv˜×1Ñ1Ñ1Ø(1×(@Ñ(@À!ÀQÀÑ(H��$×$Ñ$Ò%ä(*×(8Ñ(8×(=Ñ(=��$×$Ñ$Ò%ð		>ô
 	�‰˜Ÿ™Ôà—‘ó 	JˆAØˆNØˆNØŸ6™6Ÿ?™?°!¸$˜?Ó?ò (‘��1�aØ—5‘5˜×+Ñ+Ó,´×0@Ñ0@×0IÑ0IÒIØ" aÑ'‘NØ—U‘U˜4×-Ñ-Ó.´"×2BÑ2B×2KÑ2KÓKØ" aÑ'‘Nð	(ð  Ó" ~¸¿¹×9IÑ9IÈ!Ó9LÈqÑ9PÓ'PØ#Ÿv™vŸ™°a¸d˜ÓCò J‘G�A�q˜!Ø—u‘u˜T×/Ñ/Ó0´B×4DÑ4D×4MÑ4MÓMÜ02×0@Ñ0@×0IÑ0I˜˜$×,Ñ,Ò-òJñ	Jr+   c                 ó°   — |j                  d¬«      D ]"  \  }}}| j                  |v sŒ|| j                  = Œ$ t        j                  | j                  «       y)z7
        Removes partition data from the graph
        Tr8   N)r;   rÎ   rC   r¯   r<   )rÃ   r<   rP   rQ   r\   s        r#   rÕ   z%ArborescenceIterator._clear_partition  sR   € ð —w‘w D�wÓ)ò 	*‰GˆAˆq�!Ø×!Ñ! QÒ&Ø�d×(Ñ(Ñ)ð	*ô 	�‰˜Ÿ™Õr+   )r)   TN)rÅ   rÆ   rÇ   rÈ   r   rÂ   rÑ   rÙ   rÞ   rÜ   rÖ   rÕ   r'   r+   r#   r   r   5  sH   „ ññ( �TÔ÷ð ó ðó)'òV òD!ò2&=òP!JóF r+   r   )é   N)r)   r   )r)   r   r   N)r)   r   FN)$rÈ   r   Údataclassesr   r   Úoperatorr   Úqueuer   ÚnetworkxrC   Únetworkx.utilsr   Úrecognitionr	   r
   Ú__all__rB   ÚSTYLESrÉ   rl   r$   r*   r-   Ú_dispatchabler   r   r   r   r   r   r   Údocstring_branchingÚdocstring_arborescenceÚformatr   r'   r+   r#   ú<module>rø      sk  ðñó: ß (Ý Ý ã Ý *ç 6ò	€ð 
�ˆ€ð Ø"Ø+ñ
€ñ ˆEƒl€ñ �ÓòJó ðJòòð €×Ñ˜f iÐ0Ô1òJó 2ðJñB �ÓØ€×Ñ˜f iÐ0ÀÔEòLó Fó ðLð^ €×Ñ d¸$Ô?ð 
ØØØògó @ðgðT €×Ñ d¸$ÈdÔSàAEòó Tðð( €×Ñ d¸$ÈdÔSà A°eÀtó<ó Tð<ð~ €×Ñ d¸$ÈdÔSàAEò&ó Tð&ðR €×Ñ d¸$ÈdÔSàAEòó Tðð"Ð ð6 ðñð ð 0×6Ñ6Ø	˜+ð 7ó Ð Ô ð
 ×Ñ I°[ÐÓAðñð Ô ð )?×(EÑ(EØ	Ð1ð )Fó )Ð Ô %ð )?×(EÑ(EØ	Ð1ð )Fó )Ð Ô %÷
] ò ] r+   