gpt4 book ai didi

c++ - 从特定起始顶点 boost prim_minimum_spanning_tree

转载 作者:行者123 更新时间:2023-11-30 05:20:49 30 4
gpt4 key购买 nike

对于一个简单的图,直接调用 prim_minimum_spanning_tree 得到从顶点 0 开始的结果:

#include <iostream>

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/prim_minimum_spanning_tree.hpp>

typedef boost::property<boost::edge_weight_t, double> EdgeWeightProperty;

typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, boost::no_property, EdgeWeightProperty> Graph;

int main(int,char*[])
{
// Create a graph object
Graph g(3);

EdgeWeightProperty e1 = 5;
add_edge(0, 1, e1, g);

EdgeWeightProperty e2 = 3;
add_edge(1, 2, e2, g);

std::vector < boost::graph_traits < Graph >::vertex_descriptor > parents(num_vertices(g));

prim_minimum_spanning_tree(g, &parents[0]);

for (std::size_t i = 0; i != parents.size(); ++i) {

if (parents[i] != i) {
std::cout << "parent[" << i << "] = " << parents[i] << std::endl;
}
else {
std::cout << "parent[" << i << "] = no parent" << std::endl;
}
}


return 0;
}

但我似乎无法解密指定不同起始顶点的签名?看起来这是唯一一个采用 vertex_descriptor 的(我假设它是起始顶点?):

  prim_minimum_spanning_tree
(const VertexListGraph& g,
typename graph_traits<VertexListGraph>::vertex_descriptor s,
PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
IndexMap index_map,
DijkstraVisitor vis)

关于如何调用它有什么建议吗?

最佳答案

这需要“命名参数”签名:

prim_minimum_spanning_tree(g, &parents[0], boost::root_vertex(1));

您可以在文档页面的“命名参数”下找到可用的命名参数:http://www.boost.org/doc/libs/1_61_0/libs/graph/doc/prim_minimum_spanning_tree.html

关于c++ - 从特定起始顶点 boost prim_minimum_spanning_tree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40507809/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com