-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disjoint Set Data Structure Implementation
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
class UnionFind { | ||
public: | ||
UnionFind(int sz) : root(sz), rank(sz) { | ||
for (int i = 0; i < sz; i++) { | ||
root[i] = i; | ||
rank[i] = 1; | ||
} | ||
} | ||
|
||
int find(int x) { | ||
if (x == root[x]) { | ||
return x; | ||
} | ||
return root[x] = find(root[x]); | ||
} | ||
|
||
void unionSet(int x, int y) { | ||
int rootX = find(x); | ||
int rootY = find(y); | ||
if (rootX != rootY) { | ||
if (rank[rootX] > rank[rootY]) { | ||
root[rootY] = rootX; | ||
} else if (rank[rootX] < rank[rootY]) { | ||
root[rootX] = rootY; | ||
} else { | ||
root[rootY] = rootX; | ||
rank[rootX] += 1; | ||
} | ||
} | ||
} | ||
|
||
bool connected(int x, int y) { | ||
return find(x) == find(y); | ||
} | ||
|
||
private: | ||
vector<int> root; | ||
vector<int> rank; | ||
}; | ||
|
||
// Test Case | ||
int main() { | ||
// for displaying booleans as literal strings, instead of 0 and 1 | ||
cout << boolalpha; | ||
UnionFind uf(10); | ||
// 1-2-5-6-7 3-8-9 4 | ||
uf.unionSet(1, 2); | ||
uf.unionSet(2, 5); | ||
uf.unionSet(5, 6); | ||
uf.unionSet(6, 7); | ||
uf.unionSet(3, 8); | ||
uf.unionSet(8, 9); | ||
cout << uf.connected(1, 5) << endl; // true | ||
cout << uf.connected(5, 7) << endl; // true | ||
cout << uf.connected(4, 9) << endl; // false | ||
// 1-2-5-6-7 3-8-9-4 | ||
uf.unionSet(9, 4); | ||
cout << uf.connected(4, 9) << endl; // true | ||
|
||
return 0; | ||
} |