prim-telephone.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. IMPORTANT!!!
  10. ~~~~~~~~~~~~
  11. This example uses interfaces that have been deprecated and removed from Boost.Grpah.
  12. Someone needs to update it, as it does NOT compile.
  13. */
  14. #include <boost/config.hpp>
  15. #include <iostream>
  16. #include <fstream>
  17. #include <vector>
  18. #include <boost/lexical_cast.hpp>
  19. #include <boost/graph/graphviz.hpp>
  20. #include <boost/graph/prim_minimum_spanning_tree.hpp>
  21. int
  22. main()
  23. {
  24. using namespace boost;
  25. GraphvizGraph g_dot;
  26. read_graphviz("figs/telephone-network.dot", g_dot);
  27. typedef adjacency_list < vecS, vecS, undirectedS, no_property,
  28. property < edge_weight_t, int > > Graph;
  29. Graph g(num_vertices(g_dot));
  30. property_map < GraphvizGraph, edge_attribute_t >::type
  31. edge_attr_map = get(edge_attribute, g_dot);
  32. graph_traits < GraphvizGraph >::edge_iterator ei, ei_end;
  33. for (boost::tie(ei, ei_end) = edges(g_dot); ei != ei_end; ++ei) {
  34. int weight = lexical_cast < int >(edge_attr_map[*ei]["label"]);
  35. property < edge_weight_t, int >edge_property(weight);
  36. add_edge(source(*ei, g_dot), target(*ei, g_dot), edge_property, g);
  37. }
  38. typedef graph_traits < Graph >::vertex_descriptor Vertex;
  39. std::vector < Vertex > parent(num_vertices(g));
  40. property_map < Graph, edge_weight_t >::type weight = get(edge_weight, g);
  41. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  42. property_map<Graph, vertex_index_t>::type indexmap = get(vertex_index, g);
  43. std::vector<std::size_t> distance(num_vertices(g));
  44. prim_minimum_spanning_tree(g, *vertices(g).first, &parent[0], &distance[0],
  45. weight, indexmap, default_dijkstra_visitor());
  46. #else
  47. prim_minimum_spanning_tree(g, &parent[0]);
  48. #endif
  49. int total_weight = 0;
  50. for (int v = 0; v < num_vertices(g); ++v)
  51. if (parent[v] != v)
  52. total_weight += get(weight, edge(parent[v], v, g).first);
  53. std::cout << "total weight: " << total_weight << std::endl;
  54. for (int u = 0; u < num_vertices(g); ++u)
  55. if (parent[u] != u)
  56. edge_attr_map[edge(parent[u], u, g_dot).first]["color"] = "black";
  57. std::ofstream out("figs/telephone-mst-prim.dot");
  58. graph_property < GraphvizGraph, graph_edge_attribute_t >::type &
  59. graph_edge_attr_map = get_property(g_dot, graph_edge_attribute);
  60. graph_edge_attr_map["color"] = "gray";
  61. write_graphviz(out, g_dot);
  62. return EXIT_SUCCESS;
  63. }