From 86ef70716434ea1dabe2b6a25bb489e19793ebfe Mon Sep 17 00:00:00 2001 From: Jeremy Siek Date: Tue, 6 Feb 2001 19:40:26 +0000 Subject: [PATCH] added is_connected [SVN r8986] --- include/boost/graph/graph_utility.hpp | 49 ++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/include/boost/graph/graph_utility.hpp b/include/boost/graph/graph_utility.hpp index 18ecb2db..e2eabe37 100644 --- a/include/boost/graph/graph_utility.hpp +++ b/include/boost/graph/graph_utility.hpp @@ -313,8 +313,12 @@ namespace boost { } // is x a descendant of y? - template - inline bool is_descendant(Node x, Node y, ParentMap parent) { + template + inline bool is_descendant + (typename property_traits::value_type x, + typename property_traits::value_type y, + ParentMap parent) + { if (get(parent, x) == x) // x is the root of the tree return false; else if (get(parent, x) == y) @@ -324,19 +328,48 @@ namespace boost { } // is y reachable from x? - template + template inline bool is_reachable - (typename graph_traits::vertex_descriptor x, - typename graph_traits::vertex_descriptor y, - const Graph& g, - ColorMap color) // should be white for every vertex + (typename graph_traits::vertex_descriptor x, + typename graph_traits::vertex_descriptor y, + const IncidenceGraph& g, + VertexColorMap color) // should start out white for every vertex { - typedef typename property_traits::value_type ColorValue; + typedef typename property_traits::value_type ColorValue; dfs_visitor<> vis; depth_first_visit(g, x, vis, color); return get(color, y) != color_traits::white(); } + // Is the undirected graph connected? + // Is the directed graph strongly connected? + template + inline bool is_connected(const VertexListGraph& g, VertexColorMap color) + { + typedef typename property_traits::value_type ColorValue; + typedef color_traits Color; + typename graph_traits::vertex_iterator + ui, ui_end, vi, vi_end, ci, ci_end; + for (tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui) + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) + if (*ui != *vi) { + for (tie(ci, ci_end) = vertices(g); ci != ci_end; ++ci) + put(color, *ci, Color::white()); + if (! is_reachable(*ui, *vi, color)) + return false; + } + return true; + } + + template + bool is_self_loop + (typename graph_traits::edge_descriptor e, + const Graph& g) + { + return source(e, g) == target(e, g); + } + + template std::pair make_list(const T1& t1, const T2& t2)