zoukankan      html  css  js  c++  java
  • 图的邻接矩阵与邻接表

    邻接矩阵模板类

    #include<iostream>
    #include<cstdio>
    using namespace std;
    class Edge //
    {
    public:
        int from,to,weight;
        Edge()
        {
            from=-1;to=-1;weight=-1;
        }
        Edge(int f,int t,int w)
        {
            from=f;to=t;weight=-1;
        }
    };
    class Graph{ //
    public:
        int numVertex; //结点个数
        int numEdge; //边的条数
        int *Mark; //标记访问结点
        int *Indegree; //结点入度
        int **matrix; //邻接矩阵,存放点和边的信息
        Graph(int num)
        {
            numVertex=num;
            numEdge=0;
            Indegree=new int[numVertex];
            Mark=new int[numVertex];
            for(int i=0;i<numVertex;i++) //初始化访问标记和入度
            {
                Mark[i]=0;
                Indegree[i]=0;
            }
            int i,j;
            matrix =(int * *)new int*[numVertex];
            for(i=0;i<numVertex;i++)matrix[i]=new int[numVertex]; //构造邻接矩阵
            for(i=0;i<numVertex;i++) //初始化邻接矩阵
                for(j=0;j<numVertex;j++)
                    matrix[i][j]=0;
        }
        ~Graph(){
            delete[] Mark;
            delete[] Indegree;
            for(int i=0;i<numVertex;i++)
                delete [] matrix[i];
            delete[] matrix;
        }
    
        int VerticesNum(){return numVertex;}
        bool IsEdge(Edge oneEdge)
        {
            if(oneEdge.weight>0&&oneEdge.weight<1e9&&oneEdge.to>=0)
                return true;
            return false;
        }
        Edge FirstEdge(int oneVertex) //第一条边
        {
            Edge myEdge;
            myEdge.from=oneVertex;
            for(int i=0;i<numVertex;i++)
            {
                if(matrix[oneVertex][i]!=0)
                {
                    myEdge.to=i;
                    myEdge.weight=matrix[oneVertex][i];
                    break;
                }
            }
            return myEdge;
        }
        Edge NextEdge(Edge preEdge){ //下一条边
            Edge myEdge;
            myEdge.from=preEdge.from;
            if(preEdge.to<numVertex)
            {
                for(int i=preEdge.to+1;i<numVertex;i++)
                {
                    if(matrix[preEdge.from][i]!=0)
                    {
                        myEdge.to=i;
                        myEdge.weight=matrix[preEdge.from][i];
                        break;
                    }
                }
            }
            return myEdge;
        }
        void setEdge(int f,int t,int w) //添加一条边
        {
            if(matrix[f][t]<=0)
            {
                numEdge++;
                Indegree[t]++;
            }
            matrix[f][t]=w;
        }
        void delEdge(int f,int t) //删除一条边
        {
            if(matrix[f][t]>0)
            {
                numEdge--;
                Indegree[t]--;
            }
            matrix[f][t]=0;
        }
    };

    图的邻接表

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 using namespace std;
      5 class Edge{ //边类(单链表结点)
      6 public:
      7     int from;
      8     int to;
      9     Edge * next;
     10     int weight;
     11     Edge(int t,int w,int f=-1,Edge *nex=NULL)
     12     {
     13         to=t;
     14         weight=w;
     15         from=f;
     16         next=nex;
     17     }
     18     Edge(Edge *nex=NULL)
     19     {
     20         to=-1;from=-1;weight=-1;next=nex;
     21     }
     22 };
     23 template <class T>
     24 class Vertex{ //结点类
     25 public:
     26     T data;
     27     Edge *head; //边的头结点
     28     int inDegree; //入度
     29     int outDegree; //出度
     30     int edgeNum; //结点边数
     31     Vertex(const T d) //带结点值的构造参数
     32     {
     33         data=d;
     34         head=new Edge();
     35         inDegree=0;
     36         outDegree=0;
     37         edgeNum=0;
     38     }
     39     Vertex() 
     40     {
     41         head=new Edge();
     42         inDegree=0;
     43         outDegree=0;
     44         edgeNum=0;
     45     }
     46     void delEdge() //删除所有边
     47     {
     48         Edge *p,*q;
     49         p=head->next;
     50         while(p!=NULL)
     51         {
     52             q=p;p=p->next;
     53             delete q;
     54         }
     55         head->next=NULL;
     56     }
     57 };
     58 template <class T>class Graph{
     59 public:
     60     Vertex<T> *Vert;
     61     int vertexNum;
     62     int numEdge;
     63     int *Mark;
     64     Graph(int n)
     65     {
     66         numEdge=0;
     67         vertexNum=n;
     68         Mark=new int[n];
     69         Vert=new Vertex<T>[n];
     70         memset(Mark,0,sizeof(Mark));
     71     }
     72     void setVertex(const int i,const T d)
     73     {
     74         Vert[i].data=d;
     75     }
     76     bool insEdge(int f,int t,int w)//添加f到t权值为w的边
     77     {
     78         Edge *p,*q;
     79         q=new Edge(t,w,f);
     80         p=Vert[f].head;
     81         while(p->next!=NULL)
     82         {
     83             p=p->next;
     84         }
     85         p->next=q;
     86         Vert[f].edgeNum++;
     87         Vert[f].outDegree++;
     88         Vert[t].inDegree++;
     89         numEdge++;
     90     }
     91     bool delEdge(int f,int t) //删除f到t的边
     92     {
     93         Edge *p,*q;
     94         p=Vert[f].head;
     95         q=p->next;
     96         while(q->next!=NULL&&q->to!=t)
     97         {
     98             p=q;
     99             q=q->next;
    100         }
    101         if(q->to==t)
    102         {
    103             p->next=q->next;
    104             q->next=NULL;
    105             delete q;
    106             numEdge--;
    107             Vert[f].outDegree--;
    108             Vert[t].inDegree--;
    109             Vert[f].edgeNum--;
    110             return true;
    111         }
    112         return false;
    113     }
    114     void display()
    115     {
    116         for(int i=0;i<vertexNum;i++)
    117         {
    118             cout << i << ":";
    119             cout <<Vert[i].data<<"  "<<endl;
    120             Edge *p;
    121             p=Vert[i].head->next;
    122             while(p!=NULL)
    123             {
    124                 cout << "Edge:"<<p->to <<" " <<p->weight <<endl;
    125                 p=p->next;
    126             }
    127         }
    128         cout << "Vertex Num:" << vertexNum <<endl;
    129         cout << "Edge Num:" <<numEdge<<endl;
    130     }
    131     void Clear()
    132     {
    133         for(int i=0;i<vertexNum;i++)
    134         {
    135             Vert[i].delEdge();
    136             Vert[i].inDegree=Vert[i].outDegree=Vert[i].edgeNum=0;
    137         }
    138         numEdge=0;
    139         memset(Mark,0,sizeof(Mark));
    140     }
    141 };
    142 int main()
    143 {
    144     Graph<int> g(5);
    145     int a,b,n;
    146     while(1)
    147     {
    148         for(int i=0;i<5;i++)
    149         {
    150             cout << "node :" << i <<endl;
    151             cout << "enter the node value"<<endl;
    152             cin>>a;
    153             g.setVertex(i,a);
    154             cout << "enter the number of edge" <<endl;
    155             cin>>n;
    156             cout << "enter the destination and weight" <<endl;
    157             for(int j=0;j<n;j++)
    158             {
    159                 cin>>a>>b;
    160                 g.insEdge(i,a,b);
    161             }
    162         }
    163         g.display();
    164         cout << "enter the delete edge"<<endl;
    165         cin >> a >>b;
    166         if(g.delEdge(a,b))cout <<"success"<<endl;
    167         else cout << "fail"<<endl;
    168         g.display();
    169         g.Clear();
    170     }
    171 }
    172 /*
    173 0 2
    174 1 2 3 4
    175 1 1
    176 3 1
    177 2 0
    178 3 0
    179 4 1
    180 2 1
    181 4 2
    182 
    183 0 2
    184 1 2 3 4
    185 1 1
    186 3 1
    187 2 0
    188 3 0
    189 4 1
    190 2 1
    191 0 1
    192 
    193 0 2
    194 1 2 3 4
    195 1 1
    196 3 1
    197 2 0
    198 3 0
    199 4 1
    200 2 1
    201 0 2
    202 
    203 */
  • 相关阅读:
    centos crash debug
    go get Unknown SSL protocol error in connection to gopkg.in
    Tensorflow serving with Kubernetes
    Spring 集成 Swagger UI
    Docker Registry V2 Garbage Collection
    Docker Registry V2 with Nginx
    Zabbix磁盘性能监控
    Zabbix CPU utilization监控参数
    Windows挂载Gluster复制卷
    Redis持久化存储(三)
  • 原文地址:https://www.cnblogs.com/LowBee/p/9046229.html
Copyright © 2011-2022 走看看