Data structures / Directed acyclic graphs (DAGs)

Directed acyclic graphs (DAGs)

Arrows that only go one way, and no route that leads back to where it started. Seven steps on what makes a graph a DAG, why it can always be put in order, and where you meet one.

Beginner7 steps~6 min
Node — a task, a file or a commit
Arrow — a one-way edge: “comes before”
Back arrow — the extra edge that closes a cycle
// THE SHORT VERSION

In 30 seconds

A directed acyclic graph, or DAG, is a set of nodes joined by one-way arrows, with no way to follow the arrows round in a circle.

That one restriction buys a lot. The nodes of a DAG can always be lined up so that every arrow points forwards. This is called a topological order, and it is an order in which everything can be done.

So DAGs describe anything with dependencies: build steps, data pipelines, course prerequisites, and the commit history in Git.

Directed acyclic graphs (DAGs), step by step

STEPS01 / 07

Finding a topological order is its own lesson (Topological sort). Git’s commit graph cannot contain a cycle because a commit’s ID is a hash of its contents, parents included.

STEP 01 / 07 Edges with a direction
THE MATH
A -> B "A comes before B" 6 nodes, 7 arrows
  1. Step 1: Edges with a direction. A directed graph is a set of nodes joined by arrows, and each arrow goes one way only. Read the six nodes here as tasks and each arrow as “must come before”: A before B, A before C, and so on up to F. An arrow from A to B says nothing about getting from B back to A.
  2. Step 2: No path leads back. The graph is acyclic: follow the arrows from any node and you never come back to where you started. Starting at A, one walk goes to C, then E, then F, and stops, because no arrow leaves F. Directed plus acyclic gives the name: directed acyclic graph, or DAG.
  3. Step 3: One arrow can break it. Add a single arrow from F back to A and the walk A, C, E, F arrives at A again: a cycle. Now A must come before F and F must come before A, which is impossible. As tasks, each would wait for the other for ever. A graph with even one cycle is not a DAG.
  4. Step 4: A topological order exists. Remove that arrow again. Because there is no cycle, the nodes can be laid out in a line so that every arrow points forwards. That line is a topological order: do the tasks left to right and each one’s prerequisites are already done. Every DAG has such an order, and a graph with a cycle never does.
  5. Step 5: Often more than one order. The order is usually not unique. No arrow joins B and C, so either can go first; the same is true of D and E. This graph has five valid orders. Tasks in the same column here do not depend on each other at all, so a build tool with several processors can run them at the same time.
  6. Step 6: Builds and pipelines. Build systems and data pipelines are DAGs of tasks. A build tool knows that app needs both object files and each object file needs its source, so it compiles before it links, and only rebuilds what sits downstream of a change. A pipeline scheduler does the same with data: load, clean, join, report.
  7. Step 7: Git history is a DAG. Every Git commit records the commit or commits it was built on, so the arrows point backwards in time. A branch makes two commits share a parent; a merge commit has two parents. A commit can only point at commits that already exist, so no chain of arrows can loop back: the history is a DAG.
VALUES SO FAR
nodes6graph
arrows7, one-waydirected
cycles····acyclic
F to A····broken
order····valid
orders····choice
builds····use
Git····use

Spotted a mistake on this page?