a
    h                     @   s  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	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dZdd Zdd Zejddidd@ddZedejddiddAdd ZG d!d" d"ejZd#d$ ZG d%d& d&Z ejddd'd(d)dBd+d,Z!ejddd'd(d)dCd-d.Z"ejddd'd(d)ddd*dd/d0d1Z#ejddd'd(d)dDd2d3Z$ejddd'd(d)dEd4d5Z%d6Z&e&d7 Z'e&j(d8dd9e!_ e&j(d:dd9d; e"_ e'j(d8d<d9e$_ e'j(d:d<d9e%_ G d=d> d>Z)dS )Fu   
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)Enum)
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Edmondsmaxmin	branchingarborescence)r   r   spanning arborescenceinf   c                    s   d  fddt| D S )N c                    s   g | ]}  tjqS  )choicestringascii_letters).0nseedr   Q/var/www/auris/lib/python3.9/site-packages/networkx/algorithms/tree/branchings.py
<listcomp>A       z!random_string.<locals>.<listcomp>)joinrange)Lr#   r   r"   r$   random_string?   s    r*   c                 C   s   |  S Nr   weightr   r   r$   _min_weightD   s    r.   c                 C   s   | S r+   r   r,   r   r   r$   _max_weightH   s    r/   attrdefault)
edge_attrsr-   c                    s    t  fdd| jd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   s   | ]}|d    V  qdS )   Nget)r    edger0   r1   r   r$   	<genexpr>k   r&   z#branching_weight.<locals>.<genexpr>Tdata)sumedges)Gr0   r1   r   r7   r$   r   L   s    r      c                    s$  |t vrtd|dkr d}nd} du r6t|d  fdd| jdd	D }z|jtd
dd|d W n$ ty   |jtd
|d Y n0 t }|	|  tj
 }t|D ]h\}	\}
}}||
 || krqq||dkrqqi } dur|| < |j|
|fi | ||
| q|S )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.

    Unknown value for `kind`.r   FTNr"   c                    s$   g | ]\}}}|||  fqS r   r4   )r    uvr:   r7   r   r$   r%      r&   z$greedy_branching.<locals>.<listcomp>r9   r3   r   r   )keyreverse)KINDSnxNetworkXExceptionr*   r<   sortr   	TypeErrorZDiGraphadd_nodes_fromutils	UnionFind	enumerate	in_degreeadd_edgeunion)r=   r0   r1   kindr#   rC   r<   Bufir@   rA   wr:   r   r7   r$   r   n   s4    #



r   c                       sR   e Zd ZdZd fdd	Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
  ZS )MultiDiGraph_EdgeKeya  
    MultiDiGraph which assigns unique keys to every edge.

    Adds a dictionary edge_index which maps edge keys to (u, v, data) tuples.

    This is not a complete implementation. For Edmonds algorithm, we only use
    add_node and add_edge, so that is all that is implemented here. During
    additions, any specified keys are ignored---this means that you also
    cannot update edge attributes through add_node and add_edge.

    Why do we need this? Edmonds algorithm requires that we track edges, even
    as we change the head and tail of an edge, and even changing the weight
    of edges. We must reliably track edges across graph mutations.
    Nc                    sB   t  }|jf d|i| || _i | _dd l}d}||t d S )Nincoming_graph_datar   zMMultiDiGraph_EdgeKey has been deprecated and will be removed in NetworkX 3.4.)super__init___cls
edge_indexwarningswarnDeprecationWarning)selfrV   r0   clsr[   msg	__class__r   r$   rX      s    zMultiDiGraph_EdgeKey.__init__c                 C   sd   t  }| j|  D ]}|| q| j|  D ]}|| q2|D ]}| j|= qF| j| d S r+   )setpredvaluesupdatesuccrZ   rY   remove_node)r^   r!   keyskeydictrB   r   r   r$   rh      s    
z MultiDiGraph_EdgeKey.remove_nodec                 C   s   |D ]}|  | qd S r+   )rh   )r^   nbunchr!   r   r   r$   remove_nodes_from   s    z&MultiDiGraph_EdgeKey.remove_nodes_fromc                 K   s   |||  }}}|| j v rJ| j | \}}	}
||ks:||	krJtd|d| jj|||fi | ||| j| | | f| j |< dS )z'
        Key is now required.

        Key  is already in use.N)rZ   	ExceptionrY   rN   rg   )r^   Z
u_for_edgeZ
v_for_edgeZkey_for_edger0   r@   rA   rB   uuvv_r   r   r$   rN      s    
zMultiDiGraph_EdgeKey.add_edgec                 K   s,   |D ]"\}}}}| j |||fi | qd S r+   )rN   )r^   Zebunch_to_addr0   r@   rA   kdr   r   r$   add_edges_from   s    z#MultiDiGraph_EdgeKey.add_edges_fromc              
   C   sf   z| j | \}}}W n4 tyH } ztd||W Y d }~n"d }~0 0 | j |= | j||| d S )NzInvalid edge key )rZ   KeyErrorrY   Zremove_edge)r^   rB   r@   rA   rr   errr   r   r$   remove_edge_with_key   s    &z)MultiDiGraph_EdgeKey.remove_edge_with_keyc                 C   s   t d S r+   )NotImplementedError)r^   Zebunchr   r   r$   remove_edges_from  s    z&MultiDiGraph_EdgeKey.remove_edges_from)N)__name__
__module____qualname____doc__rX   rh   rl   rN   ru   rx   rz   __classcell__r   r   ra   r$   rU      s   	rU   c                    sB   t  || fddfddtdd D }|fS )z
    Returns the edge keys of the unique path between u and v.

    This is not a generic function. G must be a branching and an instance of
    MultiDiGraph_EdgeKey.

    c                    s$    |   |   }t|}|d S )Nr   )ri   list)rS   rq   ri   )r=   nodesr   r$   	first_key  s    zget_path.<locals>.first_keyc                    s   g | ]\}} ||qS r   r   r    rS   rq   )r   r   r$   r%     r&   zget_path.<locals>.<listcomp>r   N)rE   shortest_pathrL   )r=   r@   rA   r<   r   )r=   r   r   r$   get_path
  s    r   c                   @   s,   e Zd ZdZdddZdd ZdddZdS )r   a  
    Edmonds algorithm [1]_ for finding optimal branchings and spanning
    arborescences.

    This algorithm can find both minimum and maximum spanning arborescences and
    branchings.

    Notes
    -----
    While this algorithm can find a minimum branching, since it isn't required
    to be spanning, the minimum branching is always from the set of negative
    weight edges which is most likely the empty set for most graphs.

    References
    ----------
    .. [1] J. Edmonds, Optimum Branchings, Journal of Research of the National
           Bureau of Standards, 1967, Vol. 71B, p.233-240,
           https://archive.org/details/jresv71Bn4p233

    Nc                 C   s>   || _ d| _g | _t|dd | _dd l}d}||t d S )NTr"   z_{0}r   zEdmonds has been deprecated and will be removed in NetworkX 3.4. Please use the appropriate minimum or maximum branching or arborescence function directly.)
G_originalstorer<   r*   templater[   r\   r]   )r^   r=   r#   r[   r`   r   r   r$   rX   8  s    zEdmonds.__init__c                 C   sH  |t vrtd|| _|| _|| _|| _|dkr>t | _}n
t	 | _}|du rZt
|d}|| _dt
|d | _t  | _}	t| jjddD ]z\}
\}}}|||||i}||dur||||< |r| D ]\}}||kr|||< q|	j|||
fi | qd| _t | _i | j_g | _g | _tj | _g | _g | _dS )	a  
        So we need the code in _init and find_optimum to successfully run edmonds algorithm.
        Responsibilities of the _init function:
        - Check that the kind argument is in {min, max} or raise a NetworkXException.
        - Transform the graph if we need a minimum arborescence/branching.
          - The current method is to map weight -> -weight. This is NOT a good approach since
            the algorithm can and does choose to ignore negative weights when creating a branching
            since that is always optimal when maximzing the weights. I think we should set the edge
            weights to be (max_weight + 1) - edge_weight.
        - Transform the graph into a MultiDiGraph, adding the partition information and potoentially
          other edge attributes if we set preserve_attrs = True.
        - Setup the buckets and union find data structures required for the algorithm.
        r?   r   Nr"   Z
candidate_Tr9   r   )rD   rE   rF   r0   r1   rP   styler.   transr/   r*   _attrcandidate_attrrU   r=   rL   r   r<   r5   itemsrN   levelrQ   rZ   graphs
branchingsrJ   rK   rR   circuitsminedge_circuit)r^   r0   r1   rP   r   preserve_attrsr#   	partitionr   r=   rB   r@   rA   r:   rt   d_kd_vr   r   r$   _initJ  s>    


 
	zEdmonds._initr-   r   r   r   Fc           )   	      s  |  ||||| | j}| j| j  }	t }
tt  }| j j	} fdd}zt
|}W n~ ty   t t|	ksJ t|	rt|	sJ | jr| j   | j|	  | jg  | jd Y qY n0 ||
v rq\|
| |	| ||\}}|du r q\q\|d }|| || krZt|	||\}}||d  nd\}}| jdkr~|dkr~d}nd	}|r\|i}|d
 dur|d
 |< |	j|||d fi | d	 | | |d  | j< ||| |dur\t}d}i }|D ]P}|	j| \}}}| }|||< |tj j!krHq||k r|}|}q| j| | j| | jr| j   | j|	  | j"#| j$} | g } j%d	d	dD ]\}}}}||v r
||v rqn| }|||||f nJ||v r| }||||  7 }| }||< |||||f nqȐqȈ &| |	&| |
't| |D ]Z\}}}} j|||fi | | j|v r~|| j= |	j|||fi | ||| q~tt  }|  j$d7  _$q\| j() }dd } t| j| j$ j}!| j$dkr |  j$d8  _$| j"#| j$}"| j| j$ }#| | j| j$d  |"|!\}$}%|!*|# |$r| j| j$ }|du rt+|!,| nX| j| j$   j|% d }&|#D ]&}% j|% \}}}||&kr qqt+d|!,|% q|!| _%|-| j( |!D ]z}%| jd j|% \}}}'| j.| /|'| j. i}|rz|'0 D ]$\}}(|| j.| jfvrT|(||< qT|j||fi | q|S )a9  
        Returns a branching from G.

        Parameters
        ----------
        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`.
        kind : {'min', 'max'}
            The type of optimum to search for, either 'min' or 'max'.
        style : {'branching', 'arborescence'}
            If 'branching', then an optimal branching is found. If `style` is
            'arborescence', then a branching is found, such that if the
            branching is also an arborescence, then the branching is an
            optimal spanning arborescences. A given graph G need not have
            an optimal spanning arborescence.
        preserve_attrs : bool
            If True, preserve the other edge attributes of the original
            graph (that are not the one passed to `attr`)
        partition : str
            The edge attribute holding edge partition data. Used in the
            spanning arborescence iterator.
        seed : integer, random_state, or None (default)
            Indicator of random number generation state.
            See :ref:`Randomness<randomness>`.

        Returns
        -------
        H : (multi)digraph
            The branching.

        c                    s   d}t  } j| dddD ]r\}}}}|tjjkr:q| }|tjjkrr|}|| |||f}||f  S ||kr|}|| |||f}q||fS )aQ  
            Find the edge directed toward v with maximal weight.

            If an edge partition exists in this graph, return the included edge
            if it exists and no not return any excluded edges. There can only
            be one included edge for each vertex otherwise the edge partition is
            empty.
            NTr:   ri   INFin_edgesr5   rE   EdgePartitionEXCLUDEDINCLUDED)rA   r6   r-   r@   rr   rB   r:   
new_weightr=   r0   r   r   r$   desired_edge  s    	z*Edmonds.find_optimum.<locals>.desired_edgeNr   r3   )NNr   FTr>   r   r   c                 S   sV   || vrt |d| j| D ]0}| j| | D ]}||v r2d|f    S q2q dS )z
            Returns True if `u` is a root node in G.

            Node `u` will be a root node if its in-degree, restricted to the
            specified edges, is equal to 0.

            	 not in GFTNNro   rd   r=   r@   ZedgekeysrA   edgekeyr   r   r$   is_root  s    z%Edmonds.find_optimum.<locals>.is_root+Couldn't find edge incoming to merged node.)1r   rR   r=   rQ   rc   iterr   r   r   rd   nextStopIterationlenr
   r   r   appendcopyr   r   r   addadd_noder   r   r5   rN   r   rO   r   rZ   rE   r   r   r   formatr   r<   rl   difference_updater   rb   rf   ro   removerI   r0   r   r   ))r^   r0   r1   rP   r   r   r   r#   rR   rQ   Dr   ZG_predr   rA   r6   r-   r@   Q_nodesQ_edgesZ
acceptabledd	minweightminedgeQ_incoming_weightedge_keyr:   rT   new_node	new_edgesrB   Hr   r<   merged_nodecircuitisrootr   targetrt   valuer   r   r$   find_optimum  s    ,
















zEdmonds.find_optimum)N)r-   r   r   r   FNN)r{   r|   r}   r~   rX   r   r   r   r   r   r$   r   "  s   
\       r   )r0   r   r   )r2   Zpreserve_edge_attrsFc                    s"  dd dd 	dd}t  i t|jddD ]x\}\}}}	|	|i}
|	d urr|	|
< |r|	 D ]\}}|kr~||
|< q~|||fi |
 q8d	}t   i g 
g t t j g g fd
d} 	
fdd}dd }t	t
j}zt|}W n ty   tt ksdJ t r|t s|J 
  f    f g  d  Y qY n0 |v r֐q2|  | ||\}}|d ur2|d	kr2|d	 }| | k}|i}|d d urP|d |<  |||d fi | d| | |d  < || |r2|||| t	t
 }|d7 }q2| }t| d }|d	kr|d8 }t| }| }|
|d  d	 ||\}}|| |rJ| }|d u r>t|| nT
| \| d }|D ]$}| \}}}	||krf qqftd|| q|| |D ]l}
d	 d | \}}}
|
 i}|r|
 D ] \}}|fvr|||< q|j||fi | q|S )Nc           	      [   sl   ||v r6|| \}}}||ks&||kr6t d|d| j|||fi | ||| j| | | f||< dS )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.
        rm   rn   N)ro   rN   rg   )	r=   rZ   r@   rA   rB   rt   rp   rq   rr   r   r   r$   edmonds_add_edge  s    z+maximum_branching.<locals>.edmonds_add_edgec                 S   s`   t  }| j|  D ]}|| q| j|  D ]}|| q2|D ]
}||= qF| | dS )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)rc   rd   re   rf   rg   rh   )r=   rZ   r!   ri   rj   rB   r   r   r$   edmonds_remove_node  s    z.maximum_branching.<locals>.edmonds_remove_nodez#edmonds' secret candidate attributezedmonds new node base name Tr9   r   c                    s   d}t  } j| dddD ]j\}}}}|tjjkr:q| }|tjjkrj|}|| |||f} q||kr|}|| |||f}q||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.
        NTr   r   )rA   r6   
max_weightr@   rr   rB   r:   r   r   r   r$   edmonds_find_desired_edge~  s    z4maximum_branching.<locals>.edmonds_find_desired_edgec                    s*  |d }t | |  fddt dd D }||d  t}d}i }|D ]F}| \}} }	|	 }
|
|| < |	t jjkrqT|
|k rT|
}|}qT| |   f   f t	| }
| g }jdddD ]\}} }}	||v rF| |v r*qn|	 }||| ||f nJ| |v r|	 }
|
|||   7 }
|	 }|
|< |||||f nqq D ]}
| 
| qt  |D ]Z\}} }}		|| |fi |	 |	v r|	= 	|| |fi |	 ||  qdS )	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   c                    s,   g | ]$\}}t  |  |  d  qS )r   )r   ri   r   )rQ   r   r   r$   r%     s   z>maximum_branching.<locals>.edmonds_step_I2.<locals>.<listcomp>r   Nr3   Tr   )rE   r   rL   r   r   r5   r   r   r   strr   r<   r   rc   rO   )rA   r   r   r@   r   r   r   r   r   r:   rT   r   r   rB   r   noderQ   ZB_edge_indexr=   ZG_edge_indexr0   r   r   r   r   r   r   r   Znew_node_base_namer   Zselected_nodesrR   )r   r$   edmonds_step_I2  s`    






z*maximum_branching.<locals>.edmonds_step_I2c                 S   sV   || vrt |d| j| D ]0}| j| | D ]}||v r2d|f    S q2q dS )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.
        r   Fr   Nr   r   r   r   r$   r     s    z"maximum_branching.<locals>.is_rootr>   r3   r   r   )rE   MultiDiGraphrL   r<   r5   r   rc   rJ   rK   r   r   r   r   r   r   r
   r   r   r   r   rO   rb   r   rf   ro   r   rI   rN   )r=   r0   r1   r   r   r   rB   r@   rA   r:   rt   r   r   r   r   r   r   r   r   Zdesired_edge_weightr   r   r   r<   r   r   r   r   r   r   r   r   r$   r     s    !

%*W







	





r   c                 C   s   | j ddD ]\}}}||  ||< qt| ||||}| j ddD ]\}}}||  ||< qB|j ddD ]\}}}||  ||< qh|S )NTr9   )r<   r   )r=   r0   r1   r   r   rr   rt   rQ   r   r   r$   r     s    r   r0   r1   r   r   c               C   s   t  }t }| j|dD ]"\}}}||kr,|}||k r|}q| jddD ]&\}}}	|d ||  |	|  |	|< qFt| ||||}
| jddD ]&\}}}	|d ||  |	|  |	|< q|
jddD ]&\}}}	|d ||  |	|  |	|< q|
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   Tr   )r   r<   r   )r=   r0   r1   r   r   r   
min_weightrr   rT   rt   rQ   r   r   r$   r     s    %r   c                 C   s   t }t  }| j|dD ]"\}}}||k r,|}||kr|}q| jddD ]&\}}}	|	| | d ||  |	|< qFt| ||||}
| jddD ]&\}}}	|	| | d ||  |	|< q|
jddD ]&\}}}	|	| | d ||  |	|< qt|
stjd|
S )Nr9   Tr   z&No maximum spanning arborescence in G.)r   r<   r   r	   rE   	exceptionrF   )r=   r0   r1   r   r   r   r   rr   rT   rt   rQ   r   r   r$   r     s"    r   c                 C   s*   t | ||||d}t|s&tjd|S )Nr   z&No minimum spanning arborescence in G.)r   r	   rE   r   rF   )r=   r0   r1   r   r   rQ   r   r   r$   r     s    r   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)rP   r   minimumz+
See Also 
-------- 
    minimal_branching
r   c                   @   sZ   e Zd ZdZeddG dd dZddd	Zd
d Zdd Zdd Z	dd Z
dd ZdS )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                   @   s4   e Zd ZU dZeed< eddZeed< dd Z	dS )	z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                 C   s   t | j| j S r+   )r   	Partitionr   r   r   )r^   r   r   r$   __copy__  s    z'ArborescenceIterator.Partition.__copy__N)
r{   r|   r}   r~   float__annotations__r   r   dictr   r   r   r   r$   r   |  s   
r   r-   Nc                 C   s   |  | _|| _|| _|rtnt| _d| _|durzi }|d D ]}tj	j
||< q>|d D ]}tj	j||< qXtd|| _nd| _dS )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   )r   r=   r-   r   r   r   methodpartition_keyrE   r   r   r   r   r   init_partition)r^   r=   r-   r   r   r   er   r   r$   rX     s    

zArborescenceIterator.__init__c                 C   s   t  | _| | j | jdur*| | j | j| j| j| jddj	| jd}| j
| | jr`|n| | jdu rri n| jj | S )zu
        Returns
        -------
        ArborescenceIterator
            The iterator object for this graph
        NTr   r   r,   )r   partition_queue_clear_partitionr=   r   _write_partitionr   r-   r   sizeputr   r   r   )r^   r   r   r   r$   __iter__  s*    
	zArborescenceIterator.__iter__c                 C   s\   | j  r| `| ` t| j  }| | | j| j| j| jdd}| 	|| | 
| |S )z
        Returns
        -------
        (multi)Graph
            The spanning tree of next greatest weight, which ties broken
            arbitrarily.
        Tr   )r   emptyr=   r   r5   r   r   r-   r   
_partitionr   )r^   r   Znext_arborescencer   r   r$   __next__  s    



zArborescenceIterator.__next__c              	   C   s   |  d|j }|  d|j }|jD ]}||jvr*tjj|j|< tjj|j|< | | zL| j	| j
| j| jdd}|j| jd}| jr|n| |_| j|  W n tjy   Y n0 |j |_q*dS )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   r   r<   rE   r   r   r   r   r   r=   r-   r   r   r   r   r   r   r   rF   )r^   r   Zpartition_arborescencep1p2r   Zp1_mstZp1_mst_weightr   r   r$   r     s(    


zArborescenceIterator._partitionc                 C   s  | j jddD ]<\}}}||f|jv r<|j||f || j< qtjj|| j< q| j D ]}d}d}| j j|ddD ]D\}}}|| jtjj	kr|d7 }qn|| jtjj
krn|d7 }qn|dkrR|| j |d krR| j j|ddD ],\}}}|| jtjj	krtjj
|| j< qqRdS )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.
        Tr9   r   )rk   r:   r   N)r=   r<   r   r   rE   r   ZOPENr   r5   r   r   rM   )r^   r   r@   rA   rt   r!   Zincluded_countZexcluded_countr   r   r$   r     s     


z%ArborescenceIterator._write_partitionc                 C   s.   |j ddD ]\}}}| j|v r|| j= qdS )z7
        Removes partition data from the graph
        Tr9   N)r<   r   )r^   r=   r@   rA   rt   r   r   r$   r   :  s    
z%ArborescenceIterator._clear_partition)r-   TN)r{   r|   r}   r~   r   r   rX   r   r   r   r   r   r   r   r   r$   r   g  s   
+ ("r   )r   N)r-   r   )r-   r   r   N)r-   r   FN)r-   r   FN)r-   r   FN)r-   r   FN)*r~   r   Zdataclassesr   r   enumr   operatorr   queuer   ZnetworkxrE   Znetworkx.utilsr   Zrecognitionr	   r
   __all__rD   ZSTYLESr   r   r*   r.   r/   	_dispatchr   r   r   rU   r   r   r   r   r   r   r   Zdocstring_branchingZdocstring_arborescencer   r   r   r   r   r$   <module>   s   !OK   M       + < & 	