o
    i                      @   s.   d dl mZ d dlmZmZ G dd dZdS )    )deque)ListSetc                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	dd Z
e	dd Zdd Zdd Zdedee fddZdedee fddZdedefddZdedee fddZdefdd Zd!S )"DiGraphzReally simple unweighted directed graph data structure to track dependencies.

    The API is pretty much the same as networkx so if you add something just
    copy their API.
    c                 C   s"   i | _ i | _i | _i | _d| _d S )Nr   )_node_succ_pred_node_order_insertion_idxself r   e/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/torch/package/_digraph.py__init__   s
   
zDiGraph.__init__c                 K   sZ   || j vr#|| j |< i | j|< i | j|< | j| j|< |  jd7  _dS | j | | dS )zAdd a node to the graph.

        Args:
            n: the node. Can we any object that is a valid dict key.
            **kwargs: any attributes you want to attach to the node.
           N)r   r   r   r
   r	   update)r   nkwargsr   r   r   add_node   s   



zDiGraph.add_nodec                 C   s4   |  | |  | d| j| |< d| j| |< dS )zAdd an edge to graph between nodes ``u`` and ``v``

        ``u`` and ``v`` will be created if they do not already exist.
        TN)r   r   r   )r   uvr   r   r   add_edge*   s   

zDiGraph.add_edgec              
   C   <   zt | j| W S  ty } z	td| d|d}~ww )z.Returns an iterator over successor nodes of n.	The node  is not in the digraph.N)iterr   KeyError
ValueErrorr   r   er   r   r   
successors7      zDiGraph.successorsc              
   C   r   )z1Returns an iterator over predecessors nodes of n.r   r   N)r   r   r   r   r   r   r   r   predecessors>   r!   zDiGraph.predecessorsc                 c   s.    | j  D ]\}}|D ]}||fV  qqdS )z6Returns an iterator over all edges (u, v) in the graphN)r   items)r   r   r    succr   r   r   edgesE   s   zDiGraph.edgesc                 C   s   | j S )z6Returns a dictionary of all nodes to their attributes.)r   r   r   r   r   nodesL   s   zDiGraph.nodesc                 C   s
   t | jS )zIterate over the nodes.)r   r   r   r   r   r   __iter__Q   s   
zDiGraph.__iter__c                 C   s"   z|| j v W S  ty   Y dS w )z>Returns True if ``n`` is a node in the graph, False otherwise.F)r   	TypeError)r   r   r   r   r   __contains__U   s
   zDiGraph.__contains__srcreturnc                 C   `   t |}t|}t|dkr.| }| |D ]}||vr'|| || qt|dks|S )z2Returns a set of nodes that are reachable from srcr   )setr   lenpopleftr    addappendr   r*   resultworking_setcurr   r   r   r   forward_transitive_closure\      

z"DiGraph.forward_transitive_closurec                 C   r,   )zGReturns a set of nodes that are reachable from src in reverse directionr   )r-   r   r.   r/   r"   r0   r1   r2   r   r   r   backward_transitive_closurei   r7   z#DiGraph.backward_transitive_closuredstc                 C   sz   t  }| |}||vr|S t|}t|dkr9| }| |D ]}||v r2||| || q!t|dks| S )zAReturns a subgraph rooted at src that shows all the paths to dst.r   )	r   r6   r   r.   r/   r"   r   r1   to_dot)r   r*   r9   Zresult_graphZforward_reachable_from_srcr4   r5   r   r   r   r   	all_pathsv   s   

zDiGraph.all_pathsc                 C   st   g }|r4| | | j|  }d\}}|D ]}| j|d}|du r% n|du s-||k r1|}|}q|stt|S )z_Returns a list of nodes that show the first path that resulted in dst being added to the graph.) NN)r1   r   keysr	   getlistreversed)r   r9   path
candidatesmin_idx	candidateidxr   r   r   
first_path   s   
zDiGraph.first_pathc                 C   s"   d dd | jD }d| dS )zvReturns the dot representation of the graph.

        Returns:
            A dot representation of the graph.
        
c                 s   s&    | ]\}}d | d| dV  qdS )"z" -> "z";Nr   ).0ftr   r   r   	<genexpr>   s   $ z!DiGraph.to_dot.<locals>.<genexpr>z,digraph G {
rankdir = LR;
node [shape=box];
z
}
)joinr%   )r   r%   r   r   r   r:      s   zDiGraph.to_dotN)__name__
__module____qualname____doc__r   r   r   r    r"   propertyr%   r&   r'   r)   strr   r6   r8   r;   r   rF   r:   r   r   r   r   r      s$    

r   N)collectionsr   typingr   r   r   r   r   r   r   <module>   s    