Boost的Dijkstra算法教程

Qma*_*man 7 c++ boost dijkstra visual-c++

我很难弄清楚如何使用Boost的Dijkstra算法.我已经查看了他们的示例和文档,但我仍然无法理解如何使用它.

[增强的文档:http://www.boost.org/doc/libs/1_50_0/libs/graph/doc/dijkstra_shortest_paths.html] [迪杰斯特拉的实施例:http://www.boost.org/doc/libs/1_36_0 /libs/graph/example/dijkstra-example.cpp]

有人可以用代码示例提供一步一步的解释,以展示如何使用Boost的Dijkstra算法吗?我正在使用Boost的adjacency_list作为我的图表,就像上面的示例链接一样.(adjacency_list:http://www.boost.org/doc/libs/1_50_0/libs/graph/doc/adjacency_list.html)

Gri*_*zly 12

关于评论中的问题:

  1. 根据VC++示例源代码中的注释,使用的命名参数机制存在一些问题.因此,我假设两个分支基本上相同,VC++版本只是更冗长(我没有深入研究它足够100%肯定).
  2. property_map地图顶点/边与特定顶点/边相关联的附加数据.例如,weightmap在该示例中,将权重(旅行成本)与每个边缘相关联.
  3. predecessor_map用于记录的所有顶点路径(每个顶点从根的路径上的前身被记录).至于为什么需要它:那些信息通常是希望从算法中获得的.

  4. 参数清楚地列在说明中.请注意这两个版本,一个带有命名参数,另一个没有(后者在VC++中使用).

现在对于文档中给出的示例代码有点一步一步(注意我从未实际使用过Boost.Graph,所以这不保证正确性,我也只包含相关部分并省略了#ifVC++):

  const int num_nodes = 5;
  //names of graph nodes
  enum nodes { A, B, C, D, E };
  char name[] = "ABCDE";
  //edges of the graph
  Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
    Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B)
  };
  //weights/travelling costs for the edges
  int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };
  int num_arcs = sizeof(edge_array) / sizeof(Edge);

  //graph created from the list of edges
  graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
  //create the property_map from edges to weights
  property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);

  //create vectors to store the predecessors (p) and the distances from the root (d)
  std::vector<vertex_descriptor> p(num_vertices(g));
  std::vector<int> d(num_vertices(g));
  //create a descriptor for the source node
  vertex_descriptor s = vertex(A, g);

  //evaluate dijkstra on graph g with source s, predecessor_map p and distance_map d
  //note that predecessor_map(..).distance_map(..) is a bgl_named_params<P, T, R>, so a named parameter
  dijkstra_shortest_paths(g, s, predecessor_map(&p[0]).distance_map(&d[0]));
Run Code Online (Sandbox Code Playgroud)

正如我在评论中个人提到的,我发现柠檬比Boost.Graph更直观,所以也许你可能想看一下