-
Notifications
You must be signed in to change notification settings - Fork 0
/
unionfind.c
81 lines (65 loc) · 1.55 KB
/
unionfind.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "unionfind.h"
#include <stdlib.h>
void ForestInit(ForestP f, uint32_t size){
f->parent = malloc(sizeof(NodeId)*size);
f->treeSize = malloc(sizeof(uint32_t)*size);
f->posInRoots = malloc(sizeof(uint32_t)*size);
f->roots = malloc(sizeof(NodeId)*size);
NodeId i;
for(i = 0; i < size; ++i){
f->parent[i] = i;
f->treeSize[i] = 1;
f->posInRoots[i] = i;
f->roots[i] = i;
}
f->numRoots = size;
f->size = size;
}
ForestP ForestNew(uint32_t size){
ForestP f = malloc(sizeof(struct Forest));
ForestInit(f, size);
return f;
}
NodeId find(ForestP f, NodeId id){
if(isRoot(f,id)){
return id;
}else{
NodeId root = find(f, f->parent[id]);
f->parent[id] = root;
return root;
}
}
void ForestUninit(ForestP f){
free(f->parent);
free(f->treeSize);
free(f->posInRoots);
free(f->roots);
}
void ForestDelete(ForestP f){
ForestUninit(f);
free(f);
}
void merge(ForestP f, NodeId x, NodeId y){
NodeId rootA = find(f, x);
NodeId rootB = find(f, y);
//Swap so that rootA is larger
if(f->treeSize[rootA] < f->treeSize[rootB]){
NodeId tmp = rootA;
rootA = rootB;
rootB = tmp;
}
//Remove rootB from the roots list
NodeId atBack = f->roots[f->numRoots - 1];
uint32_t posB = f->posInRoots[rootB];
f->roots[posB] = atBack;
--(f->numRoots);
//Merge rootB into rootA
f->parent[rootB] = rootA;
f->treeSize[rootA] += f->treeSize[rootB];
}
bool isRoot(ForestP f, NodeId id){
return f->parent[id] == id;
}
bool sameForest(ForestP f, NodeId x, NodeId y){
return find(f, x) == find(f, y);
}