Critical path

Finds the longest chain of tasks that wait on each other, which sets how long the whole run takes however many tasks run in parallel.

Time complexity O(V+E), gograph function dag.CriticalPath.

How it works

When tasks run in parallel, the total time is set by the longest chain of tasks that wait on each other. That chain is the critical path. Making a task on it faster shortens the whole run, and making any other task faster doesn't, since that task already has time to spare.

dag.CriticalPath takes the vertices in topological order, as Kahn's algorithm does. Each vertex finishes at the latest finish among the vertices it depends on, plus its own weight and the weight of the edge from that dependency. The path ends at the vertex with the latest finish among those nothing depends on, and following each vertex back to the dependency that set its start gives the path. It costs O(V+E).

Weights are read with Vertex.Weight and Edge.Weight, so the graph needs no Weighted option, and an unset weight counts as 0. On a tie, the path found first in topological order wins. CriticalPathFunc takes the costs from functions instead, such as durations measured on the last run. For a graph with cycles, condense it first: each loop of tasks becomes one vertex, which WithComponentWeight can give the sum of its tasks.

Build pipeline

The jobs of a CI pipeline for a web service, and how many minutes each one takes. Each edge points from a job to a job that waits for it.

A directed graph with 7 vertices and 9 edges.

Why does the pipeline take as long as it does, and which jobs are worth making faster?

The critical path is Checkout, Install, Compile, Unit tests and Deploy, 5 tasks taking 15 in all. Every task one after another takes 26, so running in parallel saves 11. Only a faster task on the path makes the whole run shorter.

Step by step

  1. Take the tasks in topological order, so every task comes after the tasks it depends on. A task finishes at the latest finish among its dependencies, plus its own weight. Checkout, the only task with no dependencies, starts at 0.
  2. Checkout depends on nothing. It starts at 0 and takes 1, so it finishes at 1. Install and Docker image can't start before 1.
  3. Install starts at 1, when Checkout finishes, and takes 3, so it finishes at 4. Lint and Compile can't start before 4.
  4. Lint starts at 4, when Install finishes, and takes 6, so it finishes at 10. Deploy can't start before 10.
  5. Compile starts at 4, when Install finishes, and takes 3, so it finishes at 7. Unit tests and Docker image can't start before 7.
  6. Unit tests starts at 7, when Compile finishes, and takes 6, so it finishes at 13. Deploy can't start before 13.

3 more steps lead to the last one:

  1. Done. The critical path takes 15, against 26 for every task one after another. The other tasks finish before anything on the path needs them.

Use it in Go

path, cost, err := dag.CriticalPath(g)
if err != nil {
	return err
}
for _, v := range path {
	fmt.Println(v.Label(), v.Weight())
}
fmt.Println(cost)

Example graphs

More in Dependencies

  • Descendants: Finds every vertex reachable from a vertex, such as all the jobs that wait for one step of a build.
  • Ancestors: Finds every vertex that can reach a vertex, such as all the courses one course needs first.
  • Affected: Finds the changed vertices and every vertex reachable from them, such as all the cells a spreadsheet edit recomputes.
  • Transitive reduction: Drops every edge that a longer path already implies. Each vertex still reaches the same vertices.

All algorithms