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: breadthfirst and depthfirst. The two algorithms follow the same outline.
1. Initialize a
seen
set and a
tovisit
list both containing just the starting vertex
s
.
2. While the
tovisit
list is not empty:
(a) Remove the first vertex
n
from
tovisit
(b) For each neighbor vertex of
n
, if it is not in
seen
, add it to
seen
and
tovisit
(Optionally
add parent pointers back to
n
if you want to recover path)
When the
tovisit
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.
 Spring '11
 byrns
 Math, Graph Theory, Depthfirst search, Minimax, game state

Click to edit the document details