【发布时间】:2015-09-17 15:38:46
【问题描述】:
我正在尝试使用 BGL 中的 dijkstra 最短路径算法来计算未加权无向图上的简单 ST 路径。将来我可能会关心边权重,但现在我只想将边遍历视为统一成本。
我还在跟踪多个边和顶点属性,因此我将迄今为止所做的工作基于bundled properties example,这似乎与我正在尝试做的事情最接近。
现在我正试图弄清楚如何让 dijkstra 工作,这样我就可以进行 ST 搜索,但我一直无法为其设置正确的参数。
这是我目前所拥有的代码的简化示例:
#include <iostream>
#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>
// Create a struct to hold properties for each vertex
typedef struct VertexProperties
{
int p1;
} VertexProperties;
// Create a struct to hold properties for each edge
typedef struct EdgeProperties
{
int p1;
} EdgeProperties;
// Define the type of the graph
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties> Graph;
int main(int,char*[])
{
// Create a graph object
Graph g;
// Add vertices
Graph::vertex_descriptor v0 = boost::add_vertex(g);
Graph::vertex_descriptor v1 = boost::add_vertex(g);
Graph::vertex_descriptor v2 = boost::add_vertex(g);
// Set vertex properties
g[v0].p1 = 1;
g[v1].p1 = 2;
g[v2].p1 = 3;
// Add edges
std::pair<Graph::edge_descriptor, bool> e01 = boost::add_edge(v0, v1, g);
std::pair<Graph::edge_descriptor, bool> e02 = boost::add_edge(v1, v2, g);
// Set edge properties
g[e01.first].p1 = 1;
g[e02.first].p1 = 2;
std::cout << "num_verts: " << boost::num_vertices(g) << std::endl;
std::cout << "num_edges: " << boost::num_edges(g) << std::endl;
// compute ST shortest paths here...
return 0;
}
我在调用 dijkstra 算法的正确参数上被绊倒了。他们获取图形、起始顶点,然后是前驱图和距离图。到目前为止我看到的示例,例如 this one 仅使用边权重设置他们的图表,而没有捆绑的边属性,这简化了事情。
最终,我追求的是 ST 最短路径,所以我需要恢复从 S 到 T 的路径。从外观上看,我们需要设置一个前置映射,然后我们可以使用它来提取从特定 T 回到 S 的路径?
我还应该注意,我所处的环境不允许 C++11 语言功能。 :(
这里的任何帮助将不胜感激!
【问题讨论】:
-
关于调用 dijkstra 函数的问题很有趣(几乎完整地引用了文档:))但没有显示你“拥有”的东西。我还是想知道。无论如何,希望我的回答能有所启发。
-
感谢您的示例。它的确切形式不能为我编译,但我认为它可以给我一些想法。我不确定我是否遵循“展示我所拥有的”的意思......你的意思是展示答案吗?最终,我正在尝试进行 ST 搜索,并且我想恢复从 S 到 T 的最短路径。根据我的阅读,BGL 使用前任地图来做到这一点。我忘了提到我也不在使用 C++11 的环境中,所以使用 auto 会遗漏很多我需要继续前进的信息:(
-
我的意思是,您的代码实际上并没有尝试调用 dijkstra :) 恢复显式类型并不太难:livecoding.tv/video/making-my-boost-graph-answer-c03-compliant(在答案中发布了代码)
-
Ahhhh... 我只是在注释中注明了我想计算它的位置,因为我想留下一些可以编译的东西,而我之前的尝试没有产生可编译的代码。我认为工作中的防火墙对您提供的流不满意。我回家后看看 :) 谢谢!