ML iv tdZddlmZddlmZmZmZddlm Z e dZ d dZ d dZ d dZ y ) z&Helpers for manipulations with graphs.) annotations)IterableIteratorSet)TypeVarTc#Ktgigdfd |D]}|vs|Ed{y7w)aCompute Strongly Connected Components of a directed graph. Args: vertices: the labels for the vertices edges: for each vertex, gives the target vertices of its outgoing edges Returns: An iterator yielding strongly connected components, each represented as a set of vertices. Each input vertex will occur exactly once; vertices not part of a SCC are returned as singleton sets. From https://code.activestate.com/recipes/578507/. c3Kt|<j|j||D]D}|vr|Ed{|vs|dks)j|dkrFd|k(r?jt|d}|d=j ||yy7w)N)lenappendpopsetupdate) vwscc boundariesdfsedges identifiedindexstacks V/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/mypy/graph_utils.pyrz*strongly_connected_components..dfs!su:a Q%(#q %A~q6!!*$AhB/NN$AhB/  % b>U1X % NN eE!HJ'(CeAhj!   c "I & "s%A CC C C%CACN)rrreturnIterator[set[T]])r)verticesrrrrrrrs ` @@@@@rstrongly_connected_componentsr sQ"JEEJ& E>1v   s%A AAAci|D]}t|}|D]}||< i}|D]>}t}|D]}|jfd||D!||t|<@|S)zLUse original edges to organize SCCs in a graph by dependencies between them.c3(K|] }| yw)N).0xsccsmaps r zprepare_sccs..Fs5q 5s) frozensetrr)sccsrr scc_frozenrdatadepsr$s @r prepare_sccsr+9sG$s^  $A#GAJ $$79D$$'E 6A KK5E!H5 5 6#Ys^ $ Kc#K|jD]\}}|j|tj|j t|j z D]}t||< |jDchc] \}}|r | }}}|sn0||jDcic]\}}||vs |||z }}}X|r Jd|ycc}}wcc}}ww)aTopological sort. Args: data: A map from vertices to all vertices that it has an edge connecting it to. NOTE: This data structure is modified in place -- for normalization purposes, self-dependencies are removed and entries representing orphans are added. Returns: An iterator yielding sets of vertices that have an equivalent ordering. Example: Suppose the input has the following structure: {A: {B, C}, B: {D}, C: {D}} This is normalized to: {A: {B, C}, B: {D}, C: {D}, D: {}} The algorithm will yield the following values: {D} {B, C} {A} From https://code.activestate.com/recipes/577413/. z#A cyclic dependency exists amongst N)itemsdiscardrunionvalueskeys)r)kritemdepreadys rtopsortr7Ks@ 1 !  4;;=)C ,<<UT  '+zz|?)$3??  59ZZ\W cTQVEVsU{"WW  C:4(CC8t @Xs*B C* CCC*8 C$C$ C*N)rzAbstractSet[T]rdict[T, list[T]]rr)r'z list[set[T]]rr8rz)dict[AbstractSet[T], set[AbstractSet[T]]])r)zdict[T, set[T]]rzIterable[set[T]])__doc__ __future__rcollections.abcrrr AbstractSettypingrrrr+r7r!r,rr>s^,"BB CL++%5++\ /.$*Dr,