• 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/config.hpp>
9 #include <iostream>
10 #include <boost/graph/adjacency_list.hpp>
11 #include <boost/graph/prim_minimum_spanning_tree.hpp>
12 
main()13 int main()
14 {
15     using namespace boost;
16     typedef adjacency_list< vecS, vecS, undirectedS,
17         property< vertex_distance_t, int >, property< edge_weight_t, int > >
18         Graph;
19     typedef std::pair< int, int > E;
20     const int num_nodes = 5;
21     E edges[]
22         = { E(0, 2), E(1, 3), E(1, 4), E(2, 1), E(2, 3), E(3, 4), E(4, 0) };
23     int weights[] = { 1, 1, 2, 7, 3, 1, 1 };
24 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
25     Graph g(num_nodes);
26     property_map< Graph, edge_weight_t >::type weightmap = get(edge_weight, g);
27     for (std::size_t j = 0; j < sizeof(edges) / sizeof(E); ++j)
28     {
29         graph_traits< Graph >::edge_descriptor e;
30         bool inserted;
31         boost::tie(e, inserted) = add_edge(edges[j].first, edges[j].second, g);
32         weightmap[e] = weights[j];
33     }
34 #else
35     Graph g(edges, edges + sizeof(edges) / sizeof(E), weights, num_nodes);
36 #endif
37     std::vector< graph_traits< Graph >::vertex_descriptor > p(num_vertices(g));
38 
39 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
40     property_map< Graph, vertex_distance_t >::type distance
41         = get(vertex_distance, g);
42     property_map< Graph, vertex_index_t >::type indexmap = get(vertex_index, g);
43     prim_minimum_spanning_tree(g, *vertices(g).first, &p[0], distance,
44         weightmap, indexmap, default_dijkstra_visitor());
45 #else
46     prim_minimum_spanning_tree(g, &p[0]);
47 #endif
48 
49     for (std::size_t i = 0; i != p.size(); ++i)
50         if (p[i] != i)
51             std::cout << "parent[" << i << "] = " << p[i] << std::endl;
52         else
53             std::cout << "parent[" << i << "] = no parent" << std::endl;
54 
55     return EXIT_SUCCESS;
56 }
57