/** * Unions sets, which contain vertices "a" and "b" Union is always performed as merging "B" into * "A" * * @param a number of a node "a" * @param b number of a node "b" * @return number of a representative of the merged component */ public int union(int a, int b) { DisjointSet.Node repA = nodes[find(a)]; DisjointSet.Node repB = nodes[find(b)]; repB.parent = repA; return repA.id; }
/** * Performs compression of the path * * @param a * @param rootId */ private void repair(int a, int rootId) { DisjointSet.Node curr = nodes[a]; while (curr.id != rootId) { DisjointSet.Node tmp = curr.parent; curr.parent = nodes[rootId]; curr = tmp; } }