mirror of
https://github.com/boostorg/disjoint_sets.git
synced 2026-01-19 04:12:08 +00:00
Merge branch 'develop' of https://github.com/boostorg/disjoint_sets into develop
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
# Empty Jamfile because the super project still expects one to appear here.
|
||||
# Can be deleted once 'status/Jamfile.v2' has been updated in the super
|
||||
# project.
|
||||
@@ -1 +0,0 @@
|
||||
Documentation is the top-level index.html file.
|
||||
@@ -1,88 +0,0 @@
|
||||
// (C) Copyright Jeremy Siek 2004
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_DETAIL_DISJOINT_SETS_HPP
|
||||
#define BOOST_DETAIL_DISJOINT_SETS_HPP
|
||||
|
||||
namespace boost {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <class ParentPA, class Vertex>
|
||||
Vertex
|
||||
find_representative_with_path_halving(ParentPA p, Vertex v)
|
||||
{
|
||||
Vertex parent = get(p, v);
|
||||
Vertex grandparent = get(p, parent);
|
||||
while (parent != grandparent) {
|
||||
put(p, v, grandparent);
|
||||
v = grandparent;
|
||||
parent = get(p, v);
|
||||
grandparent = get(p, parent);
|
||||
}
|
||||
return parent;
|
||||
}
|
||||
|
||||
template <class ParentPA, class Vertex>
|
||||
Vertex
|
||||
find_representative_with_full_compression(ParentPA parent, Vertex v)
|
||||
{
|
||||
Vertex old = v;
|
||||
Vertex ancestor = get(parent, v);
|
||||
while (ancestor != v) {
|
||||
v = ancestor;
|
||||
ancestor = get(parent, v);
|
||||
}
|
||||
v = get(parent, old);
|
||||
while (ancestor != v) {
|
||||
put(parent, old, ancestor);
|
||||
old = v;
|
||||
v = get(parent, old);
|
||||
}
|
||||
return ancestor;
|
||||
}
|
||||
|
||||
/* the postcondition of link sets is:
|
||||
component_representative(i) == component_representative(j)
|
||||
*/
|
||||
template <class ParentPA, class RankPA, class Vertex,
|
||||
class ComponentRepresentative>
|
||||
inline void
|
||||
link_sets(ParentPA p, RankPA rank, Vertex i, Vertex j,
|
||||
ComponentRepresentative comp_rep)
|
||||
{
|
||||
i = comp_rep(p, i);
|
||||
j = comp_rep(p, j);
|
||||
if (i == j) return;
|
||||
if (get(rank, i) > get(rank, j))
|
||||
put(p, j, i);
|
||||
else {
|
||||
put(p, i, j);
|
||||
if (get(rank, i) == get(rank, j))
|
||||
put(rank, j, get(rank, j) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// normalize components has the following postcondidition:
|
||||
// i >= p[i]
|
||||
// that is, the representative is the node with the smallest index in its class
|
||||
// as its precondition it it assumes that the node container is compressed
|
||||
|
||||
template <class ParentPA, class Vertex>
|
||||
inline void
|
||||
normalize_node(ParentPA p, Vertex i)
|
||||
{
|
||||
if (i > get(p,i) || get(p, get(p,i)) != get(p,i))
|
||||
put(p,i, get(p, get(p,i)));
|
||||
else {
|
||||
put(p, get(p,i), i);
|
||||
put(p, i, i);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_DETAIL_DISJOINT_SETS_HPP
|
||||
@@ -1,220 +0,0 @@
|
||||
//
|
||||
//=======================================================================
|
||||
// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
|
||||
// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//=======================================================================
|
||||
//
|
||||
#ifndef BOOST_DISJOINT_SETS_HPP
|
||||
#define BOOST_DISJOINT_SETS_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <boost/graph/properties.hpp>
|
||||
#include <boost/pending/detail/disjoint_sets.hpp>
|
||||
|
||||
namespace boost {
|
||||
|
||||
struct find_with_path_halving {
|
||||
template <class ParentPA, class Vertex>
|
||||
Vertex operator()(ParentPA p, Vertex v) {
|
||||
return detail::find_representative_with_path_halving(p, v);
|
||||
}
|
||||
};
|
||||
|
||||
struct find_with_full_path_compression {
|
||||
template <class ParentPA, class Vertex>
|
||||
Vertex operator()(ParentPA p, Vertex v){
|
||||
return detail::find_representative_with_full_compression(p, v);
|
||||
}
|
||||
};
|
||||
|
||||
// This is a generalized functor to provide disjoint sets operations
|
||||
// with "union by rank" and "path compression". A disjoint-set data
|
||||
// structure maintains a collection S={S1, S2, ..., Sk} of disjoint
|
||||
// sets. Each set is identified by a representative, which is some
|
||||
// member of of the set. Sets are represented by rooted trees. Two
|
||||
// heuristics: "union by rank" and "path compression" are used to
|
||||
// speed up the operations.
|
||||
|
||||
// Disjoint Set requires two vertex properties for internal use. A
|
||||
// RankPA and a ParentPA. The RankPA must map Vertex to some Integral type
|
||||
// (preferably the size_type associated with Vertex). The ParentPA
|
||||
// must map Vertex to Vertex.
|
||||
template <class RankPA, class ParentPA,
|
||||
class FindCompress = find_with_full_path_compression
|
||||
>
|
||||
class disjoint_sets {
|
||||
typedef disjoint_sets self;
|
||||
|
||||
inline disjoint_sets() {}
|
||||
public:
|
||||
inline disjoint_sets(RankPA r, ParentPA p)
|
||||
: rank(r), parent(p) {}
|
||||
|
||||
inline disjoint_sets(const self& c)
|
||||
: rank(c.rank), parent(c.parent) {}
|
||||
|
||||
// Make Set -- Create a singleton set containing vertex x
|
||||
template <class Element>
|
||||
inline void make_set(Element x)
|
||||
{
|
||||
put(parent, x, x);
|
||||
typedef typename property_traits<RankPA>::value_type R;
|
||||
put(rank, x, R());
|
||||
}
|
||||
|
||||
// Link - union the two sets represented by vertex x and y
|
||||
template <class Element>
|
||||
inline void link(Element x, Element y)
|
||||
{
|
||||
detail::link_sets(parent, rank, x, y, rep);
|
||||
}
|
||||
|
||||
// Union-Set - union the two sets containing vertex x and y
|
||||
template <class Element>
|
||||
inline void union_set(Element x, Element y)
|
||||
{
|
||||
link(find_set(x), find_set(y));
|
||||
}
|
||||
|
||||
// Find-Set - returns the Element representative of the set
|
||||
// containing Element x and applies path compression.
|
||||
template <class Element>
|
||||
inline Element find_set(Element x)
|
||||
{
|
||||
return rep(parent, x);
|
||||
}
|
||||
|
||||
template <class ElementIterator>
|
||||
inline std::size_t count_sets(ElementIterator first, ElementIterator last)
|
||||
{
|
||||
std::size_t count = 0;
|
||||
for ( ; first != last; ++first)
|
||||
if (get(parent, *first) == *first)
|
||||
++count;
|
||||
return count;
|
||||
}
|
||||
|
||||
template <class ElementIterator>
|
||||
inline void normalize_sets(ElementIterator first, ElementIterator last)
|
||||
{
|
||||
for (; first != last; ++first)
|
||||
detail::normalize_node(parent, *first);
|
||||
}
|
||||
|
||||
template <class ElementIterator>
|
||||
inline void compress_sets(ElementIterator first, ElementIterator last)
|
||||
{
|
||||
for (; first != last; ++first)
|
||||
detail::find_representative_with_full_compression(parent, *first);
|
||||
}
|
||||
protected:
|
||||
RankPA rank;
|
||||
ParentPA parent;
|
||||
FindCompress rep;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
template <class ID = identity_property_map,
|
||||
class InverseID = identity_property_map,
|
||||
class FindCompress = find_with_full_path_compression
|
||||
>
|
||||
class disjoint_sets_with_storage
|
||||
{
|
||||
typedef typename property_traits<ID>::value_type Index;
|
||||
typedef std::vector<Index> ParentContainer;
|
||||
typedef std::vector<unsigned char> RankContainer;
|
||||
public:
|
||||
typedef typename ParentContainer::size_type size_type;
|
||||
|
||||
disjoint_sets_with_storage(size_type n = 0,
|
||||
ID id_ = ID(),
|
||||
InverseID inv = InverseID())
|
||||
: id(id_), id_to_vertex(inv), rank(n, 0), parent(n)
|
||||
{
|
||||
for (Index i = 0; i < n; ++i)
|
||||
parent[i] = i;
|
||||
}
|
||||
// note this is not normally needed
|
||||
template <class Element>
|
||||
inline void
|
||||
make_set(Element x) {
|
||||
parent[x] = x;
|
||||
rank[x] = 0;
|
||||
}
|
||||
template <class Element>
|
||||
inline void
|
||||
link(Element x, Element y)
|
||||
{
|
||||
extend_sets(x,y);
|
||||
detail::link_sets(&parent[0], &rank[0],
|
||||
get(id,x), get(id,y), rep);
|
||||
}
|
||||
template <class Element>
|
||||
inline void
|
||||
union_set(Element x, Element y) {
|
||||
Element rx = find_set(x);
|
||||
Element ry = find_set(y);
|
||||
link(rx, ry);
|
||||
}
|
||||
template <class Element>
|
||||
inline Element find_set(Element x) {
|
||||
return id_to_vertex[rep(&parent[0], get(id,x))];
|
||||
}
|
||||
|
||||
template <class ElementIterator>
|
||||
inline std::size_t count_sets(ElementIterator first, ElementIterator last)
|
||||
{
|
||||
std::size_t count = 0;
|
||||
for ( ; first != last; ++first)
|
||||
if (parent[*first] == *first)
|
||||
++count;
|
||||
return count;
|
||||
}
|
||||
|
||||
template <class ElementIterator>
|
||||
inline void normalize_sets(ElementIterator first, ElementIterator last)
|
||||
{
|
||||
for (; first != last; ++first)
|
||||
detail::normalize_node(&parent[0], *first);
|
||||
}
|
||||
|
||||
template <class ElementIterator>
|
||||
inline void compress_sets(ElementIterator first, ElementIterator last)
|
||||
{
|
||||
for (; first != last; ++first)
|
||||
detail::find_representative_with_full_compression(&parent[0],
|
||||
*first);
|
||||
}
|
||||
|
||||
const ParentContainer& parents() { return parent; }
|
||||
|
||||
protected:
|
||||
|
||||
template <class Element>
|
||||
inline void
|
||||
extend_sets(Element x, Element y)
|
||||
{
|
||||
Index needed = get(id,x) > get(id,y) ? get(id,x) + 1 : get(id,y) + 1;
|
||||
if (needed > parent.size()) {
|
||||
rank.insert(rank.end(), needed - rank.size(), 0);
|
||||
for (Index k = parent.size(); k < needed; ++k)
|
||||
parent.push_back(k);
|
||||
}
|
||||
}
|
||||
|
||||
ID id;
|
||||
InverseID id_to_vertex;
|
||||
RankContainer rank;
|
||||
ParentContainer parent;
|
||||
FindCompress rep;
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_DISJOINT_SETS_HPP
|
||||
@@ -1,8 +0,0 @@
|
||||
# Copyright Jeremy Siek 2002
|
||||
# Use, modification and distribution are subject to the
|
||||
# Boost Software License, Version 1.0. (See accompanying file
|
||||
# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
test-suite disjoint_sets :
|
||||
[ run disjoint_set_test.cpp ]
|
||||
;
|
||||
@@ -1,76 +0,0 @@
|
||||
// (C) Copyright Jeremy Siek 2002.
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/pending/disjoint_sets.hpp>
|
||||
#include <boost/test/minimal.hpp>
|
||||
#include <boost/cstdlib.hpp>
|
||||
|
||||
template <typename DisjointSet>
|
||||
struct test_disjoint_set {
|
||||
static void do_test()
|
||||
{
|
||||
// The following tests are pretty lame, just a basic sanity check.
|
||||
// Industrial strength tests still need to be written.
|
||||
|
||||
#if !defined(__MWERKS__) || __MWERKS__ > 0x3003
|
||||
std::size_t elts[]
|
||||
#else
|
||||
std::size_t elts[4]
|
||||
#endif
|
||||
= { 0, 1, 2, 3 };
|
||||
|
||||
const int N = sizeof(elts)/sizeof(*elts);
|
||||
|
||||
DisjointSet ds(N);
|
||||
|
||||
ds.make_set(elts[0]);
|
||||
ds.make_set(elts[1]);
|
||||
ds.make_set(elts[2]);
|
||||
ds.make_set(elts[3]);
|
||||
|
||||
BOOST_CHECK(ds.find_set(0) != ds.find_set(1));
|
||||
BOOST_CHECK(ds.find_set(0) != ds.find_set(2));
|
||||
BOOST_CHECK(ds.find_set(0) != ds.find_set(3));
|
||||
BOOST_CHECK(ds.find_set(1) != ds.find_set(2));
|
||||
BOOST_CHECK(ds.find_set(1) != ds.find_set(3));
|
||||
BOOST_CHECK(ds.find_set(2) != ds.find_set(3));
|
||||
|
||||
|
||||
ds.union_set(0, 1);
|
||||
ds.union_set(2, 3);
|
||||
BOOST_CHECK(ds.find_set(0) != ds.find_set(3));
|
||||
int a = ds.find_set(0);
|
||||
BOOST_CHECK(a == ds.find_set(1));
|
||||
int b = ds.find_set(2);
|
||||
BOOST_CHECK(b == ds.find_set(3));
|
||||
|
||||
ds.link(a, b);
|
||||
BOOST_CHECK(ds.find_set(a) == ds.find_set(b));
|
||||
BOOST_CHECK(1 == ds.count_sets(elts, elts + N));
|
||||
|
||||
ds.normalize_sets(elts, elts + N);
|
||||
ds.compress_sets(elts, elts + N);
|
||||
BOOST_CHECK(1 == ds.count_sets(elts, elts + N));
|
||||
}
|
||||
};
|
||||
|
||||
int
|
||||
test_main(int, char*[])
|
||||
{
|
||||
using namespace boost;
|
||||
{
|
||||
typedef
|
||||
disjoint_sets_with_storage<identity_property_map, identity_property_map,
|
||||
find_with_path_halving> ds_type;
|
||||
test_disjoint_set<ds_type>::do_test();
|
||||
}
|
||||
{
|
||||
typedef
|
||||
disjoint_sets_with_storage<identity_property_map, identity_property_map,
|
||||
find_with_full_path_compression> ds_type;
|
||||
test_disjoint_set<ds_type>::do_test();
|
||||
}
|
||||
return boost::exit_success;
|
||||
}
|
||||
Reference in New Issue
Block a user