nextupprevious
Next:7.5 Directed Acyclic GraphsUp:7.4 Depth First Search and Breadth First SearchPrevious:7.4 Depth First Search and Breadth First Search

7.4.1 Breadth First Search

Implementation of Breadth-First Search



void bfs (v) /* visits all vertices connected to v

in breadth-first fashion */

vertex x, y;

vertexqueue Q;

{

            mark [v] = visited ;

            enqueue (v, Q);

                while (Q not empty)

                    { x = front (Q) ;

                        dequeue (Q) ;

                        for (each vertex y adjacent to x)

                        if (mark[y] = unvisited)

                {

                        mark[y] = visited ;

                        enqueue(y,Q) ;

                        insert ((x,y), T)

            }

    }

}
 

Example: See Figure 7.11.
 


eEL,CSA_Dept,IISc,Bangalore