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/graph_utility.hpp>
18 #include <boost/graph/visitors.hpp>
19 #include <boost/graph/adjacency_list.hpp>
20 #include <boost/graph/neighbor_bfs.hpp>
21 #include <boost/property_map/property_map.hpp>
22
23 /*
24
25 This examples shows how to use the breadth_first_search() GGCL
26 algorithm, specifically the 3 argument variant of bfs that assumes
27 the graph has a color property (property) stored internally.
28
29 Two pre-defined visitors are used to record the distance of each
30 vertex from the source vertex, and also to record the parent of each
31 vertex. Any number of visitors can be layered and passed to a GGCL
32 algorithm.
33
34 The call to vertices(G) returns an STL-compatible container which
35 contains all of the vertices in the graph. In this example we use
36 the vertices container in the STL for_each() function.
37
38 Sample Output:
39
40 0 --> 2
41 1 --> 1 3 4
42 2 --> 1 3 4
43 3 --> 1 4
44 4 --> 0 1
45 distances: 1 2 1 2 0
46 parent[0] = 4
47 parent[1] = 2
48 parent[2] = 0
49 parent[3] = 2
50 parent[4] = 0
51
52 */
53
54 template <class ParentDecorator>
55 struct print_parent {
print_parentprint_parent56 print_parent(const ParentDecorator& p_) : p(p_) { }
57 template <class Vertex>
operator ()print_parent58 void operator()(const Vertex& v) const {
59 std::cout << "parent[" << v << "] = " << p[v] << std::endl;
60 }
61 ParentDecorator p;
62 };
63
64
65 template <class NewGraph, class Tag>
66 struct graph_copier
67 : public boost::base_visitor<graph_copier<NewGraph, Tag> >
68 {
69 typedef Tag event_filter;
70
graph_copiergraph_copier71 graph_copier(NewGraph& graph) : new_g(graph) { }
72
73 template <class Edge, class Graph>
operator ()graph_copier74 void operator()(Edge e, Graph& g) {
75 boost::add_edge(boost::source(e, g), boost::target(e, g), new_g);
76 }
77 private:
78 NewGraph& new_g;
79 };
80
81 template <class NewGraph, class Tag>
82 inline graph_copier<NewGraph, Tag>
copy_graph(NewGraph & g,Tag)83 copy_graph(NewGraph& g, Tag) {
84 return graph_copier<NewGraph, Tag>(g);
85 }
86
main(int,char * [])87 int main(int , char* [])
88 {
89 typedef boost::adjacency_list<
90 boost::mapS, boost::vecS, boost::bidirectionalS,
91 boost::property<boost::vertex_color_t, boost::default_color_type,
92 boost::property<boost::vertex_degree_t, int,
93 boost::property<boost::vertex_in_degree_t, int,
94 boost::property<boost::vertex_out_degree_t, int> > > >
95 > Graph;
96
97 Graph G(5);
98 boost::add_edge(0, 2, G);
99 boost::add_edge(1, 1, G);
100 boost::add_edge(1, 3, G);
101 boost::add_edge(1, 4, G);
102 boost::add_edge(2, 1, G);
103 boost::add_edge(2, 3, G);
104 boost::add_edge(2, 4, G);
105 boost::add_edge(3, 1, G);
106 boost::add_edge(3, 4, G);
107 boost::add_edge(4, 0, G);
108 boost::add_edge(4, 1, G);
109
110 typedef Graph::vertex_descriptor Vertex;
111
112 // Array to store predecessor (parent) of each vertex. This will be
113 // used as a Decorator (actually, its iterator will be).
114 std::vector<Vertex> p(boost::num_vertices(G));
115 // VC++ version of std::vector has no ::pointer, so
116 // I use ::value_type* instead.
117 typedef std::vector<Vertex>::value_type* Piter;
118
119 // Array to store distances from the source to each vertex . We use
120 // a built-in array here just for variety. This will also be used as
121 // a Decorator.
122 boost::graph_traits<Graph>::vertices_size_type d[5];
123 std::fill_n(d, 5, 0);
124
125 // The source vertex
126 Vertex s = *(boost::vertices(G).first);
127 p[s] = s;
128 boost::neighbor_breadth_first_search
129 (G, s,
130 boost::visitor(boost::make_neighbor_bfs_visitor
131 (std::make_pair(boost::record_distances(d, boost::on_tree_edge()),
132 boost::record_predecessors(&p[0],
133 boost::on_tree_edge())))));
134
135 boost::print_graph(G);
136
137 if (boost::num_vertices(G) < 11) {
138 std::cout << "distances: ";
139 #ifdef BOOST_OLD_STREAM_ITERATORS
140 std::copy(d, d + 5, std::ostream_iterator<int, char>(std::cout, " "));
141 #else
142 std::copy(d, d + 5, std::ostream_iterator<int>(std::cout, " "));
143 #endif
144 std::cout << std::endl;
145
146 std::for_each(boost::vertices(G).first, boost::vertices(G).second,
147 print_parent<Piter>(&p[0]));
148 }
149
150 return 0;
151 }
152