PageRenderTime 50ms CodeModel.GetById 18ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/graph/strong_components.hpp

http://hadesmem.googlecode.com/
C++ Header | 341 lines | 267 code | 43 blank | 31 comment | 19 complexity | 827a9b8c614f18839885b5d9281cd941 MD5 | raw file
  1//
  2//=======================================================================
  3// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  4// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  5//
  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//=======================================================================
 10//
 11
 12#ifndef BOOST_GRAPH_STRONG_COMPONENTS_HPP
 13#define BOOST_GRAPH_STRONG_COMPONENTS_HPP
 14
 15#include <stack>
 16#include <boost/config.hpp>
 17#include <boost/graph/depth_first_search.hpp>
 18#include <boost/type_traits/conversion_traits.hpp>
 19#include <boost/static_assert.hpp>
 20#include <boost/graph/overloading.hpp>
 21
 22namespace boost {
 23
 24  //==========================================================================
 25  // This is Tarjan's algorithm for strongly connected components
 26  // from his paper "Depth first search and linear graph algorithms".
 27  // It calculates the components in a single application of DFS.
 28  // We implement the algorithm as a dfs-visitor.
 29
 30  namespace detail {
 31    
 32    template <typename ComponentMap, typename RootMap, typename DiscoverTime,
 33              typename Stack>
 34    class tarjan_scc_visitor : public dfs_visitor<> 
 35    {
 36      typedef typename property_traits<ComponentMap>::value_type comp_type;
 37      typedef typename property_traits<DiscoverTime>::value_type time_type;
 38    public:
 39      tarjan_scc_visitor(ComponentMap comp_map, RootMap r, DiscoverTime d, 
 40                         comp_type& c_, Stack& s_)
 41        : c(c_), comp(comp_map), root(r), discover_time(d),
 42          dfs_time(time_type()), s(s_) { }
 43
 44      template <typename Graph>
 45      void discover_vertex(typename graph_traits<Graph>::vertex_descriptor v,
 46                           const Graph&) {
 47        put(root, v, v);
 48        put(comp, v, (std::numeric_limits<comp_type>::max)());
 49        put(discover_time, v, dfs_time++);
 50        s.push(v);
 51      }
 52      template <typename Graph>
 53      void finish_vertex(typename graph_traits<Graph>::vertex_descriptor v,
 54                         const Graph& g) {
 55        typename graph_traits<Graph>::vertex_descriptor w;
 56        typename graph_traits<Graph>::out_edge_iterator ei, ei_end;
 57        for (boost::tie(ei, ei_end) = out_edges(v, g); ei != ei_end; ++ei) {
 58          w = target(*ei, g);
 59          if (get(comp, w) == (std::numeric_limits<comp_type>::max)())
 60            put(root, v, this->min_discover_time(get(root,v), get(root,w)));
 61        }
 62        if (get(root, v) == v) {
 63          do {
 64            w = s.top(); s.pop();
 65            put(comp, w, c);
 66          } while (w != v);
 67          ++c;
 68        }
 69      }
 70    private:
 71      template <typename Vertex>
 72      Vertex min_discover_time(Vertex u, Vertex v) {
 73        return get(discover_time, u) < get(discover_time,v) ? u : v;
 74      }
 75
 76      comp_type& c;
 77      ComponentMap comp;
 78      RootMap root;
 79      DiscoverTime discover_time;
 80      time_type dfs_time;
 81      Stack& s;
 82    };
 83    
 84    template <class Graph, class ComponentMap, class RootMap,
 85              class DiscoverTime, class P, class T, class R>
 86    typename property_traits<ComponentMap>::value_type
 87    strong_components_impl
 88      (const Graph& g,    // Input
 89       ComponentMap comp, // Output
 90       // Internal record keeping
 91       RootMap root, 
 92       DiscoverTime discover_time,
 93       const bgl_named_params<P, T, R>& params)
 94    {
 95      typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
 96      function_requires< ReadWritePropertyMapConcept<ComponentMap, Vertex> >();
 97      function_requires< ReadWritePropertyMapConcept<RootMap, Vertex> >();
 98      typedef typename property_traits<RootMap>::value_type RootV;
 99      function_requires< ConvertibleConcept<RootV, Vertex> >();
100      function_requires< ReadWritePropertyMapConcept<DiscoverTime, Vertex> >();
101
102      typename property_traits<ComponentMap>::value_type total = 0;
103
104      std::stack<Vertex> s;
105      detail::tarjan_scc_visitor<ComponentMap, RootMap, DiscoverTime, 
106        std::stack<Vertex> > 
107        vis(comp, root, discover_time, total, s);
108      depth_first_search(g, params.visitor(vis));
109      return total;
110    }
111
112    //-------------------------------------------------------------------------
113    // The dispatch functions handle the defaults for the rank and discover
114    // time property maps.
115    // dispatch with class specialization to avoid VC++ bug
116
117    template <class DiscoverTimeMap>
118    struct strong_comp_dispatch2 {
119      template <class Graph, class ComponentMap, class RootMap, class P, class T, class R>
120      inline static typename property_traits<ComponentMap>::value_type
121      apply(const Graph& g,
122            ComponentMap comp,
123            RootMap r_map,
124            const bgl_named_params<P, T, R>& params,
125            DiscoverTimeMap time_map)
126      {
127        return strong_components_impl(g, comp, r_map, time_map, params);
128      }
129    };
130
131
132    template <>
133    struct strong_comp_dispatch2<detail::error_property_not_found> {
134      template <class Graph, class ComponentMap, class RootMap,
135                class P, class T, class R>
136      inline static typename property_traits<ComponentMap>::value_type
137      apply(const Graph& g,
138            ComponentMap comp,
139            RootMap r_map,
140            const bgl_named_params<P, T, R>& params,
141            detail::error_property_not_found)
142      {
143        typedef typename graph_traits<Graph>::vertices_size_type size_type;
144        size_type       n = num_vertices(g) > 0 ? num_vertices(g) : 1;
145        std::vector<size_type> time_vec(n);
146        return strong_components_impl
147          (g, comp, r_map,
148           make_iterator_property_map(time_vec.begin(), choose_const_pmap
149                                      (get_param(params, vertex_index),
150                                       g, vertex_index), time_vec[0]),
151           params);
152      }
153    };
154
155    template <class Graph, class ComponentMap, class RootMap,
156              class P, class T, class R, class DiscoverTimeMap>
157    inline typename property_traits<ComponentMap>::value_type
158    scc_helper2(const Graph& g,
159                ComponentMap comp,
160                RootMap r_map,
161                const bgl_named_params<P, T, R>& params,
162                DiscoverTimeMap time_map)
163    {
164      return strong_comp_dispatch2<DiscoverTimeMap>::apply(g, comp, r_map, params, time_map);
165    }
166
167    template <class RootMap>
168    struct strong_comp_dispatch1 {
169
170      template <class Graph, class ComponentMap, class P, class T, class R>
171      inline static typename property_traits<ComponentMap>::value_type
172      apply(const Graph& g,
173            ComponentMap comp,
174            const bgl_named_params<P, T, R>& params,
175            RootMap r_map)
176      {
177        return scc_helper2(g, comp, r_map, params, get_param(params, vertex_discover_time));
178      }
179    };
180    template <>
181    struct strong_comp_dispatch1<detail::error_property_not_found> {
182
183      template <class Graph, class ComponentMap, 
184                class P, class T, class R>
185      inline static typename property_traits<ComponentMap>::value_type
186      apply(const Graph& g,
187            ComponentMap comp,
188            const bgl_named_params<P, T, R>& params,
189            detail::error_property_not_found)
190      {
191        typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
192        typename std::vector<Vertex>::size_type
193          n = num_vertices(g) > 0 ? num_vertices(g) : 1;
194        std::vector<Vertex> root_vec(n);
195        return scc_helper2
196          (g, comp, 
197           make_iterator_property_map(root_vec.begin(), choose_const_pmap
198                                      (get_param(params, vertex_index),
199                                       g, vertex_index), root_vec[0]),
200           params, 
201           get_param(params, vertex_discover_time));
202      }
203    };
204
205    template <class Graph, class ComponentMap, class RootMap,
206              class P, class T, class R>
207    inline typename property_traits<ComponentMap>::value_type
208    scc_helper1(const Graph& g,
209               ComponentMap comp,
210               const bgl_named_params<P, T, R>& params,
211               RootMap r_map)
212    {
213      return detail::strong_comp_dispatch1<RootMap>::apply(g, comp, params,
214                                                           r_map);
215    }
216
217  } // namespace detail 
218
219  template <class Graph, class ComponentMap, 
220            class P, class T, class R>
221  inline typename property_traits<ComponentMap>::value_type
222  strong_components(const Graph& g, ComponentMap comp,
223                    const bgl_named_params<P, T, R>& params
224                    BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph, vertex_list_graph_tag))
225  {
226    typedef typename graph_traits<Graph>::directed_category DirCat;
227    BOOST_STATIC_ASSERT((is_convertible<DirCat*, directed_tag*>::value == true));
228    return detail::scc_helper1(g, comp, params, 
229                               get_param(params, vertex_root_t()));
230  }
231
232  template <class Graph, class ComponentMap>
233  inline typename property_traits<ComponentMap>::value_type
234  strong_components(const Graph& g, ComponentMap comp
235                    BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph, vertex_list_graph_tag))
236  {
237    typedef typename graph_traits<Graph>::directed_category DirCat;
238    BOOST_STATIC_ASSERT((is_convertible<DirCat*, directed_tag*>::value == true));
239    bgl_named_params<int, int> params(0);
240    return strong_components(g, comp, params);
241  }
242
243  template <typename Graph, typename ComponentMap, typename ComponentLists>
244  void build_component_lists
245    (const Graph& g,
246     typename graph_traits<Graph>::vertices_size_type num_scc,
247     ComponentMap component_number,
248     ComponentLists& components)
249  {
250    components.resize(num_scc);
251    typename graph_traits<Graph>::vertex_iterator vi, vi_end;
252    for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
253      components[component_number[*vi]].push_back(*vi);
254  }
255
256
257} // namespace boost
258
259#include <queue>
260#include <vector>
261#include <boost/graph/transpose_graph.hpp>
262#include <boost/pending/indirect_cmp.hpp>
263#include <boost/graph/connected_components.hpp> // for components_recorder
264
265namespace boost {
266
267  //==========================================================================
268  // This is the version of strongly connected components from
269  // "Intro. to Algorithms" by Cormen, Leiserson, Rivest, which was
270  // adapted from "Data Structure and Algorithms" by Aho, Hopcroft,
271  // and Ullman, who credit the algorithm to S.R. Kosaraju and M. Sharir.
272  // The algorithm is based on computing DFS forests the graph
273  // and its transpose.
274
275  // This algorithm is slower than Tarjan's by a constant factor, uses
276  // more memory, and puts more requirements on the graph type.
277
278  template <class Graph, class DFSVisitor, class ComponentsMap,
279            class DiscoverTime, class FinishTime,
280            class ColorMap>
281  typename property_traits<ComponentsMap>::value_type
282  kosaraju_strong_components(Graph& G, ComponentsMap c,
283                             FinishTime finish_time, ColorMap color)
284  {
285    function_requires< MutableGraphConcept<Graph> >();
286    // ...
287    
288    typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
289    typedef typename property_traits<ColorMap>::value_type ColorValue;
290    typedef color_traits<ColorValue> Color;
291    typename property_traits<FinishTime>::value_type time = 0;
292    depth_first_search
293     (G, make_dfs_visitor(stamp_times(finish_time, time, on_finish_vertex())),
294      color);
295
296    Graph G_T(num_vertices(G));
297    transpose_graph(G, G_T);
298
299    typedef typename property_traits<ComponentsMap>::value_type count_type;
300
301    count_type c_count(0);
302    detail::components_recorder<ComponentsMap>
303      vis(c, c_count);
304
305    // initialize G_T
306    typename graph_traits<Graph>::vertex_iterator ui, ui_end;
307    for (boost::tie(ui, ui_end) = vertices(G_T); ui != ui_end; ++ui)
308      put(color, *ui, Color::white());
309
310    typedef typename property_traits<FinishTime>::value_type D;
311    typedef indirect_cmp< FinishTime, std::less<D> > Compare;
312
313    Compare fl(finish_time);
314    std::priority_queue<Vertex, std::vector<Vertex>, Compare > Q(fl);
315
316    typename graph_traits<Graph>::vertex_iterator i, j, iend, jend;
317    boost::tie(i, iend) = vertices(G_T);
318    boost::tie(j, jend) = vertices(G);
319    for ( ; i != iend; ++i, ++j) {
320      put(finish_time, *i, get(finish_time, *j));
321       Q.push(*i);
322    }
323
324    while ( !Q.empty() ) {
325      Vertex u = Q.top();
326      Q.pop();
327      if  (get(color, u) == Color::white()) {
328        depth_first_visit(G_T, u, vis, color);
329        ++c_count; 
330      }
331    }
332    return c_count;
333  }
334
335} // namespace boost
336
337#ifdef BOOST_GRAPH_USE_MPI
338#  include <boost/graph/distributed/strong_components.hpp>
339#endif
340
341#endif // BOOST_GRAPH_STRONG_COMPONENTS_HPP