bfs.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/breadth_first_search.hpp>
  17. #include <boost/property_map/property_map.hpp>
  18. #include <boost/graph/graph_utility.hpp>
  19. /*
  20. This examples shows how to use the breadth_first_search() GGCL
  21. algorithm, specifically the 3 argument variant of bfs that assumes
  22. the graph has a color property (property) stored internally.
  23. Two pre-defined visitors are used to record the distance of each
  24. vertex from the source vertex, and also to record the parent of each
  25. vertex. Any number of visitors can be layered and passed to a GGCL
  26. algorithm.
  27. The call to vertices(G) returns an STL-compatible container which
  28. contains all of the vertices in the graph. In this example we use
  29. the vertices container in the STL for_each() function.
  30. Sample Output:
  31. 0 --> 2
  32. 1 --> 1 3 4
  33. 2 --> 1 3 4
  34. 3 --> 1 4
  35. 4 --> 0 1
  36. 0 --> 2
  37. 1 --> 1 3 4
  38. 2 --> 1 3 4
  39. 3 --> 1 4
  40. 4 --> 0 1
  41. distances: 0 2 1 2 2
  42. parent[0] = 0
  43. parent[1] = 2
  44. parent[2] = 0
  45. parent[3] = 2
  46. parent[4] = 2
  47. */
  48. template <class ParentDecorator>
  49. struct print_parent {
  50. print_parent(const ParentDecorator& p_) : p(p_) { }
  51. template <class Vertex>
  52. void operator()(const Vertex& v) const {
  53. std::cout << "parent[" << v << "] = " << p[v] << std::endl;
  54. }
  55. ParentDecorator p;
  56. };
  57. template <class NewGraph, class Tag>
  58. struct graph_copier
  59. : public boost::base_visitor<graph_copier<NewGraph, Tag> >
  60. {
  61. typedef Tag event_filter;
  62. graph_copier(NewGraph& graph) : new_g(graph) { }
  63. template <class Edge, class Graph>
  64. void operator()(Edge e, Graph& g) {
  65. boost::add_edge(boost::source(e, g), boost::target(e, g), new_g);
  66. }
  67. private:
  68. NewGraph& new_g;
  69. };
  70. template <class NewGraph, class Tag>
  71. inline graph_copier<NewGraph, Tag>
  72. copy_graph(NewGraph& g, Tag) {
  73. return graph_copier<NewGraph, Tag>(g);
  74. }
  75. int main(int , char* [])
  76. {
  77. typedef boost::adjacency_list<
  78. boost::mapS, boost::vecS, boost::bidirectionalS,
  79. boost::property<boost::vertex_color_t, boost::default_color_type,
  80. boost::property<boost::vertex_degree_t, int,
  81. boost::property<boost::vertex_in_degree_t, int,
  82. boost::property<boost::vertex_out_degree_t, int> > > >
  83. > Graph;
  84. Graph G(5);
  85. boost::add_edge(0, 2, G);
  86. boost::add_edge(1, 1, G);
  87. boost::add_edge(1, 3, G);
  88. boost::add_edge(1, 4, G);
  89. boost::add_edge(2, 1, G);
  90. boost::add_edge(2, 3, G);
  91. boost::add_edge(2, 4, G);
  92. boost::add_edge(3, 1, G);
  93. boost::add_edge(3, 4, G);
  94. boost::add_edge(4, 0, G);
  95. boost::add_edge(4, 1, G);
  96. typedef Graph::vertex_descriptor Vertex;
  97. Graph G_copy(5);
  98. // Array to store predecessor (parent) of each vertex. This will be
  99. // used as a Decorator (actually, its iterator will be).
  100. std::vector<Vertex> p(boost::num_vertices(G));
  101. // VC++ version of std::vector has no ::pointer, so
  102. // I use ::value_type* instead.
  103. typedef std::vector<Vertex>::value_type* Piter;
  104. // Array to store distances from the source to each vertex . We use
  105. // a built-in array here just for variety. This will also be used as
  106. // a Decorator.
  107. boost::graph_traits<Graph>::vertices_size_type d[5];
  108. std::fill_n(d, 5, 0);
  109. // The source vertex
  110. Vertex s = *(boost::vertices(G).first);
  111. p[s] = s;
  112. boost::breadth_first_search
  113. (G, s,
  114. boost::visitor(boost::make_bfs_visitor
  115. (std::make_pair(boost::record_distances(d, boost::on_tree_edge()),
  116. std::make_pair
  117. (boost::record_predecessors(&p[0],
  118. boost::on_tree_edge()),
  119. copy_graph(G_copy, boost::on_examine_edge())))) ));
  120. boost::print_graph(G);
  121. boost::print_graph(G_copy);
  122. if (boost::num_vertices(G) < 11) {
  123. std::cout << "distances: ";
  124. #ifdef BOOST_OLD_STREAM_ITERATORS
  125. std::copy(d, d + 5, std::ostream_iterator<int, char>(std::cout, " "));
  126. #else
  127. std::copy(d, d + 5, std::ostream_iterator<int>(std::cout, " "));
  128. #endif
  129. std::cout << std::endl;
  130. std::for_each(boost::vertices(G).first, boost::vertices(G).second,
  131. print_parent<Piter>(&p[0]));
  132. }
  133. return 0;
  134. }