This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 6.006 Intro to Algorithms Recitation 12 March 16, 2011 Breadth First Search and Depth First Search Our goal is to start from some vertex s in a connected graph G and systematically visit every other vertex in G . One reason to do this may be to look for a particular vertex in G and find a path from your start vertex s to the target vertex. There are two main approaches to searching through the graph: breadth-first and depth-first. The two algorithms follow the same outline. 1. Initialize a seen set and a to-visit list both containing just the starting vertex s . 2. While the to-visit list is not empty: (a) Remove the first vertex n from to-visit (b) For each neighbor vertex of n , if it is not in seen , add it to seen and to-visit (Optionally add parent pointers back to n if you want to recover path) When the to-visit list is empty, we will have visited all the vertices in the graph. In addition to visiting every vertex, we also checked every vertexs neighbors, or in other words every edge coming out of each vertex. The total runtime of this search iscoming out of each vertex....
View Full Document
This note was uploaded on 11/11/2011 for the course MATH 180 taught by Professor Byrns during the Spring '11 term at Montgomery College.
- Spring '11