1
WARNING, THESE SLIDES HAVE NOT YET BEEN UPDATED FOR FALL 2026
2
CS61B, Spring 2026 @ UC Berkeley
Josh Hug and Manuel Sabin
Directed Acyclic Graphs
Lecture 25 (Graphs 5)
Quick Note: Teaching in the Future is Amazingly Fun
3
Self-report on the Project 4B Process
4
Self-report on the Project 4B Process
5
Goals of Today
Today, to practice our problem solving skills, we’ll work through some very challenging A-level problems using the tools we’ve already learned about.
Will focus on graphs, but the ideas today are more general.
6
Graph Problems So Far
7
Problem | Problem Description | Solution | Efficiency |
paths | Find a path from s to every reachable vertex. | DepthFirstPaths.java | O(V+E) time Θ(V) space |
shortest paths | Find the shortest path from s to every reachable vertex. | BreadthFirstPaths.java | O(V+E) time Θ(V) space |
shortest weighted paths | Find the shortest path, considering weights, from s to every reachable vertex. | DijkstrasSP.java | O(E log V) time Θ(V) space |
shortest weighted path (extra topic, out of scope) | Find the shortest path, consider weights, from s to some target vertex (extra topic, out of scope) | A*: Same as Dijkstra’s but with h(v, goal) added to priority of each vertex. | Time depends on heuristic. Θ(V) space |
Graph Problems So Far
8
Problem | Problem Description | Solution | Efficiency |
minimum spanning tree | Find a minimum spanning tree. | LazyPrimMST.java | O(???) time Θ(???) space |
minimum spanning tree | Find a minimum spanning tree. | PrimMST.java | O(E log V) time Θ(V) space |
minimum spanning tree | Find a minimum spanning tree. | KruskalMST.java | O(E log E) time Θ(E) space |
Topological Sorting
Lecture 25, CS61B, Spring 2026
Topological Sorting
Shortest Paths on DAGs
Longest Paths
Reductions
9
Topological Sort
Suppose we have tasks A through H, where an arrow from v to w indicates that v must happen before w.
10
B
C
D
E
F
G
H
A
Solution (Spoiler Alert)
Perform a DFS traversal from every vertex with indegree 0, NOT clearing markings in between traversals.
11
B
C
D
E
F
G
H
A
Topological Sort (Demo 1/2)
12
A
B
C
D
E
F
G
H
Postorder: []
Call stack: A
*
A
B
C
D
E
F
G
H
Postorder: []
Call stack: A→B
*
A
B
C
D
E
F
G
H
Postorder: []
Call stack: A→B→E
*
A
B
C
D
E
F
G
H
Postorder: [H]
Call stack: A→B→E→H
*
A
B
C
D
E
F
G
H
Postorder: [H, E]
Call stack: A→B→E
*
A
B
C
D
E
F
G
H
Postorder: [H, E, B]
Call stack: A→B
*
A
B
C
D
E
F
G
H
*
Postorder: [H, E, B]
Call stack: A
A
B
C
D
E
F
G
H
*
Postorder: [H, E, B, D]
Call stack: A→D
Topological Sort (Demo 2/2)
13
A
B
C
D
E
F
G
H
Postorder: [H, E, B, D]
Call stack: A→D
*
A
B
C
D
E
F
G
H
Postorder: [H, E, B, D, A]
Call stack: A
*
A
B
C
D
E
F
G
H
Postorder: [H, E, B, D, A]
Call stack: C
*
*
A
B
C
D
E
F
G
H
Postorder: [H, E, B, D, A]
Call stack: C→F
*
A
B
C
D
E
F
G
H
Postorder: [H, E, B, D, A, G]
Call stack: C→F→G
*
A
B
C
D
E
F
G
H
Postorder: [H, E, B, D, A, G, F]
Call stack: C→F
*
A
B
C
D
E
F
G
H
Postorder: [H, E, B, D, A, G, F, C]
Call stack: C
Solution (Spoiler Alert)
Perform a DFS traversal from every vertex with indegree 0, NOT clearing markings in between traversals.
14
B
C
D
E
F
G
H
A
Topological Sort
The reason it’s called topological sort: Can think of this process as sorting our nodes so they appear in an order consistent with edges, e.g. [C, F, G, A, D, B, E, H]
15
A
B
C
D
E
F
G
H
A
B
C
D
E
F
G
H
Depth First Search
Be aware, that when people say “Depth First Search”, they sometimes mean with restarts, and they sometimes mean without.
For example, when we did DepthFirstPaths for reachability, we did not restart.
For Topological Sort, we restarted from every vertex with indegree 0.
16
Question (Extra Slide)
What is the runtime to find all vertices of indegree 0?
17
Question (Extra Slide)
What is the runtime to find all vertices of indegree 0?
Another better topological sort algorithm:
18
Test Your Understanding: yellkey.com/member
Give a topological ordering for the graph below (a.k.a. topological sort).
19
B
D
C
E
F
A
s
4
1
2
2
6
1
1
3
Test Your Understanding
Give a topological ordering for the graph below (a.k.a. topological sort)
20
B
D
C
E
F
A
s
4
1
2
2
6
1
1
3
B
D
C
E
F
A
s
4
2
6
1
1
1
3
2
Recall that we can think of topological sort as an ordering of “tasks”.
Directed Acyclic Graphs
A topological sort only exists if the graph is a directed acyclic graph (DAG).
DAGs appear in many real world applications, and there are many graph algorithms that only work on DAGs.
21
A
B
C
D
E
F
G
H
Graph Problems
22
Problem | Problem Description | Solution | Efficiency |
topological sort | Find an ordering of vertices that respects edges of our DAG. | Topological.java | O(V+E) time Θ(V) space |
Shortest Paths on DAGs
Lecture 25, CS61B, Spring 2026
Topological Sorting
Shortest Paths on DAGs
Longest Paths
Reductions
23
Shortest Paths Warmup
What is the shortest paths tree for the graph below, using s as the source? In what order will Dijkstra’s algorithm visit the vertices?
24
Graph from Algorithms, by Vazirani/Papadimitriou
B
D
C
E
F
A
s
4
1
2
2
6
1
1
3
Shortest Paths Warmup
What is the shortest paths tree for the graph below, using s as the source? In what order will Dijkstra’s algorithm visit the vertices?
25
B
D
C
E
F
A
s
4
1
2
2
1
1
3
1
2
5
7
6
6
Shortest Paths Warmup
If we allow negative edges, Dijkstra’s algorithm can fail.
26
B
D
C
E
F
A
s
25
1
1
1
6
1
-20
1
1
1
2
7
3
Shortest Paths Warmup
If we allow negative edges, Dijkstra’s algorithm can fail.
27
B
D
C
E
F
A
s
25
1
1
1
6
1
-20
1
1
1
2
7
3
-13
Shortest Paths Warmup
If we allow negative edges, Dijkstra’s algorithm can fail.
28
B
D
C
E
F
A
s
25
1
1
1
6
1
-20
1
1
1
7
3
-13
distTo is wrong!
Challenge
Try to come up with an algorithm for shortest paths on a DAG that works even if there are negative edges.
29
B
D
C
E
F
A
s
25
1
1
1
6
1
-20
1
∞
∞
∞
∞
∞
Challenge
Try to come up with an algorithm for shortest paths on a DAG that works even if there are negative edges.
One simple idea: Visit vertices in topological order.
30
B
D
C
E
F
A
s
25
1
1
1
6
1
-20
1
∞
∞
∞
∞
∞
The DAG SPT Algorithm: Relax in Topological Order
First: We have to find a topological order, e.g. ADBCEF. Runtime is O(V + E).
31
B
D
C
E
F
A
s
25
1
6
1
-20
1
1
1
B
D
C
E
F
A
s
25
1
1
1
6
1
-20
1
∞
∞
∞
∞
∞
The DAG SPT Algorithm: Relax in Topological Order
Second: We have to visit all the vertices in topological order, relaxing all edges as we go. Let’s see a demo.
32
B
D
C
E
F
A
s
1
6
1
-20
1
1
1
25
DAG SPT Algorithm
Visit vertices in topological order.
33
distTo edgeTo
A 0 -
B ∞ -
C ∞ -
D ∞ -
E ∞ -
F ∞ -
B
D
C
E
F
A
s
1
6
1
-20
1
1
1
Fringe: [A, D, B, C, E, F]
0
∞
∞
∞
∞
∞
25
DAG SPT Algorithm
Visit vertices in topological order.
34
distTo edgeTo
A 0 -
B ∞ -
C ∞ -
D ∞ -
E ∞ -
F ∞ -
B
D
C
D
F
A
s
1
6
1
-20
1
1
1
0
∞
∞
∞
∞
∞
Fringe: [A, D, B, C, E, F]
25
DAG SPT Algorithm
Visit vertices in topological order.
35
distTo edgeTo
A 0 -
B 1 A
C ∞ -
D 1 A
E ∞ -
F ∞ -
B
D
C
E
F
A
s
1
6
1
-20
1
1
1
0
∞
∞
∞
∞
∞
1
1
Fringe: [A, D, B, C, E, F]
25
DAG SPT Algorithm
Visit vertices in topological order.
36
distTo edgeTo
A 0 -
B 1 A
C ∞ -
D 1 A
E ∞ -
F ∞ -
B
D
C
E
F
A
s
1
6
1
-20
1
1
1
0
∞
∞
∞
1
1
Fringe: [A, D, B, C, E, F]
25
DAG SPT Algorithm
Visit vertices in topological order.
37
distTo edgeTo
A 0 -
B 1 A
C ∞ -
D 1 A
E 2 D
F ∞ -
B
D
C
E
F
A
s
1
6
1
-20
1
1
1
0
∞
∞
∞
1
1
2
Fringe: [A, D, B, C, E, F]
25
DAG SPT Algorithm
Visit vertices in topological order.
38
distTo edgeTo
A 0 -
B 1 A
C ∞ -
D 1 A
E 2 D
F ∞ -
B
D
C
E
F
A
s
1
6
1
-20
1
1
1
0
∞
∞
1
1
2
Fringe: [A, D, B, C, E, F]
25
DAG SPT Algorithm
Visit vertices in topological order.
39
distTo edgeTo
A 0 -
B 1 A
C 7 B
D 1 A
E 2 D
F ∞ -
B
D
C
E
F
A
s
1
6
1
-20
1
1
1
0
∞
∞
1
1
7
2
Fringe: [A, D, B, C, E, F]
25
DAG SPT Algorithm
Visit vertices in topological order.
40
distTo edgeTo
A 0 -
B 1 A
C 7 B
D 1 A
E 2 D
F ∞ -
B
D
C
E
F
A
s
1
6
1
-20
1
1
1
0
∞
1
1
7
2
Fringe: [A, D, B, C, E, F]
25
DAG SPT Algorithm
Visit vertices in topological order.
41
distTo edgeTo
A 0 -
B 1 A
C 7 B
D 1 A
E -13 C
F ∞ -
B
D
C
E
F
A
s
1
6
1
-20
1
1
1
0
∞
1
1
7
2
-13
8
Fringe: [A, D, B, C, E, F]
25
DAG SPT Algorithm
Visit vertices in topological order.
42
distTo edgeTo
A 0 -
B 1 A
C 7 B
D 1 A
E -13 C
F ∞ -
B
D
C
E
F
A
s
1
6
1
-20
1
1
1
0
1
1
7
-13
8
Fringe: [A, D, B, C, E, F]
25
DAG SPT Algorithm
Visit vertices in topological order.
43
distTo edgeTo
A 0 -
B 1 A
C 7 B
D 1 A
E -13 C
F -12 E
B
D
C
E
F
A
s
1
6
1
-20
1
1
1
0
∞
1
1
7
-13
-12
Fringe: [A, D, B, C, E, F]
25
DAG SPT Algorithm
Visit vertices in topological order.
44
distTo edgeTo
A 0 -
B 1 A
C 7 B
D 1 A
E -13 C
F -12 E
B
D
C
E
F
A
s
1
6
1
-20
1
1
1
0
1
1
7
-13
-12
Fringe: [A, D, B, C, E, F]
25
The DAG SPT Algorithm: Relax in Topological Order
Second: We have to visit all the vertices in topological order, relaxing all edges as we go.
Occasional question asked by students: why isn’t it O(V*E)? We’re relaxing all edges from each vertex.
45
B
D
C
E
F
A
s
1
1
6
1
-20
1
1
1
Graph Problems
Note: The DAG shortest paths solution uses the topological sort solution as a subroutine.
46
Problem | Problem Description | Solution | Efficiency |
topological sort | Find an ordering of vertices that respects edges of our DAG. | Code: Topological.java | O(V+E) time Θ(V) space |
DAG shortest paths | Find a shortest paths tree on a DAG. | Code: AcyclicSP.java | O(V+E) time Θ(V) space |
What If It’s Not a DAG?
The DAG algorithm only works on graphs that contain no cycles.
47
Longest Paths
Lecture 25, CS61B, Spring 2026
Topological Sorting
Shortest Paths on DAGs
Longest Paths
Reductions
48
The Longest Paths Problem
Consider the problem of finding the longest path tree (LPT) from s to every other vertex. The path must be simple (no cycles!).
49
B
C
D
E
F
G
A
s
5
2
1
15
3
2
11
5
1
1
4
1
The Longest Paths Problem
Consider the problem of finding the longest path tree (LPT) from s to every other vertex. The path must be simple (no cycles!).
Some surprising facts:
50
Two potentially interesting exercises after lecture:
B
C
D
E
F
G
A
s
5
2
1
15
3
2
11
5
1
1
4
1
2
13
7
15
22
20
The Longest Paths Problem on DAGs
Difficult challenge for you.
51
B
D
C
E
F
A
s
4
1
2
2
6
1
1
3
6
2
12
14
13
0
The Longest Paths Problem on DAGs
DAG LPT solution for graph G:
52
B
D
C
E
F
A
s
-4
-1
-2
-2
-6
-1
-1
-3
-6
-2
-12
-14
-13
0
The Longest Paths Problem on DAGs
DAG LPT solution for graph G:
53
B
D
C
E
F
A
s
6
2
12
14
13
0
A Note on “Mathematical Maturity”
If you have a very high degree of so-called “mathematical maturity”, this algorithm should seem plainly correct.
There’s no real need to prove anything or show demos.
Part of what you’re learning in your intense technical education here at Berkeley is mathematical maturity. Hasn’t been a major focus in 61B, but will be in other courses like EECS 16A, EECS 16B, CS 126, CS 127, CS 70, CS 170, Math 110, ...
54
Highly Recommended Exercise For Later
Play around with the longest paths problem and convince yourself that it is actually very hard.
55
Graph Problems
56
Problem | Problem Description | Solution | Efficiency |
topological sort | Find an ordering of vertices that respects edges of our DAG. | Code: Topological.java | O(V+E) time Θ(V) space |
DAG shortest paths | Find a shortest paths tree on a DAG. | Code: AcyclicSP.java | O(V+E) time Θ(V) space |
longest paths | Find a longest paths tree on a graph. | No known efficient solution. | O(???) time O(???) space |
DAG longest paths | Find a longest paths tree on a DAG. | Flip signs, run DAG SPT, flip signs again. | O(V+E) time Θ(V) space |
Reductions: Definition
Lecture 25, CS61B, Spring 2026
Topological Sorting
Shortest Paths on DAGs
Longest Paths
Reductions
57
DAG Longest Paths
The problem solving we just used probably felt a little different than usual:
58
G
DAG-LPT
Preprocess
DAG-SPT
G’
SPT of G’
Postprocess
LPT of G
Reduction
This process is known as reduction.
59
G
DAG-LPT
Preprocess
DAG-SPT
G’
SPT of G’
Postprocess
LPT of G
Reduction Analogy
This process is known as reduction.
As a real-world analog, suppose we want to climb a hill. There are many ways to do this:
60
Reduction Definition (Informal)
This process is known as reduction.
Algorithms by Dasgupta, Papadimitriou, and Vazirani defines a reduction informally as follows:
Can also define the idea formally, but way beyond the scope of our class.
Reduction is more than sign flipping. In the bonus slides, we give an example of a richer reduction (3SAT to Independent Set)
61
Reductions
Earlier I claimed that the longest paths problem is potentially the most important unsolved question in mathematics.
We might revisit this idea (NP-Completeness) in the last week of class.
62
Reductions and Decomposition
Reductions are just a more formal version of what we’ve been doing throughout the course.
At its heart, computer science is about taking a complex task and breaking it into smaller parts.
63
Generational Changes in the Human Mind
Interesting question: Are we regressing? Are LLMs partially to blame?
64
Reminder
Project 4C due Tuesday after Spring Break!.
65
Reduction to 3SAT (Optional CS170 Preview)
Lecture 25, CS61B, Spring 2026
Topological Sorting
Shortest Paths on DAGs
Longest Paths
Reductions
66
The Independent Set Problem
An independent set is a set of vertices in which no two vertices are adjacent.
The Independent-set Problem:
Example for the graph on the right and k = 9
67
The 3SAT Problem
3SAT: Given a boolean formula, does there exist a truth value for boolean variables that obeys a set of 3-variable disjunctive constraints?
68
Example: (x1 || x2 || !x3) && (x1 || !x1 || x1) && (x2 || x3 || x4)
3 variable disjunctive constraint�
3SAT Reduces to Independent Set
Proposition: 3SAT reduces to Independent-set.
Proof. Given an instance ϕ of 3-SAT, create an instance G of Independent-set:
69
Φ = (x1 or x2 or x3) and (! x1 or ! x2 or x4) and (! x1 or x3 or ! x4) and (x1 or x3 or x4)
x2
x1
x3
! x2
x4
! x1
! x4
x3
! x1
x3
x1
x4
k = number of variables = 4
3SAT Reduces to Independent Set
Find an independent set of size k = 4. Use this set to generate a solution to the 3SAT problem.
70
Φ = (x1 or x2 or x3) and (! x1 or ! x2 or x4) and (! x1 or x3 or ! x4) and (x1 or x3 or x4)
x2
x1
x3
! x2
x4
! x1
! x4
x3
! x1
x3
x1
x4
k = number of variables = 4
3SAT Reduces to Independent Set
Find an independent set of size k = 4. Use this set to generate a solution to the 3SAT problem.
71
Φ = (x1 or x2 or x3) and (! x1 or ! x2 or x4) and (! x1 or x3 or ! x4) and (x1 or x3 or x4)
x2
x1
x3
! x2
x4
! x1
! x4
x3
! x1
x3
x1
x4
k = number of variables = 4
Reduction
Since IND-SET can be used to solve 3SAT, we say that “3SAT reduces to IND-SET”.
72
3SAT
Preprocess
IND-SET
G
IND-SET for G
Postprocess
Assignment so
that Φ gives true.
Φ
x1: true
x2: false
x3: false
x4: true