Jobs and arrows
Getting dressed is six jobs. An arrow from socks to shoes means socks must come first. Arrows that point one way, with no loop back, form a DAG: a directed acyclic graph. We want a list of jobs where every arrow points forward.
For each job, count the arrows that point into it. That number is its in-degree: how many jobs it still waits for. Socks, pants and shirt wait for nothing (0). Shoes, belt and jacket each wait for 2.
A job with in-degree 0 can start now. Put every such job in a queue, in the order the graph lists them: socks, pants, shirt. A queue is a line: the first job in is the first job out.
Kahn's algorithm
The loop: take the front job and write it down. Socks is first. Then cross out its outgoing arrows: each crossed arrow lowers the count of the job it pointed to. Shoes drops from 2 to 1, so it still waits.
Take pants and write it down. Its arrow to shoes is crossed out: shoes drops from 1 to 0, so shoes joins the back of the queue. Its arrow to belt is crossed out too: belt drops from 2 to 1.
Take shirt. Belt drops from 1 to 0 and joins the queue, behind shoes. Jacket drops from 2 to 1: it still waits for the belt. A job enters the queue only when everything before it is written down.
Shoes has no outgoing arrows, so taking it changes nothing. Taking belt drops jacket to 0, and jacket is taken last. The queue is empty and all six jobs are written down.
Checking the answer
Number the jobs by their place in the list. Every arrow goes from a smaller number to a bigger one: that is a topological order. It is not the only one. Shirt, socks, pants, belt, shoes, jacket also works.
Each job enters and leaves the queue once: V = 6 times. Each arrow is counted once and crossed out once: E = 6 arrows. So the work grows like V + E, written O(V + E), the same as one BFS or DFS.
Add a wrong arrow: jacket before shirt. Now shirt, belt and jacket each wait for another in a loop. No count reaches 0, so the run stops with 3 of 6 jobs written. A short list means the graph has a cycle.
The whole function in Python
from collections import deque
def topo_sort(graph):
indeg = {u: 0 for u in graph}
for u in graph:
for v in graph[u]:
indeg[v] += 1
ready = deque(u for u in graph if indeg[u] == 0)
order = []
while ready:
u = ready.popleft()
order.append(u)
for v in graph[u]:
indeg[v] -= 1
if indeg[v] == 0:
ready.append(v)
if len(order) < len(graph):
raise ValueError("cycle: no topological order")
return order
graph = {
"socks": ["shoes"],
"pants": ["shoes", "belt"],
"shirt": ["belt", "jacket"],
"shoes": [],
"belt": ["jacket"],
"jacket": [],
}
print(topo_sort(graph))
What happens
# step 1/10 · Jobs with arrows
a DAG: directed edges, no directed cycle
u -> v means "u before v"
socks -> shoes
pants -> shoes
pants -> belt
shirt -> belt
shirt -> jacket
belt -> jacket
the same graph in Python (each job lists the jobs that wait for it):
graph = {
"socks": ["shoes"],
"pants": ["shoes", "belt"],
"shirt": ["belt", "jacket"],
"shoes": [],
"belt": ["jacket"],
"jacket": [],
}
This queue method is Kahn's algorithm (A. B. Kahn, 1962). Another way, in MIT 6.006 and Sedgewick and Wayne, runs depth-first search and lists the jobs in reverse finishing order; both take O(V + E) time. The order is unique only when every pair of neighbours in the list is joined by an arrow. Here socks, pants and shirt are all ready at the start, so several orders are correct; the queue picks the one the graph lists first.