L i$ddlmZGddZy))dequeceZdZdZdZdZdZdZdZe dZ e dZ d Z d Z d ed eefd Zd ed eefdZd edefdZded eefdZd efdZy)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. cJi|_i|_i|_i|_d|_y)Nr)_node_succ_pred _node_order_insertion_idxselfs \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/package/_digraph.py__init__zDiGraph.__init__ s+   c ||jvr\||j|<i|j|<i|j|<|j|j|<|xjdz 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)rrr r r update)r nkwargss radd_nodezDiGraph.add_nodest DJJ "DJJqMDJJqMDJJqM"&"5"5D  Q    1 $  JJqM  (rc|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)rrr )r uvs radd_edgezDiGraph.add_edge*sB a a  1 a 1 arcv t|j|S#t$r}td|d|d}~wwxYw)z.Returns an iterator over successor nodes of n. The node  is not in the digraph.N)iterrKeyError ValueErrorr res r successorszDiGraph.successors7D L 1 & & Ly+BCD! K L 838cv t|j|S#t$r}td|d|d}~wwxYw)z1Returns an iterator over predecessors nodes of n.rrN)rr rr r!s r predecessorszDiGraph.predecessors>r$r%c#jK|jjD]\}}|D]}||f yw)z6Returns an iterator over all edges (u, v) in the graphN)ritems)r rr#succs redgesz DiGraph.edgesEs@"ZZ--/ MAz" g   s13c|jS)z6Returns a dictionary of all nodes to their attributes.)rr s rnodesz DiGraph.nodesLszzrc,t|jS)zIterate over the nodes.)rrr s r__iter__zDiGraph.__iter__QsDJJrc> ||jvS#t$rYywxYw)z>Returns True if ``n`` is a node in the graph, False otherwise.F)r TypeError)r rs r __contains__zDiGraph.__contains__Us'  ? "  s  srcreturnct|}t|}t|dkDr\|j}|j |D])}||vs|j ||j |+t|dkDr\|S)z2Returns a set of nodes that are reachable from srcr)setrlenpopleftr#addappendr r3result working_setcurrs rforward_transitive_closurez"DiGraph.forward_transitive_closure\sSCj +"%%'C__S) *F?JJqM&&q) *+"  rct|}t|}t|dkDr\|j}|j |D])}||vs|j ||j |+t|dkDr\|S)zGReturns a set of nodes that are reachable from src in reverse directionr)r6rr7r8r'r9r:r;s rbackward_transitive_closurez#DiGraph.backward_transitive_closureisSCj +"%%'C&&s+ *F?JJqM&&q) *+"  rdstcPt}|j|}||vr|St|}t|dkDr]|j }|j |D]*}||vs|j |||j|,t|dkDr]|jS)zAReturns a subgraph rooted at src that shows all the paths to dst.r) rr?rr7r8r'rr:to_dot)r r3rB result_graphforward_reachable_from_srcr=r>rs r all_pathszDiGraph.all_pathsvsy %)%D%DS%I" 0 0  Cj +"%%'C&&s+ *22 ))!S1&&q)  *+"""$$rcg}|ri|j||j|j}d\}}|D].}|jj |d}|n|||ks+|}|}0|rit 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 getlistreversed)r rBpath candidatesmin_idx candidateidxs r first_pathzDiGraph.first_paths KK C--/J#LC' $ &&**9d;;?cGm!G#C  $ HTN##rcRdjd|jD}d|dS)zvReturns the dot representation of the graph. Returns: A dot representation of the graph.  c34K|]\}}d|d|dyw)"z" -> "z";N).0fts r z!DiGraph.to_dot..s#DAAaSqc,Dsz,digraph G { rankdir = LR; node [shape=box]; z } )joinr+)r r+s rrDzDiGraph.to_dots7  DDD  rN)__name__ __module__ __qualname____doc__rrrr#r'propertyr+r-r/r2strr6r?rArGrLrSrDrXrrrrs  )  LL   c c#h  s s3x %S%s%0$c$d3i$$  rrN) collectionsrrrXrrreshhr