lecture 29 - mcgill cimlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8....
TRANSCRIPT
![Page 1: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/1.jpg)
1
COMP 250
Lecture 29
graph traversal
Nov. 15/16, 2017
![Page 2: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/2.jpg)
Today
• Recursive graph traversal
• depth first
• Non-recursive graph traversal
• depth first
• breadth first
2
![Page 3: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/3.jpg)
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.
![Page 4: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/4.jpg)
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
![Page 5: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/5.jpg)
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
![Page 6: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/6.jpg)
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”
![Page 7: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/7.jpg)
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”
![Page 8: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/8.jpg)
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)
}
![Page 9: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/9.jpg)
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)
}
![Page 10: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/10.jpg)
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)
}
![Page 11: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/11.jpg)
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)
}
![Page 12: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/12.jpg)
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)
}
![Page 13: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/13.jpg)
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
![Page 14: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/14.jpg)
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 ) ?
![Page 15: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/15.jpg)
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)
![Page 16: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/16.jpg)
16
Q: Non-recursive graph traversal ?
A: Similar to tree traversal: Use a stack or a queue.
![Page 17: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/17.jpg)
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.
![Page 18: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/18.jpg)
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)
}}
}}
![Page 19: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/19.jpg)
Example: graphTraversalUsingStack(a)
19
a
g
d
a
h
e
b
i
f
c
a
![Page 20: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/20.jpg)
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?
![Page 21: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/21.jpg)
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
![Page 22: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/22.jpg)
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
![Page 23: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/23.jpg)
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
![Page 24: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/24.jpg)
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
![Page 25: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/25.jpg)
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
![Page 26: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/26.jpg)
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
![Page 27: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/27.jpg)
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
![Page 28: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/28.jpg)
Breadth first graph traversal
28
Given an input vertex, find all vertices that can be reached by paths of length 1, 2, 3, 4, ….
![Page 29: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/29.jpg)
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)
}}
}}
![Page 30: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/30.jpg)
Example
30
fc
d
a e
b
graphTraversalUsingQueue(c)
queuec
![Page 31: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/31.jpg)
Example
31
fc
d
a e
b
graphTraversalUsingQueue(c)
queuecf
![Page 32: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/32.jpg)
Example
32
fc
d
a e
b
graphTraversalUsingQueue(c)
queuecfbe
Both ‘b’, ‘e’ are visited and enqueued before ‘b’ is dequeued.
![Page 33: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/33.jpg)
Example
33
fc
d
a e
b
graphTraversalUsingQueue(c)
queuecfbee
![Page 34: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/34.jpg)
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.
![Page 35: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/35.jpg)
Example: graphTraversalUsingQueue(a)
35
g
d
a
h
e
b
i
f
c a1
![Page 36: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/36.jpg)
Example: graphTraversalUsingQueue(a)
36
g
d
a
h
e
b
i
f
c abd
1 2
3
![Page 37: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/37.jpg)
Example: graphTraversalUsingQueue(a)
37
g
d
a
h
e
b
i
f
c abddce
1 2 4
3 5
![Page 38: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/38.jpg)
Example: graphTraversalUsingQueue(a)
38
g
d
a
h
e
b
i
f
c abddceceg
1 2 4
3 5
6
![Page 39: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/39.jpg)
Example: graphTraversalUsingQueue(a)
39
g
d
a
h
e
b
i
f
c abddcecegegf
1 2 4
3 5 7
6
![Page 40: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/40.jpg)
Example: graphTraversalUsingQueue(a)
40
g
d
a
h
e
b
i
f
c abddcecegegfgfh
1 2 4
3 5 7
6 8
![Page 41: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/41.jpg)
Example: graphTraversalUsingQueue(a)
41
g
d
a
h
e
b
i
f
c abddcecegegfgfhfh
hii
1 2 4
3 5 7
6 8
![Page 42: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/42.jpg)
Example: graphTraversalUsingQueue(a)
42
g
d
a
h
e
b
i
f
c abddcecegegfgfhfhhi
1 2 4
3 5 7
6 8 9
![Page 43: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/43.jpg)
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
![Page 44: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/44.jpg)
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?
![Page 45: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/45.jpg)
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?
![Page 46: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/46.jpg)
46
HEADS UP ! Prior to traversal, ….
for each w in Vw.visited = false
How to implement this ?
![Page 47: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/47.jpg)
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 ?
![Page 48: Lecture 29 - McGill CIMlanger/250/29-graphs2-slides.pdf · ceg egf gfh fh hi i 1 2 4 3 5 7 6 8. Example: graphTraversalUsingQueue(a) 42 g d a h e b i f c a bd dce ceg egf gfh fh hi](https://reader033.vdocument.in/reader033/viewer/2022051907/5ffa0a39e454fb20e664de35/html5/thumbnails/48.jpg)
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. ]