cgv
Loading...
Searching...
No Matches
union_find.h
1#pragma once
2
3#include <vector>
4
5namespace cgv {
6 namespace data {
7
8struct union_find : public std::vector<unsigned int>
9{
11 union_find(unsigned int n) { init(n); }
13 void init(unsigned int n) {
14 resize(n);
15 for (unsigned int i=0; i<n; ++i)
16 at(i) = i;
17 }
19 unsigned int find(unsigned int i)
20 {
21 // find representative j
22 unsigned int j = i;
23 while (at(j) != j)
24 j = at(j);
25 // compress path
26 while (i != j) {
27 unsigned int tmp = at(i);
28 at(i) = j;
29 i = tmp;
30 }
31 return j;
32 }
34 void unify(unsigned int i, unsigned int j)
35 {
36 at(find(i)) = j;
37 }
38};
39
40 }
41}
the cgv namespace
Definition print.h:11
void unify(unsigned int i, unsigned int j)
union of two groups
Definition union_find.h:34
unsigned int find(unsigned int i)
find representative with path compression
Definition union_find.h:19
union_find(unsigned int n)
construct with given number of elements
Definition union_find.h:11
void init(unsigned int n)
init such that each element is a representative
Definition union_find.h:13