Graph traversal (stack)

Breadth-first and depth-first
traversal
CS1114
http://cs1114.cs.cornell.edu
Blobs are components!
A
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
E
H
0
0
0
0
0
0
0
D
F
0
0
0
0
0
0
0
0
0
G
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
B
C
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
C
A
B
D
F
E
G
H
2
Finding blobs
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
0
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
0
1
1
0
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
3
Finding blobs
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
0
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
0
1
1
0
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
Blobs are connected components!
4
Finding components
1. Pick a 1 to start with, where you don’t
know which component it is in
– When there aren’t any, you’re done
2. Give it a new component color
3. Assign the same component color to each
pixel that is part of the same component
–
Basic strategy: color any neighboring 1’s,
have them color their neighbors, and so on
5
Strategy for finding
components
 For each vertex we visit, we color its
neighbors and remember that we need to
visit them at some point
– Need to keep track of the vertices we still need
to visit in a todo list
– After we visit a vertex, we’ll pick one of the
vertices in the todo list to visit next
 This is also called graph traversal
6
Stacks and queues
 Two ways of representing a “todo list”
 Stack: Last In First Out (LIFO)
– (Think cafeteria trays)
– The newest task is the one you’ll do next
 Queue: First In First Out (FIFO)
– (Think a line of people at the cafeteria)
– The oldest task is the one you’ll do next
7
Stacks
 Two operations:
 Push: add something to the
top of the stack
 Pop: remove the thing on
top of the stack
8
Queue
 Two operations:
 Enqueue: add something to the end of
the queue
 Dequeue: remove something from the
front of the queue
9
Graph traversal
 Suppose you’re in a
maze
 What strategy can
you use to find the
exit?
10
Graph traversal
London
Paris
Oslo
Frankfurt
Stockholm
Hamburg
Vienna
Rome
Berlin
Prague
Warsaw
Naples
Pick a node to start with
11
Graph traversal (stack)
London
1
Paris
Oslo
Frankfurt
Stockholm
Hamburg
Vienna
Rome
Berlin
Prague
Warsaw
Naples
Current node: London
Todo list: [ ]
12
Graph traversal (stack)
London
1
Paris
Oslo
Frankfurt
Stockholm
Hamburg
Vienna
Rome
Berlin
Prague
Warsaw
Naples
Current node: London
Todo list: [ Paris ]
13
Graph traversal (stack)
London
1
Paris
Oslo
Frankfurt
2
Stockholm
Hamburg
Vienna
Rome
Berlin
Prague
Warsaw
Naples
Current node: Paris
Todo list: [ ]
14
Graph traversal (stack)
London
1
Paris
Oslo
Frankfurt
2
Stockholm
Hamburg
Vienna
Rome
Berlin
Prague
Warsaw
Naples
Current node: Paris
Todo list: [ Frankfurt, Berlin, Rome ]
15
Graph traversal (stack)
London
1
Paris
Oslo
Frankfurt
2
Stockholm
Hamburg
3
Rome
Vienna
Berlin
Prague
Warsaw
Naples
Current node: Rome
Todo list: [ Frankfurt, Berlin ]
16
Graph traversal (stack)
London
1
Paris
Oslo
Frankfurt
2
Stockholm
Hamburg
3
Rome
Vienna
Berlin
Prague
Warsaw
Naples
Current node: Rome
Todo list: [ Frankfurt, Berlin, Naples ]
17
Graph traversal (stack)
London
1
Paris
Oslo
Frankfurt
2
Stockholm
Hamburg
3
Rome
Vienna
Berlin
4
Prague
Warsaw
Naples
Current node: Naples
Todo list: [ Frankfurt, Berlin ]
18
Graph traversal (stack)
London
1
Paris
Oslo
Frankfurt
2
Stockholm
Hamburg
3
Rome
4
5
Berlin
Vienna
Prague
Warsaw
Naples
Current node: Berlin
Todo list: [ Frankfurt ]
19
Graph traversal (stack)
London
1
Paris
Oslo
Frankfurt
2
Stockholm
Hamburg
3
Rome
5
Vienna
Berlin
4
Prague
Warsaw
Naples
Current node: Berlin
Todo list: [ Frankfurt, Hamburg, Vienna ]
20
Graph traversal (stack)
London
1
Paris
Oslo
Frankfurt
2
Stockholm
Hamburg
3
Rome
5
Vienna
Berlin
6
4
Prague
Warsaw
Naples
Current node: Vienna
Todo list: [ Frankfurt, Hamburg ]
21
Graph traversal (stack)
London
1
Paris
Oslo
Frankfurt
2
Hamburg
3
Rome
4
Stockholm
5
Vienna
Berlin
6
Prague
Warsaw
Naples
Current node: Vienna
Todo list: [ Frankfurt, Hamburg, Prague, Warsaw ]
22
Graph traversal (stack)
London
1
Paris
Oslo
Frankfurt
2
Hamburg
3
Rome
4
Stockholm
5
Vienna
Berlin
6
Prague
Warsaw
Naples
Current node: Vienna
Todo list: [ Frankfurt, Hamburg, Prague, Warsaw ]
23
Graph traversal (stack)
London
1
Paris
Oslo
Frankfurt
2
Stockholm
Hamburg
3
Rome
5
Vienna
Berlin
6
4
Naples
Prague
7
Warsaw
Current node: Warsaw
Todo list: [ Frankfurt, Hamburg, Prague ]
24
Graph traversal (stack)
London
1
Paris
Oslo
Frankfurt
2
Stockholm
Hamburg
3
Rome
5
Vienna
Berlin
6
4
Naples
Prague
8
7
Warsaw
Current node: Prague
Todo list: [ Frankfurt, Hamburg ]
25
Graph traversal (stack)
London
1
Paris
Oslo
Frankfurt
2
Stockholm
9 Hamburg
3
Rome
4
Naples
5
Vienna
Berlin
6
Prague
8
7
Warsaw
Current node: Hamburg
Todo list: [ Frankfurt ]
26
Graph traversal (stack)
London
1
Paris
2
3
Rome
4
Naples
Oslo
Frankfurt
10
Stockholm
9 Hamburg
5
Vienna
Berlin
6
Prague
8
7
Warsaw
Current node: Frankfurt
Todo list: [ ]
27
Depth-first search (DFS)
1
2
3
4
10
9
5
6
8
7
 Call the starting node the root
 We traverse paths all the way until we get
to a dead-end, then backtrack (until we
find an unexplored path)
28
Another strategy
1. Explore all the cities that are one hop
away from the root
2. Explore all cities that are two hops away
from the root
3. Explore all cities that are three hops
away from the root
…
 This corresponds to using a queue
29
Graph traversal (queue)
London
1
Paris
Oslo
Frankfurt
Stockholm
Hamburg
Vienna
Rome
Berlin
Prague
Warsaw
Naples
Current node: London
Todo list: [ ]
30
Graph traversal (queue)
London
1
Paris
Oslo
Frankfurt
Stockholm
Hamburg
Vienna
Rome
Berlin
Prague
Warsaw
Naples
Current node: London
Todo list: [ Paris ]
31
Graph traversal (queue)
London
1
Paris
Oslo
Frankfurt
2
Stockholm
Hamburg
Vienna
Rome
Berlin
Prague
Warsaw
Naples
Current node: Paris
Todo list: [ ]
32
Graph traversal (queue)
London
1
Paris
Oslo
Frankfurt
2
Stockholm
Hamburg
Vienna
Rome
Berlin
Prague
Warsaw
Naples
Current node: Paris
Todo list: [ Frankfurt, Berlin, Rome ]
33
Graph traversal (queue)
London
1
Paris
2
Oslo
Frankfurt
3
Hamburg
Vienna
Rome
Berlin
Stockholm
Prague
Warsaw
Naples
Current node: Frankfurt
Todo list: [ Berlin, Rome ]
34
Graph traversal (queue)
London
1
Paris
2
Oslo
Frankfurt
3
Hamburg
Vienna
Rome
Stockholm
Berlin
Prague
Warsaw
Naples
Current node: Frankfurt
Todo list: [ Berlin, Rome, Hamburg ]
35
Graph traversal (queue)
London
1
Paris
2
3
Stockholm
Hamburg
4
Rome
Oslo
Frankfurt
Berlin
Vienna
Prague
Warsaw
Naples
Current node: Berlin
Todo list: [ Rome, Hamburg ]
36
Graph traversal (queue)
London
1
Paris
2
3
Stockholm
Hamburg
4
Rome
Oslo
Frankfurt
Vienna
Berlin
Prague
Warsaw
Naples
Current node: Berlin
Todo list: [ Rome, Hamburg, Vienna ]
37
Graph traversal (queue)
London
1
Paris
2
5
Rome
Oslo
Frankfurt
3
Stockholm
Hamburg
4
Berlin
Vienna
Prague
Warsaw
Naples
Current node: Rome
Todo list: [ Hamburg, Vienna ]
38
Graph traversal (queue)
London
1
Paris
2
5
Rome
Oslo
Frankfurt
3
Stockholm
Hamburg
4
Vienna
Berlin
Prague
Warsaw
Naples
Current node: Rome
Todo list: [ Hamburg, Vienna, Naples ]
39
Graph traversal (queue)
London
1
Paris
2
5
Rome
Oslo
Frankfurt
3
Stockholm
6 Hamburg
4
Berlin
Vienna
Prague
Warsaw
Naples
Current node: Hamburg
Todo list: [ Vienna, Naples ]
40
Graph traversal (queue)
London
1
Paris
2
5
Rome
Oslo
Frankfurt
3
Stockholm
6 Hamburg
4
Vienna
Berlin
7
Prague
Warsaw
Naples
Current node: Vienna
Todo list: [ Naples ]
41
Graph traversal (queue)
London
1
Paris
2
5
Rome
Oslo
Frankfurt
3
Stockholm
6 Hamburg
4
Vienna
Berlin
7
Prague
Warsaw
Naples
Current node: Vienna
Todo list: [ Naples, Prague, Warsaw ]
42
Graph traversal (queue)
London
1
Paris
2
5
Rome
Oslo
Frankfurt
3
Stockholm
6 Hamburg
4
Vienna
Berlin
7
8
Prague
Warsaw
Naples
Current node: Naples
Todo list: [ Prague, Warsaw ]
43
Graph traversal (queue)
London
1
Paris
2
5
Rome
8
Oslo
Frankfurt
3
Stockholm
6 Hamburg
4
Vienna
Berlin
7
Prague
9
Warsaw
Naples
Current node: Prague
Todo list: [ Warsaw ]
44
Graph traversal (queue)
London
1
Paris
2
5
Rome
8
Naples
Oslo
Frankfurt
3
Stockholm
6 Hamburg
4
Vienna
Berlin
7
Prague
9
10
Warsaw
Current node: Warsaw
Todo list: [ ]
45
Breadth-first search (BFS)
1
2
5
8
3
6
4
7
9
10
 We visit all the vertices at the same level
(same distance to the root) before moving
on to the next level
46
BFS vs. DFS
1
1
2
5
8
3
2
6
4
3
7
9
10
Breadth-first (queue)
4
10
9
5
6
8
7
Depth-first (stack)
47
BFS vs. DFS
(tree = graph with no cycles)
48
Basic algorithms
BREADTH-FIRST SEARCH (Graph G)
 While there is an uncolored node r
–
–
–
–
Choose a new color
Create an empty queue Q
Let r be the root node, color it, and add it to Q
While Q is not empty
•
•
Dequeue a node v from Q
For each of v’s neighbors u

If u is not colored, color it and add it to Q
49
Basic algorithms
DEPTH-FIRST SEARCH (Graph G)
 While there is an uncolored node r
–
–
–
–
Choose a new color
Create an empty stack S
Let r be the root node, color it, and push it on S
While S is not empty
•
•
Pop a node v from S
For each of v’s neighbors u

If u is not colored, color it and push it onto S
50
Queues and Stacks
 Examples of Abstract Data Types (ADTs)
 ADTs fulfill a contract:
– The contract tells you what the ADT can do,
and what the behavior is
– For instance, with a stack:
• We can push and pop
• If we push X onto S and then pop S, we get
back X, and S is as before
 Doesn’t tell you how it fulfills the contract
51
Implementing DFS
 How can we implement a stack?
–
–
–
–
Needs to support several operations:
Push (add an element to the top)
Pop (remove the element from the top)
IsEmpty
256
42
17
52
Implementing a stack
 IsEmpty
function e = IsEmpty(S)
e = (length(S) == 0);
 Push (add an element to the top)
function S = push(S, x)
S=[S x]
 Pop (remove an element from the top)
function [S, x] = pop(S)
n = length(S); x = S(n); S = S(1:n-1);
% but what happens if n = 0?
53
Implementing BFS
 How can we implement a queue?
–
–
–
–
Needs to support several operations:
Enqueue (add an element to back)
Dequeue (remove an element from front)
IsEmpty
256
42
17
 Not quite as easy as a stack…
54