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.

Course prerequisites

Fourteen courses of a computer science degree, each one semester long. Each edge points from a course to a course that requires it.

A directed graph with 14 vertices and 18 edges.

However many courses a student takes at once, how many semesters does the degree take at least, and which chain of courses sets that?

The critical path is Programming 1, Programming 2, Data structures, Algorithms and Compilers, 5 tasks taking 5 in all. Every task one after another takes 14, so running in parallel saves 9. 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. The tasks with no dependencies, Programming 1, Discrete math and Calculus 1, start at 0.
  2. Programming 1 depends on nothing. It starts at 0 and takes 1, so it finishes at 1. Programming 2 can't start before 1.
  3. Discrete math depends on nothing. It starts at 0 and takes 1, so it finishes at 1. Data structures, Probability and Algorithms can't start before 1.
  4. Calculus 1 depends on nothing. It starts at 0 and takes 1, so it finishes at 1. Calculus 2 and Linear algebra can't start before 1.
  5. Programming 2 starts at 1, when Programming 1 finishes, and takes 1, so it finishes at 2. Computer systems and Data structures can't start before 2.
  6. Calculus 2 starts at 1, when Calculus 1 finishes, and takes 1, so it finishes at 2. Probability can't start before 2.

10 more steps lead to the last one:

  1. Done. The critical path takes 5, against 14 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