Union-Find
Social network connectivity. Given a social network containing n members and a log file containing m timestamps at which times pairs of members formed friendships, design an algorithm to determine the earliest time at which all members are connected (i.e., every member is a friend of a friend of a friend … of a friend). Assume that the log file is sorted by timestamp and that friendship is an equivalence relation. The running time of your algorithm should be mlogn or better and use extra space proportional to n.
import java.util.Scanner;
public class UF {
int count;
int[] id;
int[] height;
public UF(int n) {
count = n;
id = new int[n];
height = new int[n];
for (int i = 0; i < n; i++) {
id[i] = i;
height[i] = 0;
}
}
public int count() {
return count;
}
public int find(int p) {
int root = p;
while (root != id[root]) {
root = id[root];
}
while (p != root) {
int temp = id[p];
id[p] = root;
p = temp;
}
return root;
}
public boolean connected(int p, int q) {
return find(p) == find(q);
}
public void union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) {
return;
}
if (height[p] < height[q]) {
id[rootP] = rootQ;
} else {
id[rootQ] = rootP;
if (height[rootP] == height[rootQ]) {
height[rootP]++;
}
}
count--;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
UF uf = new UF(n);
for (int i = 0; i < m; i++) {
String timeStamp = sc.next();
int p = sc.nextInt();
int q = sc.nextInt();
if (uf.connected(p, q)) {
continue;
}
uf.union(p, q);
if (uf.count == 1) {
System.out.println(timeStamp);
break;
}
}
}
}
Union-find with specific canonical element. Add a method ????() to the union-find data type so that ????(?) returns the largest element in the connected component containing i. The operations, ?????(), ?????????(), and ????() should all take logarithmic time or better.
For example, if one of the connected components is {1, 2, 6, 9}, then the ????() method should return 9 for each of the four elements in the connected components.
public class UF {
int count;
int[] id;
int[] height;
int[] max;
public UF(int n) {
count = n;
id = new int[n];
height = new int[n];
max = new int[n]
for (int i = 0; i < n; i++) {
id[i] = i;
height[i] = 0;
max[i] = i;
}
}
public int count() {
return count;
}
public int find(int i) {
return max[findRoot(i)];
}
public int findRoot(int p) {
int root = p;
while (root != id[root]) {
root = id[root];
}
while (p != root) {
int temp = id[p];
id[p] = root;
p = temp;
}
return root;
}
public boolean connected(int p, int q) {
return findRoot(p) == findRoot(q);
}
public void union(int p, int q) {
int rootP = findRoot(p);
int rootQ = findRoot(q);
if (rootP == rootQ) {
return;
}
if (height[p] < height[q]) {
id[rootP] = rootQ;
max[rootQ] = max[rootP] > max[rootQ] ? max[rootP] : max[rootQ];
} else {
id[rootQ] = rootP;
if (height[rootP] == height[rootQ]) {
height[rootP]++;
}
max[rootP] = max[rootQ] > max[rootP] ? max[rootQ] : max[rootP];
}
count--;
}
}
Successor with delete. Given a set of nn integers S={0, 1, …, n−1} and a sequence of requests of the following form:
- Remove x from S
- Find the successor of x: the smallest y in S such that y≥x.
design a data type so that all operations (except construction) take logarithmic time or better in the worst case.
public class UF {
int count;
int[] id;
int[] height;
int[] max;
boolean[] isRemoved;
public UF(int n) {
count = n;
id = new int[n];
height = new int[n];
max = new int[n];
isRemoved = new boolean[n];
for (int i = 0; i < n; i++) {
id[i] = i;
height[i] = 0;
max[i] = i;
isRemoved[i] = false;
}
}
public int count() {
return count;
}
public int findRoot(int p) {
int root = p;
while (root != id[root]) {
root = id[root];
}
while (p != root) {
int temp = id[p];
id[p] = root;
p = temp;
}
return root;
}
public boolean connected(int p, int q) {
return findRoot(p) == findRoot(q);
}
public void union(int p, int q) {
int rootP = findRoot(p);
int rootQ = findRoot(q);
if (rootP == rootQ) {
return;
}
if (height[p] < height[q]) {
id[rootP] = rootQ;
max[rootQ] = max[rootP] > max[rootQ] ? max[rootP] : max[rootQ];
} else {
id[rootQ] = rootP;
if (height[rootP] == height[rootQ]) {
height[rootP]++;
}
max[rootP] = max[rootQ] > max[rootP] ? max[rootQ] : max[rootP];
}
}
public void remove(int x) {
if (isRemoved[x] == true) {
return;
}
isRemoved[x] = true;
if (x < count - 1 && isRemoved[x + 1]) {
union(x, x + 1);
}
if (x > 0 && isRemoved[x - 1]) {
union(x, x - 1);
}
}
public int getSuccessor(int x) {
if (isRemoved[x] == false) {
return x;
}
if (max[findRoot(x)] + 1 >= count) {
return -1;
}
return max[findRoot(x)] + 1;
}
}