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