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.
Spreadsheet formulas
A sheet that works out the profit of one quarter. Row 2 holds the units sold from January to March, and G2 to G5 hold the price, unit cost, monthly fixed costs and tax rate. Rows 3 to 7 work out revenue, cost, profit before tax, tax and net profit, and E3, E7 and E8 hold the quarter's revenue, net profit and margin. Each cell takes 1 to 4 milliseconds to work out. Each edge points from a cell to a formula that reads it.
A directed graph with 25 vertices and 41 edges.
Even with a thread for every cell, how long does a recompute take, and which chain of formulas sets that?
The critical path is B2, B3, B5, B6, B7, E7 and E8, 7 tasks taking 13 in all. Every task one after another takes 43, so running in parallel saves 30. Only a faster task on the path makes the whole run shorter.
Step by step
- 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, B2, C2, D2, G2, G3, G4 and G5, start at 0.
- B2 depends on nothing. It starts at 0 and takes 1, so it finishes at 1. B3 and B4 can't start before 1.
- C2 depends on nothing. It starts at 0 and takes 1, so it finishes at 1. C3 and C4 can't start before 1.
- D2 depends on nothing. It starts at 0 and takes 1, so it finishes at 1. D3 and D4 can't start before 1.
- G2 depends on nothing. It starts at 0 and takes 1, so it finishes at 1. B3, C3 and D3 already wait at least that long for other tasks.
- G3 depends on nothing. It starts at 0 and takes 1, so it finishes at 1. B4, C4 and D4 already wait at least that long for other tasks.
21 more steps lead to the last one:
- Done. The critical path takes 13, against 43 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
- Build pipeline, 7 vertices and 9 edges
- Course prerequisites, 14 vertices and 18 edges
- Spreadsheet formulas, 25 vertices and 41 edges
- Monorepo build, 100 vertices and 266 edges
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.