增强图形库,在应用 dijkstra 算法时导致错误

Boost graph library causing errors when applying dijkstra's algorithm

本文关键字:错误 算法 dijkstra 图形库 应用 增强      更新时间:2023-10-16

我一直在遵循这个例子,这个例子,以及这个堆栈溢出帖子,试图应用Dijkstra算法来找到两个节点之间最短路径的成本。

如果我尝试遵循第一个示例,则NameMap的typedef语句会出现错误。这个错误是神秘的,冗长的,我不知道该怎么处理它。

如果我尝试遵循第二个示例(从Boost文档中复制粘贴!!),它不会编译。这个错误甚至更加神秘和冗长。

第三个(堆栈溢出post)依赖于与第一个相同的类型定义。

是用户错误吗?它可能是,但是我应该如何解释从库代码中生成的错误消息?

更新1

我正在使用g++ (Debian 4.8.2-21) 4.8.2从Debian测试。

更新2

这是一个不能工作的源代码的精简版本。有两行以"//下一行导致错误"开头的行是有问题的。

更新3 我已经改变了

typedef adjacency_list<listS, vecS, directedS, allow_parallel_edge_tag, EdgeWeightProperty> Graph;
typedef adjacency_list<listS, vecS, directedS, no_property            , EdgeWeightProperty> Graph;

您的第一次尝试没有使用vertex_name_t标签定义属性(或将其作为adjacency_list模板参数传递),因此当您尝试使用该标签创建property_map时,编译器会发出错误。

代码:

typedef property<edge_weight_t, Weight> EdgeWeightProperty;
typedef boost::adjacency_list<listS, vecS, directedS, allow_parallel_edge_tag, EdgeWeightProperty> Graph;
                                                  //  ^ What's this?

您引用的示例代码:

typedef boost::property<boost::edge_weight_t, Weight> WeightProperty;
typedef boost::property<boost::vertex_name_t, std::string> NameProperty;  // <-- not in your code
typedef boost::adjacency_list < boost::listS, boost::vecS, boost::directedS, NameProperty, WeightProperty > Graph;
                                                                         //  ^ Used here

我不知道为什么你传递allow_parallel_edge_tag作为模板参数。如果我正确阅读文档,当您使用自定义容器类型时,该结构体是为parallel_edge_traits专门化设计的。

编辑:第二种情况其实很容易诊断,一旦你有了代码。通过查看编译器发出的错误消息,我们寻找编译器没有为dijkstra_shortest_paths选择3参数重载的原因。许多消息只是告诉您,它拒绝了大约十几个参数的重载—正如它应该的那样!

现在,这个错误消息(由g++使用Coliru发出)是相关的,因为它告诉您为什么编译器拒绝了三个参数版本:

In file included from main.cpp:5:0:
/usr/local/include/boost/graph/dijkstra_shortest_paths.hpp:602:3: note: void boost::
dijkstra_shortest_paths(const VertexListGraph&, typename boost::graph_traits<Graph>::
vertex_descriptor, const boost::bgl_named_params<T, Tag, Base>&) [ /* irrelevant stuff
telling you how it deduced the template parameters here */ ] <near match>
   dijkstra_shortest_paths
   ^
/usr/local/include/boost/graph/dijkstra_shortest_paths.hpp:602:3: note:   no known conversion for
 argument 2 from 'long int [6]' to 'boost::graph_traits<boost::adjacency_list<boost::listS, 
boost::vecS, boost::directedS, boost::no_property, boost::property<boost::edge_weight_t, long int> > 
>::vertex_descriptor {aka long unsigned int}'

你传递了包含源顶点的数组s作为指定起始顶点的第二个参数,当你应该传递v0时,编译器正确地抱怨它不能将长数组转换为单个顶点。