• 图的DFS与BFS


    1.DFS

    深度优先搜索在搜索过程中访问某个顶点后,需要递归地访问此顶点的所有未访问过的相邻顶点。

    (1)递归实现

    #include <iostream>
    #define N 5
    using namespace std;
    int maze[N][N] = {
        { 0, 1, 1, 0, 0 },
        { 0, 0, 1, 0, 1 },
        { 0, 0, 1, 0, 0 },
        { 1, 1, 0, 0, 1 },
        { 0, 0, 1, 0, 0 }
    };
    int visited[N + 1] = {0};
    void DFS(int start)
    {
        visited[start] = 1;
      cout<<start<<' ';
    for (int i = 1; i <= N; i++) { if (!visited[i] && maze[start - 1][i - 1] == 1) DFS(i); } } int main() { for (int i = 1; i <= N; i++) { if (visited[i] == 1) continue; DFS(i); } return 0; }

    (2)也可以用非递归实现,借助于栈的数据结构

    #include <iostream>
    #include <stack>
    #define N 5
    using namespace std;
    int maze[N][N] = {
        { 0, 1, 1, 0, 0 },
        { 0, 0, 1, 0, 1 },
        { 0, 0, 1, 0, 0 },
        { 1, 1, 0, 0, 1 },
        { 0, 0, 1, 0, 0 }
    };
    int visited[N + 1] = {0};
    void DFS(int start)
    {
        stack<int> s;
        s.push(start);
        visited[start] = 1;
        while (!s.empty())
        {
            int v = s.top();
            cout<<v<<' ';
            s.pop();
            for (int i = 1; i <= N; i++)
            {
                if (maze[v - 1][i - 1] == 1 && !visited[i])
                {
                    visited[i] = 1;
                    s.push(i);
                }
            }
        }
    }
    int main()
    {
        for (int i = 1; i <= N; i++)
        {
            if (visited[i] == 1)
                continue;
            DFS(i);
        }
        return 0;
    }

    其中visited代表着每个节点是否被访问,一般定义成全局的数组。也有将visited定义为局部的数组,作为函数参数引用传递的。

    2、BFS

    广度优先搜索在进一步遍历图中顶点之前,先访问当前顶点的所有邻接结点。

    #include <iostream>
    #include <queue>
    #define N 5
    using namespace std;
    int maze[N][N] = {
        { 0, 1, 1, 0, 0 },
        { 0, 0, 1, 1, 0 },
        { 0, 1, 1, 1, 0 },
        { 1, 0, 0, 0, 0 },
        { 0, 0, 1, 1, 0 }
    };
    int visited[N + 1] = { 0, };
    void BFS(int start)
    {
        queue<int> Q;
        Q.push(start);
        visited[start] = 1;
        while (!Q.empty())
        {
            int front = Q.front();
            cout << front << " ";
            Q.pop();
            for (int i = 1; i <= N; i++)
            {
                if (!visited[i] && maze[front - 1][i - 1] == 1)
                {
                    visited[i] = 1;
                    Q.push(i);
                }
            }
        }
    }
    int main()
    {
        for (int i = 1; i <= N; i++)
        {
            if (visited[i] == 1)
                continue;
            BFS(i);
        }
        return 0;
    }
  • 相关阅读:
    hdu5269 ZYB loves Xor I
    UVA 11181 Probability|Given
    UVA 10277 Boastin' Red Socks
    CodeForces 566B Replicating Processes
    UVA 10714 Ants
    CodeForces 567B Berland National Library hdu-5477 A Sweet Journey
    UVA 10759 Dice Throwing
    UVA 11027
    「CF241E」Flights
    「CF891C」Envy
  • 原文地址:https://www.cnblogs.com/mini-coconut/p/9484503.html
Copyright © 2020-2023  润新知