18.04.2013 Views

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Breadth-First Search<br />

Next: Depth-First Search Up: Traversing a Graph Previous: Traversing a Graph<br />

Breadth-First Search<br />

Figure: An undirected graph and its breadth-first search tree<br />

<strong>The</strong> basic breadth-first search algorithm is given below. At some point during the traversal, every node in the graph changes<br />

state from undiscovered to discovered. In a breadth-first search of an undirected graph, we assign a direction to each edge,<br />

from the discoverer u to the discovered v. We thus denote u to be the parent p[v]. Since each node has exactly one parent,<br />

except for the root, this defines a tree on the vertices of the graph. This tree, illustrated in Figure , defines a shortest path<br />

from the root to every other node in the tree. This property makes breadth-first search very useful in shortest path problems.<br />

BFS(G,s)<br />

for each vertex do<br />

state[] = ``discovered''<br />

p[] = nil<br />

while do<br />

file:///E|/BOOK/BOOK2/NODE64.HTM (1 of 2) [19/1/2003 1:29:06]<br />

state[u] = ``undiscovered''<br />

p[u] = nil, i.e. no parent is in the BFS tree<br />

u = dequeue[Q]<br />

process vertex u as desired

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!