鱼C论坛

 找回密码
 立即注册
查看: 2752|回复: 8

[技术交流] Hello BFS, Hello DFS!

[复制链接]
发表于 2014-2-7 13:38:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 andalousie 于 2014-2-25 10:01 编辑

1.png


Graph_search.cpp
  1. #include <iostream>
  2. #include <ctime>
  3. #include "graph.h"
  4. using namespace std;

  5. int main(){   
  6.         /** Creates a graph with 12 vertices */
  7.     Graph g1(12);

  8.     /** Adds edges to the graph */
  9.     g1.addEdge(1, 2); g1.addEdge(1, 3);
  10.     g1.addEdge(2, 4); g1.addEdge(3, 4);
  11.     g1.addEdge(3, 6); g1.addEdge(4 ,7);
  12.     g1.addEdge(5, 6); g1.addEdge(5, 7);
  13.     clock_t t1;
  14.     t1 = clock();

  15.     /** Explores all vertices findable from vertex 1 */
  16.     g1.BFS(1, 3);
  17.     float diff = (double)(clock() - t1)/CLOCKS_PER_SEC ;
  18.     std::cout << std::endl << "The time taken for Breadth first search: "<< diff << std::endl;
  19.    
  20.     clock_t t2;
  21.     t2 = clock();

  22.     /** Explores all vertices findable from vertex 1 */
  23.     g1.DFS(1, 3);
  24.     float diff2 = (double)(clock() - t2)/CLOCKS_PER_SEC ;
  25.     std::cout << std::endl << "The time taken for Breadth first search: "<< diff2 << std::endl;
  26. }
复制代码
graph.h
  1. #if !defined (GRAPH_H)
  2. #define GRAPH_H

  3. /************************************************************

  4. Class Graph represents a Graph [V,E] having vertices V and
  5. edges E.

  6. ************************************************************/
  7. class Graph {
  8.     private:
  9.         int    _n;  /// n is the number of vertices in the graph
  10.         int ** _A;  /// A stores the edges between two vertices
  11.     public:
  12.         Graph(int size = 2);
  13.         ~Graph();
  14.         bool isConnected(int, int);
  15.         void addEdge(int x, int y);
  16.         void BFS(int, int);
  17.         void DFS(int, int);
  18. };

  19. #endif
复制代码

graph.cc
  1. #include <iostream>
  2. #include "graph.h"
  3. #include "queue.h"
  4. #include "stack.h"

  5. Graph::Graph(int size) {
  6.     int i, j;
  7.     if (size < 2) _n = 2;
  8.     else _n = size;
  9.     _A = new int*[_n];
  10.     for (i = 0; i < _n; ++i)
  11.         _A[i] = new int[_n];
  12.     for (i = 0; i < _n; ++i)
  13.         for (j = 0; j < _n; ++j)
  14.             _A[i][j] = 0;
  15. }

  16. Graph::~Graph() {
  17.     for (int i = 0; i < _n; ++i)
  18.     delete [] _A[i];
  19.     delete [] _A;
  20. }

  21. /******************************************************
  22. Checks if two given vertices are connected by an edge
  23. @param u vertex
  24. @param v vertex
  25. @return true if connected false if not connected
  26. ******************************************************/
  27. bool Graph::isConnected(int x, int y) {
  28.     return (_A[x-1][y-1] == 1);
  29. }

  30. /*****************************************************
  31. adds an edge E to the graph G.
  32. @param u vertex
  33. @param v vertex
  34. *****************************************************/
  35. void Graph::addEdge(int x, int y) {
  36.     _A[x-1][y-1] = _A[y-1][x-1] = 1;
  37. }

  38. /*****************************************************
  39. performs Breadth First Search
  40. @param s initial vertex
  41. *****************************************************/
  42. void Graph::BFS(int s, int required) {
  43.     Queue Q;

  44.     /** Keeps track of explored vertices */
  45.     bool *explored = new bool[_n+1];

  46.     /** Initailized all vertices as unexplored */
  47.     for (int i = 1; i <= _n; ++i)
  48.         explored[i] = false;

  49.     /** Push initial vertex to the queue */
  50.     Q.enqueue(s);
  51.     explored[s] = true; /** mark it as explored */
  52.     std::cout << "Breadth first Search starting from vertex ";
  53.     std::cout << s << " : " << std::endl;

  54.     /** Unless the queue is empty */
  55.     while (!Q.isEmpty()) {
  56.         /** Pop the vertex from the queue */
  57.         int v = Q.dequeue();

  58.         if(v == required) break;
  59.                 /** display the explored vertices */
  60.         std::cout << v << " ";

  61.         /** From the explored vertex v try to explore all the
  62.         connected vertices */
  63.         for (int w = 1; w <= _n; ++w)

  64.             /** Explores the vertex w if it is connected to v
  65.             and and if it is unexplored */
  66.             if (isConnected(v, w) && !explored[w]) {
  67.                 /** adds the vertex w to the queue */
  68.                 Q.enqueue(w);
  69.                 /** marks the vertex w as visited */
  70.                 explored[w] = true;
  71.             }
  72.     }
  73.     std::cout << std::endl;
  74.     delete [] explored;
  75. }

  76. /*****************************************************
  77. performs Depth First Search
  78. @param s initial vertex
  79. *****************************************************/
  80. void Graph::DFS(int x, int required){
  81.     IStack s;
  82.     bool *visited = new bool[_n+1];
  83.     int i;
  84.     for(i = 0; i <= _n; i++)
  85.         visited[i] = false;
  86.     s.push(x);
  87.     visited[x] = true;
  88.     std::cout << "Depth first Search starting from vertex ";
  89.     std::cout << x << " : " << std::endl;
  90.     while(!s.isEmpty()){
  91.         int k = s.pop();
  92.         if(k == required) break;
  93.         std::cout << k << " ";
  94.         for (i = _n; i >= 0 ; --i)
  95.             if (isConnected(k, i) && !visited[i]) {
  96.                 s.push(i);
  97.                 visited[i] = true;
  98.             }
  99.     }
  100.     std::cout << std::endl;
  101.     delete [] visited;
  102. }
复制代码

stack.h
  1. #if !defined (STACK_H)
  2. #define STACK_H

  3. const int maxStack=16;

  4. class IStack
  5. {
  6. public:
  7.         IStack():_top(0){}
  8.         void push(int i);
  9.         int pop();
  10.         bool isFull () const { return _top >= maxStack; }
  11.         bool isEmpty () const { return _top == 0; }
  12. private:
  13.         int _arr [maxStack];
  14.         int _top;
  15. };

  16. #endif
复制代码
stack.cc
  1. #include "stack.h"
  2. #include <cassert>
  3. #include <iostream>

  4. void IStack::push (int i)
  5. {
  6.         assert (_top < maxStack);
  7.         _arr [_top] = i;
  8.         ++_top;
  9. }

  10. int IStack::pop ()
  11. {
  12.         assert (_top > 0);
  13.         --_top;
  14.         return _arr [_top];
  15. }
复制代码
queue.h
  1. #if !defined (QUEUE_H)
  2. #define QUEUE_H

  3. const int maxPuts=16;

  4. class Queue
  5. {
  6. public:
  7.         Queue();
  8.         int  dequeue();
  9.         void enqueue(int x);
  10.         bool isEmpty() { return _putIdx == _getIdx; }
  11. private:
  12.         int _arr [maxPuts];
  13.         int _putIdx;
  14.         int _getIdx;
  15. };

  16. #endif
复制代码
queue.cc
  1. #include <cassert>
  2. #include "queue.h"

  3. Queue::Queue()
  4. : _putIdx(0),
  5.   _getIdx(0)
  6. {}

  7. int Queue::dequeue()
  8. {
  9.         assert (_getIdx<_putIdx);
  10.         ++_getIdx;
  11.         return _arr [(_getIdx-1) % maxPuts];
  12. }

  13. void Queue::enqueue(int x)
  14. {
  15.         assert (_putIdx<maxPuts+_getIdx);
  16.         _arr [_putIdx % maxPuts]=x;
  17.         ++_putIdx;
  18. }
复制代码
我刚刚参照书上写了一个DAG的拓扑排序
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-2-7 17:39:22 | 显示全部楼层
刚刚有点多余的输出,修正了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-8 16:10:29 | 显示全部楼层
支持小甲鱼,赞点鱼币回来下载
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-8 16:38:40 | 显示全部楼层
是干什么的??
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-8 20:44:25 | 显示全部楼层
路过看看 = =
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-12 11:13:49 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-13 19:00:12 | 显示全部楼层
路过看看= =!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-16 13:53:32 | 显示全部楼层
做了小改动,把单队列改成循环队列了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-12-18 14:18:41 | 显示全部楼层
大神太给力啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-18 10:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表