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 > class bfs_time_visitor:public default_bfs_visitor {
17 typedef typename property_traits < TimeMap >::value_type T;
18 public:
bfs_time_visitor(TimeMap tmap,T & t)19 bfs_time_visitor(TimeMap tmap, T & t):m_timemap(tmap), m_time(t) { }
20 template < typename Vertex, typename Graph >
discover_vertex(Vertex u,const Graph & g) const21 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
main()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 >::vertices_size_type Size;
58
59 // a vector to hold the discover time property for each vertex
60 std::vector < Size > dtime(num_vertices(g));
61 typedef
62 iterator_property_map<std::vector<Size>::iterator,
63 property_map<graph_t, vertex_index_t>::const_type>
64 dtime_pm_type;
65 dtime_pm_type dtime_pm(dtime.begin(), get(vertex_index, g));
66
67 Size time = 0;
68 bfs_time_visitor < dtime_pm_type >vis(dtime_pm, time);
69 breadth_first_search(g, vertex(s, g), visitor(vis));
70
71 // Use std::sort to order the vertices by their discover time
72 std::vector<graph_traits<graph_t>::vertices_size_type > discover_order(N);
73 integer_range < int >range(0, N);
74 std::copy(range.begin(), range.end(), discover_order.begin());
75 std::sort(discover_order.begin(), discover_order.end(),
76 indirect_cmp < dtime_pm_type, std::less < Size > >(dtime_pm));
77
78 std::cout << "order of discovery: ";
79 for (int i = 0; i < N; ++i)
80 std::cout << name[discover_order[i]] << " ";
81 std::cout << std::endl;
82
83 return EXIT_SUCCESS;
84 }
85