Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
a7fc777a
Commit
a7fc777a
authored
Oct 08, 2011
by
Davis King
Browse files
Added an implementation of the union-find algorithm.
parent
18083851
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
361 additions
and
1 deletion
+361
-1
dlib/disjoint_subsets.h
dlib/disjoint_subsets.h
+11
-0
dlib/disjoint_subsets/disjoint_subsets.h
dlib/disjoint_subsets/disjoint_subsets.h
+142
-0
dlib/disjoint_subsets/disjoint_subsets_abstract.h
dlib/disjoint_subsets/disjoint_subsets_abstract.h
+99
-0
dlib/test/CMakeLists.txt
dlib/test/CMakeLists.txt
+1
-0
dlib/test/disjoint_subsets.cpp
dlib/test/disjoint_subsets.cpp
+106
-0
dlib/test/makefile
dlib/test/makefile
+2
-1
No files found.
dlib/disjoint_subsets.h
0 → 100644
View file @
a7fc777a
// Copyright (C) 2011 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_DISJOINt_SUBSETS_
#define DLIB_DISJOINt_SUBSETS_
#include "disjoint_subsets/disjoint_subsets.h"
#endif // DLIB_DISJOINt_SUBSETS_
dlib/disjoint_subsets/disjoint_subsets.h
0 → 100644
View file @
a7fc777a
// Copyright (C) 2011 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_DISJOINT_SUBsETS_H__
#define DLIB_DISJOINT_SUBsETS_H__
#include "disjoint_subsets_abstract.h"
#include <vector>
#include "../algs.h"
namespace
dlib
{
// ----------------------------------------------------------------------------------------
class
disjoint_subsets
{
public:
void
clear
(
)
{
items
.
clear
();
}
void
set_size
(
unsigned
long
new_size
)
{
items
.
resize
(
new_size
);
for
(
unsigned
long
i
=
0
;
i
<
items
.
size
();
++
i
)
{
items
[
i
].
parent
=
i
;
items
[
i
].
rank
=
0
;
}
}
unsigned
long
size
(
)
const
{
return
items
.
size
();
}
unsigned
long
find_set
(
unsigned
long
item
)
const
{
// make sure requires clause is not broken
DLIB_ASSERT
(
item
<
size
(),
"
\t
unsigned long disjoint_subsets::find_set()"
<<
"
\n\t
item must be less than size()"
<<
"
\n\t
item: "
<<
item
<<
"
\n\t
size(): "
<<
size
()
<<
"
\n\t
this: "
<<
this
);
if
(
items
[
item
].
parent
==
item
)
{
return
item
;
}
else
{
// find root of item
unsigned
long
x
=
item
;
do
{
x
=
items
[
x
].
parent
;
}
while
(
items
[
x
].
parent
!=
x
);
// do path compression
const
unsigned
long
root
=
x
;
x
=
item
;
while
(
items
[
x
].
parent
!=
x
)
{
const
unsigned
long
prev
=
x
;
x
=
items
[
x
].
parent
;
items
[
prev
].
parent
=
root
;
}
return
root
;
}
}
unsigned
long
merge_sets
(
unsigned
long
a
,
unsigned
long
b
)
{
// make sure requires clause is not broken
DLIB_ASSERT
(
a
!=
b
&&
a
<
size
()
&&
b
<
size
()
&&
find_set
(
a
)
==
a
&&
find_set
(
b
)
==
b
,
"
\t
unsigned long disjoint_subsets::merge_sets(a,b)"
<<
"
\n\t
invalid arguments were given to this function"
<<
"
\n\t
a: "
<<
a
<<
"
\n\t
b: "
<<
b
<<
"
\n\t
size(): "
<<
size
()
<<
"
\n\t
find_set(a): "
<<
find_set
(
a
)
<<
"
\n\t
find_set(b): "
<<
find_set
(
b
)
<<
"
\n\t
this: "
<<
this
);
if
(
items
[
a
].
rank
>
items
[
b
].
rank
)
{
items
[
b
].
parent
=
a
;
return
a
;
}
else
{
items
[
a
].
parent
=
b
;
if
(
items
[
a
].
rank
==
items
[
b
].
rank
)
{
items
[
b
].
rank
=
items
[
b
].
rank
+
1
;
}
return
b
;
}
}
private:
/*
See the book Introduction to Algorithms by Cormen, Leiserson, Rivest and Stein
for a discussion of how this algorithm works.
*/
struct
data
{
unsigned
long
rank
;
unsigned
long
parent
;
};
mutable
std
::
vector
<
data
>
items
;
};
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_DISJOINT_SUBsETS_H__
dlib/disjoint_subsets/disjoint_subsets_abstract.h
0 → 100644
View file @
a7fc777a
// Copyright (C) 2011 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_DISJOINT_SUBsETS_ABSTRACT_H__
#ifdef DLIB_DISJOINT_SUBsETS_ABSTRACT_H__
#include <vector>
#include "../algs.h"
namespace
dlib
{
// ----------------------------------------------------------------------------------------
class
disjoint_subsets
{
/*!
INITIAL VALUE
- size() == 0
WHAT THIS OBJECT REPRESENTS
This object represents a set of integers which is partitioned into
a number of disjoint subsets. It supports the two fundamental operations
of finding which subset a particular integer belongs to as well as
merging subsets.
!*/
public:
void
clear
(
);
/*!
ensures
- #size() == 0
- returns this object to its initial value
!*/
void
set_size
(
unsigned
long
new_size
);
/*!
ensures
- #size() == new_size
- for all valid i:
- #find_set(i) == i
(i.e. this object contains new_size subsets, each containing exactly one element)
!*/
unsigned
long
size
(
)
const
;
/*!
ensures
- returns the total number of integer elements represented
by this object.
!*/
unsigned
long
find_set
(
unsigned
long
item
)
const
;
/*!
requires
- item < size()
ensures
- Each disjoint subset can be represented by any of its elements (since
the sets are all disjoint). In particular, for each subset we define
a special "representative element" which is used to represent it.
Therefore, this function returns the representative element for the
set which contains item.
- find_set(find_set(item)) == find_set(item)
- Note that if A and B are both elements of the same subset then we always
have find_set(A) == find_set(B).
!*/
unsigned
long
merge_sets
(
unsigned
long
a
,
unsigned
long
b
)
/*!
requires
- a != b
- a < size()
- b < size()
- find_set(a) == a
(i.e. a is the representative element of some set)
- find_set(b) == b
(i.e. b is the representative element of some set)
ensures
- #find_set(a) == #find_set(b)
(i.e. merges the set's containing a and b)
- returns #find_set(a)
!*/
};
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_DISJOINT_SUBsETS_ABSTRACT_H__
dlib/test/CMakeLists.txt
View file @
a7fc777a
...
...
@@ -34,6 +34,7 @@ set (tests
data_io.cpp
directed_graph.cpp
discriminant_pca.cpp
disjoint_subsets.cpp
ekm_and_lisf.cpp
empirical_kernel_map.cpp
entropy_coder.cpp
...
...
dlib/test/disjoint_subsets.cpp
0 → 100644
View file @
a7fc777a
// Copyright (C) 2011 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <dlib/disjoint_subsets.h>
#include "tester.h"
namespace
{
using
namespace
test
;
using
namespace
dlib
;
using
namespace
std
;
logger
dlog
(
"test.disjoint_subsets"
);
void
test_disjoint_subset
()
{
print_spinner
();
disjoint_subsets
s
;
DLIB_TEST
(
s
.
size
()
==
0
);
s
.
set_size
(
5
);
DLIB_TEST
(
s
.
size
()
==
5
);
DLIB_TEST
(
s
.
find_set
(
0
)
==
0
);
DLIB_TEST
(
s
.
find_set
(
1
)
==
1
);
DLIB_TEST
(
s
.
find_set
(
2
)
==
2
);
DLIB_TEST
(
s
.
find_set
(
3
)
==
3
);
DLIB_TEST
(
s
.
find_set
(
4
)
==
4
);
unsigned
long
id
=
s
.
merge_sets
(
1
,
3
);
DLIB_TEST
(
s
.
find_set
(
0
)
==
0
);
DLIB_TEST
(
s
.
find_set
(
1
)
==
id
);
DLIB_TEST
(
s
.
find_set
(
2
)
==
2
);
DLIB_TEST
(
s
.
find_set
(
3
)
==
id
);
DLIB_TEST
(
s
.
find_set
(
4
)
==
4
);
id
=
s
.
merge_sets
(
s
.
find_set
(
1
),
4
);
DLIB_TEST
(
s
.
find_set
(
0
)
==
0
);
DLIB_TEST
(
s
.
find_set
(
1
)
==
id
);
DLIB_TEST
(
s
.
find_set
(
2
)
==
2
);
DLIB_TEST
(
s
.
find_set
(
3
)
==
id
);
DLIB_TEST
(
s
.
find_set
(
4
)
==
id
);
unsigned
long
id2
=
s
.
merge_sets
(
0
,
2
);
DLIB_TEST
(
s
.
find_set
(
0
)
==
id2
);
DLIB_TEST
(
s
.
find_set
(
1
)
==
id
);
DLIB_TEST
(
s
.
find_set
(
2
)
==
id2
);
DLIB_TEST
(
s
.
find_set
(
3
)
==
id
);
DLIB_TEST
(
s
.
find_set
(
4
)
==
id
);
id
=
s
.
merge_sets
(
s
.
find_set
(
1
),
s
.
find_set
(
0
));
DLIB_TEST
(
s
.
find_set
(
0
)
==
id
);
DLIB_TEST
(
s
.
find_set
(
1
)
==
id
);
DLIB_TEST
(
s
.
find_set
(
2
)
==
id
);
DLIB_TEST
(
s
.
find_set
(
3
)
==
id
);
DLIB_TEST
(
s
.
find_set
(
4
)
==
id
);
DLIB_TEST
(
s
.
size
()
==
5
);
s
.
set_size
(
1
);
DLIB_TEST
(
s
.
size
()
==
1
);
DLIB_TEST
(
s
.
find_set
(
0
)
==
0
);
s
.
set_size
(
2
);
DLIB_TEST
(
s
.
size
()
==
2
);
DLIB_TEST
(
s
.
find_set
(
0
)
==
0
);
DLIB_TEST
(
s
.
find_set
(
1
)
==
1
);
id
=
s
.
merge_sets
(
0
,
1
);
DLIB_TEST
(
s
.
size
()
==
2
);
DLIB_TEST
(
id
==
s
.
find_set
(
0
));
DLIB_TEST
(
id
==
s
.
find_set
(
1
));
DLIB_TEST
(
s
.
size
()
==
2
);
s
.
clear
();
DLIB_TEST
(
s
.
size
()
==
0
);
}
class
tester_disjoint_subsets
:
public
tester
{
public:
tester_disjoint_subsets
(
)
:
tester
(
"test_disjoint_subsets"
,
"Runs tests on the disjoint_subsets component."
)
{}
void
perform_test
(
)
{
test_disjoint_subset
();
}
}
a
;
}
dlib/test/makefile
View file @
a7fc777a
...
...
@@ -49,13 +49,14 @@ SRC += create_iris_datafile.cpp
SRC
+=
data_io.cpp
SRC
+=
directed_graph.cpp
SRC
+=
discriminant_pca.cpp
SRC
+=
disjoint_subsets.cpp
SRC
+=
ekm_and_lisf.cpp
SRC
+=
empirical_kernel_map.cpp
SRC
+=
entropy_coder.cpp
SRC
+=
entropy_encoder_model.cpp
SRC
+=
geometry.cpp
SRC
+=
find_max_factor_graph_nmplp.cpp
SRC
+=
find_max_factor_graph_viterbi.cpp
SRC
+=
geometry.cpp
SRC
+=
graph.cpp
SRC
+=
hash.cpp
SRC
+=
hash_map.cpp
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment