• 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 #include <boost/graph/adjacency_list.hpp>
9 #include <boost/graph/kruskal_min_spanning_tree.hpp>
10 #include <iostream>
11 #include <fstream>
12 
main()13 int main()
14 {
15     using namespace boost;
16     typedef adjacency_list< vecS, vecS, undirectedS, no_property,
17         property< edge_weight_t, int > >
18         Graph;
19     typedef graph_traits< Graph >::edge_descriptor Edge;
20     typedef std::pair< int, int > E;
21 
22     const int num_nodes = 5;
23     E edge_array[] = { E(0, 2), E(1, 3), E(1, 4), E(2, 1), E(2, 3), E(3, 4),
24         E(4, 0), E(4, 1) };
25     int weights[] = { 1, 1, 2, 7, 3, 1, 1, 1 };
26     std::size_t num_edges = sizeof(edge_array) / sizeof(E);
27 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
28     Graph g(num_nodes);
29     property_map< Graph, edge_weight_t >::type weightmap = get(edge_weight, g);
30     for (std::size_t j = 0; j < num_edges; ++j)
31     {
32         Edge e;
33         bool inserted;
34         boost::tie(e, inserted)
35             = add_edge(edge_array[j].first, edge_array[j].second, g);
36         weightmap[e] = weights[j];
37     }
38 #else
39     Graph g(edge_array, edge_array + num_edges, weights, num_nodes);
40 #endif
41     property_map< Graph, edge_weight_t >::type weight = get(edge_weight, g);
42     std::vector< Edge > spanning_tree;
43 
44     kruskal_minimum_spanning_tree(g, std::back_inserter(spanning_tree));
45 
46     std::cout << "Print the edges in the MST:" << std::endl;
47     for (std::vector< Edge >::iterator ei = spanning_tree.begin();
48          ei != spanning_tree.end(); ++ei)
49     {
50         std::cout << source(*ei, g) << " <--> " << target(*ei, g)
51                   << " with weight of " << weight[*ei] << std::endl;
52     }
53 
54     std::ofstream fout("figs/kruskal-eg.dot");
55     fout << "graph A {\n"
56          << " rankdir=LR\n"
57          << " size=\"3,3\"\n"
58          << " ratio=\"filled\"\n"
59          << " edge[style=\"bold\"]\n"
60          << " node[shape=\"circle\"]\n";
61     graph_traits< Graph >::edge_iterator eiter, eiter_end;
62     for (boost::tie(eiter, eiter_end) = edges(g); eiter != eiter_end; ++eiter)
63     {
64         fout << source(*eiter, g) << " -- " << target(*eiter, g);
65         if (std::find(spanning_tree.begin(), spanning_tree.end(), *eiter)
66             != spanning_tree.end())
67             fout << "[color=\"black\", label=\"" << get(edge_weight, g, *eiter)
68                  << "\"];\n";
69         else
70             fout << "[color=\"gray\", label=\"" << get(edge_weight, g, *eiter)
71                  << "\"];\n";
72     }
73     fout << "}\n";
74     return EXIT_SUCCESS;
75 }
76