DFS traversal graphs - graph theory foundation

1. Analysis:

 First, FIG introduce what is (Graph): vernacular simply put, is the straight line of FIG small dot (vertex) and a number of connecting these dots (side) composed of, as shown:

Now we have to do is for all the vertices of this graph traversal again, that is all one visit. Here we use a depth-first search to traverse this figure, we get the following results:

FIG traversing the access sequence is as follows:

Each vertex above the red figures represent the apex is the first of several to be accessed, we called time stamp

2. Algorithm Design 

       The main idea of ​​the depth-first search is this: First, a vertex is not visited as the starting vertex, go unvisited vertex along the side of the current vertex: When there is no unvisited vertex, then return to the previous vertex, continue to test access to other vertices until all vertices have been visited.

       Clearly, along a depth-first search is a traversal of FIG branch until the end, and then back again the same manner traversing along one another, until all vertices have been visited so far. We still use a two-dimensional array to store the edge of this side of the figure:

FIG represented in two-dimensional array row i and column j is determined whether j is connected with i between. 1 expressed an edge, ∞ indicates no edge, to himself that he is unreachable, we initialized to 0.

The next step is to traverse the application dfs:

void dfs(int cur)
{
    cout << cur;
    sum++;
    if(sum==n)
    {
        return;
    }
    for(int i=1;i<=n;i++)
    {
        if(edge[cur][i]==1 && marked[i]==0)
        {
            marked[i]=1;
            dfs(i);
        }
    }
    return;
}

 

3. Source Code 

 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int N=111;
const int INF=999999999;
int marked[N];//用来标记这个点有没有被遍历
int edge[N][N];//用来存储边
int sum;//记录遍历顶点的个数
int n;//点的个数
int m;//边的条数

void dfs(int cur)
{
    cout << cur;
    sum++;
    if(sum==n)
    {
        return;
    }
    for(int i=1;i<=n;i++)
    {
        if(edge[cur][i]==1 && marked[i]==0)
        {
            marked[i]=1;
            dfs(i);
        }
    }
    return;
}

int main()
{
    int point_a;
    int point_b;
    cout << "请输入顶点个数和边的条数:" << endl;
    cin >> n >> m;
    for (int i=1;i<=n;i++)//初始化
    {
        for (int j=1;j<=n;j++)
        {
            if(i==j)//自己不可达自己
            {
                edge[i][j]=0; //设置为0
            }
            else
            {
                edge[i][j]=INF; //不可达,设置为∞
            }
        }
    }
    cout << "请输入边的两点:" << endl;
    for(int i=1;i<=m;i++)
    {
        cin >> point_a >> point_b ;
        edge[point_a][point_b]=1;
        edge[point_b][point_a]=1;
    }
    cout << "遍历顺序是: "<< endl;
    marked[1]=1;
    dfs(1);
    return 0;
}

 

4. Test results 

 

Published 57 original articles · won praise 9 · views 3617

Guess you like

Origin blog.csdn.net/Jayphone17/article/details/102840612