1 //=======================================================================
2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
3 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //=======================================================================
9
10 #include <boost/config.hpp>
11
12 #include <algorithm>
13 #include <vector>
14 #include <utility>
15 #include <iostream>
16
17 #include <boost/graph/visitors.hpp>
18 #include <boost/graph/adjacency_list.hpp>
19 #include <boost/graph/graph_utility.hpp>
20 #include <boost/graph/neighbor_bfs.hpp>
21 #include <boost/property_map/property_map.hpp>
22
23 /*
24
25 Sample Output:
26
27 0 --> 2
28 1 --> 1 3 4
29 2 --> 1 3 4
30 3 --> 1 4
31 4 --> 0 1
32 distances: 0 2 1 2 1
33 parent[0] = 0
34 parent[1] = 2
35 parent[2] = 0
36 parent[3] = 2
37 parent[4] = 0
38
39 */
40
41 using namespace boost;
42
43 template <class ParentDecorator>
44 struct print_parent {
print_parentprint_parent45 print_parent(const ParentDecorator& p_) : p(p_) { }
46 template <class Vertex>
operator ()print_parent47 void operator()(const Vertex& v) const {
48 std::cout << "parent[" << v << "] = " << p[v] << std::endl;
49 }
50 ParentDecorator p;
51 };
52
53 template <class DistanceMap, class PredecessorMap, class ColorMap>
54 class distance_and_pred_visitor : public neighbor_bfs_visitor<>
55 {
56 typedef typename property_traits<ColorMap>::value_type ColorValue;
57 typedef color_traits<ColorValue> Color;
58 public:
distance_and_pred_visitor(DistanceMap d,PredecessorMap p,ColorMap c)59 distance_and_pred_visitor(DistanceMap d, PredecessorMap p, ColorMap c)
60 : m_distance(d), m_predecessor(p), m_color(c) { }
61
62 template <class Edge, class Graph>
tree_out_edge(Edge e,const Graph & g) const63 void tree_out_edge(Edge e, const Graph& g) const
64 {
65 typename graph_traits<Graph>::vertex_descriptor
66 u = source(e, g), v = target(e, g);
67 put(m_distance, v, get(m_distance, u) + 1);
68 put(m_predecessor, v, u);
69 }
70 template <class Edge, class Graph>
tree_in_edge(Edge e,const Graph & g) const71 void tree_in_edge(Edge e, const Graph& g) const
72 {
73 typename graph_traits<Graph>::vertex_descriptor
74 u = source(e, g), v = target(e, g);
75 put(m_distance, u, get(m_distance, v) + 1);
76 put(m_predecessor, u, v);
77 }
78
79 DistanceMap m_distance;
80 PredecessorMap m_predecessor;
81 ColorMap m_color;
82 };
83
main(int,char * [])84 int main(int , char* [])
85 {
86 typedef adjacency_list<
87 mapS, vecS, bidirectionalS,
88 property<vertex_color_t, default_color_type>
89 > Graph;
90
91 typedef property_map<Graph, vertex_color_t>::type
92 ColorMap;
93
94 Graph G(5);
95 add_edge(0, 2, G);
96 add_edge(1, 1, G);
97 add_edge(1, 3, G);
98 add_edge(1, 4, G);
99 add_edge(2, 1, G);
100 add_edge(2, 3, G);
101 add_edge(2, 4, G);
102 add_edge(3, 1, G);
103 add_edge(3, 4, G);
104 add_edge(4, 0, G);
105 add_edge(4, 1, G);
106
107 typedef Graph::vertex_descriptor Vertex;
108
109 // Array to store predecessor (parent) of each vertex. This will be
110 // used as a Decorator (actually, its iterator will be).
111 std::vector<Vertex> p(num_vertices(G));
112 // VC++ version of std::vector has no ::pointer, so
113 // I use ::value_type* instead.
114 typedef std::vector<Vertex>::value_type* Piter;
115
116 // Array to store distances from the source to each vertex . We use
117 // a built-in array here just for variety. This will also be used as
118 // a Decorator.
119 typedef graph_traits<Graph>::vertices_size_type size_type;
120 size_type d[5];
121 std::fill_n(d, 5, 0);
122
123 // The source vertex
124 Vertex s = *(vertices(G).first);
125 p[s] = s;
126 distance_and_pred_visitor<size_type*, Vertex*, ColorMap>
127 vis(d, &p[0], get(vertex_color, G));
128 neighbor_breadth_first_search
129 (G, s, visitor(vis).
130 color_map(get(vertex_color, G)));
131
132 print_graph(G);
133
134 if (num_vertices(G) < 11) {
135 std::cout << "distances: ";
136 #ifdef BOOST_OLD_STREAM_ITERATORS
137 std::copy(d, d + 5, std::ostream_iterator<int, char>(std::cout, " "));
138 #else
139 std::copy(d, d + 5, std::ostream_iterator<int>(std::cout, " "));
140 #endif
141 std::cout << std::endl;
142
143 std::for_each(vertices(G).first, vertices(G).second,
144 print_parent<Piter>(&p[0]));
145 }
146
147 return 0;
148 }
149