使用 Dijkstra 在提升图中查找最短路径
Finding the shortest path in a boost graph using Dijkstra
我是 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
}
来自cppreference:
std::vector::operator[]
reference operator[]( size_type pos );
const_reference operator[]( size_type pos ) const;
接受size_t
指定矢量元素的位置。
但是在您的代码中,current
的类型为 tVertex
。
如果你想使用 operator[]
和 tVertex
参数,你应该覆盖它。
我无法让你的想法生效,现在不得不进行一些其他更改,这也是我现在将我的前任地图更改为:
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];
}
现在确实有效:)
我是 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
}
来自cppreference:
std::vector::operator[]
reference operator[]( size_type pos ); const_reference operator[]( size_type pos ) const;
接受size_t
指定矢量元素的位置。
但是在您的代码中,current
的类型为 tVertex
。
如果你想使用 operator[]
和 tVertex
参数,你应该覆盖它。
我无法让你的想法生效,现在不得不进行一些其他更改,这也是我现在将我的前任地图更改为:
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];
}
现在确实有效:)