The first review of the finale: graph traversal (DFS&&BFS)

I have to say that I have studied very hard in the picture here, but there are still many questions that I can’t

First of all, let's take a look at the difference between the two. What questions should we use? This is some of my own summary

https://blog.csdn.net/weixin_44067773/article/details/87471377

Then let’s take a look at the definitions of the two and how they work

BFS: Just go deep, mark where you go, then recursively go back to other points where you didn’t go, go, mark, know the end

DFS: First find the nearest nodes, then push them into the queue, then use them as the root, then find the nearest nodes, and proceed in sequence

So how to write:

BFS:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2717/pid/2107

#include<stdio.h>
#include<string.h>
int map[105][105];//Picture
int vis[105];//Mark whether the point has reached
int k;
void dfs(int i)
{     vis[i]=1;     if(i==0)         printf("%d",i);     else         printf(" %d",i);     int j;     for(j=0;j<k;j++ )     {         if(!vis[j]&&map[i][j])//This step of the loop starts from 0, first set the vis of 0 to 1, and then add it (0 does not meet the if judgment) and then Replace it with 1,1 and then mark the output 1, and then go from 1 (2,3, because 1 did not reach 2 and 3) and then loop             dfs(j);//i->j did not go through dfs     } } int main() {     int u,v;     int m,n;     while(scanf("%d",&n)!=EOF)     {         while(n--)



















        {             memset(map,0,sizeof(map));             memset(vis,0,sizeof(vis));             scanf("%d %d",&k,&m);             while(m--)             {                 scanf("% d %d",&u,&v);                 map[u][v]=1;                 map[v][u]=1;//Two-way graph, easy to withdraw and advance             }//If vis is false, execute (vis The number represented has not been scanned)                 dfs(0);                 printf("\n");         }     }     return 0; }














DFS: adjacency matrix implementation method:

http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2717/pid/2141

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int tu[500][500];
int vis[500];
int ans[500];
int k,num;
void bfs(int t)
{
    int v,i;
    queue<int>Q;
    ans[num++]=t;
    vis[t]=1;
    Q.push(t);
    while(!Q.empty())
    {
        v=Q.front();
        Q.pop();
        for(i=0;i<=k-1;i++)
        {
            if(!vis[i]&&tu[v][i])
            {
                vis[i]=1;
                ans[num++]=i;
                Q.push(i);
            }
        }
    }
}
int main()
{
    int n,m,t,u,v,i;
    scanf("%d",&n);
    while(n--)
    {
        memset(tu,0,sizeof(tu));
        memset(vis,0,sizeof(vis));
        scanf("%d %d %d",&k,&m,&t);
        for(i=0;i<=m-1;i++)
        {
            scanf("%d %d",&u,&v);
            tu[u][v]=tu[v][u]=1;
        }
        bfs(t);
        for(i=0;i<=num-1;i++)
        {
            if(i==num-1)
                printf("%d\n",ans[i]);
            else
                printf("%d ",ans[i]);
        }
    }
    return 0;
}
Manual debugging: We first push 0 (at the beginning), find the n nodes closest to 0, mark, find the n2 nodes closest to the n nodes, mark, and then push them forward

The adjacency list can be imagined as erecting the array and connecting the corresponding nodes to each end

The following is the adjacency list method: suitable for problems with a large amount of data:

http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2717/pid/2142

Reference Code:

https://blog.csdn.net/axuhongbo/article/details/60356923

 

 

 

Guess you like

Origin blog.csdn.net/weixin_44067773/article/details/87879456