dfs-example.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //=======================================================================
  2. // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //=======================================================================
  8. #include <boost/graph/adjacency_list.hpp>
  9. #include <boost/graph/depth_first_search.hpp>
  10. #include <boost/range/irange.hpp>
  11. #include <boost/pending/indirect_cmp.hpp>
  12. #include <iostream>
  13. using namespace boost;
  14. template < typename TimeMap > class dfs_time_visitor:public default_dfs_visitor {
  15. typedef typename property_traits < TimeMap >::value_type T;
  16. public:
  17. dfs_time_visitor(TimeMap dmap, TimeMap fmap, T & t)
  18. : m_dtimemap(dmap), m_ftimemap(fmap), m_time(t) {
  19. }
  20. template < typename Vertex, typename Graph >
  21. void discover_vertex(Vertex u, const Graph & g) const
  22. {
  23. put(m_dtimemap, u, m_time++);
  24. }
  25. template < typename Vertex, typename Graph >
  26. void finish_vertex(Vertex u, const Graph & g) const
  27. {
  28. put(m_ftimemap, u, m_time++);
  29. }
  30. TimeMap m_dtimemap;
  31. TimeMap m_ftimemap;
  32. T & m_time;
  33. };
  34. int
  35. main()
  36. {
  37. // Select the graph type we wish to use
  38. typedef adjacency_list < vecS, vecS, directedS > graph_t;
  39. typedef graph_traits < graph_t >::vertices_size_type size_type;
  40. // Set up the vertex names
  41. enum
  42. { u, v, w, x, y, z, N };
  43. char name[] = { 'u', 'v', 'w', 'x', 'y', 'z' };
  44. // Specify the edges in the graph
  45. typedef std::pair < int, int >E;
  46. E edge_array[] = { E(u, v), E(u, x), E(x, v), E(y, x),
  47. E(v, y), E(w, y), E(w, z), E(z, z)
  48. };
  49. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  50. graph_t g(N);
  51. for (std::size_t j = 0; j < sizeof(edge_array) / sizeof(E); ++j)
  52. add_edge(edge_array[j].first, edge_array[j].second, g);
  53. #else
  54. graph_t g(edge_array, edge_array + sizeof(edge_array) / sizeof(E), N);
  55. #endif
  56. // discover time and finish time properties
  57. std::vector < size_type > dtime(num_vertices(g));
  58. std::vector < size_type > ftime(num_vertices(g));
  59. typedef
  60. iterator_property_map<std::vector<size_type>::iterator,
  61. property_map<graph_t, vertex_index_t>::const_type>
  62. time_pm_type;
  63. time_pm_type dtime_pm(dtime.begin(), get(vertex_index, g));
  64. time_pm_type ftime_pm(ftime.begin(), get(vertex_index, g));
  65. size_type t = 0;
  66. dfs_time_visitor < time_pm_type >vis(dtime_pm, ftime_pm, t);
  67. depth_first_search(g, visitor(vis));
  68. // use std::sort to order the vertices by their discover time
  69. std::vector < size_type > discover_order(N);
  70. integer_range < size_type > r(0, N);
  71. std::copy(r.begin(), r.end(), discover_order.begin());
  72. std::sort(discover_order.begin(), discover_order.end(),
  73. indirect_cmp < time_pm_type, std::less < size_type > >(dtime_pm));
  74. std::cout << "order of discovery: ";
  75. int i;
  76. for (i = 0; i < N; ++i)
  77. std::cout << name[discover_order[i]] << " ";
  78. std::vector < size_type > finish_order(N);
  79. std::copy(r.begin(), r.end(), finish_order.begin());
  80. std::sort(finish_order.begin(), finish_order.end(),
  81. indirect_cmp < time_pm_type, std::less < size_type > >(ftime_pm));
  82. std::cout << std::endl << "order of finish: ";
  83. for (i = 0; i < N; ++i)
  84. std::cout << name[finish_order[i]] << " ";
  85. std::cout << std::endl;
  86. return EXIT_SUCCESS;
  87. }