Boost的Dijkstra算法教程(Boost's Dijkstra's Algo

2019-09-20 11:17发布

我有困难搞清楚如何使用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 )

Answer 1:

关于注释中的问题:

  1. 根据该示例VC ++的源代码的注释有一些问题与使用命名参数机制 。 所以我假定这两个分支基本上做同样想用VC ++版本只是被更详细的(我没有深入到它足够长的时间是100%肯定,虽然)。
  2. property_map映射顶点/边与特定顶点/边相关联的附加数据。 例如weightmap在该示例的权重(行驶费用)用每个边缘相关联。
  3. 所述predecessor_map用来记录所有顶点路径(每个顶点从根的路径上的前身被记录)。 至于为什么它的需要:嗯,这是信息的一个东西往往希望走出算法。

  4. 该参数在明确列出的描述 。 注意两个版本,一个名为参数,一个没有(在VC ++以后使用)。

现在有点步步的在给出的示例代码一步的文档 (注意,我从来没有实际使用Boost.Graph,所以这是对正确性没有保证,我也只包括了相关的部分和省略了#if为VC ++):

  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]));

正如我在评论中提到的,我个人觉得柠檬更直观地再使用Boost.Graph,所以也许你可能想给那看看,而不是



文章来源: Boost's Dijkstra's Algorithm Tutorial