BFS - CE177 Schlag Fall '04 Handout # 2 Breadth First...

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

View Full Document Right Arrow Icon
CE177 Schlag Fall ’04 Handout # 2 Breadth First Search The input is a simple graph. This algorithm traverses each component of the graph, numbering vertices as they are encountered ( bfs[ u ] ) and saving for each vertex u the neighbor (if any) from which it was first discovered ( p[ u ] ). During the traversal a vertex can be one of three colors, white as long as it is undiscovered, gray when it has been discovered but is still unexplored, and finally black when it has been explored. A rooted tree is obtained for each component. In the first for-loop all vertices are colored white and placed in the set New . In each iteration of the outer while-loop a vertex is removed from New and placed in the empty queue Q . This vertex is placed in Roots ; it will serve as the root of a tree for the component currently being explored. The queue Q contains the vertices that have been discovered but not yet explored (gray vertices). In each iteration of the inner while-loop an unexplored vertex is removed from Q and its edges are explored. Any of its neighbors that are discovered for the first time (are currently white) are assigned u as their parent in the tree, and are added to
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 2

BFS - CE177 Schlag Fall '04 Handout # 2 Breadth First...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online