MCPcopy Index your code
hub / github.com/python/cpython / _find_cycles

Function _find_cycles

Lib/asyncio/tools.py:113–139  ·  view source on GitHub ↗

Depth-first search for back-edges. Returns a list of cycles (each cycle is a list of task-ids) or an empty list if the graph is acyclic.

(graph)

Source from the content-addressed store, hash-verified

111
112
113def _find_cycles(graph):
114 """
115 Depth-first search for back-edges.
116
117 Returns a list of cycles (each cycle is a list of task-ids) or an
118 empty list if the graph is acyclic.
119 """
120 WHITE, GREY, BLACK = 0, 1, 2
121 color = defaultdict(lambda: WHITE)
122 path, cycles = [], []
123
124 def dfs(v):
125 color[v] = GREY
126 path.append(v)
127 for w in graph.get(v, ()):
128 if color[w] == WHITE:
129 dfs(w)
130 elif color[w] == GREY: # back-edge → cycle!
131 i = path.index(w)
132 cycles.append(path[i:] + [w]) # make a copy
133 color[v] = BLACK
134 path.pop()
135
136 for v in list(graph):
137 if color[v] == WHITE:
138 dfs(v)
139 return cycles
140
141
142# ─── PRINT TREE FUNCTION ───────────────────────────────────────

Callers 1

build_async_treeFunction · 0.85

Calls 3

defaultdictClass · 0.85
listClass · 0.85
dfsFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…