Affected
Finds the changed vertices and every vertex reachable from them, such as all the cells a spreadsheet edit recomputes.
Time complexity O(V+E), gograph function dag.Affected.
How it works
Affected takes the vertices that changed and returns them with every vertex reachable from any of them. It is one breadth-first search that starts from all the changed vertices at once, so a vertex reachable from several of them is listed once.
The changed vertices come first, in the order given, then the rest, nearest first. It costs O(V+E) for the part of the graph it reaches, however many vertices changed. Running Descendants once for each change finds the same vertices, but repeats the work wherever their descendants overlap.
Use it to decide what to rebuild, retest or redeploy after a commit, which cells a spreadsheet recomputes after an edit, or which targets a change to a shared library touches.
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.
The March units in D2 and the tax rate in G5 change. Which cells does the sheet have to recompute?
14 vertices are affected: D2 and G5, which changed, and B6, B7, C6, C7, D3, D4, D5, D6, D7, E3 and 2 more.
Step by step
- D2 and G5 changed, so they are affected. Put them in the queue.
- Take D2 from the queue. It has edges to D3 and D4. Both are new, so they join the queue.
- Take G5 from the queue. It has edges to B6, C6 and D6. All 3 are new, so they join the queue.
- Take D3 from the queue. It has edges to D5 and E3. Both are new, so they join the queue.
- Take D4 from the queue. It has an edge to D5, which was found before.
- Take B6 from the queue. It has an edge to B7, which is new, so it joins the queue.
9 more steps lead to the last one:
- Done. 14 vertices are affected: the 2 changed vertices and 12 more reachable from them.
Use it in Go
jobs, err := dag.Affected(g, "D2", "G5")
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.
- Ancestors: Finds every vertex that can reach a vertex, such as all the courses one course needs first.
- 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.