使用简单的图形,可以直接调用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)
关于如何调用它的任何建议?
答案 0 :(得分:1)
这需要&#34;命名参数&#34;签名:
prim_minimum_spanning_tree(g, &parents[0], boost::root_vertex(1));
您可以在&#34;命名参数&#34;下找到可用的命名参数。在文档页面上:http://www.boost.org/doc/libs/1_61_0/libs/graph/doc/prim_minimum_spanning_tree.html