• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
9 /*
10    IMPORTANT!!!
11    ~~~~~~~~~~~~
12    This example uses interfaces that have been deprecated and removed from
13    Boost.Grpah. Someone needs to update it, as it does NOT compile.
14 */
15 
16 #include <boost/config.hpp>
17 #include <iostream>
18 #include <fstream>
19 #include <boost/lexical_cast.hpp>
20 #include <boost/graph/graphviz.hpp>
21 #include <boost/graph/kruskal_min_spanning_tree.hpp>
22 
main()23 int main()
24 {
25     using namespace boost;
26     GraphvizGraph g_dot;
27     read_graphviz("figs/telephone-network.dot", g_dot);
28 
29     typedef adjacency_list< vecS, vecS, undirectedS, no_property,
30         property< edge_weight_t, int > >
31         Graph;
32     Graph g(num_vertices(g_dot));
33     property_map< GraphvizGraph, edge_attribute_t >::type edge_attr_map
34         = get(edge_attribute, g_dot);
35     graph_traits< GraphvizGraph >::edge_iterator ei, ei_end;
36     for (boost::tie(ei, ei_end) = edges(g_dot); ei != ei_end; ++ei)
37     {
38         int weight = lexical_cast< int >(edge_attr_map[*ei]["label"]);
39         property< edge_weight_t, int > edge_property(weight);
40         add_edge(source(*ei, g_dot), target(*ei, g_dot), edge_property, g);
41     }
42 
43     std::vector< graph_traits< Graph >::edge_descriptor > mst;
44     typedef std::vector< graph_traits< Graph >::edge_descriptor >::size_type
45         size_type;
46     kruskal_minimum_spanning_tree(g, std::back_inserter(mst));
47 
48     property_map< Graph, edge_weight_t >::type weight = get(edge_weight, g);
49     int total_weight = 0;
50     for (size_type e = 0; e < mst.size(); ++e)
51         total_weight += get(weight, mst[e]);
52     std::cout << "total weight: " << total_weight << std::endl;
53 
54     typedef graph_traits< Graph >::vertex_descriptor Vertex;
55     for (size_type i = 0; i < mst.size(); ++i)
56     {
57         Vertex u = source(mst[i], g), v = target(mst[i], g);
58         edge_attr_map[edge(u, v, g_dot).first]["color"] = "black";
59     }
60     std::ofstream out("figs/telephone-mst-kruskal.dot");
61     graph_property< GraphvizGraph, graph_edge_attribute_t >::type&
62         graph_edge_attr_map
63         = get_property(g_dot, graph_edge_attribute);
64     graph_edge_attr_map["color"] = "gray";
65     graph_edge_attr_map["style"] = "bold";
66     write_graphviz(out, g_dot);
67 
68     return EXIT_SUCCESS;
69 }
70