1、邻接矩阵(vector二维数组)的DFS(递归实现)
class Graph {
public:Graph(int vertices);void addEdge(int from, int to);void DFS(int startVertex);private:int vertices;vector<vector<int>> adjMatrix;vector<bool> visited;void DFSRecursive(int vertex);
};Graph::Graph(int vertices) {this->vertices = vertices;adjMatrix.resize(vertices, vector<int>(vertices, 0));visited.assign(vertices, false);
}// 无向图
void Graph::addEdge(int from, int to) {adjMatrix[from][to] = 1;adjMatrix[to][from] = 1; // For undirected graph
}void Graph::DFS(int startVertex) {for (int i = 0; i < vertices; i++) {visited[i] = false;}// 确保每一个点都能被遍历到,包括孤立点for (int i = 0; i < vertices; i++) {DFSRecursive(startVertex)}
}void Graph::DFSRecursive(int vertex) {visited[vertex] = true;cout << "Visited vertex: " << vertex << endl;for (int i = 0; i < vertices; i++) {if (adjMatrix[vertex][i] == 1 && !visited[i]) {DFSRecursive(i);}}
}
2、邻接表(vector实现)的DFS(栈实现)
class Graph {
public:Graph(int vertices);void addEdge(int from, int to);void DFS(int startVertex);private:int vertices;vector<vector<int>> adjList;
};Graph::Graph(int vertices) {this->vertices = vertices;adjList.resize(vertices);
}// 邻接表,每行长度不一。无向图
void Graph::addEdge(int from, int to) {adjList[from].push_back(to);adjList[to].push_back(from);
}void Graph::DFS(int startVertex) {vector<bool> visited(vertices, false);stack<int> s;visited[startVertex] = true;s.push(startVertex);while (!s.empty()) {int vertex = s.top();s.pop();cout << "Visited vertex: " << vertex << endl;for (int neighbor : adjList[vertex]) {if (!visited[neighbor]) {visited[neighbor] = true;s.push(neighbor);}}}
}
3、邻接表(vector实现)的BFS(队列实现)
class Graph {
public:Graph(int vertices);void addEdge(int from, int to);void BFS(int startVertex);private:int vertices;vector<vector<int>> adjList;
};Graph::Graph(int vertices) {this->vertices = vertices;adjList.resize(vertices);
}// 无向图
void Graph::addEdge(int from, int to) {adjList[from].push_back(to);adjList[to].push_back(from);
}void Graph::BFS(int startVertex) {vector<bool> visited(vertices, false);queue<int> q;visited[startVertex] = true;q.push(startVertex);while (!q.empty()) {int vertex = q.front();q.pop();cout << "Visited vertex: " << vertex << endl;for (int neighbor : adjList[vertex]) {if (!visited[neighbor]) {visited[neighbor] = true;q.push(neighbor);}}}
}
4、链式前向星DFS和BFS
#include <iostream>
#include <vector>
#include <queue>using namespace std;const int maxn = 100000; // 假定最大顶点数struct Edge {int to, w, next; // 终点,边权,同起点的上一条边的编号
} edge[maxn]; // 边集int head[maxn]; // head[i],表示以i为起点的第一条边在边集数组的位置(编号)
int cnt = 0; // 记录边的数量
int n, m; // 顶点数和边数void init() {for (int i = 1; i <= n; i++) head[i] = -1;cnt = 0;
}// 添加新边
void add_edge(int u, int v, int w) {edge[cnt].to = v; // 终点edge[cnt].w = w; // 权值edge[cnt].next = head[u]; // 以u为起点上一条边的编号,也就是与这个边起点相同的上一条边的编号head[u] = cnt++; // 更新以u为起点上一条边的编号
}void DFS(int startVertex) {vector<bool> visited(n + 1, false);stack<int> s;s.push(startVertex);visited[startVertex] = true;while (!s.empty()) {int vertex = s.top();s.pop();cout << "Visited vertex: " << vertex << endl;for (int j = head[vertex]; j != -1; j = edge[j].next) {int neighborVertex = edge[j].to;if (!visited[neighborVertex]) {s.push(neighborVertex);visited[neighborVertex] = true;}}}
}void BFS(int startVertex) {vector<bool> visited(n + 1, false);queue<int> q;q.push(startVertex);visited[startVertex] = true;while (!q.empty()) {int vertex = q.front();q.pop();cout << "Visited vertex: " << vertex << endl;for (int j = head[vertex]; j != -1; j = edge[j].next) {int neighborVertex = edge[j].to;if (!visited[neighborVertex]) {q.push(neighborVertex);visited[neighborVertex] = true;}}}
}int main() {cin >> n >> m;int u, v, w;init(); // 初始化for (int i = 1; i <= m; i++) // 输入m条边{cin >> u >> v >> w;add_edge(u, v, w); // 加边}int startVertex;cout << "Enter the starting vertex for DFS: ";cin >> startVertex;DFS(startVertex);cout << "Enter the starting vertex for BFS: ";cin >> startVertex;BFS(startVertex);return 0;
}