Limbo
3.5.4
Toggle main menu visibility
Loading...
Searching...
No Matches
limbo
containers
DisjointSet.h
Go to the documentation of this file.
1
12
13
#ifndef LIMBO_CONTAINERS_DISJOINTSET_H
14
#define LIMBO_CONTAINERS_DISJOINTSET_H
15
16
#include <vector>
17
#include <algorithm>
18
20
namespace
limbo
21
{
23
namespace
containers
24
{
25
28
struct
DisjointSet
29
{
35
template
<
typename
SubsetHelperType>
36
static
typename
SubsetHelperType::element_type
const
&
find_set
(SubsetHelperType
const
& gp,
typename
SubsetHelperType::element_type
const
& e)
37
{
38
if
(gp.get_parent(e) == e)
// if e is its own parent, it reaches to the top set
39
return
e;
40
return
find_set
(gp, gp.get_parent(e));
41
}
42
48
template
<
typename
SubsetHelperType>
49
static
void
union_set
(SubsetHelperType& gp,
typename
SubsetHelperType::element_type
const
& e1,
typename
SubsetHelperType::element_type
const
& e2)
50
{
51
typename
SubsetHelperType::element_type
const
& root1 =
find_set
(gp, e1);
52
typename
SubsetHelperType::element_type
const
& root2 =
find_set
(gp, e2);
53
// set parent
54
if
(gp.get_rank(root1) < gp.get_rank(root2))
55
gp.set_parent(root1, root2);
56
else
if
(gp.get_rank(root1) > gp.get_rank(root2))
57
gp.set_parent(root2, root1);
58
else
59
{
60
gp.set_parent(root2, root1);
61
gp.set_rank(root1, gp.get_rank(root1)+1);
62
}
63
}
64
68
template
<
typename
SubsetHelperType>
69
static
std::size_t
count_sets
(SubsetHelperType
const
& gp)
70
{
71
std::size_t count = 0;
72
for
(
typename
SubsetHelperType::element_type i = 0, ie = gp.size(); i != ie; ++i)
73
if
(gp.get_parent(i) == i)
// top subset
74
++count;
75
return
count;
76
}
77
82
template
<
typename
ElementType,
typename
RankType>
83
struct
SubsetHelper
84
{
86
typedef
ElementType element_type;
87
typedef
RankType rank_type;
89
90
std::vector<element_type>&
vParent
;
91
std::vector<rank_type>&
vRank
;
92
96
SubsetHelper
(std::vector<element_type>& vp, std::vector<rank_type>& vr)
97
:
vParent
(vp)
98
,
vRank
(vr)
99
{
100
// set initial parent to every vertex itself
101
for
(
unsigned
int
i = 0, ie =
vParent
.size(); i != ie; ++i)
102
vParent
[i] = i;
103
std::fill(
vRank
.begin(),
vRank
.end(), 0);
104
}
105
108
inline
element_type
const
&
get_parent
(element_type
const
& e)
const
{
return
vParent
[e];}
112
inline
void
set_parent
(element_type
const
& e, element_type
const
& p) {
vParent
[e] = p;}
116
inline
rank_type
const
&
get_rank
(element_type
const
& e)
const
{
return
vRank
[e];}
120
inline
void
set_rank
(element_type
const
& e, rank_type
const
& r) {
vRank
[e] = r;}
122
inline
std::size_t
size
()
const
{
return
vParent
.size();}
123
};
124
};
125
126
}}
// namespace limbo // namespace containers
127
128
#endif
limbo::containers
namespace for Limbo.Containers
Definition
DisjointSet.h:24
limbo
namespace for Limbo
Definition
GraphUtility.h:23
limbo::containers::DisjointSet::SubsetHelper::get_parent
element_type const & get_parent(element_type const &e) const
get parent element
Definition
DisjointSet.h:108
limbo::containers::DisjointSet::SubsetHelper::size
std::size_t size() const
Definition
DisjointSet.h:122
limbo::containers::DisjointSet::SubsetHelper::set_parent
void set_parent(element_type const &e, element_type const &p)
set parent element
Definition
DisjointSet.h:112
limbo::containers::DisjointSet::SubsetHelper::vRank
std::vector< rank_type > & vRank
reference to rank array
Definition
DisjointSet.h:91
limbo::containers::DisjointSet::SubsetHelper::vParent
std::vector< element_type > & vParent
reference to parent array
Definition
DisjointSet.h:90
limbo::containers::DisjointSet::SubsetHelper::set_rank
void set_rank(element_type const &e, rank_type const &r)
set rank
Definition
DisjointSet.h:120
limbo::containers::DisjointSet::SubsetHelper::SubsetHelper
SubsetHelper(std::vector< element_type > &vp, std::vector< rank_type > &vr)
constructor
Definition
DisjointSet.h:96
limbo::containers::DisjointSet::SubsetHelper::get_rank
rank_type const & get_rank(element_type const &e) const
get rank
Definition
DisjointSet.h:116
limbo::containers::DisjointSet
simply used for scope control
Definition
DisjointSet.h:29
limbo::containers::DisjointSet::find_set
static SubsetHelperType::element_type const & find_set(SubsetHelperType const &gp, typename SubsetHelperType::element_type const &e)
find the subset of an element e
Definition
DisjointSet.h:36
limbo::containers::DisjointSet::union_set
static void union_set(SubsetHelperType &gp, typename SubsetHelperType::element_type const &e1, typename SubsetHelperType::element_type const &e2)
union two subsets represented by element e1 and e2
Definition
DisjointSet.h:49
limbo::containers::DisjointSet::count_sets
static std::size_t count_sets(SubsetHelperType const &gp)
Definition
DisjointSet.h:69
Generated on
for Limbo by
1.17.0