Graphs and Traversals
1
Lecture 22
CS61B, Spring 2023 @ UC Berkeley
Josh Hug and Justin Yokota
Tree Definition
Lecture 22, CS61B, Spring 2023
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, Spring 2023
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
Depth First Traversals
Preorder: “Visit” a node, then traverse its children:
A
C
B
D
E
F
G
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
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/simple
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, Spring 2023
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, Spring 2023
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 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, Spring 2023
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 Trversals: s-t Connectivity
Lecture 22, CS61B, Spring 2023
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, Spring 2023
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
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 (Will Cover Monday)
DFS is a very powerful technique that can be used for many types of graph problems.
Another example:
Tree vs. Graph Traversals
Lecture 22, CS61B, Spring 2023
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, Spring 2023
Trees
Graphs
Graph Traversals
Challenge: Invent Breadth First Search (Will Cover Monday)
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.
�