Breadth-first search and depth-first search

Breadth-first search and depth-first search
There are two common methods for searching a graph: depth-first search and breadth-first search. They will eventually reach all connected vertices. Depth-first search is implemented through stacks, while breadth-first search is implemented through queues.
 
Depth-first search:
The numbers in the figure below show the order in which depth-first search vertices are visited.
 
To implement a depth-first search, a starting vertex is first chosen and three rules need to be followed:
(1) If possible, visit an adjacent unvisited vertex, mark it, and put it on the stack.
(2) When rule 1 cannot be executed, if the stack is not empty, pop a vertex from the stack.
(3) If rules 1 and 2 cannot be executed, the entire search process is completed.
 
Breadth-first search:
In depth-first search, the algorithm behaves as if it wants to move away from the starting point as quickly as possible. In contrast, in breadth-first search, the algorithm seems to want to get as close to the starting point as possible. It first visits all the neighbors of the starting vertex, and then visits further regions. It is implemented using queues.
The numbers in the figure below show the order in which the vertices are visited in a breadth-first search.
 
To achieve breadth-first search, there are three rules:
(1) Visit the next neighbor to be visited in the future, this vertex must be the neighbor of the current vertex, mark it, and insert it into the queue.
(2) If rule 1 cannot be executed because there are no unvisited vertices, then take a vertex from the head of the queue and make it the current vertex.
(3) If rule 2 cannot be executed because the queue is empty, the search ends.
/* Graph stored in the adjacency list – DFS (C language implementation) */ 
/* Visited[] is a global variable, which has been initialized to FALSE */ 
void   DFS( ALGraph *G,   int i )
{    /* DFS search the graph G stored in the adjacency table with Vi as the starting point */ 
    EdgeNode * W;
    printf( " visit vertex: %c\n " , G-> adjlist[i].Vertex );
     /* Equivalent to visiting vertex Vi */ 
    Visited[i] = TRUE;    /* mark Vi visited */ 
    for ( W = G->adjlist[i].FirstEdge; W; W = W-> Next ) 
        if ( !Visited[ W-> AdjV ] )
           DFS( G, W->AdjV );
}

/* Graph stored by adjacency matrix – BFS (C language implementation) */ 
void   BFS ( MGraph G )
{    /* Traverse the graph G in breadth first. Use auxiliary queue Q and access flags array Visited */ 
    Queue   * Q;    
    VertexType  U, V, W;
    for ( U = 0; U < G.n; ++U )  
       Visited[U] = FALSE;
    Q = CreatQueue( MaxSize ); /* Create empty queue Q */ 
    for ( U = 0 ; U<Gn; ++ U )
        if ( !Visited[U] ) { /* If U has not been visited */ 
           Visited[U] = TRUE;
           printf( " visit vertex: %c\n " , G.Vertices[U] );
            /* Equivalent to visiting vertex U */ 
           AddQ (Q, U);     /* U into the queue */ 
           while ( ! IsEmptyQ(Q) ) {
              V = DeleteQ( Q );   /*   The head element is dequeued and placed as V */ 
              for ( W = FirstAdjV(G, V); W; W = NextAdjV(G, V, W) )
                   if ( ! Visited[W ] ) {
                     Visited[W] = TRUE;
                     printf( " visit vertex: %c\n " , G.Vertices[W] );
                      /* Equivalent to visiting vertex W */
                     AddQ (Q, W);
                  }
           } /* while end */ 
} /* end BFS from U */ 
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325253397&siteId=291194637