• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 > struct print_parent
55 {
print_parentprint_parent56     print_parent(const ParentDecorator& p_) : p(p_) {}
operator ()print_parent57     template < class Vertex > void operator()(const Vertex& v) const
58     {
59         std::cout << "parent[" << v << "] = " << p[v] << std::endl;
60     }
61     ParentDecorator p;
62 };
63 
64 template < class NewGraph, class Tag >
65 struct graph_copier
66 : public boost::base_visitor< graph_copier< NewGraph, Tag > >
67 {
68     typedef Tag event_filter;
69 
graph_copiergraph_copier70     graph_copier(NewGraph& graph) : new_g(graph) {}
71 
operator ()graph_copier72     template < class Edge, class Graph > void operator()(Edge e, Graph& g)
73     {
74         boost::add_edge(boost::source(e, g), boost::target(e, g), new_g);
75     }
76 
77 private:
78     NewGraph& new_g;
79 };
80 
81 template < class NewGraph, class Tag >
copy_graph(NewGraph & g,Tag)82 inline graph_copier< NewGraph, Tag > copy_graph(NewGraph& g, Tag)
83 {
84     return graph_copier< NewGraph, Tag >(g);
85 }
86 
main(int,char * [])87 int main(int, char*[])
88 {
89     typedef boost::adjacency_list< boost::mapS, boost::vecS,
90         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(G, s,
129         boost::visitor(boost::make_neighbor_bfs_visitor(
130             std::make_pair(boost::record_distances(d, boost::on_tree_edge()),
131                 boost::record_predecessors(&p[0], boost::on_tree_edge())))));
132 
133     boost::print_graph(G);
134 
135     if (boost::num_vertices(G) < 11)
136     {
137         std::cout << "distances: ";
138 #ifdef BOOST_OLD_STREAM_ITERATORS
139         std::copy(d, d + 5, std::ostream_iterator< int, char >(std::cout, " "));
140 #else
141         std::copy(d, d + 5, std::ostream_iterator< int >(std::cout, " "));
142 #endif
143         std::cout << std::endl;
144 
145         std::for_each(boost::vertices(G).first, boost::vertices(G).second,
146             print_parent< Piter >(&p[0]));
147     }
148 
149     return 0;
150 }
151