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