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/pending/integer_range.hpp> |
---|
11 | #include <boost/pending/indirect_cmp.hpp> |
---|
12 | |
---|
13 | #include <iostream> |
---|
14 | |
---|
15 | using namespace boost; |
---|
16 | template < typename TimeMap > class dfs_time_visitor:public default_dfs_visitor { |
---|
17 | typedef typename property_traits < TimeMap >::value_type T; |
---|
18 | public: |
---|
19 | dfs_time_visitor(TimeMap dmap, TimeMap fmap, T & t) |
---|
20 | : m_dtimemap(dmap), m_ftimemap(fmap), m_time(t) { |
---|
21 | } |
---|
22 | template < typename Vertex, typename Graph > |
---|
23 | void discover_vertex(Vertex u, const Graph & g) const |
---|
24 | { |
---|
25 | put(m_dtimemap, u, m_time++); |
---|
26 | } |
---|
27 | template < typename Vertex, typename Graph > |
---|
28 | void finish_vertex(Vertex u, const Graph & g) const |
---|
29 | { |
---|
30 | put(m_ftimemap, u, m_time++); |
---|
31 | } |
---|
32 | TimeMap m_dtimemap; |
---|
33 | TimeMap m_ftimemap; |
---|
34 | T & m_time; |
---|
35 | }; |
---|
36 | |
---|
37 | |
---|
38 | int |
---|
39 | main() |
---|
40 | { |
---|
41 | // Select the graph type we wish to use |
---|
42 | typedef adjacency_list < vecS, vecS, directedS > graph_t; |
---|
43 | typedef graph_traits < graph_t >::vertices_size_type size_type; |
---|
44 | // Set up the vertex names |
---|
45 | enum |
---|
46 | { u, v, w, x, y, z, N }; |
---|
47 | char name[] = { 'u', 'v', 'w', 'x', 'y', 'z' }; |
---|
48 | // Specify the edges in the graph |
---|
49 | typedef std::pair < int, int >E; |
---|
50 | E edge_array[] = { E(u, v), E(u, x), E(x, v), E(y, x), |
---|
51 | E(v, y), E(w, y), E(w, z), E(z, z) |
---|
52 | }; |
---|
53 | #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 |
---|
54 | graph_t g(N); |
---|
55 | for (std::size_t j = 0; j < sizeof(edge_array) / sizeof(E); ++j) |
---|
56 | add_edge(edge_array[j].first, edge_array[j].second, g); |
---|
57 | #else |
---|
58 | graph_t g(edge_array, edge_array + sizeof(edge_array) / sizeof(E), N); |
---|
59 | #endif |
---|
60 | |
---|
61 | // Typedefs |
---|
62 | typedef boost::graph_traits < graph_t >::vertex_descriptor Vertex; |
---|
63 | typedef size_type* Iiter; |
---|
64 | |
---|
65 | // discover time and finish time properties |
---|
66 | std::vector < size_type > dtime(num_vertices(g)); |
---|
67 | std::vector < size_type > ftime(num_vertices(g)); |
---|
68 | size_type t = 0; |
---|
69 | dfs_time_visitor < size_type * >vis(&dtime[0], &ftime[0], t); |
---|
70 | |
---|
71 | depth_first_search(g, visitor(vis)); |
---|
72 | |
---|
73 | // use std::sort to order the vertices by their discover time |
---|
74 | std::vector < size_type > discover_order(N); |
---|
75 | integer_range < size_type > r(0, N); |
---|
76 | std::copy(r.begin(), r.end(), discover_order.begin()); |
---|
77 | std::sort(discover_order.begin(), discover_order.end(), |
---|
78 | indirect_cmp < Iiter, std::less < size_type > >(&dtime[0])); |
---|
79 | std::cout << "order of discovery: "; |
---|
80 | int i; |
---|
81 | for (i = 0; i < N; ++i) |
---|
82 | std::cout << name[discover_order[i]] << " "; |
---|
83 | |
---|
84 | std::vector < size_type > finish_order(N); |
---|
85 | std::copy(r.begin(), r.end(), finish_order.begin()); |
---|
86 | std::sort(finish_order.begin(), finish_order.end(), |
---|
87 | indirect_cmp < Iiter, std::less < size_type > >(&ftime[0])); |
---|
88 | std::cout << std::endl << "order of finish: "; |
---|
89 | for (i = 0; i < N; ++i) |
---|
90 | std::cout << name[finish_order[i]] << " "; |
---|
91 | std::cout << std::endl; |
---|
92 | |
---|
93 | return EXIT_SUCCESS; |
---|
94 | } |
---|