數據結構基礎(21) --DFS與BFS

DFS

    從圖中某個頂點V0 出發,訪問此頂點,然後依次從V0的各個未被訪問的鄰接點出發深度優先搜索遍歷圖,直至圖中所有和V0有路徑相通的頂點都被訪問到(使用堆棧).

 

  1. //使用鄰接矩陣存儲的無向圖的深度優先遍歷  
  2. template <typename Type>  
  3. void Graph<Type>::DFS()  
  4. {  
  5.     stack<int> iStack;  
  6.   
  7.     showVertex(0);  
  8.     vertexList[0]->wasVisted = true;  
  9.     iStack.push(0);  
  10.   
  11.     while (!iStack.empty())  
  12.     {  
  13.         int top = iStack.top();  
  14.         int v = getAdjUnvisitedVertex(top);  
  15.         if (v == -1)  
  16.         {  
  17.             iStack.pop();  
  18.         }  
  19.         else  
  20.         {  
  21.             showVertex(v);  
  22.             vertexList[v]->wasVisted = true;  
  23.             iStack.push(v);  
  24.         }  
  25.     }  
  26.   
  27.     //使其還可以再深/廣度優先搜索  
  28.     for (int i = 0; i < nVerts; ++i)  
  29.         vertexList[i]->wasVisted = false;  
  30. }  

BFS

   從圖中的某個頂點V0出發,並在訪問此頂點之後依次訪問V0的所有未被訪問過的鄰接點,之後按這些頂點被訪問的先後次序依次訪問它們的鄰接點,直至圖中所有和V0有路徑相通的頂點都被訪問到.

  若此時圖中尚有頂點未被訪問,則另選圖中一個未曾被訪問的頂點作起始點,重複上述過程,直至圖中所有頂點都被訪問到爲止(使用隊列)。

 

  1. //使用鄰接矩陣存儲的無向圖的廣度優先遍歷  
  2. template <typename Type>  
  3. void Graph<Type>::BFS()  
  4. {  
  5.     queue<int> iQueue;  
  6.   
  7.     showVertex(0);  
  8.     vertexList[0]->wasVisted = true;  
  9.     iQueue.push(0);  
  10.   
  11.     while (!iQueue.empty())  
  12.     {  
  13.         int front = iQueue.front();  
  14.         iQueue.pop();  
  15.         int v = getAdjUnvisitedVertex(front);  
  16.         while (v != -1)  
  17.         {  
  18.             showVertex(v);  
  19.             vertexList[v]->wasVisted = true;  
  20.             iQueue.push(v);  
  21.             v = getAdjUnvisitedVertex(front);  
  22.         }  
  23.     }  
  24.   
  25.     for (int i = 0; i < nVerts; ++i)  
  26.         vertexList[i]->wasVisted = false;  
  27. }  

-完整代碼

  1. const int MAX_VERTS = 20;  
  2. //頂點  
  3. template <typename Type>  
  4. class Vertex  
  5. {  
  6. public:  
  7.     Vertex(const Type &_node = Type())  
  8.         : node(_node), wasVisted(false) {}  
  9.   
  10. public:  
  11.     bool wasVisted; //增加一個訪問位  
  12.     Type node;  
  13. };  
  14. //圖  
  15. template <typename Type>  
  16. class Graph  
  17. {  
  18. public:  
  19.     Graph();  
  20.     ~Graph();  
  21.   
  22.     void addVertex(const Type &vertex);  
  23.     void addEdge(int start, int end);  
  24.     void printMatrix();  
  25.     void showVertex(int v);  
  26.     void DFS();  
  27.     void BFS();  
  28.   
  29. private:  
  30.     int getAdjUnvisitedVertex(int v);  
  31.   
  32. private:  
  33.     Vertex<Type>* vertexList[MAX_VERTS];  
  34.     int nVerts;  
  35.     int adjMatrix[MAX_VERTS][MAX_VERTS];  
  36. };  
  37. template <typename Type>  
  38. void Graph<Type>::DFS()  
  39. {  
  40.     stack<int> iStack;  
  41.   
  42.     showVertex(0);  
  43.     vertexList[0]->wasVisted = true;  
  44.     iStack.push(0);  
  45.   
  46.     while (!iStack.empty())  
  47.     {  
  48.         int top = iStack.top();  
  49.         int v = getAdjUnvisitedVertex(top);  
  50.         if (v == -1)  
  51.         {  
  52.             iStack.pop();  
  53.         }  
  54.         else  
  55.         {  
  56.             showVertex(v);  
  57.             vertexList[v]->wasVisted = true;  
  58.             iStack.push(v);  
  59.         }  
  60.     }  
  61.   
  62.     //使其還可以再深度優先搜索  
  63.     for (int i = 0; i < nVerts; ++i)  
  64.         vertexList[i]->wasVisted = false;  
  65. }  
  66.   
  67. template <typename Type>  
  68. void Graph<Type>::BFS()  
  69. {  
  70.     queue<int> iQueue;  
  71.   
  72.     showVertex(0);  
  73.     vertexList[0]->wasVisted = true;  
  74.     iQueue.push(0);  
  75.   
  76.     while (!iQueue.empty())  
  77.     {  
  78.         int front = iQueue.front();  
  79.         iQueue.pop();  
  80.         int v = getAdjUnvisitedVertex(front);  
  81.         while (v != -1)  
  82.         {  
  83.             showVertex(v);  
  84.             vertexList[v]->wasVisted = true;  
  85.             iQueue.push(v);  
  86.             v = getAdjUnvisitedVertex(front);  
  87.         }  
  88.     }  
  89.   
  90.     for (int i = 0; i < nVerts; ++i)  
  91.         vertexList[i]->wasVisted = false;  
  92. }  
  93. //獲取下一個尚未訪問的連通節點  
  94. template <typename Type>  
  95. int Graph<Type>::getAdjUnvisitedVertex(int v)  
  96. {  
  97.     for (int j = 0; j < nVerts; ++j)  
  98.     {  
  99.         //首先是鄰接的, 並且是未訪問過的  
  100.         if ((adjMatrix[v][j] == 1) &&  
  101.                 (vertexList[j]->wasVisted == false))  
  102.             return j;  
  103.     }  
  104.     return -1;  
  105. }  
  106. //打印節點信息  
  107. template <typename Type>  
  108. void Graph<Type>::showVertex(int v)  
  109. {  
  110.     cout << vertexList[v]->node << ' ';  
  111. }  
  112.   
  113. template <typename Type>  
  114. Graph<Type>::Graph():nVerts(0)  
  115. {  
  116.     for (int i = 0; i < MAX_VERTS; ++i)  
  117.         for (int j = 0; j < MAX_VERTS; ++j)  
  118.             adjMatrix[i][j] = 0;  
  119. }  
  120. template <typename Type>  
  121. Graph<Type>::~Graph()  
  122. {  
  123.     for (int i = 0; i < nVerts; ++i)  
  124.         delete vertexList[i];  
  125. }  
  126. template <typename Type>  
  127. void Graph<Type>::addVertex(const Type &vertex)  
  128. {  
  129.     vertexList[nVerts ++] = new Vertex<Type>(vertex);  
  130. }  
  131. template <typename Type>  
  132. void Graph<Type>::addEdge(int start, int end)  
  133. {  
  134.     //無向圖  
  135.     adjMatrix[start][end] = 1;  
  136.     adjMatrix[end][start] = 1;  
  137. }  
  138. template <typename Type>  
  139. void Graph<Type>::printMatrix()  
  140. {  
  141.     for (int i = 0; i < nVerts; ++i)  
  142.     {  
  143.         for (int j = 0; j < nVerts; ++j)  
  144.             cout << adjMatrix[i][j] << ' ';  
  145.         cout << endl;  
  146.     }  
  147. }  
  148.   
  149. //測試代碼  
  150. int main()  
  151. {  
  152.     Graph<char> g;  
  153.     g.addVertex('A');   //0  
  154.     g.addVertex('B');   //1  
  155.     g.addVertex('C');   //2  
  156.     g.addVertex('D');   //3  
  157.     g.addVertex('E');   //4  
  158.   
  159.     g.addEdge(0, 1);    //A-B  
  160.     g.addEdge(0, 3);    //A-D  
  161.     g.addEdge(1, 0);    //B-A  
  162.     g.addEdge(1, 4);    //B-E  
  163.     g.addEdge(2, 4);    //C-E  
  164.     g.addEdge(3, 0);    //D-A  
  165.     g.addEdge(3, 4);    //D-E  
  166.     g.addEdge(4, 1);    //E-B  
  167.     g.addEdge(4, 2);    //E-C  
  168.     g.addEdge(4, 3);    //E-D  
  169.   
  170.     g.printMatrix();  
  171.   
  172.     cout << "DFS: ";  
  173.     g.DFS();  
  174.     cout << "\nBFS: ";  
  175.     g.BFS();  
  176.     return 0;  


原文地址:http://blog.csdn.net/zjf280441589/article/details/42710977

發佈了62 篇原創文章 · 獲贊 16 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章