【问题标题】:Finding the shortest path in a boost graph using Dijkstra使用 Dijkstra 在提升图中查找最短路径
【发布时间】:2018-05-15 13:10:20
【问题描述】:

我是 boost 图表库的新手,我正在尝试使用 boost 图表库在图表中找到从一个顶点到另一个顶点的最短路径。我找到了一些关于如何使用前任地图的说明来做到这一点,但它对我不起作用。当我尝试调用 p[some_vertex] 时,我收到一条错误消息,指出 [] 运算符未定义。谁能告诉我为什么以及我做错了什么?

typedef property<edge_weight_t, double, property<edge_index_t, tElementIDVector>> tEdgeProperty;
typedef property<vertex_index_t, tElementID> VertexIDPorperty;
typedef adjacency_list<vecS, setS, directedS, VertexIDPorperty, tEdgeProperty> tGraph;

typedef tGraph::vertex_descriptor tVertex;
typedef tGraph::edge_descriptor tEdge;

vector<tVertex> p(num_vertices(graph));
vector<double> d(num_vertices(graph));
// call dijkstra algorithm from boost to find shortest path
dijkstra_shortest_paths(graph, s,
    predecessor_map(&p[0]).
    distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, graph))));

//find path from start to end vertex
list<tVertex> pathVertices;
tVertex current = goal;
while (current != start)
{
    pathVertices.push_front(current);
    current = p[current];     //this is where the error occures
}

【问题讨论】:

    标签: c++ boost graph shortest-path


    【解决方案1】:

    来自cppreference:

    std::vector::operator[]

    reference       operator[]( size_type pos );
    const_reference operator[]( size_type pos ) const;
    

    接受 size_t 指定向量元素的位置。

    但是在您的代码中,current 的类型是 tVertex。

    如果你想使用operator[] 和tVertex 参数,你应该覆盖它。

    【讨论】:

      【解决方案2】:

      我无法让您的想法发挥作用,现在也不得不进行一些其他更改,这就是为什么我现在将我的前任地图更改为:

      typedef boost::property_map < tGraph, boost::vertex_index_t >::type IndexMap;
      typedef boost::iterator_property_map < tVertex*, IndexMap, tVertex, tVertex& > PredecessorMap;
      tVertex s = start;
      IndexMap indexMap = get(vertex_index, graph);
      vector<tVertex> p(num_vertices(graph));
      PredecessorMap predecessorMap(&p[0], indexMap);
      vector<double> d(num_vertices(graph));
      
      // call dijkstra algorithm from boost to find shortest path
      dijkstra_shortest_paths(graph, s, predecessor_map(predecessorMap).distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, graph))));
      //find path from start to end vertex
      list<tVertex> pathVertices;
      tVertex current = goal;
      while (current != start)
      {
          pathVertices.push_front(current);
          current = predecessorMap[current];
      }
      

      这实际上现在有效:)

      【讨论】:

        猜你喜欢
        • 2014-08-07
        • 1970-01-01
        • 2012-06-23
        • 1970-01-01
        • 2021-10-29
        • 1970-01-01
        • 2016-07-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多