COMP 250 Lecture 29 graph traversal Nov. 15/16, 2017 1
Today • Recursive graph traversal • depth first • Non-recursive graph traversal • depth first • breadth first 2
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. 3
Recall: tree traversal (recursive) depthfirst__Tree (root){ if (root is not empty){ root.visited = true // “ preorder ” for each child of root depthfirst__Tree( child ) } } 4
Graph traversal (recursive) Need to specify a starting vertex. Visit all nodes that are “reachable” by a path from a starting vertex. e g a c f d h b 5
Graph traversal (recursive) depthFirst_Graph(v){ v.visited = true for each w such that (v,w) is in E // w in v.adjList _______?___________ } // Here “visiting” just means “reaching” 6
Graph traversal (recursive) depthFirst_Graph(v){ v.visited = true for each w such that (v,w) is in E // w in v.adjList if ! (w.visited) // avoids cycles depthFirst_Graph(w) } // Here “visiting” just means “reaching” 7
Call Stack for depthFirst(a) e g a c f d h b b e depthFirst_Graph(v){ v.visited = true f f f f f for each w such that (v,w) is in E if ! (w.visited) c c c c c c c depthFirst_Graph(w) a } 8
Call Stack for depthFirst(a) e g a c f d h b b e depthFirst_Graph(v){ v.visited = true f f f f f for each w such that (v,w) is in E if ! (w.visited) c depthFirst_Graph(w) a a } 9
Call Stack for depthFirst(a) e g a c f d h b b e depthFirst_Graph(v){ v.visited = true f for each w such that (v,w) is in E if ! (w.visited) c c depthFirst_Graph(w) a a a } 10
Call Stack for depthFirst(a) e g a c f d h b b e depthFirst_Graph(v){ v.visited = true f f f f f for each w such that (v,w) is in E if ! (w.visited) c c c c c c c depthFirst_Graph(w) a a a a a a a a a } 11
Call Stack for depthFirst(a) e g a c f d h b b e depthFirst_Graph(v){ v.visited = true f f f f f for each w such that (v,w) is in E if ! (w.visited) c c c c c c c depthFirst_Graph(w) a a a a a a a a a } 12
Call Tree root e g a c f d h b b e f f f f f c c c c c c c a a a a a a a a a 13
Example 2 Adjacency List a - (b,d) c b a b - (a,c,e) c - (b,f) f d - (a,e,g) e d e - (b,d,f,h) f - (c,e,i) i g h g - (d,h) h - (e,g,i) What is the call tree i - (f,h) for depthFirst( a ) ? 14
Example 2 c c b b a a f f d e e d i h i g g h call tree for depthFirst(a) 15
Q: Non-recursive graph traversal ? A: Similar to tree traversal: Use a stack or a queue. 16
Recall: depth first tree traversal (with a slight variation) treeTraversal UsingStack(root){ Visit a node before initialize empty stack s pushing it onto the visit root stack. s.push(root) while s is not empty { Every node in the cur = s.pop() tree gets visited, for each child of cur{ pushed, and then visit child popped. s.push(child) } } } 17
Generalize to graphs… graphTraversal UsingStack(v){ initialize empty stack s v.visited = true s.push(v) while (!s.empty) { u = s.pop() for each w in u.adjList{ if (!w.visited){ // the only new part w.visited = true s.push(w) } } } } 18
Example: graphTraversalUsingStack( a ) c b a a f e d i h g a 19
Example: graphTraversalUsingStack( a ) c b b a a The traversal f e d d defines a tree, but it is not a “call tree”. Why i h g not? d a b ‘a’ is popped and both ‘b’ and ‘d’ are pushed. 20
Example: graphTraversalUsingStack(a) c b b a a f e e d d i g h g g d e ‘d’ is popped and both ‘e’ and ‘g’ are pushed. a b b 21
Example: graphTraversalUsingStack(a) c b b a a f e e d d i g h h g g h d e e ‘g’ is popped and ‘h’ is pushed. a b b b 22
Example: graphTraversalUsingStack(a) c b b a a f e e d d i i g h h g g h i d e e e ‘h’ is popped and ‘ i ’ is pushed. a b b b b 23
Example: graphTraversalUsingStack(a) c b b a a f f e e d d i i g h h g g h i f d e e e e ‘ i ’ is popped and ‘f’ is pushed. a b b b b b 24
Example: graphTraversalUsingStack(a) c c b b a a f f e e d d i i g h h g g h i f c d e e e e e ‘f’ is popped and ‘c’ is pushed. a b b b b b b 25
Example: graphTraversalUsingStack(a) c c b b a a f f e e d d i i g h h g Order of nodes visited: g h i f c abdeghifc d e e e e e e a b b b b b b b b 26
Recall: breadth first tree traversal (see lecture 20) treeTraversalUsingQueue(root){ for each level i initialize empty queue q visit all nodes at level i q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur a for each child of cur d b c q.enqueue(child) } } g e f h i j k 27
Breadth first graph traversal Given an input vertex, find all vertices that can be reached by paths of length 1, 2, 3, 4, …. 28
Breadth first graph traversal graphTraversalUsing Queue(v){ initialize empty queue q v.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) } } } } 29
Example graphTraversalUsingQueue(c) queue e a c c f d b 30
Example graphTraversalUsingQueue(c) queue e a c c f f d b 31
Example graphTraversalUsingQueue(c) queue e a c c f f d be b Both ‘b’, ‘e’ are visited and enqueued before ‘b’ is dequeued. 32
Example graphTraversalUsingQueue(c) queue e a c c f f d be b e 33
graphTraversalUsingQueue(c) e a c f d b 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. 34
Example: graphTraversalUsingQueue( a ) 1 a c b a f e d i h g 35
Example: graphTraversalUsingQueue( a ) 1 2 a c b a bd 3 f e d i h g 36
Example: graphTraversalUsingQueue( a ) 1 2 4 a c b a bd dce 3 5 f e d i h g 37
Example: graphTraversalUsingQueue( a ) 1 2 4 a c b a bd dce 3 5 f e d ceg 6 i h g 38
Example: graphTraversalUsingQueue( a ) 1 2 4 a c b a bd 3 5 7 dce f e d ceg egf 6 i h g 39
Example: graphTraversalUsingQueue( a ) 1 2 4 a c b a bd 3 5 7 dce f e d ceg egf 6 8 i h g gfh 40
Example: graphTraversalUsingQueue( a ) 1 2 4 a c b a bd 3 5 7 dce f e d ceg egf 6 8 i h g gfh fh hi i 41
Example: graphTraversalUsingQueue( a ) 1 2 4 a c b a bd 3 5 7 dce f e d ceg egf 6 8 9 i h g gfh fh hi 42
Example: graphTraversalUsingQueue( a ) 1 2 4 a c b a bd 3 5 7 dce f e d ceg egf 6 8 9 i h g gfh fh hi Note order of nodes visited: i paths of length 1,2, 3, 4 43
Example: graphTraversalUsingQueue(a) c b a The traversal defines a tree, but it f e is not a “call tree”. d Why not? i h g 44
Recall: How to implement a Graph class in Java? class Graph<T> { HashMap< String, Vertex<T> > vertexMap; class Vertex<T> { ArrayList<Edge> adjList; T element; boolean visited; } class Edge { Vertex endVertex; double weight; : } }
HEADS UP ! Prior to traversal, …. for each w in V How to implement this ? w.visited = false 46
HEADS UP ! Prior to traversal, …. for each w in V How to implement this ? w.visited = false class Graph<T> { HashMap< String, Vertex<T> > vertexMap; : public void resetVisited() { } } 47
Recommend
More recommend