This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Lazy union | |
class QuickUnion | |
{ | |
int* arr; | |
public: | |
QuickUnion(int num) | |
{ | |
size = num; | |
arr = new int[num]; | |
//N array access | |
for (int i = 0; i < num; i++) | |
arr[i] = i; | |
} | |
int root(int i) | |
{ | |
while (arr[i] != i) //depth of i array accesses | |
i = arr[i]; | |
return i; | |
} | |
bool Find(int p, int q) | |
{ | |
if (root(p) == root(q))//depth of p and q array accesses | |
return true; | |
return false; | |
} | |
void Union(int p, int q) //change the root of p to point to root q | |
{ | |
int i = root(p); | |
int j = root(q); | |
arr[i] = j; | |
} | |
//2N+2 array access | |
int size; | |
}; |
No comments:
Post a Comment