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