• 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 <vector>
20 #include <boost/lexical_cast.hpp>
21 #include <boost/graph/graphviz.hpp>
22 #include <boost/graph/prim_minimum_spanning_tree.hpp>
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     typedef graph_traits< Graph >::vertex_descriptor Vertex;
44     std::vector< Vertex > parent(num_vertices(g));
45     property_map< Graph, edge_weight_t >::type weight = get(edge_weight, g);
46 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
47     property_map< Graph, vertex_index_t >::type indexmap = get(vertex_index, g);
48     std::vector< std::size_t > distance(num_vertices(g));
49     prim_minimum_spanning_tree(g, *vertices(g).first, &parent[0], &distance[0],
50         weight, indexmap, default_dijkstra_visitor());
51 #else
52     prim_minimum_spanning_tree(g, &parent[0]);
53 #endif
54 
55     int total_weight = 0;
56     for (int v = 0; v < num_vertices(g); ++v)
57         if (parent[v] != v)
58             total_weight += get(weight, edge(parent[v], v, g).first);
59     std::cout << "total weight: " << total_weight << std::endl;
60 
61     for (int u = 0; u < num_vertices(g); ++u)
62         if (parent[u] != u)
63             edge_attr_map[edge(parent[u], u, g_dot).first]["color"] = "black";
64     std::ofstream out("figs/telephone-mst-prim.dot");
65     graph_property< GraphvizGraph, graph_edge_attribute_t >::type&
66         graph_edge_attr_map
67         = get_property(g_dot, graph_edge_attribute);
68     graph_edge_attr_map["color"] = "gray";
69     write_graphviz(out, g_dot);
70 
71     return EXIT_SUCCESS;
72 }
73