使用 BFS 查找到顶点的距离
using namespace boost;
typedef adjacency_list<setS, vecS, bidirectionalS,
no_property,
property<edge_weight_t, float>> Graph_type;
typedef graph_traits<Graph_type>::edge_iterator edge_iterator;
int main() {
// create graph
Graph_type g;
add_edge(0,1,g);
add_edge(1,2,g);
add_edge(2,0,g);
// output graph
std::pair<edge_iterator, edge_iterator> ei = edges(g);
for (edge_iterator edge_iter = ei.first; edge_iter != ei.second; ++edge_iter)
std::cout << source(*edge_iter, g) << " - " << target(*edge_iter, g) << "\n";
// attempt to find shortest distance from vetex 0 to all other vertices
breadth_first_search(g,0);
return 0;
}
我试图从顶点0开始调用vartth_first_search,并获得与图中所有其他顶点的最短距离。我如何错误地调用该功能?建议使用哪种容器来存储结果?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先现代化代码:
clang and GCC 给出不同的诊断 GCC是最详尽的:
确实是检查:确实是检查文档 没有表现出2个参数。您至少需要三分之一,
boost :: nesual_parameters
对象让我们坚持使用:
现在应该告诉我们丢失了哪些参数,如果有:成功!
您可能要传递参数的原因是,如果您的图形没有隐式属性映射(例如
vertex_index_map
),或者当您想覆盖util/out的默认值时,访问者
。在您的情况下,您很有可能要覆盖IT,因为否则您将无法访问问题的答案:prints 在编译器资源管理器上实时:
奖励/警告
警告:您的图形模型“
edge> edge> edge_weight
(尽管您的示例从未将其初始化为非零值)。如果它们是相关的,那么BFS是不够的。对于非负重的重量,您应该使用Dijkstra,在我看来,这恰好更容易使用:“ nofollow noreferrer ”>实时
打印
Modernizing the code first:
Clang and GCC give different diagnostics, GCC's are most elaborate:
Indeed checking the documentation shows no overload that takes 2 arguments. You need at least a third,
boost::named_parameters
objectLet's stick in a stub:
Now it should tell us which params are missing, if any: Success!
The reason you might want to pass parameters is in case your graph doesn't have implicit property maps (like
vertex_index_map
) for mandatory arguments, or when you want to override the default for a UTIL/OUT, likevisitor
. In your case it is very likely that you want to override the it because otherwise you won't be able to access the answer to your question:Prints Live On Compiler Explorer:
BONUS/Caveats
Caution: your graph model "has"
edge_weight
(although your example never initializes it to non-zero values). If they are relevant, then BFS is not enough. For non-negative weights you should use Dijkstra, which happens to be a bit easier to use in my opinion:Live
Prints