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)
| 111 | |
| 112 | |
| 113 | def _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 ─────────────────────────────────────── |
no test coverage detected
searching dependent graphs…