summaryrefslogtreecommitdiff
path: root/java/qfind/QuickFindUF.java
blob: 4018515d7193936ee3ef1a58aa1f09a0a2ebfaaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * Quick Find algorithm
 *
 * Very slow as it uses N^2 time as it needs to access the whole array twice.
 *
 * If we have a 10^9 union commands on 10^9 objects, we'll need 10^18 operations
 * to actually finish the algorithm.
 * 30+ years of computer time.
 *
 * Quadratic algorithms don't scale with technology.
 */

public class QuickFindUF {
	private int[] id;

	/* Constructor */
	public QuickFindUF(int N) {
		id = new int[N];

		for (int i = 0; i < N; i++)
			id[i] = i;
	}

	/* Are the objects connected */
	public boolean connected(int p, int q) {
		return id[p] == id[q];
	}

	/* Connect two objects */
	public void union(int p, int q) {
		int pid = id[p];
		int qid = id[q];

		for (int i = 0; i < id.length; i++) {
			if (id[i] == pid)
				id[i] = qid;
		}
	}
}