1 //=======================================================================
2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
3 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
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 //=======================================================================
10 #include <boost/config.hpp>
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>
41 using namespace boost
;
43 template < class ParentDecorator
> struct print_parent
45 print_parent(const ParentDecorator
& p_
) : p(p_
) {}
46 template < class Vertex
> void operator()(const Vertex
& v
) const
48 std::cout
<< "parent[" << v
<< "] = " << p
[v
] << std::endl
;
53 template < class DistanceMap
, class PredecessorMap
, class ColorMap
>
54 class distance_and_pred_visitor
: public neighbor_bfs_visitor
<>
56 typedef typename property_traits
< ColorMap
>::value_type ColorValue
;
57 typedef color_traits
< ColorValue
> Color
;
60 distance_and_pred_visitor(DistanceMap d
, PredecessorMap p
, ColorMap c
)
61 : m_distance(d
), m_predecessor(p
), m_color(c
)
65 template < class Edge
, class Graph
>
66 void tree_out_edge(Edge e
, const Graph
& g
) const
68 typename graph_traits
< Graph
>::vertex_descriptor u
= source(e
, g
),
70 put(m_distance
, v
, get(m_distance
, u
) + 1);
71 put(m_predecessor
, v
, u
);
73 template < class Edge
, class Graph
>
74 void tree_in_edge(Edge e
, const Graph
& g
) const
76 typename graph_traits
< Graph
>::vertex_descriptor u
= source(e
, g
),
78 put(m_distance
, u
, get(m_distance
, v
) + 1);
79 put(m_predecessor
, u
, v
);
82 DistanceMap m_distance
;
83 PredecessorMap m_predecessor
;
87 int main(int, char*[])
89 typedef adjacency_list
< mapS
, vecS
, bidirectionalS
,
90 property
< vertex_color_t
, default_color_type
> >
93 typedef property_map
< Graph
, vertex_color_t
>::type ColorMap
;
108 typedef Graph::vertex_descriptor Vertex
;
110 // Array to store predecessor (parent) of each vertex. This will be
111 // used as a Decorator (actually, its iterator will be).
112 std::vector
< Vertex
> p(num_vertices(G
));
113 // VC++ version of std::vector has no ::pointer, so
114 // I use ::value_type* instead.
115 typedef std::vector
< Vertex
>::value_type
* Piter
;
117 // Array to store distances from the source to each vertex . We use
118 // a built-in array here just for variety. This will also be used as
120 typedef graph_traits
< Graph
>::vertices_size_type size_type
;
122 std::fill_n(d
, 5, 0);
125 Vertex s
= *(vertices(G
).first
);
127 distance_and_pred_visitor
< size_type
*, Vertex
*, ColorMap
> vis(
128 d
, &p
[0], get(vertex_color
, G
));
129 neighbor_breadth_first_search(
130 G
, s
, visitor(vis
).color_map(get(vertex_color
, G
)));
134 if (num_vertices(G
) < 11)
136 std::cout
<< "distances: ";
137 #ifdef BOOST_OLD_STREAM_ITERATORS
138 std::copy(d
, d
+ 5, std::ostream_iterator
< int, char >(std::cout
, " "));
140 std::copy(d
, d
+ 5, std::ostream_iterator
< int >(std::cout
, " "));
142 std::cout
<< std::endl
;
144 std::for_each(vertices(G
).first
, vertices(G
).second
,
145 print_parent
< Piter
>(&p
[0]));