Sub topic 2
Directed Acyclic Graphs
This chapter assumes you know graph terminology (vertices, directed edges), adjacency lists in Python, and basic Python syntax. The previous chapter modeled dependencies as directed edges. Here we look at the one property that makes those dependencies possible to satisfy: the graph has no directed cycles.
What makes a graph a DAG
A directed cycle is a path that follows edge directions and returns to its starting vertex, such as a → b → c → a. A directed acyclic graph (DAG) is a directed graph with no directed cycles.
A self-loop a → a counts as a cycle of length one. In a dependency graph, a self-loop means a task depends on itself, so it can never start.
In the course graph on the left, you can always find a course to take next. In the graph on the right, every task waits on another task in the loop, so none of them can begin.
Direction matters
The most common mistake is to treat an undirected loop as a cycle. The course graph contains the loop intro, data_structures, algorithms, discrete_math if you ignore the arrows. Following the arrows, though, you can never get back to intro. It is still a DAG.
DAGs are also more general than trees:
| Structure | Multiple parents allowed | Undirected loops allowed | Directed cycles allowed |
|---|---|---|---|
| Rooted tree | No | No | No |
| DAG | Yes | Yes | No |
| General directed graph | Yes | Yes | Yes |
The "diamond" shape above, where algorithms has two parents, is typical of real dependencies and is impossible in a tree.
Sources and sinks
A source is a vertex with no incoming edges. In a dependency graph, a source is a task that needs nothing. A sink is a vertex with no outgoing edges, meaning no other task depends on it.
Every finite, non-empty DAG has at least one source and at least one sink. Here is why. Start at any vertex and keep stepping backward along incoming edges. If you never reached a source, you would eventually revisit a vertex, because there are only finitely many. Revisiting a vertex means you have found a directed cycle. Later chapters build an algorithm directly on this fact.
graph = {
"intro": ["data_structures", "discrete_math"],
"data_structures": ["algorithms"],
"discrete_math": ["algorithms"],
"algorithms": [],
}
def sources_and_sinks(graph):
has_incoming = {v for targets in graph.values() for v in targets}
nodes = set(graph) | has_incoming
sources = sorted(n for n in nodes if n not in has_incoming)
sinks = sorted(n for n in nodes if not graph.get(n))
return sources, sinks
print(sources_and_sinks(graph))
# (['intro'], ['algorithms'])Removing vertices keeps it a DAG
If you delete a vertex and its edges from a DAG, the result is still a DAG. Deleting things cannot create a cycle. Together with the source property, this means you can repeatedly remove a source and always find a new one until the graph is empty. Keep this idea in mind for the chapters ahead.
Exercise
Is the graph with edges a → b, a → c, b → d, c → b, d → c a DAG? If not, name a directed cycle.
Show answer
No. Following b → d → c → b returns to b, so b, d, c form a directed cycle. Vertex a is a source, which shows that having a source does not make a graph acyclic.
Exercise
Take the course graph above and add the edge algorithms → intro. List its sources and sinks, and say whether it is still a DAG.
Show answer
It has no sources and no sinks, because every vertex now has both incoming and outgoing edges. It is not a DAG, since intro → data_structures → algorithms → intro is a directed cycle.