数据结构基础(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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值