Ancestors
Finds every vertex that can reach a vertex, such as all the courses one course needs first.
Time complexity O(V+E), gograph function dag.Ancestors.
How it works
Ancestors runs the same search backward. It follows edges in reverse from a vertex and collects every vertex that can reach it. With an edge from A to B read as B depending on A, these are everything the vertex depends on, directly or through others.
A gograph graph doesn't index incoming edges, so dag.Ancestors first reads every edge once to find them, then searches breadth first. It costs O(V+E) and returns the vertices nearest first. The vertex itself is in the result only when it's on a cycle.
Use it to find what has to finish before a job can start, which courses come before another, or why a module is in a build. In go mod graph, an edge goes from a module to one it requires, so the ancestors of a module are the modules that pull it in.
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.
Which cells does January's net profit in B7 depend on, directly or through other formulas?
9 vertices can reach B7: B2, B3, B4, B5, B6, G2, G3, G4 and G5.
Step by step
- The graph stores only the edges out of each vertex, so read every edge once, 41 in all, to find the edges into each one.
- Start at B7. It has edges from B5 and B6. Both are new, so they join the queue.
- Take B5 from the queue. It has edges from G4, B3 and B4. All 3 are new, so they join the queue.
- Take B6 from the queue. It has edges from G5 and B5. G5 is new, so it joins the queue. B5 was found before.
- Take G4 from the queue. It has no edges in.
- Take B3 from the queue. It has edges from B2 and G2. Both are new, so they join the queue.
5 more steps lead to the last one:
- Done. 9 vertices can reach B7.
Use it in Go
jobs, err := dag.Ancestors(g, "B7")
if err != nil {
return err
}
for _, v := range jobs {
fmt.Println(v.Label())
}
Example graphs
- Build pipeline, 7 vertices and 9 edges
- Course prerequisites, 14 vertices and 18 edges
- Spreadsheet formulas, 25 vertices and 41 edges
- Go module graph, 62 vertices and 116 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.
- 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.
- 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.
- Diff: Compares two graphs and returns the vertices, edges, weights and edge labels that differ, matching vertices by label.