当前位置:   article > 正文

单源最短路径算法dijkstra_dijistra using boost adjacency list

dijistra using boost adjacency list

boost库中最短单源路径算法,主要是 dijkstra_shortest_paths函数。

输入数据:
 

  1. {
  2. "start": "A",
  3. "end": "D",
  4. "edge_list": [
  5. ["A", "C"],
  6. ["B", "B"],
  7. ["B", "D"],
  8. ["B", "E"],
  9. ["C", "B"],
  10. ["C", "D"],
  11. ["D", "E"],
  12. ["E", "A"],
  13. ["E", "B"]
  14. ],
  15. "weight_list": [
  16. 1, 2, 1, 2, 7, 3, 1, 1, 1
  17. ]
  18. }

其中 start是 起点A,end是终点D,  然后 到各个点的权重是 weight_list.

输出为: 其中 path是 A->D的最短路径和权重, parent是 对应的父路径和权重。

  1. {
  2. "parent": [
  3. [ "A", "A", 0.0 ],
  4. [ "B", "E", 6.0 ],
  5. [ "C", "A", 1.0 ],
  6. [ "D", "C", 4.0 ],
  7. [ "E", "D", 5.0 ]
  8. ],
  9. "path": [
  10. [ "A", 0.0 ],
  11. [ "C", 1.0 ],
  12. [ "D", 3.0 ]
  13. ]
  14. }

可视化算法为:

 boost库代码的demo如下:

  1. #include <boost/config.hpp>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <boost/graph/graph_traits.hpp>
  5. #include <boost/graph/adjacency_list.hpp>
  6. #include <boost/graph/dijkstra_shortest_paths.hpp>
  7. using namespace boost;
  8. int main(int, char*[])
  9. {
  10. typedef adjacency_list_traits< listS, listS, directedS >::vertex_descriptor
  11. vertex_descriptor;
  12. typedef adjacency_list< listS, listS, directedS,
  13. property< vertex_index_t, int,
  14. property< vertex_name_t, char,
  15. property< vertex_distance_t, int,
  16. property< vertex_predecessor_t, vertex_descriptor > > > >,
  17. property< edge_weight_t, int > >
  18. graph_t;
  19. typedef std::pair< int, int > Edge;
  20. const int num_nodes = 5;
  21. enum nodes
  22. {
  23. A,
  24. B,
  25. C,
  26. D,
  27. E
  28. };
  29. Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
  30. Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B) };
  31. int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };
  32. int num_arcs = sizeof(edge_array) / sizeof(Edge);
  33. graph_traits< graph_t >::vertex_iterator i, iend;
  34. graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
  35. property_map< graph_t, edge_weight_t >::type weightmap
  36. = get(edge_weight, g);
  37. // Manually intialize the vertex index and name maps
  38. property_map< graph_t, vertex_index_t >::type indexmap
  39. = get(vertex_index, g);
  40. property_map< graph_t, vertex_name_t >::type name = get(vertex_name, g);
  41. int c = 0;
  42. for (boost::tie(i, iend) = vertices(g); i != iend; ++i, ++c)
  43. {
  44. indexmap[*i] = c;
  45. name[*i] = 'A' + c;
  46. }
  47. vertex_descriptor s = vertex(A, g);
  48. property_map< graph_t, vertex_distance_t >::type d
  49. = get(vertex_distance, g);
  50. property_map< graph_t, vertex_predecessor_t >::type p
  51. = get(vertex_predecessor, g);
  52. dijkstra_shortest_paths(g, s, predecessor_map(p).distance_map(d));
  53. std::cout << "distances and parents:" << std::endl;
  54. graph_traits< graph_t >::vertex_iterator vi, vend;
  55. for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi)
  56. {
  57. std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
  58. std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]]
  59. << std::endl;
  60. }
  61. std::cout << std::endl;
  62. return EXIT_SUCCESS;
  63. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/408709
推荐阅读
相关标签
  

闽ICP备14008679号