BGL : dijkstra_shortest_paths usage

BGL : dijkstra_shortest_paths usage

本文关键字:paths usage shortest BGL dijkstra      更新时间:2023-10-16

我很难使用dijkstra_shortest_paths()。下面是一个代码片段,它提供了大量的编译错误输出:

Vertex_t s = *departIter; /* VertexIterator_t departIter */
std::vector<Vertex_t> p( boost::num_vertices( this->m_g ) ); /* Graph_t m_g; */
std::vector<EdgeProperties_t> d( boost::num_vertices( this->m_g ) );
dijkstra_shortest_paths(
    this->m_g, s, predecessor_map(&p[0]).distance_map(&d[0])
);

下面是我的代码中使用的typedef:

struct EdgeProperties_t {
   EdgeProperties_t( float fWeight ) : m_fWeight( fWeight ) { }
   EdgeProperties_t( ) : m_fWeight( static_cast<float>(0.0) ) { }
   float m_fWeight;
};
struct VertexProperties_t {
   std::string m_sName;
};
typedef adjacency_list<
    vecS, listS, undirectedS, VertexProperties_t, EdgeProperties_t
> Graph_t;
typedef boost::graph_traits<Graph_t>::vertex_descriptor Vertex_t;
typedef boost::graph_traits<Graph_t>::vertex_iterator VertexIterator_t;

有人能告诉我它可能出了什么问题吗?我的眼睛好像被肥皂泡过了:(.

如果没有一个独立的源代码列表,很难提供帮助,但我认为一个问题是需要提供顶点索引映射。

然而,首先,std::vector<EdgeProperties_t> d(/*...*/);是不正确的。距离贴图中的值类型应为边权重总和的CopyAssignable。在这种情况下,float将起作用。

我试图使用您的图形类型defs修改BGL的dijkstra-example.cpp,结果遇到了:

。。。在dijkstra示例.cpp:7:/usr/local/include/boost/graph/dijkstra_shortest_paths.hpp:613:8:错误:否用于调用"choose_const_pmap"的匹配函数choose_const_pmap(get_param(params,vertex_index),g,vertex_index),^~~~~~~~~~~~~~~~~dijkstra example.cpp:57:3:注意:在函数模板的实例化中专业化'boost::dijkstra_shortest_paths,boost::adj_list_edge_property_map,boost::edge_weight_t,boost::no_property>'此处请求dijkstra_shortest_path(g,^/usr/local/include/boost/graph/named_function_params.hpp:305:3:注意:候选者忽略模板:替换失败[带Param=boost::param_not_found,Graph=boost::邻接列表,PropertyTag=boost:vertex_index_t]choose_const_pmap(const Param&p、const Graph&g、PropertyTag标记)^生成3个错误。

这指示BGL在获取顶点的顶点索引方面存在问题。要解决此问题,可以为dijkstra_shortest_path()指定一个名为vertex_index_map的参数。

我为我的案例找到的解决方案是以下代码:

Vertex_t startVertex = *departIter;
std::vector<Vertex_t> p( graphSize );
std::vector<float> d( graphSize );
dijkstra_shortest_paths(this->m_g, startVertex, predecessor_map(&p[0]).distance_map(&d[0]));

和以下类型定义:

typedef adjacency_list< listS,
                        vecS,
                        undirectedS,
                        property < vertex_name_t, std::string >,
                        property < edge_weight_t, float > > Graph_t;
typedef boost::graph_traits<Graph_t>::vertex_descriptor Vertex_t;

如果在Graph_t定义中交换listS和vecS,您将面临许多编译问题。希望它能帮助别人。。。

如果它仍然是实际的,请尝试一下

#include <string>
#include <vector>
#include <boost/config.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/filtered_graph.hpp>
//using namespace boost;
struct EdgeProperties_t {
   EdgeProperties_t( float fWeight ) : m_fWeight( fWeight ) { }
   EdgeProperties_t( ) : m_fWeight( static_cast<float>(0.0) ) { }
   float m_fWeight;
};
struct VertexProperties_t {
   std::string m_sName;
};
typedef boost::adjacency_list<boost::vecS, boost::listS, boost::undirectedS, VertexProperties_t, EdgeProperties_t> Graph_t;
typedef boost::graph_traits<Graph_t>::vertex_descriptor Vertex_t;
typedef boost::graph_traits<Graph_t>::vertex_iterator VertexIterator_t;
int main(int argc, char *argv[])
{
    Graph_t m_g;
    //here you should fill your graph....
    VertexIterator_t departIter = vertices(m_g).first; //iterator should point to first fertex in you graph
    Vertex_t s = *departIter;
    std::vector<Vertex_t> p( boost::num_vertices(m_g));
    std::vector<float> d( boost::num_vertices(m_g)); //here you should use type of a field, not a structure itself
    boost::dijkstra_shortest_paths(m_g, s, boost::weight_map(get(&VertexProperties_t::m_sName, m_g))
                                   .predecessor_map(&p[0])
                                   .distance_map(&d[0]));
}