``````Tree and Graph Traversals
Formulating problems as graphs and solving them with depth-first search and breadth-first search.
Kevin Lin, with thanks to many others.
1

Tree Traversals
Give the following tree traversals.
Level-order traversal

Depth-first traversal
Preorder
Inorder
Postorder
2
A
C
B
D
E
F
G
Q
Give the following tree traversals.

Q1: Level-order traversal.

Q2: Preorder depth-first traversal.

Q3: Inorder depth-first traversal.

Q4: Postorder depth-first traversal.

Tree Traversals
Give the following tree traversals.
Level-order traversal

Depth-first traversal
Preorder
Inorder
Postorder
3
A
C
B
D
E
F
G
A

Depth-First Search
Give the order of dfs calls starting from 0. Visit neighbors in increasing order.
4
Q
0
1
4
5
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
2
3
6
Q1: Give the order of dfs calls starting from 0. Visit neighbors in increasing order.

Depth-First Search
Give the order of dfs calls starting from 0. Visit neighbors in increasing order.
5
A
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
0
1
4
5
2
3
6

Give the BFS visit order starting from 0. Visit neighbors in increasing order.
6
Q
0
1
4
5
2
3
6
Q1: Give the BFS visit order starting from 0. Visit neighbors in increasing order.

Give the BFS visit order starting from 0. Visit neighbors in increasing order.
7
A
0
1
4
5
2
3
6

Particle Detection
Given a black-white image of particles, identify “blobs”.
Formulate this as a graph problem.

Describe how to solve it.

8
Q
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
0
1
2
3
4
5
0
1
2
3
4
Given a black-white image of particles, identify “blobs”.

Q1: Formulate this as a graph problem.

Q2: Describe how to solve it.

Particle Detection
Given a black-white image of particles, identify “blobs”.
Formulate this as a graph problem.

Describe how to solve it.

9
A
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
0
1
2
3
4
5
0
1
2
3
4

Two-Coloring
How difficult is it to determine whether or not a graph is bipartite?
CSE 332 student could do it.
Hire an expert.
Intractable.
No one knows.

10
Q
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
0
1
4
2
3
6
5
Q1: How difficult is it to determine whether or not a graph is bipartite?

Two-Coloring
How difficult is it to determine whether or not a graph is bipartite?
CSE 332 student could do it.
Hire an expert.
Intractable.
No one knows.

11
A
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
0
1
4
2
3
6
5

Cycle Detection 1
How difficult is it to determine whether or not a graph has a cycle? (0-5-6-4-0)
CSE 332 student could do it.
Hire an expert.
Intractable.
No one knows.

12
Q
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
0
1
4
2
3
6
5
Q1: How difficult is it to determine whether or not a graph has a cycle? (0-5-6-4-0)

Cycle Detection 1
How difficult is it to determine whether or not a graph has a cycle? (0-5-6-4-0)
CSE 332 student could do it.
Hire an expert.
Intractable.
No one knows.

13
A
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
0
1
4
2
3
6
5
Q1: How difficult is it to determine whether or not a graph has a cycle? (0-5-6-4-0)

Cycle Detection 2
How difficult is it to determine whether or not a graph has a cycle using every edge exactly once? (0-1-2-3-4-2-0-6-4-5-0)
CSE 332 student could do it.
Hire an expert.
Intractable.
No one knows.

14
Q
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
0
1
4
2
3
6
5
Q1: How difficult is it to determine whether or not a graph has a cycle using every edge exactly once? (0-1-2-3-4-2-0-6-4-5-0)

Cycle Detection 2
How difficult is it to determine whether or not a graph has a cycle using every edge exactly once? (0-1-2-3-4-2-0-6-4-5-0)
CSE 332 student could do it.
Hire an expert.
Intractable.
No one knows.

15
A
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
0
1
4
2
3
6
5
Q1: How difficult is it to determine whether or not a graph has a cycle using every edge exactly once? (0-1-2-3-4-2-0-6-4-5-0)

Cycle Detection 3
How difficult is it to determine whether or not a graph has a cycle using every vertex exactly once? (0-5-3-4-6-2-1-0)
CSE 332 student could do it.
Hire an expert.
Intractable.
No one knows.

16
Q
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
0
1
4
2
3
6
5
Q1: How difficult is it to determine whether or not a graph has a cycle using every vertex exactly once? (0-5-3-4-6-2-1-0)

Cycle Detection 3
How difficult is it to determine whether or not a graph has a cycle using every vertex exactly once? (0-5-3-4-6-2-1-0)
CSE 332 student could do it.
Hire an expert.
Intractable.
No one knows.

17
A
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
0
1
4
2
3
6
5
Q1: How difficult is it to determine whether or not a graph has a cycle using every vertex exactly once? (0-5-3-4-6-2-1-0)

Graph Traversal Problems
18
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton), Determinant Sums for Undirected Hamiltonicity (Andreas Björklund/arXiv)
Graph Problem
DFS
BFS
Runtime
s–t path
✓
✓
O(E + V)
s–t shortest path
✓
O(E + V)
connected components
✓
✓
O(E + V)
bipartite (odd cycle)
✓
✓
O(E + V)
cycle
✓
✓
O(V)
Euler cycle
✓
O(E + V)
Hamilton cycle
O*(1.657V)
``````