Topological sort with DFS

Make a plan that puts each task after all its prerequisites. This helps you assemble a model and spot instructions that ask you to fit two parts before each other.

Append a task after finishing its descendants and reverse that order. An edge back to an unfinished task reveals a cycle.

With adjacency lists, traversal takes O(V + E) time for V vertices and E edges.