dave.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <iostream>
  11. #include <iterator>
  12. #include <vector>
  13. #include <list>
  14. // Use boost::queue instead of std::queue because std::queue doesn't
  15. // model Buffer; it has to top() function. -Jeremy
  16. #include <boost/pending/queue.hpp>
  17. #include <boost/graph/adjacency_list.hpp>
  18. #include <boost/graph/visitors.hpp>
  19. #include <boost/graph/breadth_first_search.hpp>
  20. #include <boost/graph/dijkstra_shortest_paths.hpp>
  21. #include <boost/graph/graph_utility.hpp>
  22. using namespace std;
  23. using namespace boost;
  24. /*
  25. This example does a best-first-search (using dijkstra's) and
  26. simultaneously makes a copy of the graph (assuming the graph is
  27. connected).
  28. Example Graph: (p. 90 "Data Structures and Network Algorithms", Tarjan)
  29. g
  30. 3+ +2
  31. / 1 \
  32. e+----f
  33. |+0 5++
  34. | \ / |
  35. 10| d |12
  36. |8++\7|
  37. +/ | +|
  38. b 4| c
  39. \ | +
  40. 6+|/3
  41. a
  42. Sample Output:
  43. a --> c d
  44. b --> a d
  45. c --> f
  46. d --> c e f
  47. e --> b g
  48. f --> e g
  49. g -->
  50. Starting graph:
  51. a(32767); c d
  52. c(32767); f
  53. d(32767); c e f
  54. f(32767); e g
  55. e(32767); b g
  56. g(32767);
  57. b(32767); a d
  58. Result:
  59. a(0); d c
  60. d(4); f e c
  61. c(3); f
  62. f(9); g e
  63. e(4); g b
  64. g(7);
  65. b(14); d a
  66. */
  67. typedef property<vertex_color_t, default_color_type,
  68. property<vertex_distance_t,int> > VProperty;
  69. typedef int weight_t;
  70. typedef property<edge_weight_t,weight_t> EProperty;
  71. typedef adjacency_list<vecS, vecS, directedS, VProperty, EProperty > Graph;
  72. template <class Tag>
  73. struct endl_printer
  74. : public boost::base_visitor< endl_printer<Tag> >
  75. {
  76. typedef Tag event_filter;
  77. endl_printer(std::ostream& os) : m_os(os) { }
  78. template <class T, class Graph>
  79. void operator()(T, Graph&) { m_os << std::endl; }
  80. std::ostream& m_os;
  81. };
  82. template <class Tag>
  83. endl_printer<Tag> print_endl(std::ostream& os, Tag) {
  84. return endl_printer<Tag>(os);
  85. }
  86. template <class PA, class Tag>
  87. struct edge_printer
  88. : public boost::base_visitor< edge_printer<PA, Tag> >
  89. {
  90. typedef Tag event_filter;
  91. edge_printer(PA pa, std::ostream& os) : m_pa(pa), m_os(os) { }
  92. template <class T, class Graph>
  93. void operator()(T x, Graph& g) {
  94. m_os << "(" << get(m_pa, source(x, g)) << ","
  95. << get(m_pa, target(x, g)) << ") ";
  96. }
  97. PA m_pa;
  98. std::ostream& m_os;
  99. };
  100. template <class PA, class Tag>
  101. edge_printer<PA, Tag>
  102. print_edge(PA pa, std::ostream& os, Tag) {
  103. return edge_printer<PA, Tag>(pa, os);
  104. }
  105. template <class NewGraph, class Tag>
  106. struct graph_copier
  107. : public boost::base_visitor<graph_copier<NewGraph, Tag> >
  108. {
  109. typedef Tag event_filter;
  110. graph_copier(NewGraph& graph) : new_g(graph) { }
  111. template <class Edge, class Graph>
  112. void operator()(Edge e, Graph& g) {
  113. add_edge(source(e, g), target(e, g), new_g);
  114. }
  115. private:
  116. NewGraph& new_g;
  117. };
  118. template <class NewGraph, class Tag>
  119. inline graph_copier<NewGraph, Tag>
  120. copy_graph(NewGraph& g, Tag) {
  121. return graph_copier<NewGraph, Tag>(g);
  122. }
  123. template <class Graph, class Name>
  124. void print(Graph& G, Name name)
  125. {
  126. typename boost::graph_traits<Graph>::vertex_iterator ui, uiend;
  127. for (boost::tie(ui, uiend) = vertices(G); ui != uiend; ++ui) {
  128. cout << name[*ui] << " --> ";
  129. typename boost::graph_traits<Graph>::adjacency_iterator vi, viend;
  130. for(boost::tie(vi, viend) = adjacent_vertices(*ui, G); vi != viend; ++vi)
  131. cout << name[*vi] << " ";
  132. cout << endl;
  133. }
  134. }
  135. int
  136. main(int , char* [])
  137. {
  138. // Name and ID numbers for the vertices
  139. char name[] = "abcdefg";
  140. enum { a, b, c, d, e, f, g, N};
  141. Graph G(N);
  142. boost::property_map<Graph, vertex_index_t>::type
  143. vertex_id = get(vertex_index, G);
  144. std::vector<weight_t> distance(N, (numeric_limits<weight_t>::max)());
  145. typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
  146. std::vector<Vertex> parent(N);
  147. typedef std::pair<int,int> E;
  148. E edges[] = { E(a,c), E(a,d),
  149. E(b,a), E(b,d),
  150. E(c,f),
  151. E(d,c), E(d,e), E(d,f),
  152. E(e,b), E(e,g),
  153. E(f,e), E(f,g) };
  154. int weight[] = { 3, 4,
  155. 6, 8,
  156. 12,
  157. 7, 0, 5,
  158. 10, 3,
  159. 1, 2 };
  160. for (int i = 0; i < 12; ++i)
  161. add_edge(edges[i].first, edges[i].second, weight[i], G);
  162. print(G, name);
  163. adjacency_list<listS, vecS, directedS,
  164. property<vertex_color_t, default_color_type> > G_copy(N);
  165. cout << "Starting graph:" << endl;
  166. std::ostream_iterator<int> cout_int(std::cout, " ");
  167. std::ostream_iterator<char> cout_char(std::cout, " ");
  168. boost::queue<Vertex> Q;
  169. boost::breadth_first_search
  170. (G, vertex(a, G), Q,
  171. make_bfs_visitor(
  172. boost::make_list
  173. (write_property(make_iterator_property_map(name, vertex_id,
  174. name[0]),
  175. cout_char, on_examine_vertex()),
  176. write_property(make_iterator_property_map(distance.begin(),
  177. vertex_id,
  178. distance[0]),
  179. cout_int, on_examine_vertex()),
  180. print_edge(make_iterator_property_map(name, vertex_id,
  181. name[0]),
  182. std::cout, on_examine_edge()),
  183. print_endl(std::cout, on_finish_vertex()))),
  184. get(vertex_color, G));
  185. std::cout << "about to call dijkstra's" << std::endl;
  186. parent[vertex(a, G)] = vertex(a, G);
  187. boost::dijkstra_shortest_paths
  188. (G, vertex(a, G),
  189. distance_map(make_iterator_property_map(distance.begin(), vertex_id,
  190. distance[0])).
  191. predecessor_map(make_iterator_property_map(parent.begin(), vertex_id,
  192. parent[0])).
  193. visitor(make_dijkstra_visitor(copy_graph(G_copy, on_examine_edge()))));
  194. cout << endl;
  195. cout << "Result:" << endl;
  196. boost::breadth_first_search
  197. (G, vertex(a, G),
  198. visitor(make_bfs_visitor(
  199. boost::make_list
  200. (write_property(make_iterator_property_map(name, vertex_id,
  201. name[0]),
  202. cout_char, on_examine_vertex()),
  203. write_property(make_iterator_property_map(distance.begin(),
  204. vertex_id,
  205. distance[0]),
  206. cout_int, on_examine_vertex()),
  207. print_edge(make_iterator_property_map(name, vertex_id,
  208. name[0]),
  209. std::cout, on_examine_edge()),
  210. print_endl(std::cout, on_finish_vertex())))));
  211. return 0;
  212. }