DFS and BFS learning summary

DFS depth-first traversal

write picture description here

Depth traversal is to start from a vertex in the graph and go down according to a rule without repetition. It's just like not hitting the south wall and not looking back. If starting from the vertex of A and walking according to a rule (if we walk in lexicographical order), then we will go from A to B, then from B to C, and then go to C and then follow the lexicographical order, and find that A has already passed. , then go back to point C and choose another D to go down. It's the same as a preorder traversal of a tree.

Method to realize

1. Recursive implementation (implemented through an adjacency matrix)

void DFS(MGrap G. int i)
{
    int j = 0;
    visited[i] = 1;
    count++;
    for(j=0; j<G.numVertexes; j++)
    {
        if(G.arc[i][j]==1 && !visited[j])//i和j有关系相邻,并且j顶点没有被访问过
        {
            DFS(G, j);
        }
    }
}
void dfs(MGrap G,int v)  
{  
    init(&s);//使用自定义栈之前对栈进行初始化  
    push(&s,v);  //入栈第一个元素
    while(!isEmpty(&s))  
    {  
        pop(&s,&v);  //取出栈顶元素
        if(!visit[v])  //若没有访问过
        {  
            cout<<v<<' '; //输出该顶点
            visit[v]=true;  //标记已经访问过
            for(int k=0;k<M;k++)  //查找与该顶点相关联的顶点
            {  
                if(!visit[k]&&g[v][k]==1)  //若果未被访问过,且有相连的边
                {  
                    /*入栈操作*/
                    push(&s,k);  
                }  
            }  
        }  
    }  

}  

BFS breadth-first search traversal

Similar to the hierarchical search of a tree, it mainly starts from a certain vertex in the graph. After visiting this vertex, it sequentially visits the adjacent vertices that have not been visited.

write picture description here

Take A as the starting node, traverse B, C in turn, and then start traversing from the adjacent node DFE of B. The final traversal result is: ABCDFEGHI (following the first-in first-out rule)

Implementation (using adjacency matrix)

1, using the queue method

void BFS(MGrap G)
{
    int i,j;
    Queue Q;
    for(i=0; i<G.numVertexes; i++)/*初始化访问数组*/
    {
        visited[i] = -1;
    }
    InitQueue(&Q);
    for(i=0; i<G.numVertexes; i++)
    {
        if(!visited[i])
        {
            visited[i] = 1;
            printf("%c",G.vexs[i]);
            EnQueue(&Q,i);/*入队操作*/
            while(!QueueEmpty(Q))
            {
                DeQueue(&Q, &i);
                for(j=0; j<G.numVertexes; j++)
                {
                    /* 判断当前的节点与其他节点的关系 */
                    if(G.arc[i][j]==1 && !visited[j])
                    {
                        visited[j] = 1;
                        EnQueue(&Q,j);
                    }
                }
            }
        }
    }
}


Summarize:

The difference between the two is that when BFS traverses according to the depth, it needs to go back, so it is necessary to use the stack to record the position of the original vertex; DFS traverses from adjacent nodes in sequence, so whoever traverses first will be Who will be output first, so it is implemented in a queue.

Guess you like

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