(Latest Revision: Mon Apr 22 00:19:53 PST 2008 ) graphBrdthSrch

graphBrdthSrch




ALGORITHM FOR A BREADTH-FIRST TRAVERSAL

       /* Traverse a graph, starting at vertex v. */
bfs (in v:Vertex)
{
   q.createQueue() ;

       /* mark v and add it to the queue. */
   mark v as visited ;  
   q.enqueue(v)

   while ( !q.isEmpty() )
   {
      q.dequeue(w)

            /* Loop invariant: there is a path from vertex w 
               to every vertex in the queue q */
      for (each unvisited vertex u adjacent to w)
      {
         mark u as visited
         q.enqueue(u)
      } /* end for */

   } /* end while */
} /* end bfs */