-
邻接矩阵的DFS和BFS
-
#include<iostream> #include<vector> #include<queue> #define MAXVEX 100 using namespace std; struct MGraph { int numV; int numE; char vexs[MAXVEX]; int arc[MAXVEX][MAXVEX]; }; void DFS(MGraph* G, int index, vector<bool>&flag) { int j; flag[index] = true; cout << G->vexs[index] << " "; for (j = 0; j < G->numV; j++) { if (G->arc[index][j] == 1 && !flag[j]) { DFS(G, j, flag); } } } void BFS(MGraph* G) { int i, j; queue<int>q; vector<bool>flag(G->numV); for (i = 0; i < G->numV; i++) { if (!flag[i]) { flag[i] = true; q.push(i); while (!q.empty()) { int index = q.front(); cout << G->vexs[index] << " "; for (j = 0; j < G->numV; j++) { if (G->arc[index][j]==1 && !flag[j]) { q.push(j); flag[j] = true; } } q.pop(); } } } } int main() { MGraph* G = new MGraph; cout << "请输入图中的点数:" << endl; cin >> G->numV; cout << "请输入图中的边数:" << endl; cin >> G->numE; cout << "请输入顶点信息:" << endl; for (int i = 0; i < G->numV; i++) { cin >> G->vexs[i]; } cout << "请输入边的信息:" << endl; for (int i = 0; i < G->numE; i++) { int e1, e2, weight; //无向图 cin >> e1 >> e2; G->arc[e1 - 1][e2 - 1] = 1; G->arc[e2 - 1][e1 - 1] = 1; } //DFS cout << "DFS遍历结果:" << endl; vector<bool>flag(G->numV); for (int i = 0; i < G->numV; i++) { if (!flag[i]) DFS(G, i, flag); } cout << endl; //BFS cout << "BFS遍历结果:" << endl; BFS(G); cout << endl; return 0; }
-
邻接表的DFS和BFS
#include<iostream> #include<vector> #include<queue> using namespace std; struct EdgeNode { int adjvex; struct EdgeNode* next; }; struct VexNode { char data; EdgeNode* firstedge; }; struct GraphAdjList { int numVex; int numEdge; VexNode adjList[100]; }; void DFS(GraphAdjList* G, vector<bool>&flag,int index) { flag[index] = true; cout << G->adjList[index].data << " "; EdgeNode* p = G->adjList[index].firstedge; while (p) { if (!flag[p->adjvex]) { DFS(G, flag, p->adjvex); } p = p->next; } } void BFS(GraphAdjList* G) { queue<int>q; vector<bool>flag(G->numVex); for (int i = 0; i < G->numVex; i++) { if (!flag[i]) { q.push(i); flag[i] = true; while (!q.empty()) { int index = q.front(); q.pop(); cout << G->adjList[index].data << " "; EdgeNode* p = G->adjList[index].firstedge; while (p) { if (!flag[p->adjvex]) { q.push(p->adjvex); flag[p->adjvex] = true; } p = p->next; } } } } } int main() { GraphAdjList* G = new GraphAdjList(); EdgeNode* e = nullptr; cout << "请输入邻接表中的点数和边数:" << endl; cin >> G->numVex >> G->numEdge; cout << "请输入邻接表中点的信息:" << endl; for (int i = 0; i < G->numVex; i++) cin >> G->adjList[i].data; cout << "请输入邻接表中边的信息:" << endl; for (int i = 0; i < G->numEdge; i++) { //从0开始 int e1, e2; cin >> e1 >> e2; e = new EdgeNode(); e->adjvex = e2; e->next = G->adjList[e1].firstedge; G->adjList[e1].firstedge = e; e = new EdgeNode(); e->adjvex = e1; e->next = G->adjList[e2].firstedge; G->adjList[e2].firstedge = e; } vector<bool>flag(G->numVex); //DFS遍历图 cout << "DFS遍历结果:"; for (int i = 0; i < G->numVex; i++) { if (!flag[i]) { DFS(G, flag,i); } } cout << endl; //BFS遍历图 cout << "BFS遍历结果:"; BFS(G); cout << endl; return 0; }
图的遍历算法
最新推荐文章于 2024-11-02 09:55:19 发布