os/ossrv/ossrv_pub/boost_apis/boost/graph/incremental_components.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
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
sl@0
    12
#ifndef BOOST_INCREMENTAL_COMPONENTS_HPP
sl@0
    13
#define BOOST_INCREMENTAL_COMPONENTS_HPP
sl@0
    14
sl@0
    15
#include <boost/detail/iterator.hpp>
sl@0
    16
#include <boost/graph/detail/incremental_components.hpp>
sl@0
    17
sl@0
    18
namespace boost {
sl@0
    19
sl@0
    20
  // A connected component algorithm for the case when dynamically
sl@0
    21
  // adding (but not removing) edges is common.  The
sl@0
    22
  // incremental_components() function is a preparing operation. Call
sl@0
    23
  // same_component to check whether two vertices are in the same
sl@0
    24
  // component, or use disjoint_set::find_set to determine the
sl@0
    25
  // representative for a vertex.
sl@0
    26
sl@0
    27
  // This version of connected components does not require a full
sl@0
    28
  // Graph. Instead, it just needs an edge list, where the vertices of
sl@0
    29
  // each edge need to be of integer type. The edges are assumed to
sl@0
    30
  // be undirected. The other difference is that the result is stored in
sl@0
    31
  // a container, instead of just a decorator.  The container should be
sl@0
    32
  // empty before the algorithm is called. It will grow during the
sl@0
    33
  // course of the algorithm. The container must be a model of
sl@0
    34
  // BackInsertionSequence and RandomAccessContainer
sl@0
    35
  // (std::vector is a good choice). After running the algorithm the
sl@0
    36
  // index container will map each vertex to the representative
sl@0
    37
  // vertex of the component to which it belongs.
sl@0
    38
  //
sl@0
    39
  // Adapted from an implementation by Alex Stepanov. The disjoint
sl@0
    40
  // sets data structure is from Tarjan's "Data Structures and Network
sl@0
    41
  // Algorithms", and the application to connected components is
sl@0
    42
  // similar to the algorithm described in Ch. 22 of "Intro to
sl@0
    43
  // Algorithms" by Cormen, et. all.
sl@0
    44
  //  
sl@0
    45
  // RankContainer is a random accessable container (operator[] is
sl@0
    46
  // defined) with a value type that can represent an integer part of
sl@0
    47
  // a binary log of the value type of the corresponding
sl@0
    48
  // ParentContainer (char is always enough) its size_type is no less
sl@0
    49
  // than the size_type of the corresponding ParentContainer
sl@0
    50
sl@0
    51
  // An implementation of disjoint sets can be found in
sl@0
    52
  // boost/pending/disjoint_sets.hpp
sl@0
    53
  
sl@0
    54
  template <class EdgeListGraph, class DisjointSets>
sl@0
    55
  void incremental_components(EdgeListGraph& g, DisjointSets& ds)
sl@0
    56
  {
sl@0
    57
    typename graph_traits<EdgeListGraph>::edge_iterator e, end;
sl@0
    58
    for (tie(e,end) = edges(g); e != end; ++e)
sl@0
    59
      ds.union_set(source(*e,g),target(*e,g));
sl@0
    60
  }
sl@0
    61
  
sl@0
    62
  template <class ParentIterator>
sl@0
    63
  void compress_components(ParentIterator first, ParentIterator last)
sl@0
    64
  {
sl@0
    65
    for (ParentIterator current = first; current != last; ++current) 
sl@0
    66
      detail::find_representative_with_full_compression(first, current-first);
sl@0
    67
  }
sl@0
    68
  
sl@0
    69
  template <class ParentIterator>
sl@0
    70
  typename boost::detail::iterator_traits<ParentIterator>::difference_type
sl@0
    71
  component_count(ParentIterator first, ParentIterator last)
sl@0
    72
  {
sl@0
    73
    std::ptrdiff_t count = 0;
sl@0
    74
    for (ParentIterator current = first; current != last; ++current) 
sl@0
    75
      if (*current == current - first) ++count; 
sl@0
    76
    return count;
sl@0
    77
  }
sl@0
    78
  
sl@0
    79
  // This algorithm can be applied to the result container of the
sl@0
    80
  // connected_components algorithm to normalize
sl@0
    81
  // the components.
sl@0
    82
  template <class ParentIterator>
sl@0
    83
  void normalize_components(ParentIterator first, ParentIterator last)
sl@0
    84
  {
sl@0
    85
    for (ParentIterator current = first; current != last; ++current) 
sl@0
    86
      detail::normalize_node(first, current - first);
sl@0
    87
  }
sl@0
    88
  
sl@0
    89
  template <class VertexListGraph, class DisjointSets> 
sl@0
    90
  void initialize_incremental_components(VertexListGraph& G, DisjointSets& ds)
sl@0
    91
  {
sl@0
    92
    typename graph_traits<VertexListGraph>
sl@0
    93
      ::vertex_iterator v, vend;
sl@0
    94
    for (tie(v, vend) = vertices(G); v != vend; ++v)
sl@0
    95
      ds.make_set(*v);
sl@0
    96
  }
sl@0
    97
sl@0
    98
  template <class Vertex, class DisjointSet>
sl@0
    99
  inline bool same_component(Vertex u, Vertex v, DisjointSet& ds)
sl@0
   100
  {
sl@0
   101
    return ds.find_set(u) == ds.find_set(v);
sl@0
   102
  }
sl@0
   103
sl@0
   104
  // considering changing the so that it initializes with a pair of
sl@0
   105
  // vertex iterators and a parent PA.
sl@0
   106
  
sl@0
   107
  template <class IndexT>
sl@0
   108
  class component_index
sl@0
   109
  {
sl@0
   110
  public://protected: (avoid friends for now)
sl@0
   111
    typedef std::vector<IndexT> MyIndexContainer;
sl@0
   112
    MyIndexContainer header;
sl@0
   113
    MyIndexContainer index;
sl@0
   114
    typedef typename MyIndexContainer::size_type SizeT;
sl@0
   115
    typedef typename MyIndexContainer::const_iterator IndexIter;
sl@0
   116
  public:
sl@0
   117
    typedef detail::component_iterator<IndexIter, IndexT, SizeT> 
sl@0
   118
      component_iterator;
sl@0
   119
    class component {
sl@0
   120
      friend class component_index;
sl@0
   121
    protected:
sl@0
   122
      IndexT number;
sl@0
   123
      const component_index<IndexT>* comp_ind_ptr;
sl@0
   124
      component(IndexT i, const component_index<IndexT>* p) 
sl@0
   125
        : number(i), comp_ind_ptr(p) {}
sl@0
   126
    public:
sl@0
   127
      typedef component_iterator iterator;
sl@0
   128
      typedef component_iterator const_iterator;
sl@0
   129
      typedef IndexT value_type;
sl@0
   130
      iterator begin() const {
sl@0
   131
        return iterator( comp_ind_ptr->index.begin(),
sl@0
   132
                         (comp_ind_ptr->header)[number] );
sl@0
   133
      }
sl@0
   134
      iterator end() const {
sl@0
   135
        return iterator( comp_ind_ptr->index.begin(), 
sl@0
   136
                         comp_ind_ptr->index.size() );
sl@0
   137
      }
sl@0
   138
    };
sl@0
   139
    typedef SizeT size_type;
sl@0
   140
    typedef component value_type;
sl@0
   141
    
sl@0
   142
#if defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)
sl@0
   143
    template <class Iterator>
sl@0
   144
    component_index(Iterator first, Iterator last) 
sl@0
   145
    : index(std::distance(first, last))
sl@0
   146
    { 
sl@0
   147
      std::copy(first, last, index.begin());
sl@0
   148
      detail::construct_component_index(index, header);
sl@0
   149
    }
sl@0
   150
#else
sl@0
   151
    template <class Iterator>
sl@0
   152
    component_index(Iterator first, Iterator last) 
sl@0
   153
      : index(first, last)
sl@0
   154
    { 
sl@0
   155
      detail::construct_component_index(index, header);
sl@0
   156
    }
sl@0
   157
#endif
sl@0
   158
sl@0
   159
    component operator[](IndexT i) const {
sl@0
   160
      return component(i, this);
sl@0
   161
    }
sl@0
   162
    SizeT size() const {
sl@0
   163
      return header.size();
sl@0
   164
    }
sl@0
   165
    
sl@0
   166
  };
sl@0
   167
sl@0
   168
} // namespace boost
sl@0
   169
sl@0
   170
#endif // BOOST_INCREMENTAL_COMPONENTS_HPP