Graphs and Traversals
1
Lecture 22 (Graphs 1)
CS61B, Fall 2024 @ UC Berkeley
Slides credit: Josh Hug
Tree Definition
Lecture 22, CS61B, Fall 2024
Trees
Graphs
Graph Traversals
Challenge: Invent Breadth First�Search
Tree Definition (Reminder)
A tree consists of:
Green structures below are trees. Pink ones are not.
Rooted Trees Definition (Reminder)
A rooted tree is a tree where we’ve chosen one node as the “root”.
A
B
C
A
B
C
A
C
C
B
For each of these:
Trees
We’ve seen trees as nodes in a specific data structure implementation: Search Trees, Tries, Heaps, Disjoint Sets, etc.
Trees
Trees are a more general concept.
*: Not all family lineages are trees!
Tree Traversals
Lecture 22, CS61B, Fall 2024
Trees
Graphs
Graph Traversals
Challenge: Invent Breadth First�Search
Example: File System Tree
Sometimes you want to iterate over a tree. For example, suppose you want to find the total size of all files in a folder called 61b.
hw1
synthesizer
spec
GuitarHeroLite.java
hw1.md
karplus-strong.png
...
61b
proj0
audio
data
...
...
...
...
planets.txt
23433 bytes
16180 bytes
1251 bytes
851 bytes
Tree Traversal Orderings
Level Order
A
C
B
D
E
F
G
Tree Traversal Orderings
Level Order
Depth First Traversals
A
C
B
D
E
F
G
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children:
Call stack:
preOrder(D)
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children: D
Call stack:
preOrder(D)
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children: D
Call stack:
preOrder(D)
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children: D
Call stack:
preOrder(D)
preOrder(B)
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children: DB
Call stack:
preOrder(D)
preOrder(B)
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children: DB
Call stack:
preOrder(D)
preOrder(B)
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children: DB
Call stack:
preOrder(D)
preOrder(B)
preOrder(A)
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children: DBA
Call stack:
preOrder(D)
preOrder(B)
preOrder(A)
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children: DBA
Call stack:
preOrder(D)
preOrder(B)
preOrder(A)
Skipping over the steps of preOrder(null) for brevity.
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children: DBA
Call stack:
preOrder(D)
preOrder(B)
preOrder(A)
Skipping over the steps of preOrder(null) for brevity.
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children: DBA
Call stack:
preOrder(D)
preOrder(B)
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children: DBAC
Call stack:
preOrder(D)
preOrder(B)
Skipping over the steps of preOrder(C) for brevity.
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children: DBAC
Call stack:
preOrder(D)
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children: DBAC
Call stack:
preOrder(D)
preOrder(F)
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children: DBACF
Call stack:
preOrder(D)
preOrder(F)
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children: DBACFE
Call stack:
preOrder(D)
preOrder(F)
Skipping over the steps of preOrder(E) for brevity.
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Demo: Preorder Depth-First Tree Traversal
Preorder: “Visit” a node, then traverse its children: DBACFEG
Call stack:
preOrder(D)
preOrder(F)
Skipping over the steps of preOrder(G) for brevity.
A
C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
Depth First Traversals
Preorder traversal: “Visit” a node, then traverse its children: DBACFEG
Inorder traversal: Traverse left child, visit, then traverse right child:
A
C
B
D
E
F
G
inOrder(BSTNode x) {
if (x == null) return;� inOrder(x.left)� print(x.key)
inOrder(x.right)�}
preOrder(BSTNode x) {
if (x == null) return;� print(x.key)� preOrder(x.left)� preOrder(x.right)�}
A
B
C
D
E
F
G
Depth First Traversals http://yellkey.com/yes
Preorder traversal: “Visit” a node, then traverse its children: DBACFEG
Inorder traversal: Traverse left child, visit, traverse right child: ABCDEFG
Postorder traversal: Traverse left, traverse right, then visit: ???????
A
C
B
D
E
F
G
postOrder(BSTNode x) {
if (x == null) return;� postOrder(x.left)
postOrder(x.right)� print(x.key)
}
Depth First Traversals
Preorder traversal: “Visit” a node, then traverse its children: DBACFEG
Inorder traversal: Traverse left child, visit, traverse right child: ABCDEFG
Postorder traversal: Traverse left, traverse right, then visit: ACBEGFD
A
C
B
D
E
F
G
postOrder(BSTNode x) {
if (x == null) return;� postOrder(x.left)
postOrder(x.right)� print(x.key)
}
A Useful Visual Trick (for Humans, Not Algorithms)
Example: Post-Order Traversal
9
4
2
1
3
6
8
5
7
Usefulness of Tree Traversals
Lecture 22, CS61B, Fall 2024
Trees
Graphs
Graph Traversals
Challenge: Invent Breadth First�Search
What Good Are All These Traversals?
Example: Preorder Traversal for printing directory listing:
python
sc2APM
directOverlay
notes
directO.sln
directO.suo
directIO
printAPM.py
DXHookD3D11.cs
Injector.cs
What Good Are All These Traversals?
Example: Postorder Traversal for gathering file sizes.
python
sc2APM
directOverlay
notes
directO.sln
directO.suo
directIO
printAPM.py
DXHookD3D11.cs
Injector.cs
18381
8798
38912
881
324
874
postOrder(BSTNode x) {
if (x == null) return 0;� int total = 0;
for (BSTNode c : x.children())� total += postOrder(c)� total += x.fileSize();
return total;�}
What Good Are All These Traversals?
Example: Postorder Traversal for gathering file sizes.
python
sc2APM
directOverlay
notes
directO.sln
directO.suo
directIO
printAPM.py
DXHookD3D11.cs
Injector.cs
18381
8798
38912
881
324
874
postOrder(BSTNode x) {
if (x == null) return 0;� int total = 0;
for (BSTNode c : x.children())� total += postOrder(c)� total += x.fileSize();
return total;�}
27179
66972
874
68170
Graph Definition
Lecture 22, CS61B, Fall 2024
Trees
Graphs
Graph Traversals
Challenge: Invent Breadth First�Search
Trees and Hierarchical Relationships
Trees are fantastic for representing strict hierarchical relationships.
This is not a tree: Contains cycles!
A
B
Tree Definition (Revisited)
A tree consists of:
Green structures on slide are trees. Pink ones are not.
Graph Definition
A graph consists of:
Green structures below are graphs.
Graph Example: BART
Is the BART graph a tree?��
Graph Definition
A simple graph is a graph with:
Green graph below is simple, pink graphs are not.
Graph Definition
A simple graph is a graph with:
In 61B, unless otherwise explicitly stated, all graphs will be simple.
Graph Types
a
b
d
c
a
b
d
c
e
a
b
d
c
a
b
d
c
Acyclic:
Cyclic:
Directed
Undirected
With Edge Labels
b
d
c
e
a
1
2
3
1
Graph Terminology
Figure from Algorithms 4th Edition
Graph Example: The Paris Metro
This schematic map of the Paris Metro is a graph:
Directed Graph Example
Edge captures ‘is-a-type-of’ relationship. Example: descent is-a-type-of movement.
Not a tree!
Some Famous Graph Problems
Lecture 22, CS61B, Fall 2024
Trees
Graphs
Graph Traversals
Challenge: Invent Breadth First�Search
Graph Queries
There are lots of interesting questions we can ask about a graph:
S
T
Graph Queries More Theoretically
Some well known graph problems and their common names:
Often can’t tell how difficult a graph problem is without very deep consideration.
Graph Problem Difficulty
Some well known graph problems:
Difficulty can be deceiving.
Graph problems are among the most mathematically rich areas of CS theory.
Motivation for Graph Traversals: s-t Connectivity
Lecture 22, CS61B, Fall 2024
Trees
Graphs
Graph Traversals
Challenge: Invent Breadth First�Search
s-t Connectivity
Let’s solve a classic graph problem called the s-t connectivity problem.
Requires us to traverse the graph somehow.
1
2
3
4
5
6
7
8
0
s
t
s-t Connectivity
Let’s solve a classic graph problem called the s-t connectivity problem.
Requires us to traverse the graph somehow.
1
2
3
4
5
6
7
8
0
s
t
s-t Connectivity
One possible recursive algorithm for connected(s, t).
What is wrong with the algorithm above?
1
2
3
4
5
6
7
8
0
s
t
s-t Connectivity
One possible recursive algorithm for connected(s, t).
What is wrong with the algorithm above?
1
2
3
4
5
6
7
8
0
s
t
s-t Connectivity
One possible recursive algorithm for connected(s, t).
What is wrong with it? Can get caught in an infinite loop. Example:
1
2
3
4
5
6
7
8
0
s
t
s-t Connectivity
One possible recursive algorithm for connected(s, t).
What is wrong with it? Can get caught in an infinite loop.
1
2
3
4
5
6
7
8
0
s
t
Depth First Search
Lecture 22, CS61B, Fall 2024
Trees
Graphs
Graph Traversals
Challenge: Invent Breadth First�Search
s-t Connectivity
One possible recursive algorithm for connected(s, t).
Basic idea is same as before, but visit each vertex at most once.
1
2
3
4
5
6
7
8
0
s
t
Demo: s-t Connectivity
connected(s, t):
1
2
3
4
5
6
7
8
0
s
t
Demo: s-t Connectivity
connected(s, t):
1
2
3
4
5
6
7
8
0
s
t
mark(0).
Is 0 == 7? No.
isMarked(1)? No.
*
Call stack: 0
Demo: s-t Connectivity
connected(s, t):
1
2
3
4
5
6
7
8
0
s
t
mark(1).
Is 1 == 7? No.
isMarked(0)? Yes.
isMarked(2)?
*
Call stack: 0 → 1
Demo: s-t Connectivity
connected(s, t):
1
2
3
4
5
6
7
8
0
s
t
mark(2).
Is 2 == 7? No.
isMarked(1)? Yes.
isMarked(5)?
*
Call stack: 0 → 1 → 2
Demo: s-t Connectivity
connected(s, t):
1
2
3
4
5
6
7
8
0
s
t
mark(5).
Is 5 == 7? No.
isMarked(2)? Yes.
isMarked(4)?
*
Call stack: 0 → 1 → 2 → 5
Demo: s-t Connectivity
connected(s, t):
1
2
3
4
5
6
7
8
0
s
t
mark(4).
Is 4 == 7? No.
isMarked(1)? Yes.
isMarked(3)? No.
*
Call stack: 0 → 1 → 2 → 5 → 4
Demo: s-t Connectivity
connected(s, t):
1
2
3
4
5
6
7
8
0
s
t
mark(3).
Is 3 == 7? No.
isMarked(4)? Yes.
No more neighbors! Return false.
*
Call stack: 0 → 1 → 2 → 5 → 4 → 3
Demo: s-t Connectivity
connected(s, t):
1
2
3
4
5
6
7
8
0
s
t
mark(4).
Is 4 == 7? No.
isMarked(1)? Yes.
isMarked(3)? No.
isMarked(5)? Yes.
No more neighbors, so return false.
*
Call stack: 0 → 1 → 2 → 5 → 4
Demo: s-t Connectivity
connected(s, t):
1
2
3
4
5
6
7
8
0
s
t
mark(5).
Is 5 == 7? No.
isMarked(2)? Yes.
isMarked(4)?
isMarked(6)?
*
Call stack: 0 → 1 → 2 → 5
Demo: s-t Connectivity
connected(s, t):
1
2
3
4
5
6
7
8
0
s
t
mark(6).
Is 6 == 7? No.
isMarked(5)? Yes.
isMarked(7)? No.
*
Call stack: 0 → 1 → 2 → 5 → 6
Demo: s-t Connectivity
connected(s, t):
1
2
3
4
5
6
7
8
0
s
t
mark(7).
Is 7 == 7? Yes. Return true!
*
Call stack: 0 → 1 → 2 → 5 → 6 → 7
Demo: s-t Connectivity
connected(s, t):
mark(6).
Is 6 == 7? No.
isMarked(5)? Yes.
isMarked(7)? No.
1
2
3
4
5
6
7
8
0
s
t
*
Call stack: 0 → 1 → 2 → 5 → 6
Demo: s-t Connectivity
connected(s, t):
1
2
3
4
5
6
7
8
0
s
t
mark(5).
Is 5 == 7? No.
isMarked(2)? Yes.
isMarked(4)?
isMarked(5)? Yes.
isMarked(6)?
*
Call stack: 0 → 1 → 2 → 5
Demo: s-t Connectivity
connected(s, t):
1
2
3
4
5
6
7
8
0
s
t
mark(2).
Is 2 == 7? No.
isMarked(1)? Yes.
isMarked(5)?
*
Call stack: 0 → 1 → 2
Demo: s-t Connectivity
connected(s, t):
1
2
3
4
5
6
7
8
0
s
t
mark(1).
Is 1 == 7? No.
isMarked(0)? Yes.
isMarked(2)?
*
Call stack: 0 → 1
Demo: s-t Connectivity
connected(s, t):
1
2
3
4
5
6
7
8
0
s
t
mark(0).
Is 0 == 7? No.
isMarked(1)? No.
*
Call stack: 0
Demo: s-t Connectivity
connected(s, t):
1
2
3
4
5
6
7
8
0
s
t
*
mark(0).
Is 0 == 7? No.
isMarked(1)? No.
Depth First Traversal
This idea of exploring a neighbor’s entire subgraph before moving on to the next neighbor is known as Depth First Traversal or Depth First Search.
1
2
3
4
5
6
7
8
0
s
t
Depth First Traversal
This idea of exploring a neighbor’s entire subgraph before moving on to the next neighbor is known as Depth First Traversal.
1
2
3
4
5
6
7
8
0
s
t
Entirely possible for 1’s subgraph to include 3!
From: https://xkcd.com/761/
The Power of Depth First Search
DFS is a very powerful technique that can be used for many types of graph problems.
Another example:
Demo: DepthFirstPaths
dfs(v):
Start by calling dfs(0).
1
2
3
4
5
6
7
8
0
s
# marked edgeTo
0 F -
1 F -
2 F -
3 F -
4 F -
5 F -
6 F -
7 F -
8 F -
Order of dfs calls: 0
Order of dfs returns:
Call stack:
dfs(0)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(0).
isMarked(1)? No.
*
# marked edgeTo
0 T -
1 F 0
2 F -
3 F -
4 F -
5 F -
6 F -
7 F -
8 F -
Order of dfs returns:
Order of dfs calls: 01
dfs(0):
Call stack:
dfs(0)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(1).
isMarked(0)? Yes.
isMarked(2)? No.
*
# marked edgeTo
0 T -
1 T 0
2 F 1
3 F -
4 F -
5 F -
6 F -
7 F -
8 F -
Order of dfs returns:
Order of dfs calls: 012
dfs(1):
Call stack:
dfs(0) → dfs(1)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(2).
isMarked(1)? Yes.
isMarked(5)? No.
*
# marked edgeTo
0 T -
1 T 0
2 T 1
3 F -
4 F -
5 F 2
6 F -
7 F -
8 F -
Order of dfs returns:
Order of dfs calls: 0125
dfs(2):
Call stack:
dfs(0) → dfs(1) → dfs(2)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(5).
isMarked(2)? Yes.
isMarked(4)? No.
*
# marked edgeTo
0 T -
1 T 0
2 T 1
3 F -
4 F 5
5 T 2
6 F -
7 F -
8 F -
Order of dfs returns:
Order of dfs calls: 01254
dfs(5):
Call stack:
dfs(0) → dfs(1) → dfs(2) → dfs(5)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(4).
isMarked(1)? Yes.
isMarked(3)? No.
*
# marked edgeTo
0 T -
1 T 0
2 T 1
3 F 4
4 T 5
5 T 2
6 F -
7 F -
8 F -
Order of dfs returns:
Order of dfs calls: 012543
dfs(4):
Call stack:
dfs(0) → dfs(1) → dfs(2) → dfs(5) → dfs(4)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(3).
isMarked(4)? Yes.
No more children, so return.
*
# marked edgeTo
0 T -
1 T 0
2 T 1
3 T 4
4 T 5
5 T 2
6 F -
7 F -
8 F -
Order of dfs returns: 3
Order of dfs calls: 012543
dfs(3):
Call stack:
dfs(0) → dfs(1) → dfs(2) → dfs(5) → dfs(4) → dfs(3)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(4).
isMarked(3)? No.
No more children, so return.
*
# marked edgeTo
0 T -
1 T 0
2 T 1
3 T 4
4 T 5
5 T 2
6 F -
7 F -
8 F -
Order of dfs returns: 34
Order of dfs calls: 012543
dfs(4):
Call stack:
dfs(0) → dfs(1) → dfs(2) → dfs(5) → dfs(4)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(5).
isMarked(2)? Yes.
isMarked(4)? No.
isMarked(6)? No.
*
# marked edgeTo
0 T -
1 T 0
2 T 1
3 T 4
4 T 5
5 T 2
6 F 5
7 F -
8 F -
Order of dfs returns: 34
Order of dfs calls: 0125436
dfs(5):
Call stack:
dfs(0) → dfs(1) → dfs(2) → dfs(5)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(6).
isMarked(5)? Yes.
isMarked(7)? No.
*
# marked edgeTo
0 T -
1 T 0
2 T 1
3 T 4
4 T 5
5 T 2
6 T 5
7 F 6
8 F -
Order of dfs returns: 34
Order of dfs calls: 01254367
dfs(6):
Call stack:
dfs(0) → dfs(1) → dfs(2) → dfs(5) → dfs(6)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(7).
isMarked(6)? Yes.
No more children, so return.
*
# marked edgeTo
0 T -
1 T 0
2 T 1
3 T 4
4 T 5
5 T 2
6 T 5
7 T 6
8 F -
Order of dfs returns: 347
Order of dfs calls: 01254367
dfs(7):
Call stack:
dfs(0) → dfs(1) → dfs(2) → dfs(5) → dfs(6) → dfs(7)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(6).
isMarked(5)? Yes.
isMarked(7)? No.
No more children, so return.
*
# marked edgeTo
0 T -
1 T 0
2 T 1
3 T 4
4 T 5
5 T 2
6 T 5
7 T 6
8 F -
Order of dfs returns: 3476
Order of dfs calls: 01254367
dfs(6):
Call stack:
dfs(0) → dfs(1) → dfs(2) → dfs(5) → dfs(6)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(5).
isMarked(2)? Yes.
isMarked(4)? No.
isMarked(6)? No.
isMarked(8)? No.
# marked edgeTo
0 T -
1 T 0
2 T 1
3 T 4
4 T 5
5 T 2
6 T 5
7 T 6
8 F 5
*
Order of dfs calls: 012543678
Order of dfs returns: 3476
dfs(5):
Call stack:
dfs(0) → dfs(1) → dfs(2) → dfs(5)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(8)
isMarked(5)? Yes.
No more children, so return.
# marked edgeTo
0 T -
1 T 0
2 T 1
3 T 4
4 T 5
5 T 2
6 T 5
7 T 6
8 T 5
*
Order of dfs calls: 012543678
Order of dfs returns: 34768
dfs(8):
Call stack:
dfs(0) → dfs(1) → dfs(2) → dfs(5) → dfs(8)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(5).
isMarked(2)? Yes.
isMarked(4)? No.
isMarked(6)? No.
isMarked(8)? No.
No more children, so return.
# marked edgeTo
0 T -
1 T 0
2 T 1
3 T 4
4 T 5
5 T 2
6 T 5
7 T 6
8 T 5
*
Order of dfs calls: 012543678
Order of dfs returns: 347685
dfs(5):
Call stack:
dfs(0) → dfs(1) → dfs(2) → dfs(5)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(2).
isMarked(1)? Yes.
isMarked(5)? No.
No more children, so return.
*
# marked edgeTo
0 T -
1 T 0
2 T 1
3 T 4
4 T 5
5 T 2
6 T 5
7 T 6
8 T 5
Order of dfs calls: 012543678
Order of dfs returns: 3476852
dfs(2):
Call stack:
dfs(0) → dfs(1) → dfs(2)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(1).
isMarked(0)? Yes.
isMarked(2)? No.
isMarked(4)? Yes.
No more children, so return.
*
# marked edgeTo
0 T -
1 T 0
2 T 1
3 T 4
4 T 5
5 T 2
6 T 5
7 T 6
8 T 5
Order of dfs calls: 012543678
Order of dfs returns: 34768521
dfs(1):
Call stack:
dfs(0) → dfs(1)
Demo: DepthFirstPaths
dfs(v):
1
2
3
4
5
6
7
8
0
s
mark(0).
isMarked(1)? No.
No more children, so return.
*
# marked edgeTo
0 T -
1 T 0
2 T 1
3 T 4
4 T 5
5 T 2
6 T 5
7 T 6
8 T 5
Order of dfs calls: 012543678
Order of dfs returns: 347685210
dfs(0):
Call stack:
dfs(0)
Tree vs. Graph Traversals
Lecture 22, CS61B, Fall 2024
Trees
Graphs
Graph Traversals
Challenge: Invent Breadth First�Search
Tree Traversals
There are many tree traversals:
A
C
B
D
E
F
G
Graph Traversals
There are many tree traversals:
A
C
B
D
E
F
G
1
2
3
4
5
6
7
8
0
s
What we just did in DepthFirstPaths is called “DFS Preorder.”
Graph Traversals
There are many tree traversals:
A
C
B
D
E
F
G
1
2
3
4
5
6
7
8
0
s
Could also do actions in DFS Postorder.
Graph Traversals
Just as there are many tree traversals:
A
C
B
D
E
F
G
1
2
3
4
5
6
7
8
0
s
So too are there many graph traversals, given some source:
Graph Traversals
Just as there are many tree traversals:
A
C
B
D
E
F
G
1
2
3
4
5
6
7
8
0
s
So too are there many graph traversals, given some source:
Challenge: Invent Breadth First Search
Lecture 22, CS61B, Fall 2024
Trees
Graphs
Graph Traversals
Challenge: Invent Breadth First Search
Shortest Paths Challenge Before Next Lecture
Goal: Given the graph above, find the length of the shortest path from s to all other vertices.
Will discuss a solution in the next lecture.
0
1
2
3
4
5
6
7
s
Summary
Graphs are a more general idea than a tree.
Graph problems vary widely in difficulty.
�