Prerequisites and topological ordering
A topological order puts every prerequisite before the task that needs it. Start with tasks that have no unfinished prerequisites.
Simple example
Tasks 1 and 2 must both finish before 3. Take ready tasks in ascending order and print one valid schedule.
offsets = [0, 1, 2, 2]
edges = [2, 2]
incoming = [0, 0, 2]
done = 0
while done < 3:
task = 0
while task < 3 and incoming[task] != 0:
task += 1
if task == 3:
break
print(task + 1)
incoming[task] = -1
i = offsets[task]
while i < offsets[task + 1]:
incoming[edges[i]] -= 1
i += 1
done += 1
1 2 3
Everyday examples
Prepare an order
Picking and printing the label can happen separately. Sealing needs both. Can sealing be the first task?
Finish A and B in either order. Only then does C have zero remaining prerequisites.
Schedule a room opening
Painting must finish before signs go up, and signs must be up before opening. Which task is last?
Every arrow points to a later task. Opening comes after both painting and signage.
Spot a circular requirement
Course A requires B, and B requires A. Which course is ready first?
Neither has zero prerequisites. Report the cycle so the schedule can be corrected.
Cost and limits
Several valid orders may exist. If unfinished tasks remain when no task is ready, the dependency graph contains a directed cycle.