- // Copyright 2008 All Rights Reserved.
- // Author: Zhiyi ZHOU
- #pragma once
- #ifndef _GRAPH_H_2008_
- #define _GRAPH_H_2008_
- #include <assert.h>
- #include <vector>
- #include <queue>
- #include <stack>
- #include <algorithm>
- #include <functional>
- using namespace std;
- class Vertex;
- class Edge{
- protected:
- int label;
- int weight;
- Vertex* source;
- Vertex* target;
- public:
- Edge(){
- label=weight=0;
- source=target=NULL;
- }
- Edge(const Vertex& source,const Vertex& target,int label=0,int weight=0){
- this->label=label;
- this->weight=weight;
- this->source=const_cast<Vertex*>(&source);
- this->target=const_cast<Vertex*>(&target);
- }
- virtual ~Edge(){};
- inline virtual int getLabel()const{return label;}
- inline virtual void setLabel(int value){label=value;}
- inline virtual int getWeight()const{return weight;}
- inline virtual void setWeight(int value){weight=value;}
- inline bool operator==(const Edge& edge)const{
- return label==edge.getLabel();
- }
- virtual void print()const{};
- inline virtual Vertex& getSource()const{
- return *source;
- }
- virtual Vertex& setSource(Vertex& vertex){
- source=&vertex;
- return *source;
- }
- virtual Vertex& getTarget()const{
- return *target;
- }
- virtual Vertex& setTarget(Vertex& vertex){
- target=&vertex;
- return *target;
- }
- };
- class Vertex{
- protected:
- int index;
- int outDegree;
- int inDegree;
- public:
- Vertex(int index=0):index(index){
- outDegree=0;
- inDegree=0;
- };
- inline virtual int getIndex()const{return index;}
- inline virtual void setIndex(int index){this->index=index;}
- inline int getOutDegree()const{return outDegree;}
- inline int getInDegree()const{return inDegree;}
- virtual Edge* getFirstEdge()const=0;
- virtual Edge* getNextEdge(const Edge& edge)const=0;
- virtual void setInEdge(Edge* edge)=0;
- virtual void setOutEdge(Edge* edge)=0;
- virtual Edge& addEdge(Vertex& vertex,int weight,bool directed=false)=0;
- virtual Edge& removeEdge(const Vertex& vertex,bool directed=false)=0;
- virtual bool pointTo(const Vertex& vertex)const=0;
- virtual void print()const{}
- virtual ~Vertex(){}
- };
- class Graph
- {
- protected:
- bool directed;
- vector<bool> mark;
- vector<bool> edgeMark;
- public:
- typedef unsigned int size_t;
- typedef Vertex& Reference;
- typedef const Vertex& const_Reference;
- Graph(bool tag=true){
- directed=tag;
- }
- bool isDirectedGraph()const{return directed;}
- virtual size_t getVerticesNum()const=0;
- virtual size_t getEdgeNum()const=0;
- virtual Reference getVertex(size_t index)const=0;
- virtual void setVertex(Vertex& vertex,size_t index)=0;
- Reference operator[](size_t index)const{
- return getVertex(index);
- }
- virtual ~Graph(void){}
- bool isVisited(const Vertex& vertex)const{
- return mark.at(vertex.getIndex());
- }
- void visit(const Vertex& vertex){
- mark[vertex.getIndex()].flip();
- }
- bool isVisited(const Edge& edge)const{
- return edgeMark.at(edge.getLabel());
- }
- void visit(const Edge& edge){
- edgeMark[edge.getLabel()]=true;
- if(!isDirectedGraph()){
- Edge* e=findEdge(edge.getTarget(),edge.getSource());
- if(e!=NULL)
- edgeMark[e->getLabel()]=true;
- }
- }
- void clearState(){
- mark.assign(mark.size(),false);
- edgeMark.assign(edgeMark.size(),false);
- }
- protected:
- Edge* findEdge(const Vertex& src,const Vertex& target)const{
- if(&src==NULL || &target==NULL)
- return NULL;
- size_t index=target.getIndex();
- for(Edge* edge=src.getFirstEdge();edge!=NULL;edge=src.getNextEdge(*edge)){
- if(edge->getTarget().getIndex()==index)
- return edge;
- }
- return NULL;
- }
- virtual Vertex& addVertex(Vertex& vertex)=0;
- virtual Vertex& remove(size_t index)=0;
- };
- #endif
图的接口定义
最新推荐文章于 2022-03-25 19:12:29 发布