graph traversal
play

Graph traversal Topological sort Breadth-first search Cinda Heeren - PowerPoint PPT Presentation

Graph traversal Topological sort Breadth-first search Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 1 Geoffrey Tien Announcements All CPSC 221 deadlines postponed to 23:59, Apr.09 TA evaluations and instructor/course


  1. Graph traversal Topological sort Breadth-first search Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 1 Geoffrey Tien

  2. Announcements • All CPSC 221 deadlines postponed to 23:59, Apr.09 • TA evaluations and instructor/course evaluations will be done online – Check your e-mail for links/instructions soon Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 2 Geoffrey Tien

  3. Total order 1 2 5 4 7 6 3 means A must go before B A B Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 3 Geoffrey Tien

  4. Partial order Getting dressed pants shirt socks coat undies belt watch pocket boots knife Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 4 Geoffrey Tien

  5. Topological sort • Given a graph, 𝐻 = 𝑊, 𝐹 , output all vertices in V such that no vertex is output before any other vertex with an edge to it • Classic application: course prerequisites MATH 100 MATH 101 CPSC 110 CPSC 121 MATH 200 MATH 221 CPSC 213 CPSC 221 CPSC 210 STAT 241 CPSC 310 CPSC 313 CPSC 320 Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 5 Geoffrey Tien

  6. Topological sort • Label each vertex's in-degree (# of inbound edges) • Initialize a queue to contain all vertices with in-degree zero • While there are vertices remaining in the queue – Pick a vertex 𝑤 from the queue and output it – Reduce the in-degree of all vertices adjacent to 𝑤 – Put any of these with updated zero in-degree on the queue – Remove 𝑤 from the queue Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 6 Geoffrey Tien

  7. Topological sort Queue: 1 6 Vertex In-degree 1 0 2 2 1 3 3 1 1 6 4 2 5 2 5 4 6 0 Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 7 Geoffrey Tien

  8. Topological sort Queue: 1 6 2 Vertex In-degree 1 0 2 2 0 1 3 3 1 1 6 4 1 2 5 2 5 4 6 0 1 6 Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 8 Geoffrey Tien

  9. Topological sort Queue: 2 3 Vertex In-degree 1 0 2 2 0 3 0 3 1 1 6 4 1 5 2 5 4 6 0 1 6 2 Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 9 Geoffrey Tien

  10. Topological sort Queue: 3 4 Vertex In-degree 1 0 2 2 0 3 3 0 1 6 0 4 1 1 5 2 5 4 6 0 1 6 2 3 Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 10 Geoffrey Tien

  11. Topological sort Queue: 4 5 Vertex In-degree 1 0 2 2 0 3 3 0 1 6 4 0 0 5 1 5 4 6 0 1 6 2 3 4 5 Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 11 Geoffrey Tien

  12. How are algorithms affected? Adjacency list vs. adjacency matrix • Label each vertex's in-degree (# of inbound edges) 𝑃 𝑛 + 𝑜 vs 𝑃 𝑜 2 • Initialize a queue to contain all vertices with in-degree zero 𝑃 𝑜 • While there are vertices remaining in the queue 𝑜 times... – Pick a vertex 𝑤 from the queue and output it – Reduce the in-degree of all vertices adjacent to 𝑤 𝑃 𝑒𝑓𝑕 𝑤 vs 𝑃 𝑜 – Put any of these with updated zero in-degree on the queue – Remove 𝑤 from the queue For the adjacency list, how 2 many vertices/edges have 3 been processed in total? 1 6 4 5 Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 12 Geoffrey Tien

  13. Breadth-first search • Visits all vertices within 𝑒 "hops" away from starting vertex, before visiting vertices within 𝑒 + 1 hops, where 𝑒 begins at 0 start 2 hops 1 hop 4 hops 3 hops This looks very similar to a level-order traversal in a tree! How to control it? Use a queue! Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 13 Geoffrey Tien

  14. Breadth-first search • Need to be able to identify the neighbours of a given node, and to check if a vertex has been added to the queue already – Adjacency matrix / list, and a Boolean array of size 𝑊 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 6 1, 4, 7 7 1, 4, 6 Identified: 9 8 0 1 2 3 4 5 6 7 8 9 8 2 9 2 Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 14 Geoffrey Tien

  15. 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 6 1, 4, 7 7 1, 4, 6 T T T T T T T T T T Identified: 9 8 0 1 2 3 4 5 6 7 8 9 8 2 9 2 0 1 3 2 4 6 7 8 9 5 Queue: 0 1 3 2 4 6 7 8 9 5 Visited: 1 hop 2 hops 3 hops Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 15 Geoffrey Tien

  16. Breadth-first search Algorithm Algorithm BFS 𝐻, 𝑤 { mark 𝑤 as identified enqueue( 𝑤 ) while (queue is not empty) { 𝑣 = dequeue() for all vertices 𝑥 adjacent to 𝑣 , do { if ( 𝑥 has not been identified or visited) { mark 𝑥 as identified enqueue( 𝑥 ) } } output 𝑣 to visited collection } } A for loop nested inside a while loop. Running time? Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 16 Geoffrey Tien

  17. Readings for this lesson • Carrano & Henry – Chapter 20.3 (Graph traversals) • Next class – Carrano & Henry, Chapter 20.4.2 (Spanning trees) – Carrano & Henry, Chapter 20.4.3 (Minimum spanning trees) Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 17 Geoffrey Tien

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend