東京大学 情報理工学系研究科 電子情報学専攻 2020年8月実施 専門 第3問
Author
Description
There are
(1) The pseudo code in the next page shows an union-find algorithm that keeps track of the equivalent class of each object. The function init initializes the array parant that records the equivalence classes (ignore the array sizes in this question). Every time the above mentioned triplet is recorded, the function union is executed and it updates the array parent. When parent after the following triplets have been recorded.
(2) Describe the order of the worst-case time complexity of the function find and union with reasons.
(3) Consider recording the number of objects in each equivalence class using the array sizes. Fill in (X) and (Y) in the pseudo code so that the function size returns the number of objects in the equivalence class including the designated object
(4) The time complexity of the function find and union can be improved by modifying the function union using the array sizes. Modify and show the code (X) in the pseudo code. Describe the order of the worst-case time complexity of the improved union function with reasons.
(5) Consider finding the time when the designated objects
Union-find algorithm:
int parent[N];
int sizes[N];
void init() {
for (int i = 0; i < N; ++i) {
parent[i] = i;
sizes[i] = 1;
}
}
int find(int i) {
while (parent[i] != i) {
i = parent[i];
}
return i;
}
void union(int a, int b) {
int i = find(a);
int j = find(b);
parent[i] = j;
(X)
}
int size(int a) {
(Y)
}
Kai
(1)
| Time \ Node | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 2 | 3 | 4 | 5 |
| 1 | 3 | 1 | 2 | 3 | 4 | 5 |
| 2 | 3 | 1 | 2 | 3 | 2 | 5 |
| 3 | 3 | 5 | 2 | 3 | 2 | 5 |
| 4 | 3 | 5 | 2 | 5 | 2 | 5 |
(2)
Reason: Union and find operation must traverse a chain in the worst case when tree is a list.
(3)
X: sizes[j] += sizes[i];
Y: return sizes[find(a)];
(4)
void union(int a, int b) {
int i = find(a);
int j = find(b);
if (i == j) {
return;
} else if (sizes[i] > sizes[j]) {
parent[j] = i;
sizes[i] += sizes[j];
} else {
parent[i] = j;
sizes[j] += sizes[i];
}
}
Complexity:
Reason: By always attaching the smaller tree to the root of the larger tree, the depth of any node only increases when it is merged into a tree of equal or larger size, which guarantees the tree height is at most logarithmic with respect to
(5)
① Initialize a time array, when Time[a] = t, the union will be
② To find the connection time for
Complexity:
Reason: Since union by size is used, the depth of the tree is