The mill’s job cards from drying to loading on the big board: Kahn with in-degrees and its queue, or DFS tracing to the end (which job enters the order…
Why it matters
Build order (make, bundlers), package installs (npm, apt), database migrations, a spreadsheet recalculating cells, courses with prerequisites: all topological orders. And when dependencies loop, those tools refuse, exactly because no such order exists.
The key idea
Take what waits for no one
Common mistakes
The topo-sort never finishes: fewer jobs ordered There is a dependency loop: its jobs never reach in-degree 0. Check urutan.length === tugas.length; if it is less, walk back along the arrows between the ones left to find the loop and break one dependency.
A different order every time That is normal: when several jobs have in-degree 0 together, any may go first. If you need a stable result, take in-degree 0 jobs by a fixed rule (by name, say, with a priority queue).
In-degree: arrows in or out? In. For a dependency [a, b] (a before b), the arrow is a → b and it is b’s in-degree that goes up. A job with in-degree 0 waits for no one, so it may go first.
Try it in this playground
A complete work order Run Kahn until every job is ordered.
Try DFS Switch to DFS and run to the end: the order fills from the back.
Add a dependency Add a dependency of your own under the scene.
Find a loop Make the dependencies loop and run until the loop is named.