C语言图的遍历DFS/BFS

#include <iostream>
#include <queue>
#include <cstring> 
using namespace std;
const int N = 10010; // 图的顶点最大个数
int e[N][N]; // 储存图信息的邻接矩阵 
int book[N]; // 标记顶点是否被访问 

 // 对第 n 个顶点进行深度优先遍历 
void dfs(int n, int sum) {
    if(n != 0) { // 输出格式控制 
        cout << " ";
    }
    cout << n+1; // 输出顶点信息 
    for(int i = 0; i < sum; i++) { // 对当前所有的顶点进行讨论
        // 如果顶点 i 和顶点 n 之间存在边直接相连,并且顶点 i 未被访问 
        if(e[n][i] == 1 && book[i] == 0) {  
            book[i] = 1; // 标记这个顶点已经被访问 
            dfs(i, sum); // 对这个顶点继续进行深度优先遍历 
        }
    }
}

// 对图进行广度优先遍历 
void bfs(int n) {
    queue<int> que;
    book[0] = 1; // 标记第一个顶点已经被访问
    que.push(0);
    int s;
    while(!que.empty()) {
        s = que.front(); // 获取队头元素 
        que.pop(); // 队头元素出队
        if(s != 0) { // 输出格式控制
            cout << " ";
        }
        cout << s+1; // 输出顶点信息 
        for(int i = 0; i < n; i++) {
            // 如果顶点 i 和顶点 n 之间存在边直接相连,并且顶点 i 未被访问 
            if(e[s][i] == 1 && book[i] == 0) {
                book[i] = 1; // 标记这个顶点已经被访问 
                que.push(i); // 这个顶点入队尾 
            }
        } 
    }
}

int main() {
    int n, m; // 图的顶点个数和边的条数 
    cin >> n >> m;
    int x, y; // 边的开始顶点和结束顶点 
    for(int i = 0; i < m; i++) {
        cin >> x >> y;
        e[--x][--y] = e[y][x] = 1; // 因为是无向图,所以要双向储存 
    }

    cout << "深度优先遍历结果:" << endl;
    book[0] = 1; // 标记第一个顶点已经被访问
    dfs(0, n); // 从第一个顶点开始深度优先遍历 

    memset(book, 0, sizeof(book)); // 重置访问标记 
    cout << endl << "广度优先遍历结果:" << endl;
    bfs(n);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42778026/article/details/88804954