random_spanning_tree_test.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2010 The Trustees of Indiana University.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Jeremiah Willcock
  6. // Andrew Lumsdaine
  7. #include <boost/graph/random_spanning_tree.hpp>
  8. #include <boost/graph/grid_graph.hpp>
  9. #include <boost/array.hpp>
  10. #include <boost/random.hpp>
  11. #include <boost/property_map/shared_array_property_map.hpp>
  12. #include <boost/property_map/dynamic_property_map.hpp>
  13. #include <boost/graph/graphviz.hpp>
  14. #include <boost/lexical_cast.hpp>
  15. #include <boost/graph/property_maps/constant_property_map.hpp>
  16. #include <string>
  17. #include <iostream>
  18. #include <fstream>
  19. #include <boost/graph/iteration_macros.hpp>
  20. using namespace boost;
  21. using namespace std;
  22. typedef grid_graph<2> graph_type;
  23. typedef graph_traits<graph_type> gt;
  24. template <typename Graph, typename PredMap, typename WeightMap>
  25. void write_spanning_tree(const Graph& g, PredMap pred, WeightMap weight, string filename) {
  26. shared_array_property_map<string, typename property_map<Graph, edge_index_t>::const_type> edge_style(num_edges(g), get(edge_index, g));
  27. shared_array_property_map<string, typename property_map<Graph, vertex_index_t>::const_type> vertex_pos(num_vertices(g), get(vertex_index, g));
  28. BGL_FORALL_EDGES_T(e, g, Graph) {
  29. put(edge_style, e, (get(pred, target(e, g)) == source(e, g) || get(pred, source(e, g)) == target(e, g)) ? "bold" : "dotted");
  30. }
  31. BGL_FORALL_VERTICES_T(v, g, Graph) {
  32. put(vertex_pos, v, lexical_cast<string>(v[0]) + "," + lexical_cast<string>(v[1]));
  33. }
  34. dynamic_properties dp;
  35. dp.property("style", edge_style);
  36. dp.property("pos", vertex_pos);
  37. dp.property("len", weight);
  38. dp.property("node_id", get(vertex_index, g));
  39. ofstream out(filename.c_str());
  40. write_graphviz_dp(out, g, dp);
  41. }
  42. int main(int, char**) {
  43. boost::array<size_t, 2> sizes = {{ 5, 5 }};
  44. graph_type g(sizes);
  45. shared_array_property_map<gt::vertex_descriptor, property_map<graph_type, vertex_index_t>::const_type> pred(num_vertices(g), get(vertex_index, g));
  46. shared_array_property_map<double, property_map<graph_type, edge_index_t>::const_type> weight(num_edges(g), get(edge_index, g));
  47. BGL_FORALL_EDGES(e, g, graph_type) {put(weight, e, (1. + get(edge_index, g, e)) / num_edges(g));}
  48. boost::mt19937 gen;
  49. random_spanning_tree(g, gen, predecessor_map(pred));
  50. // write_spanning_tree(g, pred, constant_property_map<gt::edge_descriptor, double>(1.), "unweight_random_st.dot");
  51. random_spanning_tree(g, gen, predecessor_map(pred));
  52. // write_spanning_tree(g, pred, constant_property_map<gt::edge_descriptor, double>(1.), "unweight_random_st2.dot");
  53. random_spanning_tree(g, gen, predecessor_map(pred).weight_map(weight));
  54. // write_spanning_tree(g, pred, weight, "weight_random_st.dot");
  55. return 0;
  56. }