
    Vh                     $    d dl mZ  G d d      Zy)    )dequec                       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    F/home/dcms/DCMS/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.successors7   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.edgesE   s@      "ZZ--/ 	MAz" g	s   13c                     | j                   S )z6Returns a dictionary of all nodes to their attributes.)r   r   s    r   nodeszDiGraph.nodesL   s     zzr   c                 ,    t        | j                        S )zIterate over the nodes.)r   r   r   s    r   __iter__zDiGraph.__iter__Q   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__U   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   r3   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   )r6   r   r7   r8   r'   r9   r:   r;   s         r   backward_transitive_closurez#DiGraph.backward_transitive_closurei   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   r?   r   r7   r8   r'   r   r:   to_dot)r   r3   rB   result_graphforward_reachable_from_srcr=   r>   r   s           r   	all_pathszDiGraph.all_pathsv   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   rB   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   rD   zDiGraph.to_dot   s7     		DDD  	 	r   N)__name__
__module____qualname____doc__r   r   r   r#   r'   propertyr+   r-   r/   r2   strr6   r?   rA   rG   rL   rS   rD   rX   r   r   r   r      s     )  LL     c c#h s s3x %S %s %0$c $d3i $$ r   r   N)collectionsr   r   rX   r   r   <module>re      s    h hr   