Graphlib

In python 3.9, there’s a graphlib library, which contains a topological sort implementation.

Here’s a way to look at the dependencies for course schedule, a leetcode problem using topological sort to find a valid path through.

ts = TopologicalSorter()
for key, val in prerequisites:
    ts.add(val, key)

try:
    list(ts.static_order())
    return True
except:
    return False