os/ossrv/ossrv_pub/boost_apis/boost/graph/connected_components.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
//
sl@0
     2
//=======================================================================
sl@0
     3
// Copyright 1997-2001 University of Notre Dame.
sl@0
     4
// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
sl@0
     5
//
sl@0
     6
// Distributed under the Boost Software License, Version 1.0. (See
sl@0
     7
// accompanying file LICENSE_1_0.txt or copy at
sl@0
     8
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
     9
//=======================================================================
sl@0
    10
//
sl@0
    11
#ifndef BOOST_GRAPH_CONNECTED_COMPONENTS_HPP
sl@0
    12
#define BOOST_GRAPH_CONNECTED_COMPONENTS_HPP
sl@0
    13
sl@0
    14
#include <boost/config.hpp>
sl@0
    15
#include <boost/graph/depth_first_search.hpp>
sl@0
    16
#include <boost/graph/properties.hpp>
sl@0
    17
#include <boost/graph/graph_concepts.hpp>
sl@0
    18
sl@0
    19
#include <boost/static_assert.hpp>
sl@0
    20
sl@0
    21
namespace boost {
sl@0
    22
sl@0
    23
  namespace detail {
sl@0
    24
sl@0
    25
    // This visitor is used both in the connected_components algorithm
sl@0
    26
    // and in the kosaraju strong components algorithm during the
sl@0
    27
    // second DFS traversal.
sl@0
    28
    template <class ComponentsMap>
sl@0
    29
    class components_recorder : public dfs_visitor<>
sl@0
    30
    {
sl@0
    31
      typedef typename property_traits<ComponentsMap>::value_type comp_type;
sl@0
    32
    public:
sl@0
    33
      components_recorder(ComponentsMap c, 
sl@0
    34
                          comp_type& c_count)
sl@0
    35
        : m_component(c), m_count(c_count) {}
sl@0
    36
sl@0
    37
      template <class Vertex, class Graph>
sl@0
    38
      void start_vertex(Vertex, Graph&) {
sl@0
    39
        if (m_count == (std::numeric_limits<comp_type>::max)())
sl@0
    40
          m_count = 0; // start counting components at zero
sl@0
    41
        else
sl@0
    42
          ++m_count;
sl@0
    43
      }
sl@0
    44
      template <class Vertex, class Graph>
sl@0
    45
      void discover_vertex(Vertex u, Graph&) {
sl@0
    46
        put(m_component, u, m_count);
sl@0
    47
      }
sl@0
    48
    protected:
sl@0
    49
      ComponentsMap m_component;
sl@0
    50
      comp_type& m_count;
sl@0
    51
    };
sl@0
    52
sl@0
    53
  } // namespace detail
sl@0
    54
sl@0
    55
  // This function computes the connected components of an undirected
sl@0
    56
  // graph using a single application of depth first search.
sl@0
    57
sl@0
    58
  template <class Graph, class ComponentMap, class P, class T, class R>
sl@0
    59
  inline typename property_traits<ComponentMap>::value_type
sl@0
    60
  connected_components(const Graph& g, ComponentMap c, 
sl@0
    61
                       const bgl_named_params<P, T, R>& params)
sl@0
    62
  {
sl@0
    63
    if (num_vertices(g) == 0) return 0;
sl@0
    64
sl@0
    65
    typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
sl@0
    66
    function_requires< WritablePropertyMapConcept<ComponentMap, Vertex> >();
sl@0
    67
    typedef typename boost::graph_traits<Graph>::directed_category directed;
sl@0
    68
    BOOST_STATIC_ASSERT((boost::is_same<directed, undirected_tag>::value));
sl@0
    69
sl@0
    70
    typedef typename property_traits<ComponentMap>::value_type comp_type;
sl@0
    71
    // c_count initialized to "nil" (with nil represented by (max)())
sl@0
    72
    comp_type c_count((std::numeric_limits<comp_type>::max)());
sl@0
    73
    detail::components_recorder<ComponentMap> vis(c, c_count);
sl@0
    74
    depth_first_search(g, params.visitor(vis));
sl@0
    75
    return c_count + 1;
sl@0
    76
  }
sl@0
    77
sl@0
    78
  template <class Graph, class ComponentMap>
sl@0
    79
  inline typename property_traits<ComponentMap>::value_type
sl@0
    80
  connected_components(const Graph& g, ComponentMap c)
sl@0
    81
  {
sl@0
    82
    if (num_vertices(g) == 0) return 0;
sl@0
    83
sl@0
    84
    typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
sl@0
    85
    function_requires< WritablePropertyMapConcept<ComponentMap, Vertex> >();
sl@0
    86
    typedef typename boost::graph_traits<Graph>::directed_category directed;
sl@0
    87
    BOOST_STATIC_ASSERT((boost::is_same<directed, undirected_tag>::value));
sl@0
    88
sl@0
    89
    typedef typename property_traits<ComponentMap>::value_type comp_type;
sl@0
    90
    // c_count initialized to "nil" (with nil represented by (max)())
sl@0
    91
    comp_type c_count((std::numeric_limits<comp_type>::max)());
sl@0
    92
    detail::components_recorder<ComponentMap> vis(c, c_count);
sl@0
    93
    depth_first_search(g, visitor(vis));
sl@0
    94
    return c_count + 1;
sl@0
    95
  }
sl@0
    96
sl@0
    97
  
sl@0
    98
} // namespace boost
sl@0
    99
sl@0
   100
sl@0
   101
#endif // BOOST_GRAPH_CONNECTED_COMPONENTS_HPP