图的邻接矩阵实现

  1. // Copyright 2008 All Rights Reserved.
  2. // Author: Zhiyi ZHOU
  3. #ifndef _MATRIXGRPAH_H_2008_
  4. #define _MATRIXGRPAH_H_2008_
  5. #include "Graph.h"
  6. #include <assert.h>
  7. class MatrixVertex:public Vertex{
  8. public:
  9.     typedef vector<Edge*> EdgeVec;
  10.     typedef EdgeVec::iterator EdgeIterator;
  11.     typedef EdgeVec::size_type size_t;
  12. private:
  13.     EdgeVec outEdges;
  14.     EdgeVec inEdges;
  15.     int curIndex;
  16. protected:
  17.     void setInEdge(Edge* edge){
  18.         inEdges.push_back(edge);
  19.         //inEdges[edge->getSource().getIndex()]=edge;
  20.         inDegree++;
  21.     }
  22.     void setOutEdge(Edge* edge){
  23.         //outEdges[edge->getTarget().getIndex()]=edge;
  24.         outEdges.push_back(edge);
  25.         outDegree++;
  26.     }
  27. public:
  28.     MatrixVertex(size_t index,int size):Vertex(index){
  29.         //outEdges.assign(size,NULL);
  30.         //inEdges.assign(size,NULL);
  31.     }
  32.     ~MatrixVertex(){
  33.         for(size_t i=0;i<outEdges.size();i++){
  34.             if(outEdges[i]!=NULL)
  35.                 delete outEdges[i];
  36.         }
  37.     }
  38.     Edge* getFirstEdge()const{
  39.         //size_t i=0;
  40.         //while(i<outEdges.size() && outEdges[i]==NULL)i++;
  41.         //curIndex=0;
  42.         return outEdges.front();
  43.     }
  44.     Edge* getNextEdge(const Edge& edge)const{
  45.         assert(&edge!=NULL);
  46.         //size_t i=edge.getTarget().getIndex()+1;
  47.         //if(*outEdges.at(curIndex)==edge){
  48.         //  if(++const_cast<int>(curIndex)<outEdges.size())
  49.         //      return outEdges[curIndex];
  50.         //  return NULL;
  51.         //}
  52.         for(EdgeVec::const_iterator iter=outEdges.begin();iter!=outEdges.end();iter++){
  53.             if(**iter==edge){
  54.                 if(++iter!=outEdges.end())
  55.                     return *iter;
  56.                 break;
  57.             }
  58.         }
  59.         return NULL;
  60.     }
  61.     Edge& addEdge(Vertex& vertex,int weight,bool directed=false){
  62.         //assert(vertex.getIndex()<outEdges.size());
  63.         Edge* edge=new Edge(*this,vertex,0,weight);
  64.         int destIndex=vertex.getIndex();
  65.         this->setOutEdge(edge);
  66.         if(!directed){
  67.             vertex.setInEdge(edge);
  68.         }
  69.         return *edge;
  70.     }
  71.     Edge& removeEdge(const Vertex& vertex,bool directed=false){
  72.         //assert(vertex.getIndex()<outEdges.size());
  73.         Edge* edge=outEdges[vertex.getIndex()];
  74.         outEdges[vertex.getIndex()]=NULL;
  75.         outDegree--;
  76.         if(!directed){
  77.             inEdges[vertex.getIndex()]=NULL;
  78.             inDegree--;
  79.         }
  80.         return *edge;
  81.     }
  82.     bool pointTo(const Vertex& vertex)const{
  83.         //assert(vertex.getIndex()<outEdges.size());
  84.         return outEdges[vertex.getIndex()]!=NULL;
  85.     }
  86. };
  87. class MatrixGraph:virtual public Graph{
  88. public:
  89.     typedef vector<Vertex*> VertexVec;
  90.     typedef VertexVec::iterator VertexIterator;
  91.     typedef VertexVec::size_type size_t;
  92.     typedef vector<Edge*> EdgeVec;
  93.     typedef EdgeVec::iterator EdgeIterator;
  94. private:
  95.     VertexVec vertexs;
  96.     EdgeVec edges;
  97.     int edgeSize;
  98. protected:
  99.     void initialize(Vertex& vertex,int* weight,int size){
  100.         size_t i=0;
  101.         for(;i<size;i++){
  102.             if(weight[i]!=0){
  103.                 Edge* e=&vertex.addEdge(this->getVertex(i),weight[i]);
  104.                 e->setLabel(edgeSize++);
  105.                 edges.push_back(e);
  106.             }
  107.         }
  108.     }
  109. public:
  110.     MatrixGraph(bool tag=true):Graph(tag){
  111.         edgeSize=0;
  112.     }
  113.     MatrixGraph(int** weight,size_t size,bool tag=true):Graph(tag){
  114.         edgeSize=0;
  115.         mark.assign(size,false);
  116.         for(size_t i=0;i<size;i++){
  117.             vertexs.push_back(new MatrixVertex(i,size));
  118.         }
  119.         for(size_t i=0;i<size;i++){
  120.             initialize(*vertexs[i],weight[i],size);
  121.         }
  122.         edgeMark.assign(getEdgeNum(),false);
  123.     }
  124.     ~MatrixGraph(){
  125.         for(size_t i=0;i<vertexs.size();i++){
  126.             if(vertexs[i]!=NULL)
  127.                 delete vertexs[i];
  128.         }
  129.     }
  130.     inline size_t getVerticesNum()const{
  131.         return vertexs.size();
  132.     }
  133.     inline size_t getEdgeNum()const{
  134.         return edges.size();
  135.     }
  136.     inline Vertex& getVertex(size_t index)const{
  137.         assert(index<vertexs.size());
  138.         return *vertexs[index];
  139.     }
  140.     void setVertex(Vertex& vertex,size_t index){
  141.         assert(index<vertexs.size());
  142.         vertex.setIndex(index);
  143.         vertexs[index]=&vertex;
  144.     }
  145. protected:
  146.     Vertex& addVertex(Vertex& vertex){
  147.         vertex.setIndex(vertexs.size());
  148.         vertexs.push_back(&vertex);
  149.         return vertex;
  150.     }
  151.     Vertex& remove(size_t index){
  152.         Vertex* value=NULL;
  153.         return *value;
  154.     }
  155. };
  156. #endif

你可能感兴趣的:(图的邻接矩阵实现)