/** * File: graph_dfs.cpp * Created Time: 2023-03-02 * Author: Krahets (krahets@163.com) */ #include "../utils/common.hpp" #include "./graph_adjacency_list.cpp" /* 深度优先遍历辅助函数 */ void dfs(GraphAdjList &graph, unordered_set &visited, vector &res, Vertex *vet) { res.push_back(vet); // 记录访问顶点 visited.emplace(vet); // 标记该顶点已被访问 // 遍历该顶点的所有邻接顶点 for (Vertex *adjVet : graph.adjList[vet]) { if (visited.count(adjVet)) continue; // 跳过已被访问的顶点 // 递归访问邻接顶点 dfs(graph, visited, res, adjVet); } } /* 深度优先遍历 */ // 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点 vector graphDFS(GraphAdjList &graph, Vertex *startVet) { // 顶点遍历序列 vector res; // 哈希表,用于记录已被访问过的顶点 unordered_set visited; dfs(graph, visited, res, startVet); return res; } /* Driver Code */ int main() { /* 初始化无向图 */ vector v = valsToVets(vector{0, 1, 2, 3, 4, 5, 6}); vector> edges = {{v[0], v[1]}, {v[0], v[3]}, {v[1], v[2]}, {v[2], v[5]}, {v[4], v[5]}, {v[5], v[6]}}; GraphAdjList graph(edges); cout << "\n初始化后,图为" << endl; graph.print(); /* 深度优先遍历 */ vector res = graphDFS(graph, v[0]); cout << "\n深度优先遍历(DFS)顶点序列为" << endl; printVector(vetsToVals(res)); // 释放内存 for (Vertex *vet : v) { delete vet; } return 0; }