
    sg                     4    d dl mZ d dlmZmZ  G d d      Zy)    )deque)ListSetc                       e Zd ZdZd Zd Zd Zd Zd Ze	d        Z
e	d        Zd	 Zd
 Zdedee   fdZdedee   fdZdedefdZdedee   fdZdefdZy)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                 J    i | _         i | _        i | _        i | _        d| _        y )Nr   )_node_succ_pred_node_order_insertion_idxselfs    I/var/www/html/venv/lib/python3.12/site-packages/torch/package/_digraph.py__init__zDiGraph.__init__   s+    
 

     c                    || j                   vr\|| j                   |<   i | j                  |<   i | j                  |<   | j                  | j                  |<   | xj                  dz  c_        y| j                   |   j                  |       y)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kwargss      r   add_nodezDiGraph.add_node   st     DJJ"DJJqMDJJqMDJJqM"&"5"5DQ1$JJqM  (r   c                     | j                  |       | j                  |       d| j                  |   |<   d| j                  |   |<   y)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vs      r   add_edgezDiGraph.add_edge+   sB     	aa  

1a

1ar   c                 v    	 t        | j                  |         S # t        $ r}t        d| d      |d}~ww xY w)z.Returns an iterator over successor nodes of n.	The node  is not in the digraph.N)iterr
   KeyError
ValueErrorr   r   es      r   
successorszDiGraph.successors8   D    	L

1&& 	Ly+BCD!K	L    	838c                 v    	 t        | j                  |         S # t        $ r}t        d| d      |d}~ww xY w)z1Returns an iterator over predecessors nodes of n.r   r   N)r    r   r!   r"   r#   s      r   predecessorszDiGraph.predecessors?   r&   r'   c              #   j   K   | j                   j                         D ]  \  }}|D ]  }||f 
  yw)z6Returns an iterator over all edges (u, v) in the graphN)r
   items)r   r   r%   succs       r   edgeszDiGraph.edgesF   s@      "ZZ--/ 	MAz" g	s   13c                     | j                   S )z6Returns a dictionary of all nodes to their attributes.)r	   r   s    r   nodeszDiGraph.nodesM   s     zzr   c                 ,    t        | j                        S )zIterate over the nodes.)r    r	   r   s    r   __iter__zDiGraph.__iter__R   s    DJJr   c                 >    	 || j                   v S # t        $ r Y yw xY w)z>Returns True if ``n`` is a node in the graph, False otherwise.F)r	   	TypeError)r   r   s     r   __contains__zDiGraph.__contains__V   s'    	

?" 		s    	srcreturnc                    t        |      }t        |      }t        |      dkD  r\|j                         }| j	                  |      D ])  }||vs|j                  |       |j                  |       + t        |      dkD  r\|S )z2Returns a set of nodes that are reachable from srcr   )setr   lenpopleftr%   addappendr   r5   resultworking_setcurr   s         r   forward_transitive_closurez"DiGraph.forward_transitive_closure]   s     SCj+"%%'C__S) *F?JJqM&&q)* +" r   c                    t        |      }t        |      }t        |      dkD  r\|j                         }| j	                  |      D ])  }||vs|j                  |       |j                  |       + t        |      dkD  r\|S )zGReturns a set of nodes that are reachable from src in reverse directionr   )r8   r   r9   r:   r)   r;   r<   r=   s         r   backward_transitive_closurez#DiGraph.backward_transitive_closurej   s     SCj+"%%'C&&s+ *F?JJqM&&q)* +" r   dstc                 P   t               }| j                  |      }||vr|S t        |      }t        |      dkD  r]|j	                         }| j                  |      D ]*  }||v s|j                  ||       |j                  |       , t        |      dkD  r]|j                         S )zAReturns a subgraph rooted at src that shows all the paths to dst.r   )	r   rA   r   r9   r:   r)   r   r<   to_dot)r   r5   rD   result_graphforward_reachable_from_srcr?   r@   r   s           r   	all_pathszDiGraph.all_pathsw   s     y%)%D%DS%I"00
 Cj+"%%'C&&s+ *22 ))!S1&&q)	* +" ""$$r   c                    g }|ri|j                  |       | j                  |   j                         }d\  }}|D ].  }| j                  j	                  |d      }| n|||k  s+|}|}0 |rit        t        |            S )z_Returns a list of nodes that show the first path that resulted in dst being added to the graph.) NN)r<   r   keysr   getlistreversed)r   rD   path
candidatesmin_idx	candidateidxs          r   
first_pathzDiGraph.first_path   s    KKC--/J#LC' $	&&**9d;;?cGm!G#C$	  HTN##r   c                 R    dj                  d | j                  D              }d| dS )zvReturns the dot representation of the graph.

        Returns:
            A dot representation of the graph.
        
c              3   4   K   | ]  \  }}d | d| d  yw)"z" -> "z";N ).0fts      r   	<genexpr>z!DiGraph.to_dot.<locals>.<genexpr>   s#     DAAaSqc,Ds   z,digraph G {
rankdir = LR;
node [shape=box];
z
}
)joinr-   )r   r-   s     r   rF   zDiGraph.to_dot   s7     		DDD  	 	r   N)__name__
__module____qualname____doc__r   r   r   r%   r)   propertyr-   r/   r1   r4   strr   rA   rC   rI   r   rU   rF   rZ   r   r   r   r      s     )  LL     c c#h s s3x %S %s %0$c $d3i $$ r   r   N)collectionsr   typingr   r   r   rZ   r   r   <module>rh      s     h hr   