1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/stdapis/boost/graph/strong_components.hpp Tue Mar 16 16:12:26 2010 +0000
1.3 @@ -0,0 +1,334 @@
1.4 +//
1.5 +//=======================================================================
1.6 +// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
1.7 +// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
1.8 +//
1.9 +// Distributed under the Boost Software License, Version 1.0. (See
1.10 +// accompanying file LICENSE_1_0.txt or copy at
1.11 +// http://www.boost.org/LICENSE_1_0.txt)
1.12 +//=======================================================================
1.13 +//
1.14 +
1.15 +#ifndef BOOST_GRAPH_STRONG_COMPONENTS_HPP
1.16 +#define BOOST_GRAPH_STRONG_COMPONENTS_HPP
1.17 +
1.18 +#include <stack>
1.19 +#include <boost/config.hpp>
1.20 +#include <boost/graph/depth_first_search.hpp>
1.21 +#include <boost/type_traits/conversion_traits.hpp>
1.22 +#include <boost/static_assert.hpp>
1.23 +
1.24 +namespace boost {
1.25 +
1.26 + //==========================================================================
1.27 + // This is Tarjan's algorithm for strongly connected components
1.28 + // from his paper "Depth first search and linear graph algorithms".
1.29 + // It calculates the components in a single application of DFS.
1.30 + // We implement the algorithm as a dfs-visitor.
1.31 +
1.32 + namespace detail {
1.33 +
1.34 + template <typename ComponentMap, typename RootMap, typename DiscoverTime,
1.35 + typename Stack>
1.36 + class tarjan_scc_visitor : public dfs_visitor<>
1.37 + {
1.38 + typedef typename property_traits<ComponentMap>::value_type comp_type;
1.39 + typedef typename property_traits<DiscoverTime>::value_type time_type;
1.40 + public:
1.41 + tarjan_scc_visitor(ComponentMap comp_map, RootMap r, DiscoverTime d,
1.42 + comp_type& c_, Stack& s_)
1.43 + : c(c_), comp(comp_map), root(r), discover_time(d),
1.44 + dfs_time(time_type()), s(s_) { }
1.45 +
1.46 + template <typename Graph>
1.47 + void discover_vertex(typename graph_traits<Graph>::vertex_descriptor v,
1.48 + const Graph&) {
1.49 + put(root, v, v);
1.50 + put(comp, v, (std::numeric_limits<comp_type>::max)());
1.51 + put(discover_time, v, dfs_time++);
1.52 + s.push(v);
1.53 + }
1.54 + template <typename Graph>
1.55 + void finish_vertex(typename graph_traits<Graph>::vertex_descriptor v,
1.56 + const Graph& g) {
1.57 + typename graph_traits<Graph>::vertex_descriptor w;
1.58 + typename graph_traits<Graph>::out_edge_iterator ei, ei_end;
1.59 + for (tie(ei, ei_end) = out_edges(v, g); ei != ei_end; ++ei) {
1.60 + w = target(*ei, g);
1.61 + if (get(comp, w) == (std::numeric_limits<comp_type>::max)())
1.62 + put(root, v, this->min_discover_time(get(root,v), get(root,w)));
1.63 + }
1.64 + if (get(root, v) == v) {
1.65 + do {
1.66 + w = s.top(); s.pop();
1.67 + put(comp, w, c);
1.68 + } while (w != v);
1.69 + ++c;
1.70 + }
1.71 + }
1.72 + private:
1.73 + template <typename Vertex>
1.74 + Vertex min_discover_time(Vertex u, Vertex v) {
1.75 + return get(discover_time, u) < get(discover_time,v) ? u : v;
1.76 + }
1.77 +
1.78 + comp_type& c;
1.79 + ComponentMap comp;
1.80 + RootMap root;
1.81 + DiscoverTime discover_time;
1.82 + time_type dfs_time;
1.83 + Stack& s;
1.84 + };
1.85 +
1.86 + template <class Graph, class ComponentMap, class RootMap,
1.87 + class DiscoverTime, class P, class T, class R>
1.88 + typename property_traits<ComponentMap>::value_type
1.89 + strong_components_impl
1.90 + (const Graph& g, // Input
1.91 + ComponentMap comp, // Output
1.92 + // Internal record keeping
1.93 + RootMap root,
1.94 + DiscoverTime discover_time,
1.95 + const bgl_named_params<P, T, R>& params)
1.96 + {
1.97 + typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
1.98 + function_requires< ReadWritePropertyMapConcept<ComponentMap, Vertex> >();
1.99 + function_requires< ReadWritePropertyMapConcept<RootMap, Vertex> >();
1.100 + typedef typename property_traits<RootMap>::value_type RootV;
1.101 + function_requires< ConvertibleConcept<RootV, Vertex> >();
1.102 + function_requires< ReadWritePropertyMapConcept<DiscoverTime, Vertex> >();
1.103 +
1.104 + typename property_traits<ComponentMap>::value_type total = 0;
1.105 +
1.106 + std::stack<Vertex> s;
1.107 + detail::tarjan_scc_visitor<ComponentMap, RootMap, DiscoverTime,
1.108 + std::stack<Vertex> >
1.109 + vis(comp, root, discover_time, total, s);
1.110 + depth_first_search(g, params.visitor(vis));
1.111 + return total;
1.112 + }
1.113 +
1.114 + //-------------------------------------------------------------------------
1.115 + // The dispatch functions handle the defaults for the rank and discover
1.116 + // time property maps.
1.117 + // dispatch with class specialization to avoid VC++ bug
1.118 +
1.119 + template <class DiscoverTimeMap>
1.120 + struct strong_comp_dispatch2 {
1.121 + template <class Graph, class ComponentMap, class RootMap, class P, class T, class R>
1.122 + inline static typename property_traits<ComponentMap>::value_type
1.123 + apply(const Graph& g,
1.124 + ComponentMap comp,
1.125 + RootMap r_map,
1.126 + const bgl_named_params<P, T, R>& params,
1.127 + DiscoverTimeMap time_map)
1.128 + {
1.129 + return strong_components_impl(g, comp, r_map, time_map, params);
1.130 + }
1.131 + };
1.132 +
1.133 +
1.134 + template <>
1.135 + struct strong_comp_dispatch2<detail::error_property_not_found> {
1.136 + template <class Graph, class ComponentMap, class RootMap,
1.137 + class P, class T, class R>
1.138 + inline static typename property_traits<ComponentMap>::value_type
1.139 + apply(const Graph& g,
1.140 + ComponentMap comp,
1.141 + RootMap r_map,
1.142 + const bgl_named_params<P, T, R>& params,
1.143 + detail::error_property_not_found)
1.144 + {
1.145 + typedef typename graph_traits<Graph>::vertices_size_type size_type;
1.146 + size_type n = num_vertices(g) > 0 ? num_vertices(g) : 1;
1.147 + std::vector<size_type> time_vec(n);
1.148 + return strong_components_impl
1.149 + (g, comp, r_map,
1.150 + make_iterator_property_map(time_vec.begin(), choose_const_pmap
1.151 + (get_param(params, vertex_index),
1.152 + g, vertex_index), time_vec[0]),
1.153 + params);
1.154 + }
1.155 + };
1.156 +
1.157 + template <class Graph, class ComponentMap, class RootMap,
1.158 + class P, class T, class R, class DiscoverTimeMap>
1.159 + inline typename property_traits<ComponentMap>::value_type
1.160 + scc_helper2(const Graph& g,
1.161 + ComponentMap comp,
1.162 + RootMap r_map,
1.163 + const bgl_named_params<P, T, R>& params,
1.164 + DiscoverTimeMap time_map)
1.165 + {
1.166 + return strong_comp_dispatch2<DiscoverTimeMap>::apply(g, comp, r_map, params, time_map);
1.167 + }
1.168 +
1.169 + template <class RootMap>
1.170 + struct strong_comp_dispatch1 {
1.171 +
1.172 + template <class Graph, class ComponentMap, class P, class T, class R>
1.173 + inline static typename property_traits<ComponentMap>::value_type
1.174 + apply(const Graph& g,
1.175 + ComponentMap comp,
1.176 + const bgl_named_params<P, T, R>& params,
1.177 + RootMap r_map)
1.178 + {
1.179 + return scc_helper2(g, comp, r_map, params, get_param(params, vertex_discover_time));
1.180 + }
1.181 + };
1.182 + template <>
1.183 + struct strong_comp_dispatch1<detail::error_property_not_found> {
1.184 +
1.185 + template <class Graph, class ComponentMap,
1.186 + class P, class T, class R>
1.187 + inline static typename property_traits<ComponentMap>::value_type
1.188 + apply(const Graph& g,
1.189 + ComponentMap comp,
1.190 + const bgl_named_params<P, T, R>& params,
1.191 + detail::error_property_not_found)
1.192 + {
1.193 + typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
1.194 + typename std::vector<Vertex>::size_type
1.195 + n = num_vertices(g) > 0 ? num_vertices(g) : 1;
1.196 + std::vector<Vertex> root_vec(n);
1.197 + return scc_helper2
1.198 + (g, comp,
1.199 + make_iterator_property_map(root_vec.begin(), choose_const_pmap
1.200 + (get_param(params, vertex_index),
1.201 + g, vertex_index), root_vec[0]),
1.202 + params,
1.203 + get_param(params, vertex_discover_time));
1.204 + }
1.205 + };
1.206 +
1.207 + template <class Graph, class ComponentMap, class RootMap,
1.208 + class P, class T, class R>
1.209 + inline typename property_traits<ComponentMap>::value_type
1.210 + scc_helper1(const Graph& g,
1.211 + ComponentMap comp,
1.212 + const bgl_named_params<P, T, R>& params,
1.213 + RootMap r_map)
1.214 + {
1.215 + return detail::strong_comp_dispatch1<RootMap>::apply(g, comp, params,
1.216 + r_map);
1.217 + }
1.218 +
1.219 + } // namespace detail
1.220 +
1.221 + template <class Graph, class ComponentMap,
1.222 + class P, class T, class R>
1.223 + inline typename property_traits<ComponentMap>::value_type
1.224 + strong_components(const Graph& g, ComponentMap comp,
1.225 + const bgl_named_params<P, T, R>& params)
1.226 + {
1.227 + typedef typename graph_traits<Graph>::directed_category DirCat;
1.228 + BOOST_STATIC_ASSERT((is_convertible<DirCat*, directed_tag*>::value == true));
1.229 + return detail::scc_helper1(g, comp, params,
1.230 + get_param(params, vertex_root_t()));
1.231 + }
1.232 +
1.233 + template <class Graph, class ComponentMap>
1.234 + inline typename property_traits<ComponentMap>::value_type
1.235 + strong_components(const Graph& g, ComponentMap comp)
1.236 + {
1.237 + typedef typename graph_traits<Graph>::directed_category DirCat;
1.238 + BOOST_STATIC_ASSERT((is_convertible<DirCat*, directed_tag*>::value == true));
1.239 + bgl_named_params<int, int> params(0);
1.240 + return strong_components(g, comp, params);
1.241 + }
1.242 +
1.243 + template <typename Graph, typename ComponentMap, typename ComponentLists>
1.244 + void build_component_lists
1.245 + (const Graph& g,
1.246 + typename graph_traits<Graph>::vertices_size_type num_scc,
1.247 + ComponentMap component_number,
1.248 + ComponentLists& components)
1.249 + {
1.250 + components.resize(num_scc);
1.251 + typename graph_traits<Graph>::vertex_iterator vi, vi_end;
1.252 + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
1.253 + components[component_number[*vi]].push_back(*vi);
1.254 + }
1.255 +
1.256 +
1.257 +} // namespace boost
1.258 +
1.259 +#include <queue>
1.260 +#include <vector>
1.261 +#include <boost/graph/transpose_graph.hpp>
1.262 +#include <boost/pending/indirect_cmp.hpp>
1.263 +#include <boost/graph/connected_components.hpp> // for components_recorder
1.264 +
1.265 +namespace boost {
1.266 +
1.267 + //==========================================================================
1.268 + // This is the version of strongly connected components from
1.269 + // "Intro. to Algorithms" by Cormen, Leiserson, Rivest, which was
1.270 + // adapted from "Data Structure and Algorithms" by Aho, Hopcroft,
1.271 + // and Ullman, who credit the algorithm to S.R. Kosaraju and M. Sharir.
1.272 + // The algorithm is based on computing DFS forests the graph
1.273 + // and its transpose.
1.274 +
1.275 + // This algorithm is slower than Tarjan's by a constant factor, uses
1.276 + // more memory, and puts more requirements on the graph type.
1.277 +
1.278 + template <class Graph, class DFSVisitor, class ComponentsMap,
1.279 + class DiscoverTime, class FinishTime,
1.280 + class ColorMap>
1.281 + typename property_traits<ComponentsMap>::value_type
1.282 + kosaraju_strong_components(Graph& G, ComponentsMap c,
1.283 + FinishTime finish_time, ColorMap color)
1.284 + {
1.285 + function_requires< MutableGraphConcept<Graph> >();
1.286 + // ...
1.287 +
1.288 + typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
1.289 + typedef typename property_traits<ColorMap>::value_type ColorValue;
1.290 + typedef color_traits<ColorValue> Color;
1.291 + typename property_traits<FinishTime>::value_type time = 0;
1.292 + depth_first_search
1.293 + (G, make_dfs_visitor(stamp_times(finish_time, time, on_finish_vertex())),
1.294 + color);
1.295 +
1.296 + Graph G_T(num_vertices(G));
1.297 + transpose_graph(G, G_T);
1.298 +
1.299 + typedef typename property_traits<ComponentsMap>::value_type count_type;
1.300 +
1.301 + count_type c_count(0);
1.302 + detail::components_recorder<ComponentsMap>
1.303 + vis(c, c_count);
1.304 +
1.305 + // initialize G_T
1.306 + typename graph_traits<Graph>::vertex_iterator ui, ui_end;
1.307 + for (tie(ui, ui_end) = vertices(G_T); ui != ui_end; ++ui)
1.308 + put(color, *ui, Color::white());
1.309 +
1.310 + typedef typename property_traits<FinishTime>::value_type D;
1.311 + typedef indirect_cmp< FinishTime, std::less<D> > Compare;
1.312 +
1.313 + Compare fl(finish_time);
1.314 + std::priority_queue<Vertex, std::vector<Vertex>, Compare > Q(fl);
1.315 +
1.316 + typename graph_traits<Graph>::vertex_iterator i, j, iend, jend;
1.317 + tie(i, iend) = vertices(G_T);
1.318 + tie(j, jend) = vertices(G);
1.319 + for ( ; i != iend; ++i, ++j) {
1.320 + put(finish_time, *i, get(finish_time, *j));
1.321 + Q.push(*i);
1.322 + }
1.323 +
1.324 + while ( !Q.empty() ) {
1.325 + Vertex u = Q.top();
1.326 + Q.pop();
1.327 + if (get(color, u) == Color::white()) {
1.328 + depth_first_visit(G_T, u, vis, color);
1.329 + ++c_count;
1.330 + }
1.331 + }
1.332 + return c_count;
1.333 + }
1.334 +
1.335 +} // namespace boost
1.336 +
1.337 +#endif // BOOST_GRAPH_STRONG_COMPONENTS_HPP