{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# rec12 - 6.006 Intro to Algorithms Recitation 12 Breadth...

This preview shows pages 1–2. Sign up to view the full content.

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 vertex’s neighbors, or in other words every edge coming out of each vertex. The total runtime of this search is

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 2

rec12 - 6.006 Intro to Algorithms Recitation 12 Breadth...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online