Chinaunix首页 | 论坛 | 博客
  • 博客访问: 268801
  • 博文数量: 84
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 927
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-06 23:00
个人简介

growing

文章分类

全部博文(84)

文章存档

2017年(6)

2016年(61)

2015年(17)

我的朋友

分类: C/C++

2016-05-22 14:48:32

实现代码:

  1. template<class V,class E>
  2. struct LinkEdge
  3. {
  4.     LinkEdge(int srcIndex = - 1,int dstIndex = -1,const E&weight = E())
  5.         :_srcIndex(srcIndex)
  6.         ,_dstIndex(dstIndex)
  7.         ,_weight(weight)
  8.         ,_next(NULL)
  9.     {}
  10.     int _srcIndex;//原顶点下标
  11.     int _dstIndex;//目标顶点下标
  12.     E _weight;
  13.     LinkEdge<V,E>* _next;
  14. };

  15. template<class V,class E>
  16. struct LinkVertex
  17. {
  18.     LinkVertex(const V&Vertex = V())
  19.         :_Vertex(Vertex)
  20.         ,_head(NULL)
  21.     {}
  22.     V _Vertex;//顶点
  23.     LinkEdge<V,E>* _head;
  24. };
  25. template<class V,class E>
  26. struct CompareLinkEdge
  27. {
  28.     bool operator()(LinkEdge<V,E>*l,LinkEdge<V,E>*r)
  29.     {
  30.         return l->_weight < r->_weight;
  31.     }
  32. };
  33. template<class V,class E>
  34. class GraphLink
  35. {
  36. public:
  37.     GraphLink(bool dir = false)
  38.         :_LinkTable(NULL)
  39.         ,_VertexSize(0)
  40.         ,_HasDir(dir)
  41.     {}
  42.     GraphLink(const V* Array,int size,bool dir = false)
  43.         :_VertexSize(size)
  44.         ,_HasDir(dir)
  45.     {
  46.         _LinkTable = new LinkVertex<V,E>[size];
  47.         for(int i = 0;i < size;++i)
  48.         {
  49.             _LinkTable[i] = Array[i];
  50.         }
  51.     }
  52.     ~GraphLink()
  53.     {
  54.         delete[] _LinkTable;
  55.         _VertexSize = 0;
  56.     }
  57. public:
  58.     int GetVertexIndex(const V&vertex)
  59.     {
  60.         for(int i = 0;i < _VertexSize;++i)
  61.         {
  62.             if(_LinkTable[i]._Vertex == vertex)
  63.             {
  64.                 return i;
  65.             }
  66.         }
  67.         return -1;
  68.     }
  69.     void _ADDEdge(int srcIndex,int dstIndex,const E& weight)
  70.     {
  71.         LinkEdge<V,E>*tmp = new LinkEdge<V,E>(srcIndex,dstIndex,weight);

  72.         tmp->_next = _LinkTable[srcIndex]._head;
  73.         _LinkTable[srcIndex]._head = tmp;

  74.     }
  75.     void AddEdge(const V&src,const V&dst,const E& weight)
  76.     {
  77.         int srcIndex = GetVertexIndex(src);
  78.         int dstIndex = GetVertexIndex(dst);
  79.         assert(srcIndex != -1 && dstIndex != -1);

  80.         //有向图or无向图
  81.         if(_HasDir)
  82.         {
  83.             _ADDEdge(srcIndex,dstIndex,weight);
  84.         }
  85.         else
  86.         {
  87.             _ADDEdge(srcIndex,dstIndex,weight);
  88.             _ADDEdge(dstIndex,srcIndex,weight);
  89.         }
  90.     }
  91.     void Display()
  92.     {
  93.         for(int i = 0;i < _VertexSize;++i)
  94.         {
  95.             cout<<_LinkTable[i]._Vertex<<"["<<i<<"]->";
  96.             LinkEdge<V,E>*cur = _LinkTable[i]._head;
  97.             while(cur)
  98.             {
  99.                 cout<<cur->_weight<<"["<<cur->_dstIndex<<"]->";
  100.                 cur = cur->_next;
  101.             }
  102.             cout<<"NULL"<<endl;
  103.         }
  104.     }
  105.     void DFS(const int cur)//深度优先
  106.     {
  107.         cout<<"DFS:";
  108.         bool *visited = new bool[_VertexSize];
  109.         memset(visited,0,sizeof(bool)*_VertexSize);

  110.         _DFS(cur,visited);
  111.         cout<<endl;
  112.     }
  113.     void BFS(const int cur)//广度优先
  114.     {
  115.         cout<<"BFS:";
  116.         bool *visited = new bool[_VertexSize];
  117.         memset(visited,0,sizeof(bool)*_VertexSize);

  118.         _BFS(cur,visited);
  119.         cout<<endl;
  120.     }
  121. protected:
  122.     void _BFS(int cur,bool *visited)
  123.     {
  124.         visited[cur] = true;

  125.         queue<int> q;
  126.         q.push(cur);
  127.         while(!q.empty())
  128.         {
  129.             cur = q.front();
  130.             q.pop();
  131.             
  132.             LinkEdge<V,E>* edge = _GetFirstEdge(cur);
  133.             while(edge)
  134.             {
  135.                 if (visited[edge->_dstIndex] == false)
  136.                 {
  137.                     cout<<_LinkTable[edge->_srcIndex]._Vertex<<"->"
  138.                         <<_LinkTable[edge->_dstIndex]._Vertex<<" ";

  139.                     visited[edge->_dstIndex] = true;
  140.                     q.push(edge->_dstIndex);
  141.                 }

  142.                 edge = edge->_next;
  143.             }
  144.         }
  145.     }
  146.     LinkEdge<V,E>* _GetFirstEdge(const int src)
  147.     {
  148.         return _LinkTable[src]._head;
  149.     }
  150.     LinkEdge<V,E>* _GetNextEdge(const int src,const int cur)
  151.     {
  152.         LinkEdge<V,E>* edge = _LinkTable[src]._head;
  153.         while(edge)
  154.         {
  155.             if(edge->_dstIndex == cur)
  156.             {
  157.                 return edge->_next;//找邻接表里的下一条边
  158.             }
  159.             edge = edge->_next;
  160.         }
  161.         return NULL;
  162.     }
  163.     void _DFS(const int src,bool *visited)
  164.     {
  165.         //cout<<_LinkTable[src]._Vertex<<" ";
  166.         visited[src] = true;

  167.         LinkEdge<V,E>* edge = _GetFirstEdge(src);

  168.         while(edge)
  169.         {
  170.             if(visited[edge->_dstIndex] == false)
  171.             {
  172.                 cout<<_LinkTable[edge->_srcIndex]._Vertex<<"->"<<_LinkTable[edge->_dstIndex]._Vertex<<" ";

  173.                 _DFS(edge->_dstIndex,visited);
  174.             }
  175.             edge = edge->_next;
  176.         }
  177.     }
  178. private:
  179.     LinkVertex<V,E>* _LinkTable;
  180.     int _VertexSize;
  181.     bool _HasDir;
  182. };
测试:

  1. void testLink()
  2. {
  3.     char a[5] = {'A','B','C','D','E'};
  4.     GraphLink<char,int> g(a,5);
  5.     g.AddEdge('A', 'D', 10);
  6.     g.AddEdge('A', 'E', 20);
  7.     g.AddEdge('B', 'C', 10);
  8.     g.AddEdge('B', 'D', 20);
  9.     g.AddEdge('B', 'E', 30);
  10.     g.AddEdge('C', 'E', 40);
  11.     g.Display();
  12.     g.DFS(0);
  13.     g.BFS(0);
  14. }




阅读(1679) | 评论(0) | 转发(6) |
给主人留下些什么吧!~~