我必须在boost库中使用kruskals算法来查找最小生成树的权重。我想我管理了那个
#include <iostream>
#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/kruskal_min_spanning_tree.hpp>
#include<vector>
using namespace std;
using namespace boost;
int main(){
typedef adjacency_list <vecS,vecS,undirectedS,no_property,property <edge_weight_t,int> > Graph;
typedef graph_traits <Graph>::edge_descriptor Edge;
typedef graph_traits <Graph>::vertex_descriptor Vertex;
int a,b,c,no_vertices,no_edges;
cin>>no_vertices>>no_edges;
Graph g(no_vertices);
property_map <Graph,edge_weight_t>::type weightmap=get(edge_weight,g);
vector <Edge> spanning_tree;
for(int i=0;i<no_edges;i++)
{
bool success;
Edge e;
cin>>a>>b>>c;
tie(e,success)=add_edge(a,b,g);
weightmap[e]=c;
}
kruskal_minimum_spanning_tree(g,back_inserter(spanning_tree));
//weight of spanning tree
int ww=0;
graph_traits<Graph>::edge_iterator ei, ei_end;
for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
{
ww=ww+weightmap[*ei];
}
cout<<"\n"<<ww;
return 0;
}
现在我需要找到顶点0和最远点之间的距离(权重之和)?关于我如何做到这一点的任何暗示?
我正在考虑使用顶点迭代器,但后来我将权重存储在weightMap中,那么如果我遍历图形的顶点,如何访问它呢?
编辑:我修改了我的程序,决定使用kruskal和prim1.kruskal用于生成树的重量 2.prim算法,用于每个顶点距顶点0的距离(在地图距离中存储的生成树中)
不幸的是出了点问题,距离[* vertex]这是第三个顶点并没有给出答案2,但给出了一个
生成树的权重也是14而不是7
我的虚拟输入是:
5 6
0 1 1
0 2 2
1 2 5
1 3 1
3 2 2
2 4 3
这是我的节目:
#include <boost/config.hpp>
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/prim_minimum_spanning_tree.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/kruskal_min_spanning_tree.hpp>
using namespace std;
int
main()
{
using namespace boost;
typedef adjacency_list < vecS, vecS, undirectedS,
property<vertex_distance_t, int>, property < edge_weight_t, int > > Graph;
int num_nodes,num_edges,a,b,c;
cin>>num_nodes>>num_edges;
Graph g(num_nodes);
property_map<Graph, edge_weight_t>::type weightmap = get(edge_weight, g);
for (int j = 0; j < num_edges ; ++j) {
cin>>a>>b>>c;
graph_traits<Graph>::edge_descriptor e;
bool inserted;
tie(e, inserted) = add_edge(a, b, g);
weightmap[e] = c;
}
vector < graph_traits < Graph >::vertex_descriptor > p(num_vertices(g));
cout<<num_vertices(g);
property_map<Graph, vertex_distance_t>::type distance = get(vertex_distance, g);
property_map<Graph, vertex_index_t>::type indexmap = get(vertex_index, g);
prim_minimum_spanning_tree
(g, *vertices(g).first, &p[0], distance, weightmap, indexmap,
default_dijkstra_visitor());
vector <graph_traits<Graph>::edge_descriptor> spanning_tree;
kruskal_minimum_spanning_tree(g,back_inserter(spanning_tree));
int ww=0;
typedef graph_traits < Graph >::edge_descriptor Edge;
for (vector<Edge>::iterator et= spanning_tree.begin(); et != spanning_tree.end(); ++et)
{
ww=ww+weightmap[*et];
}
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
std::pair<vertex_iter, vertex_iter> vp;
for (vp = vertices(g); vp.first != vp.second; ++vp.first)
{
cout<<distance[*vp.first];
}
prim_minimum_spanning_tree
(g, *vertices(g).first, &p[0], distance, weightmap, indexmap,
default_dijkstra_visitor());
return EXIT_SUCCESS;
}
感谢你:)
答案 0 :(得分:1)
我不确定如何解释Kruskal MST算法的结果(特别是边缘列表)。这可能是你想要的:
int ww = 0;
for (auto const& e : spanning_tree) {
std::cout << "Traversing: " << source(e,g) << " -> " << target(e,g) << ", cost " << weightmap[e] << "\n";
ww += weightmap[e];
}
cout << "\n" << ww;
否则,您可能希望将前任地图传递给Kruskal,并将其读取为您所需的路径。
同时请参阅上面的草图 Live On Coliru