This article follows one small example from the first line to the last. Nothing is skipped, so you can reproduce it on paper.

The input

A release pipeline has six tasks. Each may depend on others finishing first.

TaskMust wait for
compilenothing
migrate (database changes)nothing
unitcompile
image (container build)compile
integ (integration tests)image, migrate
deployunit, integ

The goal: produce a sequence in which every task appears after everything it waits for. That is a topological ordering of a directed graph, where each task is a node and each “must wait for” is an arrow from the prerequisite to the dependant. It exists only if the graph has no cycles. Build tools, spreadsheet recalculation, package installers and course-prerequisite checkers all solve this same problem.

Preparing two pieces of bookkeeping

The method here is Kahn’s algorithm. It needs, for each task, an in-degree: the number of prerequisites not yet done. And for each task, a list of who is waiting on it.

  • In-degrees: compile 0, migrate 0, unit 1, image 1, integ 2, deploy 2.
  • Who waits: compile is awaited by unit and image; migrate by integ; image by integ; unit by deploy; integ by deploy; deploy by nobody.

Put every task with in-degree 0 into a queue: compile and migrate. These can start immediately.

The run

Repeat: take a task from the front of the queue, append it to the output, and for each task waiting on it, reduce that task’s in-degree by one. Anything that reaches zero joins the queue.

  1. Take compile. Output: compile. Unit drops to 0, image drops to 0. Both join the queue. Queue is now migrate, unit, image.
  2. Take migrate. Integ drops from 2 to 1. Nothing new joins. Queue: unit, image.
  3. Take unit. Deploy drops from 2 to 1. Queue: image.
  4. Take image. Integ drops to 0 and joins. Queue: integ.
  5. Take integ. Deploy drops to 0 and joins. Queue: deploy.
  6. Take deploy. Nobody waits on it. Queue is empty.

Output: compile, migrate, unit, image, integ, deploy. Six tasks in, six out. Check any arrow: integ comes after both image and migrate, deploy after both unit and integ. Valid.

Notice that this is not the only valid answer. Swapping unit and image would also work. A topological ordering is usually not unique, and the tie-breaking depends on how you order the queue. That freedom matters in practice, because tasks that are in the queue at the same moment, such as unit and image at step 2, have no relationship with each other and can run in parallel. A real build system takes everything currently at in-degree 0 and runs it concurrently.

Introduce a cycle

Someone adds the rule that compile must wait for deploy, perhaps because a version stamp is read from the deployed environment. Now compile has in-degree 1, and the loop compile, image, integ, deploy, compile is closed.

Start again. Only migrate has in-degree 0. Take it: integ drops to 1. The queue is empty. We have output one task out of six.

That is the cycle test, and it costs nothing extra: if the output has fewer tasks than the input, a cycle exists. Every task left over is either on the cycle or downstream of it. To report the actual loop, collect the leftover tasks and walk their prerequisite arrows until a task repeats. Good tools print that path, because “circular dependency detected” without names is infuriating.

Cost, and the alternative

Every task is queued once and every arrow is examined once. With V tasks and E arrows, the time is O(V + E). That is as good as reading the input.

The other classic route is depth-first search. Visit each task; before finishing a task, first visit its prerequisites; add the task to the output once its prerequisites are done. Mark tasks as “in progress” while exploring, and if you meet an in-progress task again you have found a cycle. Same O(V + E). DFS finds the cycle path directly and is natural to write recursively, though deep graphs can hit a recursion limit. Kahn’s version is iterative and exposes parallelism more visibly. Pick by what you need.

Where else you will meet it

  • Airflow and similar schedulers require pipelines to be directed acyclic graphs, and reject loops at load time.
  • Package managers resolve install order this way, though version constraints add a much harder problem on top.
  • Spring and other dependency-injection containers fail on start-up when two beans need each other in their constructors.
  • Database migrations with foreign keys: the order in which you create or delete tables follows the same rule.

One more thing the algorithm does not do: it gives you an order, not the fastest schedule. If tasks have durations, the shortest total time is determined by the longest chain of dependent tasks, the critical path, which you can compute by extending the same traversal.

Figures and names in the examples are illustrative.

Leave a Reply

Trending