跳到主要内容

東京大学 情報理工学系研究科 コンピュータ科学専攻 2019年2月実施 問題2

Author

kainoj

Description

Consider the Java program below to sort an array AA in an ascending order. MM, NN, and KK are positive integers, and AA is an array of NN nonnegative integers where 0A[i]<M0 \leq A[i] < M for all i{0,,N1}i \in \{0, \dots, N-1\}.
In this program, list is a class of an integer list with the following methods:

  • lst.size(): returns the number of elements in the list lst.
  • lst.get(i): returns the element at the ii-th position in the list lst (the position number starts from 0).
  • lst.insert(i, x): inserts xx to the list lst at the ii-th position.

BB is an array of size KK, whose elements are all initialized to empty lists. Suppose that the execution time of each of the above methods is constant. You can ignore overflow errors.

void mysort(int M, int N, int K, int[] A, list[] B) {
for (int i = 0; i < N; i++) {
int m = A[i] * K / M;
int j = 0;
for (; j < B[m].size(); j++) {
if (A[i] <= B[m].get(j)) {
break;
}
}
B[m].insert(j, A[i]);
}
int i = 0;
for (int m = 0; m < K; m++) {
for (int j = 0; j < B[m].size(); j++) {
A[i] = [ blank X ];
i = i + 1;
}
}
}

Answer the following questions:

(1) Answer an appropriate expression to fill the blank   X  \boxed{\ \ X\ \ }.

(2) Let CC be the number of times the line 66 is executed. Answer the largest value of CC in terms of NN and KK. Also, answer the expected value of CC in terms of NN and KK, assuming that A[i]A[i] is distributed independently uniformly randomly over the set {0,,M1}\{0, \ldots, M-1\}. Suppose that KMK \ll M for this question.

(3) Explain how the expected running time of this program varies when KK changes, assuming that A[i]A[i] is distributed independently uniformly randomly.

(4) Discuss advantages and disadvantages of this algorithm in comparison to the quicksort algorithm.

题目描述

题中 Java 程序用于将数组 AA 按升序排序。M,N,KM,N,K 为正整数, AANN 个非负整数,且对所有 i{0,,N1}i\in\{0,\ldots,N-1\} 均有 0A[i]<M0\le A[i]<Mlist 是整数列表类,size() 返回元素数, get(i) 返回从 00 开始编号的第 ii 个元素,insert(i,x) 在第 ii 个位置插入 xx;这些方法均视为常数时间。BB 是长度为 KK 的数组,每个元素初始为空列表。 忽略整数溢出。

程序先按

m=A[i]K/Mm=A[i]K/M

把每个元素分配到桶 B[m]B[m],并通过顺序扫描把它插入桶内的有序位置;随后按桶号从小到大把各桶元素写回 AA。回答下列问题。

(1)给出填入空白 X\boxed{X} 的合适表达式,使程序正确写回排序结果。

(2)令 CC 为程序第 6 行的执行次数。用 N,KN,K 表示 CC 的最大值;再假设每个 A[i]A[i] 独立地在 {0,,M1}\{0,\ldots,M-1\} 上均匀分布,并假设 KMK\ll M,求 CC 的期望。

(3)在上述独立均匀分布假设下,说明程序的期望运行时间如何随 KK 改变。

(4)与快速排序相比,讨论该算法的优点和缺点。

Kai

Setting of the problem: we got NN element and KK buckets.

(1)

B[m].get(j)

(2)

The input sequence might be in an increasing order and all elements might fall into one bucket. Thus, line 66 will be executed:

C=0+1++(n1)=n(n1)2C = 0 + 1 + \cdots + (n-1) = \frac{n(n-1)}{2}

times. Note that it has to be an increasing order, because items are being inserted at the beginning of a bucket. If we wanna do evil, we must make every item traverse the whole bucket, until the end. It is possible when every inserted item is bigger than any element in the bucket, that is, input sequence is of increasing order.

(3)

Let nin_i denote size of ii-th bucket. Line 66 is de facto an insertion sort, which means that ii-th~bucket will be sorted in O(n2)O(n^2). Total running time will be to:

C=i=1KO(ni2) C = \sum_{i=1}^{K} O(n_i^2)

Taking expectation:

E(C)=E[i=1KO(ni2)]=i=1KE[O(ni2)]=i=1KO(E[ni2]) \mathbb{E}(C) = \mathbb{E}[\sum_{i=1}^{K} O(n_i^2)] = \sum_{i=1}^{K} \mathbb{E}[O(n_i^2)] = \sum_{i=1}^{K} O(\mathbb{E}[n_i^2])

What is [Eni2][\mathbb{E}n_i^2]? Note that, by definition Var(X)=E[X2](EX)2Var(X) = \mathbb{E}[X^2] - (\mathbb{E}X)^2 for any random variable XX. Let Xi,jX_{i,j} be an indicator random variable:

Xi,j={1element j went to bucket i0otherwise X_{i,j} = \begin{cases} 1 & \text{element $j$ went to bucket $i$} \\ 0 & \text{otherwise} \end{cases}
ni=j=1NXi,j n_i = \sum_{j=1}^{N} X_{i, j}

Since we have KK buckets and every of them is equally likely, probability of "going to bucket ii" is 1K\frac{1}{K}. Thus, expectation of nin_i is:

E(ni)=j=1NE(Xi,j)=NK \mathbb{E}(n_i) = \sum_{j=1}^{N} \mathbb{E}(X_{i, j}) = \frac{N}{K}

We can also notice, that, nin_i is just a binomial random variable with expectation npnp and variance np(1p)np(1-p). Here a trial is mapping an item into bucket, and the success is placing it into ii-th bucket. There are n=Nn=N trials and probability of success if 1K\frac{1}{K}.

E[ni2]=Var[ni]+(E[ni])2=N1K(11K)+(NK)2=N2+NKNK2\begin{aligned} \mathbb{E}[n_i^2] = Var[n_i] + (\mathbb{E}[n_i])^2 &= N\frac{1}{K}(1-\frac{1}{K}) + (\frac{N}{K})^2 \\ &= \frac{N^2 + NK - N}{K^2} \end{aligned}

Finally:

E[C]=O(i=1KE[ni2])=O(i=1KN2+NKNK2)=O(N2K+N)\begin{aligned} \mathbb{E}[C] = O(\sum_{i=1}^{K}\mathbb{E}[n_i^2]) = O(\sum_{i=1}^{K} \frac{N^2 + NK - N}{K^2}) = O(\frac{N^2}{K} + N) \end{aligned}

The following contribute to total expected running time:

  • finding a bucket for each element O(N)O(N)
  • sorting each bucket, O(N2K+N)O(\frac{N^2}{K} + N)
  • for each bucket, getting its content: O(KNK)=O(N)O(K\frac{N}{K}) = O(N)

Total running time:

O(N2K+3N) O(\frac{N^2}{K} + 3N)

When KK is O(N)O(N), then we get O(N)O(N) expected running time. When KK is O(1)O(1), then the running time is O(N2)O(N^2).

(4)

  • BS is stable, QS isn't
  • BS isn't in-place, QS is
  • BS runs O(N)O(N) expected, QS is O(NlogN)O(NlogN)
  • BS has upper limit on keys, QS hasn't