zoukankan      html  css  js  c++  java
  • 图的数据结构2

    #include "LList.h"
    #define UNVISITED 0
    //A graph ADT
    class Graph

    {
    public:
    virtual int n()=0;
    virtual int e()=0;
    virtual int first(int)=0;
    virtual int next(int,int)=0;
    virtual int setEdge(int ,int,int)=0;
    virtual int delEdge(int,int)=0;
    virtual int weight(int ,int)=0;
    virtual int getMark(int)=0;
    virtual void setMark(int,int)=0;
    };

    class Edge
    {
    public:
    int vertex,weight;
    Edge()
    {
    vertex=-1;weight=-1;
    }
    Edge(int v,int w)
    {
    vertex=v;weight=w;
    }
    };

    //Implement adjacency matrix
    class Graphm:public Graph

    {
    private:
    int numVertex,numEdge;
    int **matrix;
    int *mark;
    public:
    Graphm(int numVert)
    {
    int i,j;
    numVertex=numVert;
    mark=new int[numVert];
    for(i=0;i<numVertex;i++)
    mark[i]=UNVISITED;
    matrix=(int**) new int*[numVertex];
    for(i=0;i<numVertex;i++)
    matrix[i]=new int[numVertex];
    for(i=0;i<numVertex;i++)
    for(int j=0;j<numVertex;j++)
    matrix[i][j]=0;
    }

    ~Graphm()
    {
    delete []mark;
    for(int i=0;i<numVertex;i++)
    delete [] matrix[i];
    delete []matrix;
    }
    int n()
    {
    return numVertex;
    }
    int e()
    {
    return numEdge;
    }
    //Return v's first neighbour
    int first(int v)

    {
    int i;
    for(i=0;i<numVertex;i++)
    if(matrix[v][i]!=0) return i;
    return i;
    }

    int next(int v1,int v2)
    {
    int i;
    for(i=v2+1;i<numVertex;i++)
    if(matrix[v1][i]!=0) return i;
    return i;
    }

    void setEdge(int v1,int v2,int wgt)
    {
    Assert(wgt>0,"Illegal weight value");
    if(matrix[v1][v2]==0) numEdge++;
    matrix[v1][v2]=wgt;
    }
    void delEdge(int v1,int v2)
    {
    if(matrix[v1][v2]!=0) numEdge--;
    matrix[v1][v2]=0;
    }

    int weight(int v1,int v2)
    {
    return matrix[v1][v2];
    }
    int getMark(int v)
    {
    return mark[v];
    }
    void setMark(int v,int val)
    {
    mark[v]=val;
    }
    };

    //Implement adjacency list
    class Graphl:public Graph

    {
    private:
    int numVertex,numEdge;
    List<Edge>** vertex;
    int *mark;
    public:
    Graphl(int numVert)
    {
    int i,j;
    numVertex=numVert;
    numEdge=0;
    mark=new int [numVert];
    for(i=0;i<numVertex;i++)
    mark[i]=UNVISITED;
    vertex=(List<Edge>**)new List<Edge>*[numVertex];
    for(i=0;i<numVertex;i++)
    vertex[i]=new LList<Edge>();
    }
    ~Graphl()
    {
    delete []mark;
    for(int i=0;i<numVertex;i++)
    delete []vertex[i];
    delete []vertex;
    }
    int n(){return numVertex;}
    int e(){return numEdge;}
    int first(int v)
    {
    Edge it;
    vertex[v]->setStart();
    if(vertex[v]->getValue(it))
    else return numVertex;
    }
    int next(int v1,int v2)
    {
    Edge it;
    vertex[v1]->getValue(it);
    if(it.vertex==v2)
    vertex[v1]->next();
    else
    {
    vertex[v1]->setStart();
    while(vertex[v1]->getValue(it)&&(it.vertex<=v2))
    vertex[v1]->next();
    }
    if(vertex[v1]->getValue(it)) return it.vertex;
    else
    return numVertex;
    }

    void setEdge(int v1,int v2,int wgt)
    {
    Assert(wgt>0,"Illegal weight value");
    Edge it(v2,wgt);
    Edge curr;
    vertex[v1]->getValue(curr);
    if(curr.vertex!=v2)
    for(vertex[v1]->setStart();vertex[v1]->getValue(curr);vertex[v1]->next())
    if(curr.vertex>=v2)
    break;
    if(curr.vertex==v2)
    vertex[v1]->remove(curr);
    else numEdge++;
    vertex[v1]->insert(it);
    }

    void delEdge(int v1,int v2)
    {
    Edge curr;
    vertex[v1]->getValue(curr);
    if(curr.vertex!=v2)
    for(vertex[v1]->setStart();vertex[v1]->getValue(curr);vertex[v1]->next())
    if(curr.vertex>=v2) break;
    if(curr.vertex==v2)
    {
    vertex[v1]->remove(curr);
    numEdge--;
    }
    }

    int weight(int v1,int v2)
    {
    Edge curr;
    vertex[v1]->getValue(curr);
    if(curr.vertex!=v2)
    for(vertex[v1]->setStart();
    vertex[v1]->getValue(curr);vertex[v1]->next())
    if(curr.vertex==v2)
    return curr.weight;
    else
    return 0;
    }
    int getMark(int v)
    {
    return mark[v];
    }
    void setMark(int v,int val)
    {
    mark[v]=val;
    }
    };


    //Graph Traveral
    void graphTraverse(const Graph* G)

    {
    for(v=0;v<G->n();v++)
    G->setMark(v,UNVISITED);
    for(v=0;v<G->n();v++)
    if(G->getMark(v)==UNVISITED)
    doTraverse(G,v);
    }

    //Depth first search
    void DFS(Graph* G,int v)

    {
    PreVisit(G,v);
    G->setMark(v,VISITED);
    for(int w=G->first(v);w<G->n();w=G->next(v,w))
    if(G->getMark(w)==UNVISITED)
    DFS(G,w);
    PostVisit(G,v);
    }
    Live together,or Die alone!
  • 相关阅读:
    invalid byte 1 of 1-byte UTF-8 sequence
    MySQL的时间进位问题
    MyBatis返回主键
    No matching bean of type [xx] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency
    在eclipse中下载包含子模块(Submodules)的git项目
    ERWin & ERStudio图里的实线和虚线的含义[转]
    Win7玩游戏偶尔自动跳转到桌面的解决办法[转]
    SpringMVC静态资源处理[转]
    RocketMQ术语[转]
    手机收不到验证码
  • 原文地址:https://www.cnblogs.com/hzhida/p/2354735.html
Copyright © 2011-2022 走看看