Implement a undirected graph,represent by a adjacent matrix

本文介绍了一种使用邻接矩阵表示图数据结构的方法,并详细解释了如何通过模板类MatrixGraph实现这一表示法。该类提供了插入顶点、边、删除顶点、边等操作,同时实现了获取顶点位置、邻接顶点等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

and this a undirected graph represent by adjacent list..
 
  1. //MatrixGraph.h 
  2. #include<iostream> 
  3. using namespace std; 
  4. const int maxWeight = 10000;   //infinity number 
  5. const int defaultVertices = 30;   //default the biggest number of vertices (=n) 
  6. /* 
  7. template<typename T,typename E> 
  8. struct Graph 
  9.     int maxVertices; 
  10.     int numEdges,numVertices;    //current number of edge and vertices 
  11.         //T GetVertexPos(T vertex);   //get the position of the current vertex in graph 
  12. }; 
  13. */ 
  14. //use Adjacent matrix represent graph's class definition 
  15. template<typename T,typename E> 
  16. class MatrixGraph 
  17.     int maxVertices; 
  18.     int numEdges,numVertices;    //current number of edge and vertex 
  19.     T *VerticesList;     //vertex table 
  20.     E **Edge;           //Adjacent matrix 
  21.     int GetVertexPos(T vertex)     //get the position of the current vertex in graph 
  22.     { 
  23.         for(int i=0;i<numVertices;i++) 
  24.               if (VerticesList[i] == vertex) 
  25.                 return i; 
  26.         return -1; 
  27.     } 
  28.     friend istream& operator >>(istream& in,MatrixGraph<T,E>& G); 
  29.     friend ostream& operator <<(ostream& out,MatrixGraph<T,E>& G); 
  30. public
  31.     MatrixGraph(int sz=defaultVertices); 
  32.     ~MatrixGraph() 
  33.     { 
  34.         delete [] VerticesList; 
  35.         delete [] Edge; 
  36.     } 
  37.     T GetValue(int i)       //get the vertex's value. 
  38.     { 
  39.         return i>=0 && i<=numVertices ? VerticesList[i] : NULL; 
  40.     } 
  41.     E GetWeight(int v1,int v2) 
  42.     { 
  43.         return v1 != -1 && v2 != -1 ? Edge[v1][v2] : 0; 
  44.     } 
  45.     int NumberOfVertices() { return numVertices;} 
  46.     int NumberOfEdges() { return numEdges;} 
  47.     int GetFirstNeighbor(int v); 
  48.     //get adjacent vertex of w and w is the neighbor of v 
  49.     int GetNextNeightbor(int v,int w);   
  50.     bool InsertVertex(const T& vertex); 
  51.     bool InsertEdge(int v1,int v2,E cost); 
  52.     bool RemoveVertex(int v); 
  53.     bool RemoveEdge(int v1,int v2); 
  54. };
  55. /MatrixGraph.cpp 
  56. //implement of the MatrixGraph 
  57. #include "MatrixGraph.h" 
  58. template<typename T,typename E> 
  59. MatrixGraph<T,E>::MatrixGraph(int sz) 
  60.     maxVertices = sz; 
  61.     numVertices = numEdges = 0; 
  62.     VerticesList = new T[maxVertices];    //create array of vertex table  
  63.     Edge =  new int *[maxVertices];  //create array of AdjacentMatrix 
  64.     for (int i=0;i<maxVertices;i++) 
  65.           Edge[i] = new int[maxVertices]; 
  66.     for (int i=0;i<maxVertices;i++) 
  67.           for (int j=0;j<maxVertices;j++) 
  68.             Edge[i][j] = (i==j) ? 0 : maxWeight; 
  69. template<typename T,typename E> 
  70. int MatrixGraph<T,E>::GetFirstNeighbor(int v) 
  71.     if (v != -1) 
  72.           for (int col=0;col<numVertices;col++) 
  73.             if (Edge[v][col]>0 && Edge[v][col]<maxWeight) return col; 
  74.     return -1; 
  75. template<typename T,typename E> 
  76. int MatrixGraph<T,E>::GetNextNeightbor(int v,int w) 
  77.     //get adjacent vertex of 'w' and which 'w' if the adjacent vertex of' v' 
  78.     if ((v != -1) && (w != -1)) 
  79.           for(int col=w+1;col<numVertices;col++) 
  80.             if (Edge[v][col]>0 && Edge[v][col]<maxWeight) return col; 
  81.     return -1; 
  82. template<typename T,typename E> 
  83. bool MatrixGraph<T,E>::InsertVertex(const T& vertex) 
  84.     if (numVertices == maxVertices) return false
  85.     VerticesList[numVertices++] = vertex; 
  86.     return true
  87. template<typename T,typename E> 
  88. bool MatrixGraph<T,E>::RemoveVertex(int v) 
  89.     //remove vertex and edge which is related with this vertex 
  90.     if (v<0 && v>=numVertices) return false
  91.     if (numVertices == 1) return false//only one vertex leave,don't delete it 
  92.     //remove this vertex from vertex table 
  93.     VerticesList[v] = VerticesList[numVertices-1];    
  94.     for (int i=0;i<numVertices;i++)      //delete edge which is related with 'v' 
  95.           if (Edge[i][v]>0 && Edge[i][v]<maxWeight) 
  96.             numEdges--; 
  97.     for (int j=0;j<numVertices;j++) 
  98.           Edge[j][v] = Edge[j][numVertices-1];  //use the last column to fill with the 'v' column 
  99.     numVertices--; 
  100.     for (int k=0;k<numVertices;k++) //use the last row to fill with the 'v' row 
  101.           Edge[v][k] = Edge[numVertices][k];   
  102.     return true
  103. template<typename T,typename E> 
  104. bool MatrixGraph<T,E>::InsertEdge(int v1,int v2,E cost) 
  105.     if ((v1>-1 && v1<numVertices) && 
  106.         (v2>-1 && v2<numVertices) && Edge[v1][v2] == maxWeight) 
  107.     { 
  108.         Edge[v1][v2] = Edge[v2][v1] = cost; 
  109.         numEdges++; 
  110.         return true
  111.     } 
  112.     return false
  113. template<typename T,typename E> 
  114. bool MatrixGraph<T,E>::RemoveEdge(int v1,int v2) 
  115.     if((v1>-1 && v1<numVertices) && (v2>-1 && v2<numVertices) && 
  116.        (Edge[v1][v2]>0 && Edge[v1][v2]<maxWeight)) 
  117.     { 
  118.         Edge[v1][v2] = Edge[v2][v1] = maxWeight; 
  119.         numEdges--; 
  120.         return true
  121.     } 
  122.     return false
  123. template<typename T,typename E> 
  124. istream& operator >>(istream& in,MatrixGraph<T,E>& G) 
  125.     int n,m; 
  126.     T v1,v2; 
  127.     E weight; 
  128.     cout<<"please the numbers of vertex and edge:"
  129.     cin>>n>>m;    //input vertices number,edges number; 
  130.     for (int i=0;i<n;i++)      //create data of vertex table 
  131.     { 
  132.         in>>v1; 
  133.         G.insertVertex(v1); 
  134.     } 
  135.     int j=0; 
  136.     while (j<m) 
  137.     { 
  138.         in>>v1>>v2>>weight;              //input  information 
  139.         int pos1 = G.GetVertexPos(v1); 
  140.         int pos2 = G.GetVertexPos(v2); 
  141.         if (pos1==-1 || pos2==-1) cout<<"Error,please input again"<<endl; 
  142.         else j++,G.InsertEdge(pos1,pos2,weight); 
  143.     } 
  144.     return in; 
  145. template<typename T,typename E> 
  146. ostream& operator <<(ostream& out,MatrixGraph<T,E>& G) 
  147.     //out all the information of graph's vertices and edges 
  148.     int n = G.NumberOfVertices(),m = G.NumberOfEdges(); 
  149.     cout<<"number of vertices is "<<n<<", number of edges if "<<m<<endl; 
  150.     for (int i=0;i<n;i++) 
  151.           for (int j=i+1;j<n;j++) 
  152.           { 
  153.               int w = G.GetWeight(i,j); 
  154.               if (w>0 && w<maxWeight) 
  155.               { 
  156.                   int e1 = G.GetValues(i),e2 = G.GetValue(j); 
  157.                   out<<"("<<e1<<","<<e2<<","<<w<<")"<<endl; 
  158.               } 
  159.           } 
  160.     return out; 
  161. //a test for class MatrixGraph 
  162. #include "MatrixGraph.cpp" 
  163. int main(void)
  164. {
  165.     MatrixGraph<int,int> AdjMatG(10); 
  166.     int n,m;
  167.     int v,v1,v2;
  168.     int weight;
  169.     cout<<"please the numbers of vertex and edge:";
  170.     cin>>n>>m;    //input vertices number,edges number;
  171.     for(int i=0;i<n;i++)
  172.     {
  173.        cin>>v;
  174.        AdjMatG.InsertVertex(v);   //insert vertex
  175.     }
  176.     for(int j=0;j<m;j++)
  177.     {
  178.         cin>>v1>>v2>>weight;
  179.         AdjMatG.InsertEdge(v1,v2,weight);
  180.     }
  181.     AdjMatG.Display();
  182.     return 0;
  183. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值