1
COMP 250
Lecture 29
graph traversal
Nov. 15/16, 2017
Today
• Recursive graph traversal
• depth first
• Non-recursive graph traversal
• depth first
• breadth first
2
3
Heads up!
There were a few mistakes in the slides for Sec. 001 for today’s lecture. So if you are following the lecture recordings and using these (corrected) slides, then you will notice some differences.
depthfirst__Tree (root){if (root is not empty){
root.visited = true // “preorder”for each child of root
depthfirst__Tree( child )}
}
Recall: tree traversal (recursive)
4
Graph traversal (recursive)
Need to specify a starting vertex.
Visit all nodes that are “reachable” by a path from a starting vertex.
f
g
c
d
a e
b h5
depthFirst_Graph(v){v.visited = truefor each w such that (v,w) is in E // w in v.adjList
_______?___________}
Graph traversal (recursive)
6
// Here “visiting” just means “reaching”
depthFirst_Graph(v){v.visited = truefor each w such that (v,w) is in E // w in v.adjListif ! (w.visited) // avoids cycles
depthFirst_Graph(w)}
Graph traversal (recursive)
7
// Here “visiting” just means “reaching”
b e f f f f f
c c c c c c ca
f
g
c
d
a e
b h
Call Stack for depthFirst(a)
8
depthFirst_Graph(v){v.visited = truefor each w such that (v,w) is in E
if ! (w.visited)depthFirst_Graph(w)
}
b e f f f f f
c a a
f
g
c
d
a e
b h
Call Stack for depthFirst(a)
9
depthFirst_Graph(v){v.visited = truefor each w such that (v,w) is in E
if ! (w.visited)depthFirst_Graph(w)
}
b ef
c ca a a
f
g
c
d
a e
b h
Call Stack for depthFirst(a)
10
depthFirst_Graph(v){v.visited = truefor each w such that (v,w) is in E
if ! (w.visited)depthFirst_Graph(w)
}
b ef f f f f
c c c c c c ca a a a a a a a a
f
g
c
d
a e
b h
Call Stack for depthFirst(a)
11
depthFirst_Graph(v){v.visited = truefor each w such that (v,w) is in E
if ! (w.visited)depthFirst_Graph(w)
}
b e f f f f f
c c c c c c ca a a a a a a a a
f
g
c
d
a e
b h
Call Stack for depthFirst(a)
12
depthFirst_Graph(v){v.visited = truefor each w such that (v,w) is in E
if ! (w.visited)depthFirst_Graph(w)
}
Call Tree
13
f
g
c
d
a e
b h
root
b e f f f f f
c c c c c c ca a a a a a a a a
Example 2
14
g
d
a
h
e
b
i
f
ca - (b,d)b - (a,c,e) c - (b,f)d - (a,e,g) e - (b,d,f,h)f - (c,e,i) g - (d,h) h - (e,g,i) i - (f,h)
Adjacency List
What is the call treefor depthFirst( a ) ?
Example 2
15
g
d
a
h
e
b
i
f
c
g
d
a
h
e
b
i
f
c
call tree for depthFirst(a)
16
Q: Non-recursive graph traversal ?
A: Similar to tree traversal: Use a stack or a queue.
Recall: depth first tree traversal(with a slight variation)
17
treeTraversalUsingStack(root){initialize empty stack svisit root s.push(root)while s is not empty {
cur = s.pop()for each child of cur{
visit child s.push(child)
} }
}
Visit a node before pushing it onto the stack.
Every node in the tree gets visited, pushed, and then popped.
Generalize to graphs…
18
graphTraversalUsingStack(v){initialize empty stack sv.visited = true s.push(v) while (!s.empty) {
u = s.pop()for each w in u.adjList{
if (!w.visited){ // the only new partw.visited = true s.push(w)
}}
}}
Example: graphTraversalUsingStack(a)
19
a
g
d
a
h
e
b
i
f
c
a
Example: graphTraversalUsingStack(a)
20
d
a b
g
d
a
h
e
b
i
f
c
d a b ‘a’ is popped and both ‘b’ and ‘d’ are pushed.
The traversal defines a tree, but it is not a “call tree”. Why not?
Example: graphTraversalUsingStack(a)
21
g
d
a
e
b
g
d
a
h
e
b
i
f
c
g d e ‘d’ is popped and both ‘e’ and ‘g’ are pushed.
a b b
Example: graphTraversalUsingStack(a)
22
g
d
a
e
b
g
d
a
h
e
b
i
f
c
g hd e e ‘g’ is popped and ‘h’ is pushed.
a b b b
h
Example: graphTraversalUsingStack(a)
23
g
d
a
e
b
g
d
a
h
e
b
i
f
c
g h id e e e ‘h’ is popped and ‘i’ is pushed.
a b b b b
h i
Example: graphTraversalUsingStack(a)
24
g
d
a
e
b
g
d
a
h
e
b
i
f
c
g h i fd e e e e ‘i’ is popped and ‘f’ is pushed.
a b b b b b
h i
f
Example: graphTraversalUsingStack(a)
25
g
d
a
e
b
g
d
a
h
e
b
i
f
c
g h i f cd e e e e e ‘f’ is popped and ‘c’ is pushed.
a b b b b b b
h i
f
c
Example: graphTraversalUsingStack(a)
26
g
d
a
e
b
g
d
a
h
e
b
i
f
c
g h i f cd e e e e e e
a b b b b b b b b
h i
f
c
Order of nodes visited:abdeghifc
a
b c d
ie f hg
j k
treeTraversalUsingQueue(root){initialize empty queue qq.enqueue(root)while q is not empty {
cur = q.dequeue()visit curfor each child of cur
q.enqueue(child)}
}
Recall: breadth first tree traversal(see lecture 20)
27
for each level ivisit all nodes at level i
Breadth first graph traversal
28
Given an input vertex, find all vertices that can be reached by paths of length 1, 2, 3, 4, ….
Breadth first graph traversal
29
graphTraversalUsingQueue(v){initialize empty queue qv.visited = true q.enqueue(v)while (! q.empty) {
u = q.dequeue()for each w in u.adjList{
if (!w.visited){ w.visited = true q.enqueue(w)
}}
}}
Example
30
fc
d
a e
b
graphTraversalUsingQueue(c)
queuec
Example
31
fc
d
a e
b
graphTraversalUsingQueue(c)
queuecf
Example
32
fc
d
a e
b
graphTraversalUsingQueue(c)
queuecfbe
Both ‘b’, ‘e’ are visited and enqueued before ‘b’ is dequeued.
Example
33
fc
d
a e
b
graphTraversalUsingQueue(c)
queuecfbee
34
fc
d
a e
b
graphTraversalUsingQueue(c)
It defines a tree whose root is the starting vertex. It finds the shortest path (number of vertices) to all vertices reachable from starting vertex.
Example: graphTraversalUsingQueue(a)
35
g
d
a
h
e
b
i
f
c a1
Example: graphTraversalUsingQueue(a)
36
g
d
a
h
e
b
i
f
c abd
1 2
3
Example: graphTraversalUsingQueue(a)
37
g
d
a
h
e
b
i
f
c abddce
1 2 4
3 5
Example: graphTraversalUsingQueue(a)
38
g
d
a
h
e
b
i
f
c abddceceg
1 2 4
3 5
6
Example: graphTraversalUsingQueue(a)
39
g
d
a
h
e
b
i
f
c abddcecegegf
1 2 4
3 5 7
6
Example: graphTraversalUsingQueue(a)
40
g
d
a
h
e
b
i
f
c abddcecegegfgfh
1 2 4
3 5 7
6 8
Example: graphTraversalUsingQueue(a)
41
g
d
a
h
e
b
i
f
c abddcecegegfgfhfh
hii
1 2 4
3 5 7
6 8
Example: graphTraversalUsingQueue(a)
42
g
d
a
h
e
b
i
f
c abddcecegegfgfhfhhi
1 2 4
3 5 7
6 8 9
Example: graphTraversalUsingQueue(a)
43
g
d
a
h
e
b
i
f
c abddcecegegfgfhfhhii
1 2 4
3 5 7
6 8 9
Note order of nodes visited: paths of length 1,2, 3, 4
Example: graphTraversalUsingQueue(a)
44
g
d
a
h
e
b
i
f
cThe traversal defines a tree, but it is not a “call tree”. Why not?
class Graph<T> {HashMap< String, Vertex<T> > vertexMap;
class Vertex<T> {ArrayList<Edge> adjList; T element;boolean visited;
}
class Edge {Vertex endVertex;double weight;
:
}
}
Recall: How to implement a Graph class in Java?
46
HEADS UP ! Prior to traversal, ….
for each w in Vw.visited = false
How to implement this ?
47
class Graph<T> { HashMap< String, Vertex<T> > vertexMap;
:public void resetVisited() {
}}
HEADS UP ! Prior to traversal, ….
for each w in Vw.visited = false
How to implement this ?
48
class Graph<T> { HashMap< String, Vertex<T> > vertexMap;
:public void resetVisited() {for( Vertex<T> v : vertexMap.values() ){
v.visited = false;}
}
HEADS UP ! Prior to traversal, ….
for each w in Vw.visited = false
How to implement this ?
[ASIDE: I did something unnecessarily complicated on the Sec.001 slides.What I have above is better. ]