dfs_parenthesis.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. //
  10. // Sample output
  11. // DFS parenthesis:
  12. // (0(2(3(4(11)4)3)2)0)
  13. #include <boost/config.hpp>
  14. #include <assert.h>
  15. #include <iostream>
  16. #include <vector>
  17. #include <algorithm>
  18. #include <utility>
  19. #include "boost/graph/visitors.hpp"
  20. #include "boost/graph/adjacency_list.hpp"
  21. #include "boost/graph/breadth_first_search.hpp"
  22. #include "boost/graph/depth_first_search.hpp"
  23. using namespace boost;
  24. using namespace std;
  25. struct open_paren : public base_visitor<open_paren> {
  26. typedef on_discover_vertex event_filter;
  27. template <class Vertex, class Graph>
  28. void operator()(Vertex v, Graph&) {
  29. std::cout << "(" << v;
  30. }
  31. };
  32. struct close_paren : public base_visitor<close_paren> {
  33. typedef on_finish_vertex event_filter;
  34. template <class Vertex, class Graph>
  35. void operator()(Vertex v, Graph&) {
  36. std::cout << v << ")";
  37. }
  38. };
  39. int
  40. main(int, char*[])
  41. {
  42. using namespace boost;
  43. typedef adjacency_list<> Graph;
  44. typedef std::pair<int,int> E;
  45. E edge_array[] = { E(0, 2),
  46. E(1, 1), E(1, 3),
  47. E(2, 1), E(2, 3),
  48. E(3, 1), E(3, 4),
  49. E(4, 0), E(4, 1) };
  50. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  51. Graph G(5);
  52. for (std::size_t j = 0; j < sizeof(edge_array) / sizeof(E); ++j)
  53. add_edge(edge_array[j].first, edge_array[j].second, G);
  54. #else
  55. Graph G(edge_array, edge_array + sizeof(edge_array)/sizeof(E), 5);
  56. #endif
  57. std::cout << "DFS parenthesis:" << std::endl;
  58. depth_first_search(G, visitor(make_dfs_visitor(std::make_pair(open_paren(),
  59. close_paren()))));
  60. std::cout << std::endl;
  61. return 0;
  62. }