bfs-example.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/breadth_first_search.hpp>
  10. #include <boost/pending/indirect_cmp.hpp>
  11. #include <boost/range/irange.hpp>
  12. #include <iostream>
  13. using namespace boost;
  14. template < typename TimeMap > class bfs_time_visitor:public default_bfs_visitor {
  15. typedef typename property_traits < TimeMap >::value_type T;
  16. public:
  17. bfs_time_visitor(TimeMap tmap, T & t):m_timemap(tmap), m_time(t) { }
  18. template < typename Vertex, typename Graph >
  19. void discover_vertex(Vertex u, const Graph & g) const
  20. {
  21. put(m_timemap, u, m_time++);
  22. }
  23. TimeMap m_timemap;
  24. T & m_time;
  25. };
  26. int
  27. main()
  28. {
  29. using namespace boost;
  30. // Select the graph type we wish to use
  31. typedef adjacency_list < vecS, vecS, undirectedS > graph_t;
  32. // Set up the vertex IDs and names
  33. enum { r, s, t, u, v, w, x, y, N };
  34. const char *name = "rstuvwxy";
  35. // Specify the edges in the graph
  36. typedef std::pair < int, int >E;
  37. E edge_array[] = { E(r, s), E(r, v), E(s, w), E(w, r), E(w, t),
  38. E(w, x), E(x, t), E(t, u), E(x, y), E(u, y)
  39. };
  40. // Create the graph object
  41. const int n_edges = sizeof(edge_array) / sizeof(E);
  42. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  43. // VC++ has trouble with the edge iterator constructor
  44. graph_t g(N);
  45. for (std::size_t j = 0; j < n_edges; ++j)
  46. add_edge(edge_array[j].first, edge_array[j].second, g);
  47. #else
  48. typedef graph_traits<graph_t>::vertices_size_type v_size_t;
  49. graph_t g(edge_array, edge_array + n_edges, v_size_t(N));
  50. #endif
  51. // Typedefs
  52. typedef graph_traits < graph_t >::vertices_size_type Size;
  53. // a vector to hold the discover time property for each vertex
  54. std::vector < Size > dtime(num_vertices(g));
  55. typedef
  56. iterator_property_map<std::vector<Size>::iterator,
  57. property_map<graph_t, vertex_index_t>::const_type>
  58. dtime_pm_type;
  59. dtime_pm_type dtime_pm(dtime.begin(), get(vertex_index, g));
  60. Size time = 0;
  61. bfs_time_visitor < dtime_pm_type >vis(dtime_pm, time);
  62. breadth_first_search(g, vertex(s, g), visitor(vis));
  63. // Use std::sort to order the vertices by their discover time
  64. std::vector<graph_traits<graph_t>::vertices_size_type > discover_order(N);
  65. integer_range < int >range(0, N);
  66. std::copy(range.begin(), range.end(), discover_order.begin());
  67. std::sort(discover_order.begin(), discover_order.end(),
  68. indirect_cmp < dtime_pm_type, std::less < Size > >(dtime_pm));
  69. std::cout << "order of discovery: ";
  70. for (int i = 0; i < N; ++i)
  71. std::cout << name[discover_order[i]] << " ";
  72. std::cout << std::endl;
  73. return EXIT_SUCCESS;
  74. }