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