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