1 / 40

Alyce Brady CS 510: Computer Algorithms

Breadth-First Graph Traversal Algorithm. Alyce Brady CS 510: Computer Algorithms. Search: Look for a given node stop when node found, even if not all nodes were visited Traversal: Always visit all nodes. Search vs Traversal. Similar to Breadth-first Traversal of a Binary Tree

quang
Download Presentation

Alyce Brady CS 510: Computer Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Breadth-First Graph Traversal Algorithm Alyce Brady CS 510: Computer Algorithms

  2. Search: Look for a given node stop when node found, even if not all nodes were visited Traversal: Always visit all nodes Search vs Traversal

  3. Similar to Breadth-first Traversal of a Binary Tree Choose a starting vertex Search all adjacent vertices Return to each adjacent vertex in turn and visit all of its adjacent vertices Breadth-first Search

  4. breadth-first-search mark starting vertex as visited; put on queue while the queue is not empty dequeue the next node for all unvisited vertices adjacent to this one mark vertex as visited add vertex to queue Pseudo-Code forBreadth-First Search

  5. Breadth-First Search Queue: A B C Current: D E F G

  6. Breadth-First Search Queue: v A A B C Current: D E F G

  7. Breadth-First Search Queue: v A A B C Current: D E F G A

  8. Breadth-First Search Queue: v A B C Current: D E F G A A

  9. Breadth-First Search Queue: v B A v B C Current: D E F G A A

  10. Breadth-First Search Queue: v C B A v v B C Current: D E F G A A

  11. Breadth-First Search Queue: v C B A v v B C Current: D E F G B A

  12. Breadth-First Search Queue: v C A v v B C Current: D E F G B A B

  13. Breadth-First Search Queue: v C A v v B C Current: D E F G B A B

  14. Breadth-First Search Queue: v D C A v v B C v Current: D E F G B A B

  15. Breadth-First Search Queue: v E D C A v v B C v Current: D E F G B v A B

  16. Breadth-First Search Queue: v F E D C A v v B C v D E F G Current: v v B A B

  17. Breadth-First Search Queue: v F E D C A v v B C v D E F G Current: v v C A B

  18. Breadth-First Search Queue: v F E D A v v B C v D E F G Current: v v C A B C

  19. Breadth-First Search Queue: v F E D A v v B C v D E F G Current: v v C A B C

  20. Breadth-First Search Queue: v F E D A v v B C v D E F G Current: v v C A B C

  21. Breadth-First Search Queue: v G F E D A v v B C v v D E F G Current: v v C A B C

  22. Breadth-First Search Queue: v G F E D A v v B C v v D E F G Current: v v D A B C

  23. Breadth-First Search Queue: v G F E A v v B C v v D E F G Current: v v D A B C D

  24. Breadth-First Search Queue: v G F E A v v B C v v D E F G Current: v v D A B C D

  25. Breadth-First Search Queue: v G F E A v v B C v v D E F G Current: v v D A B C D

  26. Breadth-First Search Queue: v G F E A v v B C v v D E F G Current: v v E A B C D

  27. Breadth-First Search Queue: v G F A v v B C v v D E F G Current: v v E A B C D E

  28. Breadth-First Search Queue: v G F A v v B C v v D E F G Current: v v E A B C D E

  29. Breadth-First Search Queue: v G F A v v B C v v D E F G Current: v v E A B C D E

  30. Breadth-First Search Queue: v G F A v v B C v v D E F G Current: v v F A B C D E

  31. Breadth-First Search Queue: v G A v v B C v v D E F G Current: v v F A B C D E F

  32. Breadth-First Search Queue: v G A v v B C v v D E F G Current: v v F A B C D E F

  33. Breadth-First Search Queue: v G A v v B C v v D E F G Current: v v F A B C D E F

  34. Breadth-First Search Queue: v G A v v B C v v D E F G Current: v v G A B C D E F

  35. Breadth-First Search Queue: v A v v B C v v D E F G Current: v v G A B C D E F G

  36. Breadth-First Search Queue: v A v v B C v v D E F G Current: v v G A B C D E F G

  37. Breadth-First Search A B C D E F G A B C D E F G

  38. Was this a true search? How would we make it a true search? Was this a true traversal? How would we make it a true traversal?

  39. Time Complexity Adjacency Lists Each node is added to queue once Each node is checked for each incoming edge O (v + e) Adjacency Matrix Have to check all entries in matrix: O(n2) Time and Space Complexityfor Breadth-First Search

  40. Space Complexity Queue to handle unexplored nodes Worst case: all nodes put on queue (if all are adjacent to first node) O(n) Time and Space Complexityfor Breadth-First Search

More Related