[12] | 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 | struct VertexProps { |
---|
| 31 | boost::default_color_type color; |
---|
| 32 | std::size_t discover_time; |
---|
| 33 | }; |
---|
| 34 | |
---|
| 35 | int |
---|
| 36 | main() |
---|
| 37 | { |
---|
| 38 | using namespace boost; |
---|
| 39 | // Select the graph type we wish to use |
---|
| 40 | typedef adjacency_list < listS, listS, undirectedS, |
---|
| 41 | VertexProps> graph_t; |
---|
| 42 | // Set up the vertex IDs and names |
---|
| 43 | enum { r, s, t, u, v, w, x, y, N }; |
---|
| 44 | const char *name = "rstuvwxy"; |
---|
| 45 | // Specify the edges in the graph |
---|
| 46 | typedef std::pair < int, int >E; |
---|
| 47 | E edge_array[] = { E(r, s), E(r, v), E(s, w), E(w, r), E(w, t), |
---|
| 48 | E(w, x), E(x, t), E(t, u), E(x, y), E(u, y) |
---|
| 49 | }; |
---|
| 50 | // Create the graph object |
---|
| 51 | const int n_edges = sizeof(edge_array) / sizeof(E); |
---|
| 52 | #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 |
---|
| 53 | // VC++ has trouble with the edge iterator constructor |
---|
| 54 | graph_t g; |
---|
| 55 | std::vector<graph_traits<graph_t>::vertex_descriptor> verts; |
---|
| 56 | for (std::size_t i = 0; i < N; ++i) |
---|
| 57 | verts.push_back(add_vertex(g)); |
---|
| 58 | for (std::size_t j = 0; j < n_edges; ++j) |
---|
| 59 | add_edge(verts[edge_array[j].first], verts[edge_array[j].second], g); |
---|
| 60 | #else |
---|
| 61 | typedef graph_traits<graph_t>::vertices_size_type v_size_t; |
---|
| 62 | graph_t g(edge_array, edge_array + n_edges, v_size_t(N)); |
---|
| 63 | #endif |
---|
| 64 | |
---|
| 65 | // Typedefs |
---|
| 66 | typedef graph_traits<graph_t>::vertex_descriptor Vertex; |
---|
| 67 | typedef graph_traits<graph_t>::vertices_size_type Size; |
---|
| 68 | typedef Size* Iiter; |
---|
| 69 | |
---|
| 70 | Size time = 0; |
---|
| 71 | typedef property_map<graph_t, std::size_t VertexProps::*>::type dtime_map_t; |
---|
| 72 | dtime_map_t dtime_map = get(&VertexProps::discover_time, g); |
---|
| 73 | bfs_time_visitor < dtime_map_t > vis(dtime_map, time); |
---|
| 74 | breadth_first_search(g, vertex(s, g), color_map(get(&VertexProps::color, g)). |
---|
| 75 | visitor(vis)); |
---|
| 76 | |
---|
| 77 | // a vector to hold the discover time property for each vertex |
---|
| 78 | std::vector < Size > dtime(num_vertices(g)); |
---|
| 79 | graph_traits<graph_t>::vertex_iterator vi, vi_end; |
---|
| 80 | std::size_t c = 0; |
---|
| 81 | for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi, ++c) |
---|
| 82 | dtime[c] = dtime_map[*vi]; |
---|
| 83 | |
---|
| 84 | // Use std::sort to order the vertices by their discover time |
---|
| 85 | std::vector<graph_traits<graph_t>::vertices_size_type > discover_order(N); |
---|
| 86 | integer_range < int >range(0, N); |
---|
| 87 | std::copy(range.begin(), range.end(), discover_order.begin()); |
---|
| 88 | std::sort(discover_order.begin(), discover_order.end(), |
---|
| 89 | indirect_cmp < Iiter, std::less < Size > >(&dtime[0])); |
---|
| 90 | |
---|
| 91 | std::cout << "order of discovery: "; |
---|
| 92 | for (int i = 0; i < N; ++i) |
---|
| 93 | std::cout << name[discover_order[i]] << " "; |
---|
| 94 | std::cout << std::endl; |
---|
| 95 | |
---|
| 96 | return EXIT_SUCCESS; |
---|
| 97 | } |
---|