1 //=======================================================================
2 // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
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 #include <boost/property_map/property_map.hpp>
16 using namespace boost;
17 template < typename TimeMap > class bfs_time_visitor:public default_bfs_visitor {
18 typedef typename property_traits < TimeMap >::value_type T;
20 bfs_time_visitor(TimeMap tmap, T & t):m_timemap(tmap), m_time(t) { }
21 template < typename Vertex, typename Graph >
22 void discover_vertex(Vertex u, const Graph & g) const
24 put(m_timemap, u, m_time++);
32 boost::default_color_type color;
33 std::size_t discover_time;
40 using namespace boost;
41 // Select the graph type we wish to use
42 typedef adjacency_list < listS, listS, undirectedS,
44 // Set up the vertex IDs and names
45 enum { r, s, t, u, v, w, x, y, N };
46 const char *name = "rstuvwxy";
47 // Specify the edges in the graph
48 typedef std::pair < int, int >E;
49 E edge_array[] = { E(r, s), E(r, v), E(s, w), E(w, r), E(w, t),
50 E(w, x), E(x, t), E(t, u), E(x, y), E(u, y)
52 // Create the graph object
53 const int n_edges = sizeof(edge_array) / sizeof(E);
54 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
55 // VC++ has trouble with the edge iterator constructor
57 std::vector<graph_traits<graph_t>::vertex_descriptor> verts;
58 for (std::size_t i = 0; i < N; ++i)
59 verts.push_back(add_vertex(g));
60 for (std::size_t j = 0; j < n_edges; ++j)
61 add_edge(verts[edge_array[j].first], verts[edge_array[j].second], g);
63 typedef graph_traits<graph_t>::vertices_size_type v_size_t;
64 graph_t g(edge_array, edge_array + n_edges, v_size_t(N));
68 typedef graph_traits<graph_t>::vertices_size_type Size;
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)).
77 // a vector to hold the discover time property for each vertex
78 std::vector < Size > dtime(num_vertices(g));
80 iterator_property_map<std::vector<Size>::iterator,
81 property_map<graph_t, unsigned int VertexProps::*>::type>
83 graph_traits<graph_t>::vertex_iterator vi, vi_end;
85 for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi, ++c) {
86 dtime[c] = dtime_map[*vi];
87 put(&VertexProps::index, g, *vi, c);
89 dtime_pm_type dtime_pm(dtime.begin(), get(&VertexProps::index, g));
91 // Use std::sort to order the vertices by their discover time
92 std::vector<graph_traits<graph_t>::vertices_size_type > discover_order(N);
93 integer_range < int >range(0, N);
94 std::copy(range.begin(), range.end(), discover_order.begin());
95 std::sort(discover_order.begin(), discover_order.end(),
98 make_iterator_property_map(
100 typed_identity_property_map<std::size_t>())));
102 std::cout << "order of discovery: ";
103 for (int i = 0; i < N; ++i)
104 std::cout << name[discover_order[i]] << " ";
105 std::cout << std::endl;