-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisjoint.h
More file actions
90 lines (69 loc) · 1.89 KB
/
disjoint.h
File metadata and controls
90 lines (69 loc) · 1.89 KB
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
82
83
84
85
86
87
88
89
90
#ifndef DISJOINT_H
#define DISJOINT_H
#include<string>
#include<vector>
#include<unordered_map>
namespace collections
{
template <class T>
class Disjoint_Set
{
/*
struct Item
{
//size_t ID{0};
//size_t Size{1};
size_t Parent{0};
};
*/
std::vector<size_t> parents; //Vector to store the index of the parent.
std::unordered_map<T,size_t> r_it; //map to hadle the relation of Item vs Index Position into the vector parents;
size_t find_rec(size_t pos);
public:
void MakeUnionFind(std::vector<T> s); //An order from 0 to N
long Find(T);
long Union(long,long);
};
template <class T>
void Disjoint_Set<T>::MakeUnionFind(std::vector<T> s)
{
if(s.size() == 0) return;
parents.resize(s.size());
for(size_t inx = 0; inx < s.size(); inx++) //T(n)
{
parents[inx] = inx; //At the begin all the items has its parent as itself
T key = s[inx];
r_it.insert(std::make_pair(key,inx)); //This map link the Item to its index into the parents vector.
//r_it[s[inx]] = inx;
}
}
template <typename T>
size_t Disjoint_Set<T>::find_rec(size_t index)
{
if( index == parents[index])
return index;
//Compress the path;
size_t parent = find_rec(parents[index]);
parents[index] = parent;
return parent;
}
template <typename T>
long Disjoint_Set<T>::Find(T u)
{
//std::unordered_map<T,size_t>::iterator it;
auto it = r_it.find(u);
if(it == r_it.end()) return -1;
size_t post_itm = r_it[u];
return static_cast<long>(find_rec(post_itm)); //O(log(n))
}
template <typename T>
long Disjoint_Set<T>::Union(long u, long v)
{
long s = parents.size();
if((u < 0 && u >= s) || (v < 0 && v >= s)) return -1; //Index out of range
else if(u == v) return -2; //The items are already linked
parents[u] = static_cast<size_t>(v);
return v;
}
} //collections
#endif // DISJOINT_H