400 likes | 532 Views
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
E N D
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 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
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
Breadth-First Search Queue: A B C Current: D E F G
Breadth-First Search Queue: v A A B C Current: D E F G
Breadth-First Search Queue: v A A B C Current: D E F G A
Breadth-First Search Queue: v A B C Current: D E F G A A
Breadth-First Search Queue: v B A v B C Current: D E F G A A
Breadth-First Search Queue: v C B A v v B C Current: D E F G A A
Breadth-First Search Queue: v C B A v v B C Current: D E F G B A
Breadth-First Search Queue: v C A v v B C Current: D E F G B A B
Breadth-First Search Queue: v C A v v B C Current: D E F G B A B
Breadth-First Search Queue: v D C A v v B C v Current: D E F G B A B
Breadth-First Search Queue: v E D C A v v B C v Current: D E F G B v A B
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Breadth-First Search A B C D E F G A B C D E F G
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?
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
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