visitor.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. //
  12. // DFS categorized directed graph
  13. // tree: 0 --> 2
  14. // tree: 2 --> 1
  15. // back: 1 --> 1
  16. // tree: 1 --> 3
  17. // back: 3 --> 1
  18. // tree: 3 --> 4
  19. // back: 4 --> 0
  20. // back: 4 --> 1
  21. // forward or cross: 2 --> 3
  22. // BFS categorized directed graph
  23. // tree: 0 --> 2
  24. // tree: 2 --> 1
  25. // tree: 2 --> 3
  26. // cycle: 1 --> 1
  27. // cycle: 1 --> 3
  28. // cycle: 3 --> 1
  29. // tree: 3 --> 4
  30. // cycle: 4 --> 0
  31. // cycle: 4 --> 1
  32. #include <boost/config.hpp>
  33. #include <iostream>
  34. #include <vector>
  35. #include <algorithm>
  36. #include <utility>
  37. #include <string>
  38. #include <boost/graph/visitors.hpp>
  39. #include <boost/graph/graph_utility.hpp>
  40. #include <boost/graph/adjacency_list.hpp>
  41. #include <boost/graph/breadth_first_search.hpp>
  42. #include <boost/graph/depth_first_search.hpp>
  43. using namespace boost;
  44. using namespace std;
  45. template <class Tag>
  46. struct edge_printer : public base_visitor<edge_printer<Tag> > {
  47. typedef Tag event_filter;
  48. edge_printer(std::string edge_t) : m_edge_type(edge_t) { }
  49. template <class Edge, class Graph>
  50. void operator()(Edge e, Graph& G) {
  51. std::cout << m_edge_type << ": " << source(e, G)
  52. << " --> " << target(e, G) << std::endl;
  53. }
  54. std::string m_edge_type;
  55. };
  56. template <class Tag>
  57. edge_printer<Tag> print_edge(std::string type, Tag) {
  58. return edge_printer<Tag>(type);
  59. }
  60. int
  61. main(int, char*[])
  62. {
  63. using namespace boost;
  64. typedef adjacency_list<> Graph;
  65. typedef std::pair<int,int> E;
  66. E edges[] = { E(0, 2),
  67. E(1, 1), E(1, 3),
  68. E(2, 1), E(2, 3),
  69. E(3, 1), E(3, 4),
  70. E(4, 0), E(4, 1) };
  71. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  72. Graph G(5);
  73. for (std::size_t j = 0; j < sizeof(edges)/sizeof(E); ++j)
  74. add_edge(edges[j].first, edges[j].second, G);
  75. #else
  76. Graph G(edges, edges + sizeof(edges)/sizeof(E), 5);
  77. #endif
  78. typedef boost::graph_traits<Graph>::vertices_size_type size_type;
  79. std::vector<size_type> d(num_vertices(G));
  80. std::vector<size_type> f(num_vertices(G));
  81. cout << "DFS categorized directed graph" << endl;
  82. depth_first_search(G, visitor(make_dfs_visitor(
  83. make_list(print_edge("tree", on_tree_edge()),
  84. print_edge("back", on_back_edge()),
  85. print_edge("forward or cross", on_forward_or_cross_edge())
  86. ))));
  87. cout << endl << "BFS categorized directed graph" << endl;
  88. boost::breadth_first_search
  89. (G, vertex(0, G), visitor(make_bfs_visitor(
  90. std::make_pair(print_edge("tree", on_tree_edge()),
  91. print_edge("cycle", on_non_tree_edge())))));
  92. return 0;
  93. }