服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++求所有顶点之间的最短路径(用Dijkstra算法)

C++求所有顶点之间的最短路径(用Dijkstra算法)

2021-09-02 15:46ChanJose C/C++

这篇文章主要为大家详细介绍了C++用Dijkstra算法求所有顶点之间的最短路径,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++求所有顶点之间最短路径的具体代码,供大家参考,具体内容如下

一、思路: 不能出现负权值的边

C++求所有顶点之间的最短路径(用Dijkstra算法)

(1)轮流以每一个顶点为源点,重复执行Dijkstra算法n次,就可以求得每一对顶点之间的最短路径及最短路径长度,总的执行时间为O(n的3次方)

(2)另一种方法:用Floyd算法,总的执行时间为O(n的3次方)(另一文章会写)

二、实现程序:

1.Graph.h:有向图

  1. #ifndef Graph_h
  2. #define Graph_h
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. const int DefaultVertices = 30;
  7.  
  8. template <class T, class E>
  9. struct Edge { // 边结点的定义
  10. int dest; // 边的另一顶点位置
  11. E cost; // 表上的权值
  12. Edge<T, E> *link; // 下一条边链指针
  13. };
  14.  
  15. template <class T, class E>
  16. struct Vertex { // 顶点的定义
  17. T data; // 顶点的名字
  18. Edge<T, E> *adj; // 边链表的头指针
  19. };
  20.  
  21. template <class T, class E>
  22. class Graphlnk {
  23. public:
  24. const E maxValue = 100000; // 代表无穷大的值(=∞)
  25. Graphlnk(int sz=DefaultVertices); // 构造函数
  26. ~Graphlnk(); // 析构函数
  27. void inputGraph(); // 建立邻接表表示的图
  28. void outputGraph(); // 输出图中的所有顶点和边信息
  29. T getValue(int i); // 取位置为i的顶点中的值
  30. E getWeight(int v1, int v2); // 返回边(v1, v2)上的权值
  31. bool insertVertex(const T& vertex); // 插入顶点
  32. bool insertEdge(int v1, int v2, E weight); // 插入边
  33. bool removeVertex(int v); // 删除顶点
  34. bool removeEdge(int v1, int v2); // 删除边
  35. int getFirstNeighbor(int v); // 取顶点v的第一个邻接顶点
  36. int getNextNeighbor(int v,int w); // 取顶点v的邻接顶点w的下一邻接顶点
  37. int getVertexPos(const T vertex); // 给出顶点vertex在图中的位置
  38. int numberOfVertices(); // 当前顶点数
  39. private:
  40. int maxVertices; // 图中最大的顶点数
  41. int numEdges; // 当前边数
  42. int numVertices; // 当前顶点数
  43. Vertex<T, E> * nodeTable; // 顶点表(各边链表的头结点)
  44. };
  45.  
  46. // 构造函数:建立一个空的邻接表
  47. template <class T, class E>
  48. Graphlnk<T, E>::Graphlnk(int sz) {
  49. maxVertices = sz;
  50. numVertices = 0;
  51. numEdges = 0;
  52. nodeTable = new Vertex<T, E>[maxVertices]; // 创建顶点表数组
  53. if(nodeTable == NULL) {
  54. cerr << "存储空间分配错误!" << endl;
  55. exit(1);
  56. }
  57. for(int i = 0; i < maxVertices; i++)
  58. nodeTable[i].adj = NULL;
  59. }
  60.  
  61. // 析构函数
  62. template <class T, class E>
  63. Graphlnk<T, E>::~Graphlnk() {
  64. // 删除各边链表中的结点
  65. for(int i = 0; i < numVertices; i++) {
  66. Edge<T, E> *p = nodeTable[i].adj; // 找到其对应链表的首结点
  67. while(p != NULL) { // 不断地删除第一个结点
  68. nodeTable[i].adj = p->link;
  69. delete p;
  70. p = nodeTable[i].adj;
  71. }
  72. }
  73. delete []nodeTable; // 删除顶点表数组
  74. }
  75.  
  76. // 建立邻接表表示的图
  77. template <class T, class E>
  78. void Graphlnk<T, E>::inputGraph() {
  79. int n, m; // 存储顶点树和边数
  80. int i, j, k;
  81. T e1, e2; // 顶点
  82. E weight; // 边的权值
  83.  
  84. cout << "请输入顶点数和边数:" << endl;
  85. cin >> n >> m;
  86. cout << "请输入各顶点:" << endl;
  87. for(i = 0; i < n; i++) {
  88. cin >> e1;
  89. insertVertex(e1); // 插入顶点
  90. }
  91.  
  92. cout << "请输入图的各边的信息:" << endl;
  93. i = 0;
  94. while(i < m) {
  95. cin >> e1 >> e2 >> weight;
  96. j = getVertexPos(e1);
  97. k = getVertexPos(e2);
  98. if(j == -1 || k == -1)
  99. cout << "边两端点信息有误,请重新输入!" << endl;
  100. else {
  101. insertEdge(j, k, weight); // 插入边
  102. i++;
  103. }
  104. } // while
  105. }
  106.  
  107. // 输出有向图中的所有顶点和边信息
  108. template <class T, class E>
  109. void Graphlnk<T, E>::outputGraph() {
  110. int n, m, i;
  111. T e1, e2; // 顶点
  112. E weight; // 权值
  113. Edge<T, E> *p;
  114.  
  115. n = numVertices;
  116. m = numEdges;
  117. cout << "图中的顶点数为" << n << ",边数为" << m << endl;
  118. for(i = 0; i < n; i++) {
  119. p = nodeTable[i].adj;
  120. while(p != NULL) {
  121. e1 = getValue(i); // 有向边<i, p->dest>
  122. e2 = getValue(p->dest);
  123. weight = p->cost;
  124. cout << "<" << e1 << ", " << e2 << ", " << weight << ">" << endl;
  125. p = p->link; // 指向下一个邻接顶点
  126. }
  127. }
  128. }
  129.  
  130. // 取位置为i的顶点中的值
  131. template <class T, class E>
  132. T Graphlnk<T, E>::getValue(int i) {
  133. if(i >= 0 && i < numVertices)
  134. return nodeTable[i].data;
  135. return NULL;
  136. }
  137.  
  138. // 返回边(v1, v2)上的权值
  139. template <class T, class E>
  140. E Graphlnk<T, E>::getWeight(int v1, int v2) {
  141. if(v1 != -1 && v2 != -1) {
  142. if(v1 == v2) // 说明是同一顶点
  143. return 0;
  144. Edge<T , E> *p = nodeTable[v1].adj; // v1的第一条关联的边
  145. while(p != NULL && p->dest != v2) { // 寻找邻接顶点v2
  146. p = p->link;
  147. }
  148. if(p != NULL)
  149. return p->cost;
  150. }
  151. return maxValue; // 边(v1, v2)不存在,就存放无穷大的值
  152. }
  153.  
  154. // 插入顶点
  155. template <class T, class E>
  156. bool Graphlnk<T, E>::insertVertex(const T& vertex) {
  157. if(numVertices == maxVertices) // 顶点表满,不能插入
  158. return false;
  159. nodeTable[numVertices].data = vertex; // 插入在表的最后
  160. numVertices++;
  161. return true;
  162. }
  163.  
  164. // 插入边
  165. template <class T, class E>
  166. bool Graphlnk<T, E>::insertEdge(int v1, int v2, E weight) {
  167. if(v1 == v2) // 同一顶点不插入
  168. return false;
  169. if(v1 >= 0 && v1 < numVertices && v2 >= 0 && v2 < numVertices) {
  170. Edge<T, E> *p = nodeTable[v1].adj; // v1对应的边链表头指针
  171. while(p != NULL && p->dest != v2) // 寻找邻接顶点v2
  172. p = p->link;
  173. if(p != NULL) // 已存在该边,不插入
  174. return false;
  175. p = new Edge<T, E>; // 创建新结点
  176. p->dest = v2;
  177. p->cost = weight;
  178. p->link = nodeTable[v1].adj; // 链入v1边链表
  179. nodeTable[v1].adj = p;
  180. numEdges++;
  181. return true;
  182. }
  183. return false;
  184. }
  185.  
  186. // 有向图删除顶点较麻烦
  187. template <class T, class E>
  188. bool Graphlnk<T, E>::removeVertex(int v) {
  189. if(numVertices == 1 || v < 0 || v > numVertices)
  190. return false; // 表空或顶点号超出范围
  191.  
  192. Edge<T, E> *p, *s;
  193. // 1.清除顶点v的边链表结点w 边<v,w>
  194. while(nodeTable[v].adj != NULL) {
  195. p = nodeTable[v].adj;
  196. nodeTable[v].adj = p->link;
  197. delete p;
  198. numEdges--; // 与顶点v相关联的边数减1
  199. } // while结束
  200. // 2.清除<w, v>,与v有关的边
  201. for(int i = 0; i < numVertices; i++) {
  202. if(i != v) { // 不是当前顶点v
  203. s = NULL;
  204. p = nodeTable[i].adj;
  205. while(p != NULL && p->dest != v) {// 在顶点i的链表中找v的顶点
  206. s = p;
  207. p = p->link; // 往后找
  208. }
  209. if(p != NULL) { // 找到了v的结点
  210. if(s == NULL) { // 说明p是nodeTable[i].adj
  211. nodeTable[i].adj = p->link;
  212. } else {
  213. s->link = p->link; // 保存p的下一个顶点信息
  214. }
  215. delete p; // 删除结点p
  216. numEdges--; // 与顶点v相关联的边数减1
  217. }
  218. }
  219. }
  220. numVertices--; // 图的顶点个数减1
  221. nodeTable[v].data = nodeTable[numVertices].data; // 填补,此时numVertices,比原来numVertices小1,所以,这里不需要numVertices-1
  222. nodeTable[v].adj = nodeTable[numVertices].adj;
  223. // 3.要将填补的顶点对应的位置改写
  224. for(int i = 0; i < numVertices; i++) {
  225. p = nodeTable[i].adj;
  226. while(p != NULL && p->dest != numVertices) // 在顶点i的链表中找numVertices的顶点
  227. p = p->link; // 往后找
  228. if(p != NULL) // 找到了numVertices的结点
  229. p->dest = v; // 将邻接顶点numVertices改成v
  230. }
  231. return true;
  232. }
  233.  
  234. // 删除边
  235. template <class T, class E>
  236. bool Graphlnk<T, E>::removeEdge(int v1, int v2) {
  237. if(v1 != -1 && v2 != -1) {
  238. Edge<T, E> * p = nodeTable[v1].adj, *q = NULL;
  239. while(p != NULL && p->dest != v2) { // v1对应边链表中找被删除边
  240. q = p;
  241. p = p->link;
  242. }
  243. if(p != NULL) { // 找到被删除边结点
  244. if(q == NULL) // 删除的结点是边链表的首结点
  245. nodeTable[v1].adj = p->link;
  246. else
  247. q->link = p->link; // 不是,重新链接
  248. delete p;
  249. return true;
  250. }
  251. }
  252. return false; // 没有找到结点
  253. }
  254.  
  255. // 取顶点v的第一个邻接顶点
  256. template <class T, class E>
  257. int Graphlnk<T, E>::getFirstNeighbor(int v) {
  258. if(v != -1) {
  259. Edge<T, E> *p = nodeTable[v].adj; // 对应链表第一个边结点
  260. if(p != NULL) // 存在,返回第一个邻接顶点
  261. return p->dest;
  262. }
  263. return -1; // 第一个邻接顶点不存在
  264. }
  265.  
  266. // 取顶点v的邻接顶点w的下一邻接顶点
  267. template <class T, class E>
  268. int Graphlnk<T, E>::getNextNeighbor(int v,int w) {
  269. if(v != -1) {
  270. Edge<T, E> *p = nodeTable[v].adj; // 对应链表第一个边结点
  271. while(p != NULL && p->dest != w) // 寻找邻接顶点w
  272. p = p->link;
  273. if(p != NULL && p->link != NULL)
  274. return p->link->dest; // 返回下一个邻接顶点
  275. }
  276. return -1; // 下一个邻接顶点不存在
  277. }
  278.  
  279. // 给出顶点vertex在图中的位置
  280. template <class T, class E>
  281. int Graphlnk<T, E>::getVertexPos(const T vertex) {
  282. for(int i = 0; i < numVertices; i++)
  283. if(nodeTable[i].data == vertex)
  284. return i;
  285. return -1;
  286. }
  287.  
  288. // 当前顶点数
  289. template <class T, class E>
  290. int Graphlnk<T, E>::numberOfVertices() {
  291. return numVertices;
  292. }
  293.  
  294. #endif /* Graph_h */

2.Dijkstra.h

  1. #ifndef Dijkstra_h
  2. #define Dijkstra_h
  3. #include "Graph.h"
  4.  
  5. template <class T, class E>
  6. void ShortestPath(Graphlnk<T, E> &G, E dist[], int path[]) {
  7. int n = G.numberOfVertices(); // 顶点数
  8.  
  9. for(int i = 0; i < n; i++) {
  10. Dijkstra(G, i, dist, path); // 调用Dijkstra函数
  11. printShortestPath(G, i, dist, path); // 输出最短路径
  12. cout << endl;
  13. }
  14. }
  15.  
  16. // Dijkstra算法
  17. template <class T, class E>
  18. void Dijkstra(Graphlnk<T, E> &G, int v, E dist[], int path[]) {
  19. // Graph是一个带权有向图,dist[]是当前求到的从顶点v到顶点j的最短路径长度,同时用数组
  20. // path[]存放求到的最短路径
  21. int n = G.numberOfVertices(); // 顶点数
  22. bool *s = new bool[n]; // 最短路径顶点集
  23. int i, j, k, u;
  24. E w, min;
  25.  
  26. for(i = 0; i < n; i++) {
  27. dist[i] = G.getWeight(v,i); // 数组初始化,获取(v,i)边的权值
  28. s[i] = false; // 该顶点未被访问过
  29. if(i != v && dist[i] < G.maxValue) // 顶点i是v的邻接顶点
  30. path[i] = v; // 将v标记为顶点i的最短路径
  31. else
  32. path[i] = -1; // 说明该顶点i与顶点v没有边相连
  33. }
  34. s[v] = true; // 标记为访问过,顶点v加入s集合中
  35. dist[v] = 0;
  36. for(i = 0; i < n-1; i++) {
  37. min = G.maxValue;
  38. u = v; // 选不在生成树集合s[]中的顶点
  39. // 1.找v的权值最小且未被访问过的邻接顶点w,<v,w>
  40. for(j = 0; j < n; j++) {
  41. if(s[j] == false && dist[j] < min) {
  42. u = j;
  43. min = dist[j];
  44. }
  45. }
  46. s[u] = true; // 将顶点u加入到集合s
  47. for(k = 0; k < n; k++) { // 修改
  48. w = G.getWeight(u, k);
  49. if(s[k] == false && w < G.maxValue && dist[u] + w < dist[k]) {
  50. // 顶点k未被访问过,且从v->u->k的路径比v->k的路径短
  51. dist[k] = dist[u] + w;
  52. path[k] = u; // 修改到k的最短路径
  53. }
  54. }
  55. }
  56. }
  57.  
  58. // 从path数组读取最短路径的算法
  59. template <class T, class E>
  60. void printShortestPath(Graphlnk<T, E> &G, int v, E dist[], int path[]) {
  61. int i, j, k, n = G.numberOfVertices();
  62. int *d = new int[n];
  63.  
  64. cout << "从顶点" << G.getValue(v) << "到其他各顶点的最短路径为:" << endl;
  65. for(i = 0; i < n; i++) {
  66. if(i != v) { // 如果不是顶点v
  67. j = i;
  68. k = 0;
  69. while(j != v) {
  70. d[k++] = j;
  71. j = path[j];
  72. }
  73. cout << "顶点" << G.getValue(i) << "的最短路径为:" << G.getValue(v);
  74. while(k > 0)
  75. cout << "->" << G.getValue(d[--k]);
  76. cout << ",最短路径长度为:" << dist[i] << endl;
  77. }
  78. }
  79. }
  80.  
  81. #endif /* Dijkstra_h */

3.main.cpp

  1. /*
  2. 测试数据:
  3. 4 8
  4. 0 1 2 3
  5. 0 1 1
  6. 0 3 4
  7. 1 2 9
  8. 1 3 2
  9. 2 0 3
  10. 2 1 5
  11. 2 3 8
  12. 3 2 6
  13. */
  14.  
  15. #include "Dijkstra.h"
  16.  
  17. const int maxSize = 40;
  18.  
  19. int main(int argc, const char * argv[]) {
  20. Graphlnk<char, int> G; // 声明图对象
  21. int dist[maxSize], path[maxSize];
  22.  
  23. // 创建图
  24. G.inputGraph();
  25. cout << "图的信息如下:" << endl;
  26. G.outputGraph();
  27. // 求所有顶点之间的最短路径
  28. ShortestPath(G, dist, path);
  29. return 0;
  30. }

测试结果:

C++求所有顶点之间的最短路径(用Dijkstra算法)

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

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

延伸 · 阅读

精彩推荐