Dev HelpersOpen library
← Back

Detect a cycle in a dependency graph

Detect a cycle in a dependency graph in Python.

Open this example in the interactive library

Source code

from graphlib import CycleError, TopologicalSorter
try:
    list(TopologicalSorter({'a': {'b'}, 'b': {'a'}}).static_order())
except CycleError:
    print('Cycle detected')

Sample output

Cycle detected