QuickFind/Union & Weighted quick union

closed account (GybDjE8b)
1. Write C++ implementation of quickfind , quickunion and weightedquickunion.
2. Show the contents of the id[] array (based on your solution for problem 1) after each union operation to solve the connectivity problem for the sequence: 0-2 , 1-4, 2-5, 3-6, 0-4, 6-0, and 1-3.
a. For quickfind.
b. For quickunion.
c. For weightedquickunion.

Did i do this right? I dont really understand what #2 is asking me

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public int find(int p) //quick find
{ return id[p]; }
public void union(int p, int q)
{ 
int pID = find(p);
int qID = find(q);
in the same component.
if (pID == qID) return;
for (int i = 0; i < id.length; i++)
if (id[i] == pID) id[i] = qID;
count--;
}



private int find(int p) //quick Union
{ 
while (p != id[p]) p = id[p];
return p;
}
public void union(int p, int q)
{ 
int pRoot = find(p);
int qRoot = find(q);
if (pRoot == qRoot) return;
id[pRoot] = qRoot;
count--;
}


public class WeightedQuickUnionUF
{
private int[] id; 
private int[] sz; 
private int count; 
public WeightedQuickUnionUF(int N)
{
count = N;
id = new int[N];
for (int i = 0; i < N; i++) id[i] = i;
sz = new int[N];
for (int i = 0; i < N; i++) sz[i] = 1;
}
public int count()
{ return count; }
public boolean connected(int p, int q)
{ return find(p) == find(q); }
private int find(int p)
{ 
while (p != id[p]) p = id[p];
return p;
}
public void union(int p, int q)
{
int i = find(p);
int j = find(q);
if (i == j) return;
if (sz[i] < sz[j]) { id[i] = j; sz[j] += sz[i]; }
else { id[j] = i; sz[i] += sz[j]; }
count--;
}
}
closed account (GybDjE8b)
help?
Topic archived. No new replies allowed.