当前位置:   article > 正文

动态规划之Floyd算法;_好的,请问以下哪些算法是动态规划算法? kruskal算法 floyd算法 dijkstra算法 拓

好的,请问以下哪些算法是动态规划算法? kruskal算法 floyd算法 dijkstra算法 拓扑

如题;这是一套完整的可运行的代码;需要读者有一定的基础去阅读;

语言是用C语言实现;在C++环境中编写;在C++中可直接运行;在C语言中需要改部分头文件和输出语句;

头文件;这要是代码的声明部分;

Prim算法, Kruskal算法, Dijkstra算法, Floyd算法中;只有Floyd算法是动态规划(Dynamic Programming);

其他的三个都是贪心算法(Greedy algorithm);如果需要对算法有更深刻的理解;就要学习动态规划;精髓是解决问题的思想;将复杂的问题划分为小问题求解;

  1. # ifndef _AMGRAPH_
  2. # define _AMGRAPH_
  3. # include <iostream>
  4. using namespace std;
  5. # define MaxVertexNum 256
  6. typedef char VertexType;
  7. typedef int EdgeType;
  8. typedef struct
  9. {
  10. VertexType vertex[MaxVertexNum];
  11. EdgeType edge[MaxVertexNum][MaxVertexNum];
  12. int vertexNum;
  13. int edgeNum;
  14. }AMGraph, * PAMGraph;
  15. int LocateVertex(PAMGraph g, VertexType x);
  16. PAMGraph CreateGraph(void);
  17. void BFS(PAMGraph g, int i);
  18. void BFSTraversal(PAMGraph g);
  19. # define INFINITY 10000
  20. typedef struct
  21. {
  22. int adjvertex;
  23. int lowcost;
  24. }AuxArray;
  25. void MiniSpanTreePrim(PAMGraph g, int i);
  26. void ShortestPathDij(PAMGraph g, int u, int * D, int * P);
  27. void ShortestPathFloyd(PAMGraph g, int ** D, int ** P);
  28. //void Show(int i, int j, int ** D, int ** P);
  29. # endif

实现文件;主要是代码的实现;

  1. # include "AMGraph.h"
  2. # include "SeqQueue.h"
  3. bool visited[MaxVertexNum] = { 0 };
  4. int LocateVertex(PAMGraph g, VertexType x)
  5. {
  6. int i = 0;
  7. while ((i < g->vertexNum) && (g->vertex[i] != x))
  8. {
  9. i += 1;
  10. }
  11. if (i >= g->vertexNum)
  12. {
  13. return -1;
  14. }
  15. else
  16. {
  17. return i;
  18. }
  19. }
  20. PAMGraph CreateGraph(void)
  21. {
  22. PAMGraph g = (PAMGraph)malloc(sizeof(AMGraph));
  23. if (NULL != g)
  24. {
  25. //输入顶点数和边数;
  26. cout << "Please input the vertexNum and edgeNum: " << endl;
  27. cin >> g->vertexNum >> g->edgeNum;
  28. //输入顶点值;
  29. for (int i = 0; i < g->vertexNum; i++)
  30. {
  31. cout << "Please input the element value: " << endl;
  32. cin >> g->vertex[i];
  33. }
  34. //输入边节点的值;
  35. for (int i = 0; i < g->vertexNum; i++)
  36. {
  37. for (int j = 0; j < g->vertexNum; j++)
  38. {
  39. if (i == j)
  40. {
  41. g->edge[i][j] = 0;
  42. }
  43. else
  44. {
  45. g->edge[i][j] = INFINITY;
  46. }
  47. }
  48. }
  49. int i = 0;
  50. int j = 0;
  51. for (int k = 0; k < g->edgeNum; k++)
  52. {
  53. cout << "Please input the two index: " << endl;
  54. cin >> i >> j;
  55. int weight = 0;
  56. cout << "Please input weight value: " << endl;
  57. cin >> weight;
  58. g->edge[i][j] = weight;
  59. //g->edge[j][i] = weight;//如果是无向图要加上;
  60. }
  61. return g;
  62. }
  63. else
  64. {
  65. cout << "Memory allocate is error! " << endl;
  66. system("pause");
  67. exit(0);
  68. }
  69. }
  70. void BFS(PAMGraph g, int i)
  71. {
  72. PSeqQueue q = InitSeqQueue();
  73. cout << g->vertex[i] << endl;
  74. visited[i] = true;
  75. InSeqQueue(q, i);
  76. while (!EmptySeqQueue(q))
  77. {
  78. OutSeqQueue(q, &i);
  79. for (int j = 0; j < g->vertexNum; j++)
  80. {
  81. if (g->edge[i][j] == 1 && !visited[j])
  82. {
  83. cout << g->vertex[j] << endl;
  84. visited[j] = true;
  85. InSeqQueue(q, j);
  86. }
  87. }
  88. }
  89. }
  90. void BFSTraversal(PAMGraph g)
  91. {
  92. for (int i = 0; i < g->vertexNum; i++)
  93. {
  94. visited[i] = false;
  95. }
  96. for (int i = 0; i < g->vertexNum; i++)
  97. {
  98. if (!visited[i])
  99. {
  100. BFS(g, i);
  101. }
  102. }
  103. }
  104. void MiniSpanTreePrim(PAMGraph g, int u)
  105. {
  106. //定义迭代变量;
  107. int i = 0;
  108. int j = 0;
  109. int k = 0;
  110. //动态生成辅助数组;
  111. AuxArray * array = new AuxArray[g->vertexNum];
  112. //将辅助数组从u开始初始化;
  113. for (i = 0; i < g->vertexNum; i++)
  114. {
  115. array[i].adjvertex = u;
  116. array[i].lowcost = g->edge[u][i];
  117. }
  118. for (i = 0; i < g->vertexNum - 1; i++)
  119. {
  120. //设置象征意义的无穷大;
  121. int v = INFINITY;
  122. //查找最小的边;
  123. for (j = 0; j < g->vertexNum; j++)
  124. {
  125. //当前元素不是U集且小于v;
  126. if ((array[j].lowcost != 0) && (array[j].lowcost < v))
  127. {
  128. v = array[j].lowcost;
  129. k = j;
  130. }
  131. }
  132. //将最小的边置0;即进入U集;
  133. array[k].lowcost = 0;
  134. //更新辅助数组;
  135. for (j = 0; j < g->vertexNum; j++)
  136. {
  137. if (g->edge[k][j] < array[j].lowcost)
  138. {
  139. array[j].adjvertex = k;
  140. array[j].lowcost = g->edge[k][j];
  141. }
  142. }
  143. }
  144. //总权值;
  145. int w = 0;
  146. //统计信息;
  147. for (i = 0; i < g->vertexNum; i++)
  148. {
  149. //跳过开始的元素;
  150. if (i != u)
  151. {
  152. cout << i << "->" << array[i].adjvertex << ", " << g->edge[i][array[i].adjvertex] << endl;
  153. w += g->edge[i][array[i].adjvertex];
  154. }
  155. }
  156. cout << "Total weight = " << w << endl;
  157. }
  158. void ShortestPathDij(PAMGraph g, int u, int * D, int * P)
  159. {
  160. //定义迭代变量;
  161. int i = 0;
  162. int j = 0;
  163. int k = 0;
  164. int min = 0;
  165. //定义标志数组;
  166. int * flag = (int *)malloc(g->vertexNum * sizeof(int));
  167. //初始化D, P, flag数组;
  168. for (i = 0; i < g->vertexNum; i++)
  169. {
  170. flag[i] = false;
  171. D[i] = g->edge[u][i];
  172. P[i] = -1;
  173. if (D[i] < INFINITY)
  174. {
  175. P[i] = u;
  176. }
  177. }
  178. flag[u] = true;
  179. P[u] = -1;
  180. //主for循环;
  181. for (i = 0; i < g->vertexNum; i++)
  182. {
  183. //查找最短路径;
  184. k = -1;
  185. min = INFINITY;
  186. for (j = 0; j < g->vertexNum; j++)
  187. {
  188. if ((!flag[j]) && (D[j] < min))
  189. {
  190. k = j;
  191. min = D[j];
  192. }
  193. }
  194. if (k == -1)
  195. {
  196. break;
  197. }
  198. flag[k] = true;
  199. //更新D 和 P数组;
  200. for (j = 0; j < g->vertexNum; j++)
  201. {
  202. if ((!flag[j]) && (min + g->edge[k][j] < D[j]))
  203. {
  204. D[j] = min + g->edge[k][j];
  205. P[j] = k;
  206. }
  207. }
  208. }
  209. }
  210. void ShortestPathFloyd(PAMGraph g, int ** D, int ** P)
  211. {
  212. int i = 0;
  213. int j = 0;
  214. int k = 0;
  215. for (i = 0; i < g->vertexNum; i++)
  216. {
  217. for (j = 0; j < g->vertexNum; j++)
  218. {
  219. D[i][j] = g->edge[i][j];
  220. //这个if-else是记录路径的,目前我还没有搞清楚;
  221. if (D[i][j] < INFINITY)
  222. {
  223. P[i][j] = i;
  224. }
  225. else
  226. {
  227. P[i][j] = -1;
  228. }
  229. }
  230. }
  231. for (k = 0; k < g->vertexNum; k++)
  232. {
  233. for (i = 0; i < g->vertexNum; i++)
  234. {
  235. for (j = 0; j < g->vertexNum; j++)
  236. {
  237. if (D[i][k] + D[k][j] < D[i][j])
  238. {
  239. D[i][j] = D[i][k] + D[k][j];
  240. P[i][j] = k;
  241. }
  242. }
  243. }
  244. }
  245. return;
  246. }
  247. //void Show(int i, int j, int ** D, int ** P)
  248. //{
  249. // cout << D[i][j] << " " << j << "<-";
  250. // while (P[i][j] != -1)
  251. // {
  252. // cout << P[i][j] << "<-";
  253. // j = P[i][j];
  254. // }
  255. //
  256. // return;
  257. //}

Main函数;

  1. # include "AMGraph.h"
  2. int main(int argc, char ** argv)
  3. {
  4. AMGraph g;
  5. g.vertexNum = 6;
  6. g.edgeNum = 8;
  7. for (int i = 0; i < g.vertexNum; i++)
  8. {
  9. for (int j = 0; j < g.vertexNum; j++)
  10. {
  11. if (i == j)
  12. {
  13. g.edge[i][j] = 0;
  14. }
  15. else
  16. {
  17. g.edge[i][j] = INFINITY;
  18. }
  19. }
  20. }
  21. g.edge[0][5] = 100;
  22. g.edge[0][4] = 30;
  23. g.edge[0][2] = 10;
  24. g.edge[1][2] = 5;
  25. g.edge[2][3] = 50;
  26. g.edge[3][5] = 10;
  27. g.edge[4][3] = 20;
  28. g.edge[4][5] = 60;
  29. //cout << "----------------------------" << endl;
  30. //BFSTraversal(g);
  31. //cout << "----------------------------" << endl;
  32. //MiniSpanTreePrim(g, 3);
  33. //int * D = new int[g.vertexNum];
  34. //int * P = new int[g.vertexNum];
  35. //ShortestPathDij(&g, 0, D, P);
  36. //for (int i = 0; i < g.vertexNum; i++)
  37. //{
  38. // cout << D[i] << endl;
  39. //}
  40. //for (int i = 0; i < g.vertexNum; i++)
  41. //{
  42. // if (P[i] == -1)
  43. // {
  44. // continue;
  45. // }
  46. //
  47. // cout << i << "<-";
  48. // int j = i;
  49. // while (P[j] != -1)
  50. // {
  51. // cout << P[j] << "<-";
  52. // j = P[j];
  53. // }
  54. //
  55. // cout << ", " << D[i] << endl;
  56. //}
  57. AMGraph gg;
  58. gg.vertexNum = 3;
  59. gg.edgeNum = 5;
  60. for (int i = 0; i < gg.vertexNum; i++)
  61. {
  62. for (int j = 0; j < gg.vertexNum; j++)
  63. {
  64. //gg.edge[i][j] = INFINITY;
  65. if (i == j)
  66. {
  67. gg.edge[i][j] = 0;
  68. }
  69. else
  70. {
  71. gg.edge[i][j] = INFINITY;
  72. }
  73. }
  74. }
  75. gg.edge[0][1] = 4;
  76. gg.edge[0][2] = 11;
  77. gg.edge[2][0] = 3;
  78. gg.edge[1][0] = 6;
  79. gg.edge[1][2] = 2;
  80. int ** DD = (int **)malloc(gg.vertexNum * sizeof(int *));
  81. for (int i = 0; i < gg.vertexNum; i++)
  82. {
  83. DD[i] = (int *)malloc(gg.vertexNum * sizeof(int));
  84. }
  85. int ** PP = new int *[gg.vertexNum];
  86. for (int i = 0; i < gg.vertexNum; i++)
  87. {
  88. PP[i] = new int[gg.vertexNum];
  89. }
  90. ShortestPathFloyd(&gg, DD, PP);
  91. cout << endl << "----------------------------------" << endl;
  92. for (int i = 0; i < gg.vertexNum; i++)
  93. {
  94. for (int j = 0; j < gg.vertexNum; j++)
  95. {
  96. cout << DD[i][j] << endl;
  97. }
  98. }
  99. cout << endl << "----------------------------------" << endl;
  100. Show(0, 1, DD, PP);
  101. cout << "--------" << endl;
  102. Show(2, 0, DD, PP);
  103. cout << "--------" << endl;
  104. Show(0, 2, DD, PP);
  105. system("pause");
  106. return 0;
  107. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/822045
推荐阅读
相关标签
  

闽ICP备14008679号