Weighted Union Code
ID union(ID x, ID y) {
assert(up[x] == -1);
assert(up[y] == -1);
if (weight[x] > weight[y]) {
up[y] = x;
weight[x] += weight[y];
}
else {
up[x] = y;
weight[y] += weight[x];
}
}
typedef ID int;
new runtime of union:
new runtime of find:
Previous slide
Next slide
Back to first slide
View graphic version