Ready-set tracker

Hands out the tasks of a graph without cycles as soon as every task they depend on is done, so a slow task holds up only the tasks that wait for it.

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

How it works

A Tracker runs a dependency graph the way a build system does. Ready returns the tasks that can start, and Done marks tasks as finished, which makes ready every task that was waiting only on them. An edge from A to B means B depends on A, so Ready returns B once A is done.

Levels split the same graph into rounds, and each round waits for its slowest task. The tracker starts a task the moment its own dependencies finish, so a long task in one round doesn't hold up the tasks of the next round that never waited for it.

The tracker leaves running the tasks to the caller, and with them how many run at once. This page uses two workers that take the ready tasks first in, first out, and each task takes its weight in time. NewTracker copies the graph in O(V+E), and all the Ready and Done calls of a run add up to O(V+E) too. It returns ErrDAGHasCycle for a graph with a cycle, and Done returns ErrNotReady for a task Ready hasn't returned.

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.

With two threads recomputing the sheet, each cell as soon as the cells it reads are up to date, how many milliseconds does a full recompute take?

With 2 workers, the last of the 25 tasks finishes at time 25. Running the levels one after another on the same workers takes 28, since each level waits for its slowest task. One task at a time would take 43.

Step by step

  1. The tracker hands out a task once every task it depends on is done, and 2 workers take the ready tasks in the order it returns them. Each task takes its weight in time, and the time it ends shows next to its name once it starts. The first call to Ready returns the 7 tasks that depend on nothing: B2, C2, D2, G2, G3, G4 and G5.
  2. At time 0, B2 and C2 start, to end at 1 and 1. Every worker is busy, so 5 ready tasks wait for one to come free.
  3. At time 1, B2 and C2 finish. Done makes no new task ready. D2 and G2 start, to end at 2 and 2. Every worker is busy, so 3 ready tasks wait for one to come free.
  4. At time 2, D2 and G2 finish. Done makes B3, C3 and D3 ready. G3 and G4 start, to end at 3 and 3. Every worker is busy, so 4 ready tasks wait for one to come free.
  5. At time 3, G3 and G4 finish. Done makes B4, C4 and D4 ready. G5 and B3 start, to end at 4 and 5. Every worker is busy, so 5 ready tasks wait for one to come free.
  6. At time 4, G5 finishes. Done makes no new task ready. C3 starts and takes 2, so it ends at 6. Every worker is busy, so 4 ready tasks wait for one to come free.

15 more steps lead to the last one:

  1. Done. At time 25, E8 finishes, the last of the 25 tasks. Running the levels one after another on 2 workers would take 28, and one task at a time 43.

Use it in Go

t, err := dag.NewTracker(g)
if err != nil {
	return err
}
workers := make(chan struct{}, 2)
finished := make(chan string, g.Order())
for t.Remaining() > 0 {
	for _, task := range t.Ready() {
		go func() {
			workers <- struct{}{}
			run(task) // task.Weight() is how long it takes
			<-workers
			finished <- task.Label()
		}()
	}
	if err := t.Done(<-finished); err != nil {
		return err
	}
}

Example graphs

More in Ordering

  • Topological sort: Puts the vertices of a graph without cycles in an order where every edge points forward, taking vertices in the order they become ready.
  • Stable topological sort: Puts the vertices of a graph without cycles in an order where every edge points forward, taking the ready vertex that comes first alphabetically.
  • Topological levels: Splits a graph without cycles into levels, so that every edge goes to a later level and each level can run at the same time.

All algorithms