neighbor_bfs.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. #include <boost/config.hpp>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <utility>
  13. #include <iostream>
  14. #include <boost/graph/visitors.hpp>
  15. #include <boost/graph/adjacency_list.hpp>
  16. #include <boost/graph/graph_utility.hpp>
  17. #include <boost/graph/neighbor_bfs.hpp>
  18. #include <boost/property_map/property_map.hpp>
  19. /*
  20. Sample Output:
  21. 0 --> 2
  22. 1 --> 1 3 4
  23. 2 --> 1 3 4
  24. 3 --> 1 4
  25. 4 --> 0 1
  26. distances: 0 2 1 2 1
  27. parent[0] = 0
  28. parent[1] = 2
  29. parent[2] = 0
  30. parent[3] = 2
  31. parent[4] = 0
  32. */
  33. using namespace boost;
  34. template <class ParentDecorator>
  35. struct print_parent {
  36. print_parent(const ParentDecorator& p_) : p(p_) { }
  37. template <class Vertex>
  38. void operator()(const Vertex& v) const {
  39. std::cout << "parent[" << v << "] = " << p[v] << std::endl;
  40. }
  41. ParentDecorator p;
  42. };
  43. template <class DistanceMap, class PredecessorMap, class ColorMap>
  44. class distance_and_pred_visitor : public neighbor_bfs_visitor<>
  45. {
  46. typedef typename property_traits<ColorMap>::value_type ColorValue;
  47. typedef color_traits<ColorValue> Color;
  48. public:
  49. distance_and_pred_visitor(DistanceMap d, PredecessorMap p, ColorMap c)
  50. : m_distance(d), m_predecessor(p), m_color(c) { }
  51. template <class Edge, class Graph>
  52. void tree_out_edge(Edge e, const Graph& g) const
  53. {
  54. typename graph_traits<Graph>::vertex_descriptor
  55. u = source(e, g), v = target(e, g);
  56. put(m_distance, v, get(m_distance, u) + 1);
  57. put(m_predecessor, v, u);
  58. }
  59. template <class Edge, class Graph>
  60. void tree_in_edge(Edge e, const Graph& g) const
  61. {
  62. typename graph_traits<Graph>::vertex_descriptor
  63. u = source(e, g), v = target(e, g);
  64. put(m_distance, u, get(m_distance, v) + 1);
  65. put(m_predecessor, u, v);
  66. }
  67. DistanceMap m_distance;
  68. PredecessorMap m_predecessor;
  69. ColorMap m_color;
  70. };
  71. int main(int , char* [])
  72. {
  73. typedef adjacency_list<
  74. mapS, vecS, bidirectionalS,
  75. property<vertex_color_t, default_color_type>
  76. > Graph;
  77. typedef property_map<Graph, vertex_color_t>::type
  78. ColorMap;
  79. Graph G(5);
  80. add_edge(0, 2, G);
  81. add_edge(1, 1, G);
  82. add_edge(1, 3, G);
  83. add_edge(1, 4, G);
  84. add_edge(2, 1, G);
  85. add_edge(2, 3, G);
  86. add_edge(2, 4, G);
  87. add_edge(3, 1, G);
  88. add_edge(3, 4, G);
  89. add_edge(4, 0, G);
  90. add_edge(4, 1, G);
  91. typedef Graph::vertex_descriptor Vertex;
  92. // Array to store predecessor (parent) of each vertex. This will be
  93. // used as a Decorator (actually, its iterator will be).
  94. std::vector<Vertex> p(num_vertices(G));
  95. // VC++ version of std::vector has no ::pointer, so
  96. // I use ::value_type* instead.
  97. typedef std::vector<Vertex>::value_type* Piter;
  98. // Array to store distances from the source to each vertex . We use
  99. // a built-in array here just for variety. This will also be used as
  100. // a Decorator.
  101. typedef graph_traits<Graph>::vertices_size_type size_type;
  102. size_type d[5];
  103. std::fill_n(d, 5, 0);
  104. // The source vertex
  105. Vertex s = *(vertices(G).first);
  106. p[s] = s;
  107. distance_and_pred_visitor<size_type*, Vertex*, ColorMap>
  108. vis(d, &p[0], get(vertex_color, G));
  109. neighbor_breadth_first_search
  110. (G, s, visitor(vis).
  111. color_map(get(vertex_color, G)));
  112. print_graph(G);
  113. if (num_vertices(G) < 11) {
  114. std::cout << "distances: ";
  115. #ifdef BOOST_OLD_STREAM_ITERATORS
  116. std::copy(d, d + 5, std::ostream_iterator<int, char>(std::cout, " "));
  117. #else
  118. std::copy(d, d + 5, std::ostream_iterator<int>(std::cout, " "));
  119. #endif
  120. std::cout << std::endl;
  121. std::for_each(vertices(G).first, vertices(G).second,
  122. print_parent<Piter>(&p[0]));
  123. }
  124. return 0;
  125. }