DFS on Algorithms

1. DFS is a search algorithm that goes deep into every possible branch path as far as it can go, and each node can only be visited once.
2. Basic idea: starting from a vertex a in the graph - visiting vertex a - starting from the unvisited adjacent points of a in turn, and traversing the graph in depth first, until the vertices in the graph that have a path with a are visited - If there are still unvisited vertices in the graph at this time, start from an unvisited vertex and perform depth-first traversal again until all vertices in the graph have been visited.
3. Give an example!
Output the full permutation of 1 to n ( full permutation function usage )

For example, the full permutation of 1 to 3 is 123, 132, 213, 231, 312, 321

If we think about it, we will find that each of us is arranged in order. The first place is 1, then the second place is 2, and the third place is 3. This completes the first arrangement, and the second place. For this permutation, we replace the 2 in the second position with a 3, then there is only 2 left in the third position, thus completing the second permutation, and so on.

It can be practical, for example, imagine this problem as putting a ball
in How to put a ball in a box?

for(int i = 0 ; i <= n ; ++i)
{
    a[step] = i;
}
//这一步表示将第i号球放到第step个盒子里

If the ball was spared, use an array of tags to mark which balls were spared.

for(int i = 1;i <= n; ++i)
{
    if(book[i] = 0)//book[i] = 0表示i号球没用被放过,还在手上
    {
        a[step] = i;//将i号球放到第step个盒子里
        book[i] = 1;//将book[i]变为1,表示第i个球已经被放过
    }
}

In this way, we have processed the step box, and then we need to process the next (ie step + 1) box, then we can think of recursion, that is, encapsulating the operation just now into a function named dfs The
code is as follows:

void dis(int step)
{
    for(int i = 1; i <= n ; ++i)
    {
        //判断球是否被放过
        if(book[i] == 0)//说明i号球没有被放过
        {
            a[step] = i;将第i号球放到第step个盒子里
            book[i] = 1;
        }
    }
    return;
}

After writing this as a function, you can call it every time, that is to say, the method to process the step+1th box is dfs(step + 1)

void dis(int step)
{
    for(int i = 1; i <= n ; ++i)
    {
        //判断球是否被放过
        if(book[i] == 0)//说明i号球没有被放过
        {
            a[step] = i;将第i号球放到第step个盒子里
            book[i] = 1;
            dfs(step + 1);//函数递归调用
            book[i] = 0;//这一步非常重要。必须将球收回才能进行下一步
        }
    }
    return;
}

There is one last question, how to judge the end of a permutation?

if(step == n + 1)//如果站在第n + 1个盒子前,表示前n个盒子已经放好球了
{
    for(int i = 1; i <= n ; ++i)//输出一种排列
        cout << a[i] 
        cout << endl;
        return ;
}

Here is the overall code:

#include<iostream>
using namespace std;
int a[10];
int flag[10];
int n;
void dfs(int step)
{
    if(step == n + 1)
    {
        for(int i = 1 ; i <= n;i++ )
            cout << a[i] << " " ;
        cout << endl;
        return;
    }
    for(int i = 1 ; i <= n ; i++)
    {
        if(flag[i] == 0)
        {
        a[step] = i;
        flag[i] = 1;
            dfs(step + 1);
            flag[i] = 0;
        }

    }
    return;
}
int main()
{
    cin >> n;
    dfs(1);

    return 0;
}

Basic model of dfs:

void dfs(int step)
{
判断边界
    {
        继续下一步dfs(step + 1);
    }
}

Guess you like

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