当前位置:首页 > 通信资讯 > 正文

c语言图的邻接矩阵(图邻接矩阵c代码实现)

本文实例为大家分享了C++实现图的邻接矩阵表示代码,供大家参考,具体内容如下

1.遇到的问题:教材中写着子类Graphmtx(我用GrapMatrix)继承基类Graph

c语言图的邻接矩阵(图邻接矩阵c代码实现)

但是我在子类GraphMatrix中使用父类Graph的保护成员属性:maxVertices 显示没有声明(如下图)。

c语言图的邻接矩阵(图邻接矩阵c代码实现)

原来,c++中声明一个模板类及子类,在子类中如果需要访问父类的protected变量,需要使用父类的类作用域限定符,否则会报“identifier not found”错误。如果不是模板类,可以直接访问。

例如:要如下这样使用父类的保护成员属性,太麻烦了。

c语言图的邻接矩阵(图邻接矩阵c代码实现)

所以,我就不用继承基类的方法了。直接把Graph父类的保护成员属性放到GrapMatrix类中。

2.实现程序:

(1)GraphMatrix.h

  1. #ifndef GraphMatrix_h
  2. #define GraphMatrix_h
  3. #include <iostream>
  4. using namespace std;
  5. const int DefaultVertices = 30; // 默认最大顶点数
  6. template <class T, class E>
  7. class GraphMatrix {
  8. public:
  9. const E maxWeight = 100000; // 代表无穷大的值(=∞)
  10. GraphMatrix(int sz=DefaultVertices); // 构造函数
  11. ~GraphMatrix(); // 析构函数
  12. void inputGraph(); // 创建基于邻接矩阵的图
  13. void outputGraph(); // 输出图的所有顶点和边信息
  14. T getValue(int i); // 取顶点i的值,i不合理返回0
  15. E getWeight(int v1, int v2); // 取边(v1, v2)上的权值
  16. int getFirstNeighbor(int v); // 取顶点v的第一个邻接顶点
  17. int getNextNeighbor(int v, int w); // 取v的邻接顶点w的下一个邻接顶点
  18. bool insertVertex(const T& vertex); // 插入顶点vertice
  19. bool insertEdge(int v1, int v2, E cost); // 插入边(v1, v2)权值为cost
  20. bool removeVertex(int v); // 删去顶点v和所有与它相关联的边
  21. bool removeEdge(int v1, int v2); // 在图中删去边(v1, v2)
  22. int getVertexPos(T vertex); // 给出顶点vertice在图中的位置
  23. private:
  24. int maxVertices; // 图中最大的顶点数
  25. int numEdges; // 当前边数
  26. int numVertices; // 当前顶点数
  27. T *VerticesList; // 顶点表
  28. E **Edge; // 邻接矩阵
  29. };
  30. // 构造函数
  31. template <class T, class E>
  32. GraphMatrix<T, E>::GraphMatrix(int sz) {
  33. int i, j;
  34. maxVertices = sz;
  35. numVertices = 0;
  36. numEdges = 0;
  37. VerticesList = new T[maxVertices]; // 创建顶点表数组
  38. Edge = new E*[maxVertices]; // 创建邻接矩阵数组
  39. for(i = 0; i < maxVertices; i++)
  40. Edge[i] = new E[maxVertices];
  41. for(i = 0; i < maxVertices; i++) { // 邻接矩阵初始化
  42. for(j = 0; j < maxVertices; j++)
  43. {
  44. if(i == j) // 矩阵对角处,即为同一顶点
  45. Edge[i][j] = 0;
  46. else // 不是同一顶点的,即两顶点一开始没有边相连,为无穷大∞
  47. Edge[i][j] = maxWeight;
  48. }
  49. }
  50. }
  51. // 析构函数
  52. template <class T, class E>
  53. GraphMatrix<T, E>::~GraphMatrix() {
  54. delete []VerticesList; // 释放动态分配的空间
  55. delete []Edge;
  56. }
  57. // 创建基于邻接矩阵的图
  58. template <class T, class E>
  59. void GraphMatrix<T, E>::inputGraph() {
  60. int i, j, k;
  61. int n, m; // 要输入的顶点数和边数
  62. T e1, e2; // 边的两端顶点
  63. E weight; // 边对应的权值
  64. cout << "请输入顶点数和边数:" << endl;
  65. cin >> n >> m;
  66. cout << "请输入顶点:" << endl;
  67. for(i = 0; i < n; i++) { // 建立顶点表数据
  68. cin >> e1;
  69. insertVertex(e1); // 插入
  70. }
  71. cout << "请输入边的两端顶点和权值:" << endl;
  72. i = 0;
  73. while(i < m){ // 输入边
  74. cin >> e1 >> e2 >> weight; // 输入端点信息
  75. j = getVertexPos(e1); // 查顶点号
  76. k = getVertexPos(e2);
  77. if(j == -1 || k == -1)
  78. cout << "边两端点信息有误,重新输入!" << endl;
  79. else {
  80. insertEdge(j, k, weight);
  81. i++;
  82. }
  83. } // for结束
  84. }
  85. // 输出图的所有顶点和边信息
  86. template <class T, class E>
  87. void GraphMatrix<T, E>::outputGraph() {
  88. int i, j, n, m;
  89. T e1, e2;
  90. E w;
  91. n = numVertices;
  92. m = numEdges;
  93. cout << "顶点数为:" << n << ",边数为:" << m << endl;
  94. for(i = 0; i < n; i++) {
  95. for(j = i+1; j < n; j++) {
  96. w = getWeight(i, j); // 取边上权值
  97. if(w > 0 && w < maxWeight) { // 有效,即这两顶点存在边
  98. e1 = getValue(i);
  99. e2 = getValue(j);
  100. cout << "(" << e1 << "," << e2 << "," << w << ")" << endl;
  101. }
  102. }
  103. } // for
  104. }
  105. // 给出顶点vertice在图中的位置
  106. template <class T, class E>
  107. int GraphMatrix<T, E>::getVertexPos(T vertex) {
  108. for(int i = 0; i < numVertices; i++)
  109. if(VerticesList[i] == vertex)
  110. return i;
  111. return -1;
  112. }
  113. // 取顶点i的值,i不合理返回NULL
  114. template <class T, class E>
  115. T GraphMatrix<T, E>::getValue(int i) {
  116. if(i >= 0 && i < numVertices)
  117. return VerticesList[i];
  118. return NULL;
  119. }
  120. // 取边(v1, v2)上的权值
  121. template <class T, class E>
  122. E GraphMatrix<T, E>::getWeight(int v1, int v2) {
  123. if(v1 != -1 && v2 != -1) // 存在这两个顶点
  124. return Edge[v1][v2];
  125. return 0;
  126. }
  127. // 取顶点v的第一个邻接顶点
  128. template <class T, class E>
  129. int GraphMatrix<T, E>::getFirstNeighbor(int v) {
  130. if(v != -1) {
  131. for(int col = 0; col < numVertices; col++)
  132. if(Edge[v][col] > 0 && Edge[v][col] <maxWeight)
  133. return col;
  134. }
  135. return -1;
  136. }
  137. // 取v的邻接顶点w的下一个邻接顶点
  138. template <class T, class E>
  139. int GraphMatrix<T, E>::getNextNeighbor(int v, int w) {
  140. if(v != -1 && w != -1) {
  141. for(int col = w+1; col < numVertices; col++) {
  142. if(Edge[v][col] > 0 && Edge[v][col] < maxWeight)
  143. return col;
  144. }
  145. }
  146. return -1;
  147. }
  148. // 插入顶点vertice
  149. template <class T, class E>
  150. bool GraphMatrix<T, E>::insertVertex(const T& vertex) {
  151. if(numVertices == maxVertices) // 顶点表满
  152. return false;
  153. VerticesList[numVertices++] = vertex;
  154. return true;
  155. }
  156. // 插入边(v1, v2)权值为cost
  157. template <class T, class E>
  158. bool GraphMatrix<T, E>::GraphMatrix<T, E>::insertEdge(int v1, int v2, E cost) {
  159. if(v1 > -1 && v1 < numVertices && v2 > -1 && v2 < numVertices && Edge[v1][v2] == maxWeight) { // 顶点v1,v2都存在,并且v1,v2没有边
  160. Edge[v1][v2] = Edge[v2][v1] = cost;
  161. numEdges++;
  162. return true;
  163. }
  164. return false;
  165. }
  166. // 删去顶点v和所有与它相关联的边
  167. template <class T, class E>
  168. bool GraphMatrix<T, E>::removeVertex(int v) {
  169. if(v < 0 && v > numVertices) // v不在图中,不删除
  170. return false;
  171. if(numVertices == 1) // 只剩一个顶点,不删除
  172. return false;
  173. int i, j;
  174. VerticesList[v] = VerticesList[numVertices-1]; // 用最后一个顶点替代当前要删的顶点
  175. // 删除与v相关联边数
  176. for(i = 0; i < numVertices; i++) {
  177. if(Edge[i][v] > 0 && Edge[i][v] < maxWeight)
  178. numEdges--;
  179. }
  180. // 用最后一列,填补第v列
  181. for(i = 0; i < numVertices; i++)
  182. Edge[i][v] = Edge[i][numVertices-1];
  183. numVertices--; // 顶点数减1
  184. // 用最后一行,填补第v行
  185. for(j = 0; j < numVertices; j++)
  186. Edge[v][j] = Edge[numVertices][j];
  187. return true;
  188. }
  189. // 在图中删去边(v1, v2)
  190. template <class T, class E>
  191. bool GraphMatrix<T, E>::removeEdge(int v1, int v2) {
  192. if(v1 > -1 && v1 < numVertices && v2 > -1 && v2 < numVertices && Edge[v1][v2] < maxWeight) {
  193. Edge[v1][v2] = Edge[v2][v1] = maxWeight;
  194. numEdges--; // 边数减1
  195. return true;
  196. }
  197. return false;
  198. }
  199. #endif /* GraphMatrix_h */

(2)main.cpp

  1. // 测试数据:
  2. /*
  3. 5 7
  4. A B C D E
  5. A B 24 A C 46 B C 15 B E 67 C B 37 C D 53 E D 31
  6. */
  7. #include "GraphMatrix.h"
  8. int main(int argc, const char * argv[]) {
  9. GraphMatrix<char, int> st; // 声明对象
  10. bool finished = false;
  11. int choice;
  12. char e1, e2, next;
  13. int weight;
  14. while(!finished) {
  15. cout << "[1]创建基于邻接矩阵的图" << endl;
  16. cout << "[2]输出图的所有顶点和边信息" << endl;
  17. cout << "[3]取顶点v的第一个邻接顶点" << endl;
  18. cout << "[4]取v的邻接顶点w的下一个邻接顶点" << endl;
  19. cout << "[5]插入顶点" << endl;
  20. cout << "[6]插入边" << endl;
  21. cout << "[7]删除顶点" << endl;
  22. cout << "[8]删除边" << endl;
  23. cout << "[9]退出" << endl;
  24. cout << "请输入选择[1-9]:";
  25. cin >> choice;
  26. switch(choice) {
  27. case 1:
  28. st.inputGraph();
  29. break;
  30. case 2:
  31. st.outputGraph();
  32. break;
  33. case 3:
  34. cout << "请输入顶点:";
  35. cin >> e1;
  36. e2 = st.getValue(st.getFirstNeighbor(st.getVertexPos(e1)));
  37. if(e2)
  38. cout << "顶点" << e1 << "的第一个邻接顶点为:" << e2 << endl;
  39. else
  40. cout << "顶点" << e1 << "没有邻接顶点!" << endl;
  41. break;
  42. case 4:
  43. cout << "请输入顶点v和邻接顶点w:";
  44. cin >> e1 >> e2;
  45. next = st.getValue(st.getNextNeighbor(st.getVertexPos(e1), st.getVertexPos(e2)));
  46. if(next)
  47. cout << "顶点" << e1 << "的邻接顶点" << e2 << "的下一个邻接顶点为:" << next << endl;
  48. else
  49. cout << "顶点" << e1 << "的邻接顶点" << e2 << "没有下一个邻接顶点!" << endl;
  50. break;
  51. case 5:
  52. cout << "请输入要插入的顶点:";
  53. cin >> e1;
  54. if(st.insertVertex(e1))
  55. cout << "插入成功!" << endl;
  56. else
  57. cout << "表已满,插入失败!" << endl;
  58. break;
  59. case 6:
  60. cout << "请输入要插入的边的信息:" << endl;
  61. cin >> e1 >> e2 >> weight;
  62. st.insertEdge(st.getVertexPos(e1), st.getVertexPos(e2), weight);
  63. break;
  64. case 7:
  65. cout << "请输入要删除的顶点:";
  66. cin >> e1;
  67. if(st.removeVertex(st.getVertexPos(e1)))
  68. cout << "顶点" << e1 << "已删除!" << endl;
  69. else
  70. cout << "顶点" << e1 << "不在图中!" << endl;
  71. break;
  72. case 8:
  73. cout << "请输入要删除的边的两个端点:" << endl;
  74. cin >> e1 >> e2;
  75. st.removeEdge(st.getVertexPos(e1), st.getVertexPos(e2));
  76. break;
  77. case 9:
  78. finished = true;
  79. break;
  80. default:
  81. cout << "选择输入错误,请重新输入!" << endl;
  82. }
  83. }
  84. return 0;
  85. }

测试结果:

c语言图的邻接矩阵(图邻接矩阵c代码实现)

c语言图的邻接矩阵(图邻接矩阵c代码实现)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/chuanzhouxiao/article/details/88686127

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。