1 //=======================================================================
2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
3 // Copyright 2003 Bruce Barr
4 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //=======================================================================
11 // Nonrecursive implementation of depth_first_visit_impl submitted by
12 // Bruce Barr, schmoost <at> yahoo.com, May/June 2003.
13 #ifndef BOOST_GRAPH_RECURSIVE_DFS_HPP
14 #define BOOST_GRAPH_RECURSIVE_DFS_HPP
16 #include <boost/config.hpp>
17 #include <boost/graph/graph_traits.hpp>
18 #include <boost/graph/graph_concepts.hpp>
19 #include <boost/graph/properties.hpp>
20 #include <boost/graph/visitors.hpp>
21 #include <boost/graph/named_function_params.hpp>
22 #include <boost/graph/detail/mpi_include.hpp>
23 #include <boost/ref.hpp>
24 #include <boost/implicit_cast.hpp>
25 #include <boost/optional.hpp>
26 #include <boost/parameter.hpp>
27 #include <boost/concept/assert.hpp>
28 #include <boost/tti/has_member_function.hpp>
36 template < class Visitor, class Graph > class DFSVisitorConcept
41 BOOST_CONCEPT_ASSERT((CopyConstructibleConcept< Visitor >));
42 vis.initialize_vertex(u, g);
43 vis.start_vertex(u, g);
44 vis.discover_vertex(u, g);
45 vis.examine_edge(e, g);
48 vis.forward_or_cross_edge(e, g);
49 // vis.finish_edge(e, g); // Optional for user
50 vis.finish_vertex(u, g);
56 typename graph_traits< Graph >::vertex_descriptor u;
57 typename graph_traits< Graph >::edge_descriptor e;
65 template < class T, class T2 >
66 bool operator()(const T&, const T2&) const
72 BOOST_TTI_HAS_MEMBER_FUNCTION(finish_edge)
74 template < bool IsCallable > struct do_call_finish_edge
76 template < typename E, typename G, typename Vis >
77 static void call_finish_edge(Vis& vis, E e, const G& g)
79 vis.finish_edge(e, g);
83 template <> struct do_call_finish_edge< false >
85 template < typename E, typename G, typename Vis >
86 static void call_finish_edge(Vis&, E, const G&)
91 template < typename E, typename G, typename Vis >
92 void call_finish_edge(Vis& vis, E e, const G& g)
93 { // Only call if method exists
94 #if ((defined(__GNUC__) && (__GNUC__ > 4) \
95 || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 9))) \
96 || defined(__clang__) \
97 || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 1200)))
98 do_call_finish_edge< has_member_function_finish_edge< Vis, void,
99 boost::mpl::vector< E, const G& > >::value >::call_finish_edge(vis,
102 do_call_finish_edge< has_member_function_finish_edge< Vis,
103 void >::value >::call_finish_edge(vis, e, g);
107 // Define BOOST_RECURSIVE_DFS to use older, recursive version.
108 // It is retained for a while in order to perform performance
110 #ifndef BOOST_RECURSIVE_DFS
112 // If the vertex u and the iterators ei and ei_end are thought of as the
113 // context of the algorithm, each push and pop from the stack could
114 // be thought of as a context shift.
115 // Each pass through "while (ei != ei_end)" may refer to the out-edges of
116 // an entirely different vertex, because the context of the algorithm
117 // shifts every time a white adjacent vertex is discovered.
118 // The corresponding context shift back from the adjacent vertex occurs
119 // after all of its out-edges have been examined.
121 // See https://lists.boost.org/Archives/boost/2003/06/49265.php for FAQ.
123 template < class IncidenceGraph, class DFSVisitor, class ColorMap,
124 class TerminatorFunc >
125 void depth_first_visit_impl(const IncidenceGraph& g,
126 typename graph_traits< IncidenceGraph >::vertex_descriptor u,
127 DFSVisitor& vis, ColorMap color, TerminatorFunc func = TerminatorFunc())
129 BOOST_CONCEPT_ASSERT((IncidenceGraphConcept< IncidenceGraph >));
130 BOOST_CONCEPT_ASSERT((DFSVisitorConcept< DFSVisitor, IncidenceGraph >));
132 typename graph_traits< IncidenceGraph >::vertex_descriptor Vertex;
133 typedef typename graph_traits< IncidenceGraph >::edge_descriptor Edge;
134 BOOST_CONCEPT_ASSERT((ReadWritePropertyMapConcept< ColorMap, Vertex >));
135 typedef typename property_traits< ColorMap >::value_type ColorValue;
136 BOOST_CONCEPT_ASSERT((ColorValueConcept< ColorValue >));
137 typedef color_traits< ColorValue > Color;
138 typedef typename graph_traits< IncidenceGraph >::out_edge_iterator Iter;
139 typedef std::pair< Vertex,
140 std::pair< boost::optional< Edge >, std::pair< Iter, Iter > > >
143 boost::optional< Edge > src_e;
145 std::vector< VertexInfo > stack;
147 // Possible optimization for vector
148 // stack.reserve(num_vertices(g));
150 put(color, u, Color::gray());
151 vis.discover_vertex(u, g);
152 boost::tie(ei, ei_end) = out_edges(u, g);
155 // If this vertex terminates the search, we push empty range
156 stack.push_back(std::make_pair(u,
157 std::make_pair(boost::optional< Edge >(),
158 std::make_pair(ei_end, ei_end))));
162 stack.push_back(std::make_pair(u,
164 boost::optional< Edge >(), std::make_pair(ei, ei_end))));
166 while (!stack.empty())
168 VertexInfo& back = stack.back();
170 src_e = back.second.first;
171 boost::tie(ei, ei_end) = back.second.second;
173 // finish_edge has to be called here, not after the
174 // loop. Think of the pop as the return from a recursive call.
177 call_finish_edge(vis, src_e.get(), g);
181 Vertex v = target(*ei, g);
182 vis.examine_edge(*ei, g);
183 ColorValue v_color = get(color, v);
184 if (v_color == Color::white())
186 vis.tree_edge(*ei, g);
188 stack.push_back(std::make_pair(u,
189 std::make_pair(src_e, std::make_pair(++ei, ei_end))));
191 put(color, u, Color::gray());
192 vis.discover_vertex(u, g);
193 boost::tie(ei, ei_end) = out_edges(u, g);
201 if (v_color == Color::gray())
203 vis.back_edge(*ei, g);
207 vis.forward_or_cross_edge(*ei, g);
209 call_finish_edge(vis, *ei, g);
213 put(color, u, Color::black());
214 vis.finish_vertex(u, g);
218 #else // BOOST_RECURSIVE_DFS is defined
220 template < class IncidenceGraph, class DFSVisitor, class ColorMap,
221 class TerminatorFunc >
222 void depth_first_visit_impl(const IncidenceGraph& g,
223 typename graph_traits< IncidenceGraph >::vertex_descriptor u,
224 DFSVisitor& vis, // pass-by-reference here, important!
225 ColorMap color, TerminatorFunc func)
227 BOOST_CONCEPT_ASSERT((IncidenceGraphConcept< IncidenceGraph >));
228 BOOST_CONCEPT_ASSERT((DFSVisitorConcept< DFSVisitor, IncidenceGraph >));
230 typename graph_traits< IncidenceGraph >::vertex_descriptor Vertex;
231 BOOST_CONCEPT_ASSERT((ReadWritePropertyMapConcept< ColorMap, Vertex >));
232 typedef typename property_traits< ColorMap >::value_type ColorValue;
233 BOOST_CONCEPT_ASSERT((ColorValueConcept< ColorValue >));
234 typedef color_traits< ColorValue > Color;
235 typename graph_traits< IncidenceGraph >::out_edge_iterator ei, ei_end;
237 put(color, u, Color::gray());
238 vis.discover_vertex(u, g);
241 for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei)
243 Vertex v = target(*ei, g);
244 vis.examine_edge(*ei, g);
245 ColorValue v_color = get(color, v);
246 if (v_color == Color::white())
248 vis.tree_edge(*ei, g);
249 depth_first_visit_impl(g, v, vis, color, func);
251 else if (v_color == Color::gray())
252 vis.back_edge(*ei, g);
254 vis.forward_or_cross_edge(*ei, g);
255 call_finish_edge(vis, *ei, g);
257 put(color, u, Color::black());
258 vis.finish_vertex(u, g);
263 } // namespace detail
265 template < class VertexListGraph, class DFSVisitor, class ColorMap >
266 void depth_first_search(const VertexListGraph& g, DFSVisitor vis,
268 typename graph_traits< VertexListGraph >::vertex_descriptor start_vertex)
270 typedef typename graph_traits< VertexListGraph >::vertex_descriptor Vertex;
271 BOOST_CONCEPT_ASSERT((DFSVisitorConcept< DFSVisitor, VertexListGraph >));
272 typedef typename property_traits< ColorMap >::value_type ColorValue;
273 typedef color_traits< ColorValue > Color;
275 typename graph_traits< VertexListGraph >::vertex_iterator ui, ui_end;
276 for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
278 Vertex u = implicit_cast< Vertex >(*ui);
279 put(color, u, Color::white());
280 vis.initialize_vertex(u, g);
283 if (start_vertex != detail::get_default_starting_vertex(g))
285 vis.start_vertex(start_vertex, g);
286 detail::depth_first_visit_impl(
287 g, start_vertex, vis, color, detail::nontruth2());
290 for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
292 Vertex u = implicit_cast< Vertex >(*ui);
293 ColorValue u_color = get(color, u);
294 if (u_color == Color::white())
296 vis.start_vertex(u, g);
297 detail::depth_first_visit_impl(
298 g, u, vis, color, detail::nontruth2());
303 template < class VertexListGraph, class DFSVisitor, class ColorMap >
304 void depth_first_search(
305 const VertexListGraph& g, DFSVisitor vis, ColorMap color)
307 typedef typename boost::graph_traits< VertexListGraph >::vertex_iterator vi;
308 std::pair< vi, vi > verts = vertices(g);
309 if (verts.first == verts.second)
312 depth_first_search(g, vis, color, detail::get_default_starting_vertex(g));
315 template < class Visitors = null_visitor > class dfs_visitor
319 dfs_visitor(Visitors vis) : m_vis(vis) {}
321 template < class Vertex, class Graph >
322 void initialize_vertex(Vertex u, const Graph& g)
324 invoke_visitors(m_vis, u, g, ::boost::on_initialize_vertex());
326 template < class Vertex, class Graph >
327 void start_vertex(Vertex u, const Graph& g)
329 invoke_visitors(m_vis, u, g, ::boost::on_start_vertex());
331 template < class Vertex, class Graph >
332 void discover_vertex(Vertex u, const Graph& g)
334 invoke_visitors(m_vis, u, g, ::boost::on_discover_vertex());
336 template < class Edge, class Graph >
337 void examine_edge(Edge u, const Graph& g)
339 invoke_visitors(m_vis, u, g, ::boost::on_examine_edge());
341 template < class Edge, class Graph > void tree_edge(Edge u, const Graph& g)
343 invoke_visitors(m_vis, u, g, ::boost::on_tree_edge());
345 template < class Edge, class Graph > void back_edge(Edge u, const Graph& g)
347 invoke_visitors(m_vis, u, g, ::boost::on_back_edge());
349 template < class Edge, class Graph >
350 void forward_or_cross_edge(Edge u, const Graph& g)
352 invoke_visitors(m_vis, u, g, ::boost::on_forward_or_cross_edge());
354 template < class Edge, class Graph >
355 void finish_edge(Edge u, const Graph& g)
357 invoke_visitors(m_vis, u, g, ::boost::on_finish_edge());
359 template < class Vertex, class Graph >
360 void finish_vertex(Vertex u, const Graph& g)
362 invoke_visitors(m_vis, u, g, ::boost::on_finish_vertex());
365 BOOST_GRAPH_EVENT_STUB(on_initialize_vertex, dfs)
366 BOOST_GRAPH_EVENT_STUB(on_start_vertex, dfs)
367 BOOST_GRAPH_EVENT_STUB(on_discover_vertex, dfs)
368 BOOST_GRAPH_EVENT_STUB(on_examine_edge, dfs)
369 BOOST_GRAPH_EVENT_STUB(on_tree_edge, dfs)
370 BOOST_GRAPH_EVENT_STUB(on_back_edge, dfs)
371 BOOST_GRAPH_EVENT_STUB(on_forward_or_cross_edge, dfs)
372 BOOST_GRAPH_EVENT_STUB(on_finish_edge, dfs)
373 BOOST_GRAPH_EVENT_STUB(on_finish_vertex, dfs)
378 template < class Visitors >
379 dfs_visitor< Visitors > make_dfs_visitor(Visitors vis)
381 return dfs_visitor< Visitors >(vis);
383 typedef dfs_visitor<> default_dfs_visitor;
385 // Boost.Parameter named parameter variant
390 template < typename Graph > struct depth_first_search_impl
392 typedef void result_type;
393 template < typename ArgPack >
394 void operator()(const Graph& g, const ArgPack& arg_pack) const
396 using namespace boost::graph::keywords;
397 boost::depth_first_search(g,
398 arg_pack[_visitor | make_dfs_visitor(null_visitor())],
399 boost::detail::make_color_map_from_arg_pack(g, arg_pack),
400 arg_pack[_root_vertex
401 || boost::detail::get_default_starting_vertex_t<
406 BOOST_GRAPH_MAKE_FORWARDING_FUNCTION(depth_first_search, 1, 4)
409 BOOST_GRAPH_MAKE_OLD_STYLE_PARAMETER_FUNCTION(depth_first_search, 1)
411 template < class IncidenceGraph, class DFSVisitor, class ColorMap >
412 void depth_first_visit(const IncidenceGraph& g,
413 typename graph_traits< IncidenceGraph >::vertex_descriptor u,
414 DFSVisitor vis, ColorMap color)
416 vis.start_vertex(u, g);
417 detail::depth_first_visit_impl(g, u, vis, color, detail::nontruth2());
420 template < class IncidenceGraph, class DFSVisitor, class ColorMap,
421 class TerminatorFunc >
422 void depth_first_visit(const IncidenceGraph& g,
423 typename graph_traits< IncidenceGraph >::vertex_descriptor u,
424 DFSVisitor vis, ColorMap color, TerminatorFunc func = TerminatorFunc())
426 vis.start_vertex(u, g);
427 detail::depth_first_visit_impl(g, u, vis, color, func);
431 #include BOOST_GRAPH_MPI_INCLUDE(<boost/graph/distributed/depth_first_search.hpp>)