bfs-name-printer.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <fstream>
  11. #include <boost/graph/adjacency_list.hpp>
  12. #include <boost/graph/breadth_first_search.hpp>
  13. using namespace boost;
  14. template <typename Graph, typename VertexNameMap, typename TransDelayMap>
  15. void
  16. build_router_network(Graph & g, VertexNameMap name_map,
  17. TransDelayMap delay_map)
  18. {
  19. typename graph_traits < Graph >::vertex_descriptor a, b, c, d, e;
  20. a = add_vertex(g);
  21. name_map[a] = 'a';
  22. b = add_vertex(g);
  23. name_map[b] = 'b';
  24. c = add_vertex(g);
  25. name_map[c] = 'c';
  26. d = add_vertex(g);
  27. name_map[d] = 'd';
  28. e = add_vertex(g);
  29. name_map[e] = 'e';
  30. typename graph_traits<Graph>::edge_descriptor ed;
  31. bool inserted;
  32. boost::tie(ed, inserted) = add_edge(a, b, g);
  33. delay_map[ed] = 1.2;
  34. boost::tie(ed, inserted) = add_edge(a, d, g);
  35. delay_map[ed] = 4.5;
  36. boost::tie(ed, inserted) = add_edge(b, d, g);
  37. delay_map[ed] = 1.8;
  38. boost::tie(ed, inserted) = add_edge(c, a, g);
  39. delay_map[ed] = 2.6;
  40. boost::tie(ed, inserted) = add_edge(c, e, g);
  41. delay_map[ed] = 5.2;
  42. boost::tie(ed, inserted) = add_edge(d, c, g);
  43. delay_map[ed] = 0.4;
  44. boost::tie(ed, inserted) = add_edge(d, e, g);
  45. delay_map[ed] = 3.3;
  46. }
  47. template <typename VertexNameMap>
  48. class bfs_name_printer : public default_bfs_visitor {
  49. // inherit default (empty) event point actions
  50. public:
  51. bfs_name_printer(VertexNameMap n_map) : m_name_map(n_map) {
  52. }
  53. template <typename Vertex, typename Graph>
  54. void discover_vertex(Vertex u, const Graph &) const
  55. {
  56. std::cout << get(m_name_map, u) << ' ';
  57. }
  58. private:
  59. VertexNameMap m_name_map;
  60. };
  61. struct VP {
  62. char name;
  63. };
  64. struct EP {
  65. double weight;
  66. };
  67. int
  68. main()
  69. {
  70. typedef adjacency_list < listS, vecS, directedS, VP, EP> graph_t;
  71. graph_t g;
  72. property_map<graph_t, char VP::*>::type name_map = get(&VP::name, g);
  73. property_map<graph_t, double EP::*>::type delay_map = get(&EP::weight, g);
  74. build_router_network(g, name_map, delay_map);
  75. typedef property_map<graph_t, char VP::*>::type VertexNameMap;
  76. graph_traits<graph_t>::vertex_descriptor a = *vertices(g).first;
  77. bfs_name_printer<VertexNameMap> vis(name_map);
  78. std::cout << "BFS vertex discover order: ";
  79. breadth_first_search(g, a, visitor(vis));
  80. std::cout << std::endl;
  81. }