1 | //======================================================================= |
---|
2 | // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, |
---|
3 | // |
---|
4 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
5 | // accompanying file LICENSE_1_0.txt or copy at |
---|
6 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
7 | //======================================================================= |
---|
8 | #include <boost/config.hpp> |
---|
9 | #include <iostream> |
---|
10 | #include <fstream> |
---|
11 | #include <boost/lexical_cast.hpp> |
---|
12 | #include <boost/graph/graphviz.hpp> |
---|
13 | #include <boost/graph/kruskal_min_spanning_tree.hpp> |
---|
14 | |
---|
15 | int |
---|
16 | main() |
---|
17 | { |
---|
18 | using namespace boost; |
---|
19 | GraphvizGraph g_dot; |
---|
20 | read_graphviz("figs/telephone-network.dot", g_dot); |
---|
21 | |
---|
22 | typedef adjacency_list < vecS, vecS, undirectedS, no_property, |
---|
23 | property < edge_weight_t, int > > Graph; |
---|
24 | Graph g(num_vertices(g_dot)); |
---|
25 | property_map < GraphvizGraph, edge_attribute_t >::type |
---|
26 | edge_attr_map = get(edge_attribute, g_dot); |
---|
27 | graph_traits < GraphvizGraph >::edge_iterator ei, ei_end; |
---|
28 | for (tie(ei, ei_end) = edges(g_dot); ei != ei_end; ++ei) { |
---|
29 | int weight = lexical_cast < int >(edge_attr_map[*ei]["label"]); |
---|
30 | property < edge_weight_t, int >edge_property(weight); |
---|
31 | add_edge(source(*ei, g_dot), target(*ei, g_dot), edge_property, g); |
---|
32 | } |
---|
33 | |
---|
34 | std::vector < graph_traits < Graph >::edge_descriptor > mst; |
---|
35 | typedef std::vector < graph_traits < Graph >::edge_descriptor >::size_type size_type; |
---|
36 | kruskal_minimum_spanning_tree(g, std::back_inserter(mst)); |
---|
37 | |
---|
38 | property_map < Graph, edge_weight_t >::type weight = get(edge_weight, g); |
---|
39 | int total_weight = 0; |
---|
40 | for (size_type e = 0; e < mst.size(); ++e) |
---|
41 | total_weight += get(weight, mst[e]); |
---|
42 | std::cout << "total weight: " << total_weight << std::endl; |
---|
43 | |
---|
44 | typedef graph_traits < Graph >::vertex_descriptor Vertex; |
---|
45 | for (size_type i = 0; i < mst.size(); ++i) { |
---|
46 | Vertex u = source(mst[i], g), v = target(mst[i], g); |
---|
47 | edge_attr_map[edge(u, v, g_dot).first]["color"] = "black"; |
---|
48 | } |
---|
49 | std::ofstream out("figs/telephone-mst-kruskal.dot"); |
---|
50 | graph_property < GraphvizGraph, graph_edge_attribute_t >::type & |
---|
51 | graph_edge_attr_map = get_property(g_dot, graph_edge_attribute); |
---|
52 | graph_edge_attr_map["color"] = "gray"; |
---|
53 | graph_edge_attr_map["style"] = "bold"; |
---|
54 | write_graphviz(out, g_dot); |
---|
55 | |
---|
56 | return EXIT_SUCCESS; |
---|
57 | } |
---|