深度优先搜索树与广度优先搜索树
Posted yang901112
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了深度优先搜索树与广度优先搜索树相关的知识,希望对你有一定的参考价值。
DFS使用c++中的stack,BFS使用c++中的queue
1 #include <iostream> 2 #define MAX_VERTS 20 3 #include <stack> 4 #include <queue> 5 6 using namespace std; 7 8 class Vertex 9 { 10 public: 11 Vertex(char lab) 12 { 13 Label = lab; 14 wasVisited = false; 15 } 16 17 public: 18 bool wasVisited; 19 char Label; 20 }; 21 22 class Graph 23 { 24 public: 25 Graph(); 26 ~Graph(); 27 void addVertex(char lab); 28 void addEdge(int start, int End); 29 void printMatrix(); 30 void showVertex(int v); 31 void DFS(); 32 void BFS(); 33 private: 34 Vertex *vertexList[MAX_VERTS]; 35 int nVerts; 36 int adjMat[MAX_VERTS][MAX_VERTS]; 37 int getAdjUnivisitedVertex(int v); 38 }; 39 40 void Graph::BFS() 41 { 42 queue<int> gQueue; 43 vertexList[0]->wasVisited = true; 44 showVertex(0); 45 gQueue.push(0); 46 int vert1, vert2; 47 while(gQueue.size()>0) 48 { 49 vert1 = gQueue.front(); 50 gQueue.pop(); 51 vert2 = getAdjUnivisitedVertex(vert1); 52 while(vert2 != -1) 53 { 54 vertexList[vert2]->wasVisited = true; 55 showVertex(vert2); 56 gQueue.push(vert2); 57 vert2 = getAdjUnivisitedVertex(vert1); 58 } 59 } 60 cout << endl; 61 for(int j=0; j<nVerts; j++) 62 vertexList[j]->wasVisited = false; 63 } 64 65 void Graph::DFS() 66 { 67 stack<int> gStack; 68 vertexList[0]->wasVisited = true; 69 showVertex(0); 70 gStack.push(0); 71 int v; 72 while(gStack.size()>0) 73 { 74 v = getAdjUnivisitedVertex(gStack.top()); 75 if(v == -1) 76 gStack.pop(); 77 else 78 { 79 vertexList[v]->wasVisited = true; 80 showVertex(v); 81 gStack.push(v); 82 } 83 } 84 cout << endl; 85 for(int j=0; j<nVerts; j++) 86 vertexList[j]->wasVisited = false; 87 } 88 89 int Graph::getAdjUnivisitedVertex(int v) 90 { 91 for(int j=0; j<nVerts; j++) 92 if((adjMat[v][j]==1) && (vertexList[j]->wasVisited==false)) 93 return j; 94 return -1; 95 } 96 97 void Graph::showVertex(int v) 98 { 99 cout << vertexList[v]->Label << " "; 100 } 101 102 Graph::Graph() 103 { 104 nVerts = 0; 105 for(int i=0; i<MAX_VERTS; i++) 106 for(int j=0; j<MAX_VERTS; j++) 107 adjMat[i][j] = 0; 108 } 109 110 void Graph::addVertex(char lab) 111 { 112 vertexList[nVerts++] = new Vertex(lab); 113 } 114 115 void Graph::addEdge(int start, int End) 116 { 117 adjMat[start][End] = 1; 118 adjMat[End][start] = 1; 119 } 120 121 void Graph::printMatrix() 122 { 123 for(int i=0; i<nVerts; i++) 124 { 125 for(int j=0; j<nVerts; j++) 126 cout << adjMat[i][j] << " "; 127 cout << endl; 128 } 129 } 130 131 Graph::~Graph() 132 { 133 for(int i=0; i<nVerts; i++) 134 delete vertexList[i]; 135 } 136 137 int main() 138 { 139 140 Graph g; 141 g.addVertex(‘A‘); 142 g.addVertex(‘B‘); 143 g.addVertex(‘C‘); 144 g.addVertex(‘D‘); 145 g.addVertex(‘E‘); 146 147 g.addEdge(0,1); // A-B 148 g.addEdge(0,3); // A-D 149 g.addEdge(1,0); // B-A 150 g.addEdge(1,4); // B-E 151 g.addEdge(2,4); // C-E 152 g.addEdge(3,0); // D-A 153 g.addEdge(3,4); // D-E 154 g.addEdge(4,1); 155 g.addEdge(4,2); 156 g.addEdge(4,3); 157 g.printMatrix(); 158 cout << endl; 159 160 cout << "深度优先搜索:" ; 161 g.DFS(); 162 cout << endl; 163 cout << "广度优先搜索:" ; 164 g.BFS(); 165 return 0; 166 }
以上是关于深度优先搜索树与广度优先搜索树的主要内容,如果未能解决你的问题,请参考以下文章