Graph traversal Breadthfirst search Depthfirst search Spanning trees
Graph traversal Breadth-first search Depth-first search Spanning trees March 25, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1
Breadth-first search • start 1 hop 2 hops 3 hops 4 hops This looks very similar to a level-order traversal in a tree! How to control it? Use a queue! March 25, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 2
Breadth-first search • 5 0 1, 3 1 0, 2, 4, 6, 7 0 4 2 1, 3, 8, 9 3 0, 2 3 1 7 4 1, 5, 6, 7 5 4 2 6 7 1, 4, 6 Identified: 9 8 6 1, 4, 7 0 1 2 3 4 5 6 7 8 9 8 2 9 2 March 25, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 3
Breadth-first search 5 0 1, 3 1 0, 2, 4, 6, 7 0 4 2 1, 3, 8, 9 3 0, 2 3 1 7 4 1, 5, 6, 7 5 4 2 6 9 6 1, 4, 7 Identified: T T T T T 0 1 2 3 4 5 6 7 8 9 8 7 1, 4, 6 8 2 9 2 Queue: 0 Visited: 0 March 25, 2019 1 3 2 4 6 7 8 9 1 3 1 hop 2 4 6 7 2 hops 8 9 5 3 hops 5 Cinda Heeren / Will Evans / Geoffrey Tien 4
Breadth-first search Algorithm A for loop nested inside a while loop. Running time? March 25, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 5
Depth-first search • Visits vertices along a single path as far as it can go, and then backtracks to the first junction and resumes down another path March 25, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 6
Depth-first search Algorithm March 25, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 7
Depth-first search 5 0 1, 3 1 0, 2, 4, 6, 7 0 4 2 1, 3, 8, 9 3 0, 2 3 1 7 4 1, 5, 6, 7 5 4 2 9 6 6 1, 4, 7 Visited: T T T T T 0 1 2 3 4 5 6 7 8 9 8 7 1, 4, 6 8 2 9 2 Visit order: 0 1 2 3 8 9 4 5 6 7 Finish order: 3 8 9 2 5 7 6 4 1 0 March 25, 2019 Cinda Heeren / Will Evans / Geoffrey Tien Running time? Can also implement like BFS using a stack 8
Applications of graph traversal ? Other applications: • Topological sort (DFS) • Modeling traffic flow • Cycle detection • Shortest path in maze (BFS) • … March 25, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 9
Spanning trees March 25, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 10
Spanning trees • d c b d c h a d c i b h Cinda Heeren / Will Evans / Geoffrey Tien e g f a March 25, 2019 i g f h b a e e i Spanning trees 11
Spanning tree properties • d c b f a h d c b a h i e g f A tree, but not a spanning tree d c b i g f a h e i Contains all the vertices and has the correct number of edges, but has a cycle and is not connected March 25, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 12
Constructing spanning trees Back to traversals • Spanning trees can be constructed by performing a traversal starting from any vertex, marking traveled edges and visited vertices – e. g. Breadth-first search, Depth-first search March 25, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 13
DFS spanning tree Root the DFS spanning tree at any vertex, e. g. d Mark vertices and edges used in DFS traversal d c b h h a b a, c, h b c b, d, f c d c, f, g g f a e a b e g i f d c b g f a h March 25, 2019 e i e g d, e i i h b, i g f c, d, i i f, g, h Cinda Heeren / Will Evans / Geoffrey Tien 14
BFS spanning tree Root the BFS spanning tree at any vertex, e. g. f Mark vertices and edges used in BFS traversal d c b h e g f a a b a, c, h b c b, d, f g d c, f, g e g i i d c b h March 25, 2019 i c, d, i g d, e i h b, i g f a e e f h i f, g, h Cinda Heeren / Will Evans / Geoffrey Tien 15
What else can traversals do? • • • Spanning trees Shortest path in unweighted graphs (BFS) Cycle detection Counting connected components. . . March 25, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 16
Readings for this lesson • Carrano & Henry – Chapters 20. 4. 2 (Spanning trees) • Next class: – Carrano & Henry, Chapter 20. 4. 3 (Minimum spanning trees) March 25, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 17
- Slides: 17