跳到主要内容

東北大学 工学研究科 電気・情報系 2019年3月実施 基礎科目 問題4 情報基礎2

Author

祭音Myyura

Description

日本語版

長さ N2N \ge 2 の配列 A=(A[1],A[N])A=(A[1], \ldots,A[N]) の各要素に実数が格納されている.配列 AA の二つの要素番号を引数にとる手続き PPQQ を次のように定義する.

  • 手続き P(ij)P(i,j) は、もし A[i]<A[j]A[i] < A[j] の場合に 11、それ以外の場合に 00 を返す.
  • 手続き Q(ij)Q(i,j) は、配列 AAii 番目と jj 番目の要素を入れ替える.ただし、もし i=ji=j なら配列 AA は変化しない.

手続き PP および QQ 以外では配列 AA にはアクセスしないという条件下で、配列 AA 内の実数を昇順に整列するアルゴリズムを考える、本問では、手続き PP の呼び出し回数を計算量と呼ぶ.

以下の問に答えよ.

(1) 上記条件を満たす整列アルゴリズムに対する計算量の漸近的下界を Ω\Omega 記法を用いて示せ.

(2) Fig. 4(a) に示す整列アルゴリズム Alg1 の疑似コードを考える.手続き PP の呼び出し回数を NN を用いて示せ、また、答の求め方も説明せよ.

(3) Fig. 4(b) に示す整列アルゴリズム Alg2 について、手続き QQの総呼び出し回数が N1N- 1 回以下となるように,(A), (B),(C) に入る適切な疑似コードを示せ.ただし、疑似コードは Fig.4(a) に示す疑似コードの表記に従うものとする.

(4) Fig. 4(cc) に示す整列アルゴリズム Alg3 の疑似コードを考える.ただし、Fig. 4(cc) 中に示す手続き RR が利用できる.

  • (a) Alg3 の基本戦略と処理の概要を言葉で簡潔に説明せよ.
  • (b) 配列 AA の初期値を (2,5,4,3,2)(2 , 5 , 4 , 3 , 2 ) とする. Alg3 の 6 行目および 19 行目にある手続き QQの実行直後に毎回配列 AA の値を表示することを考える.配列 AA の値を表示される順番に全て示せ.
  • (cc) Alg3 の計算量を Θ\Theta 記法を用いて示せ.

English Version

Every element in an array A=(A[1],,A[N])A=(A[1],\ldots, A[N]) of length N2N \ge 2 contains a real number. We define procedures PP and QQ, which take two element indices of the array AA as their arguments, as follows:

  • The procedure P(i,j)P(i, j) returns 11 if A[i]<A[j]A[i] < A[j], and 0 otherwise.
  • The procedure Q(i,j)Q(i, j) swaps the ii-th and jj-th elements in the array AA, where the array AA is unchanged if i=ji= j.

We consider algorithms for sorting the real numbers in the array AA in ascending order under the condition that algorithms never access the array AA except for the procedures PP and QQ. In this question, we refer to the number of calls to the procedure PP as the computational complexity.

Answer the following questions.

(1) Give the asymptotic lower bound of the computational complexity in big Ω\Omega notation for sorting algorithms that satisfy the above condition.

(2) Consider the pseudocode for the sorting algorithm Alg1 shown in Fig. 4(a). Give the number of calls to the procedure PP as an expression of NN. Moreover, explain your derivation of the result.

(3) Following the notation of the pseudocode shown in Fig. 4(a), give an appropriate pseudocode by filling ( A ) , ( B ) , and ( C ) of the sorting algorithm Alg2 shown in Fig. 4(b) so that the total number of calls to the procedure QQ is N1N- 1 or less.

(4) Consider the pseudocode for the sorting algorithm Alg3 shown in Fig. 4(cc), where a procedure RR, also shown in Fig. 4(cc), is available.

  • (a) Succinctly describe the fundamental strategy and an outline of the process of Alg3 in words.
  • (b) Suppose that the initial values of the array AA are (2,5,4,3,2)(2, 5, 4, 3, 2). We consider displaying the values of the array AA every time right after the procedure QQ at line 6 and line 19 in Alg3 is performed. Show all the values of the array AA in the order in which they are displayed.
  • (cc) Give the computational complexity of Alg3 in big Θ\Theta notation.

Figs

fig. 4(a)
Alg1 (N):
for i := 1 to N-1 do
for j := 2 to N-i+1 do
if P(j, j-1) = 1 then
Q (j,j-1)
endif
endfor
endfor
fig. 4(b)
Alg2 (N):
for i := 1 to N-1 do
k := i
for j := i+1 to N do
if ( A ) then
( B )
endif
endfor
( C )
endfor
fig. 4(cc)
Alg3 (N):
for i := 1 to N do
R(N-i+1, N)
endfor
for i := 1 to N-1 do
Q(1, N-i+1)
R(1, N-i)
endfor
R(i, m):
k := i
while k <= m do
j := k
k := k*2
if k <= m then
if k+1 <= m and P(k, k+1) = 1 then
k := k+1
endif
if P(j, k) = 1 then
Q(j, k)
endif
endif
endwhile

题目描述

长度 N2N\ge2 的数组 A=(A[1],,A[N])A=(A[1],\ldots,A[N]) 保存实数。算法只能通过以下两个过程访问数组:

  • P(i,j):若 A[i]<A[j]A[i]<A[j] 返回 1,否则返回 0
  • Q(i,j):交换 A[i]A[i]A[j]A[j],当 i=ji=j 时数组不变。

目标是把数组按升序排列,并把 P 的调用次数定义为计算量。图 4 中三段完整伪代码见上文。

  1. 用大 Ω\Omega 记号给出所有满足限制的比较排序算法的渐近计算量下界。
  2. 对图 4(a) 的 Alg1,用 NN 表示 P 的调用总数,并说明推导过程。
  3. 补全图 4(b) 的 Alg2 中 A、B、C,使 Q 的总调用数不超过 N1N-1
  4. 对图 4(c) 中使用过程 RAlg3
    1. 简述其基本策略和处理过程;
    2. 初始数组为 (2, 5, 4, 3, 2),每当第 6 行和第 19 行执行 Q 后立即输出数组,按顺序写出所有输出状态;
    3. 用大 Θ\Theta 记号给出 Alg3 的计算量。

Kai

(1)

只需考虑 NN 个元素互异的输入。比较决策树至少有 N!N! 个叶结点,而每次调用 PP 只有两种结果。设树高为 hh,则最坏情形下

2hN!,hlog2(N!)=Ω(NlogN).2^h\geq N!,\qquad h\geq \log_2(N!)=\Omega(N\log N).

(2)

固定 ii 时,内层循环调用 PP 恰好 NiN-i 次,与输入数值无关。因此总调用次数为

(N1)+(N2)+(N3)++2+1=N(N1)2(N-1) + (N-2) + (N-3) + \cdots + 2 + 1 = \frac{N(N-1)}{2}

(3)

  • ( A ): P(j, k) = 1
  • ( B ): k := j
  • ( C ): Q(i, k)

(4)

(a)

先从后向前调用 RR,将数组建成最大堆;随后把堆顶最大值与堆末元素交换,缩小堆范围,并调用 RR 恢复堆性质,直至堆只剩一个元素。

(b)

(5,2,4,3,2)(5,3,4,2,2)(2,3,4,2,5)(4,3,2,2,5)(2,3,2,4,5)(3,2,2,4,5)(2,2,3,4,5)(2,2,3,4,5)\begin{aligned} (5, 2, 4, 3, 2) \\ (5, 3, 4, 2, 2) \\ (2, 3, 4, 2, 5) \\ (4, 3, 2, 2, 5) \\ (2, 3, 2, 4, 5) \\ (3, 2, 2, 4, 5) \\ (2, 2, 3, 4, 5) \\ (2, 2, 3, 4, 5) \end{aligned}

最后两次输出相同,因为最后的 Q(1,2)Q(1,2) 交换了两个相等元素,但仍须输出。

(cc)

Θ(NlogN).\boxed{\Theta(N\log N)}.

建堆部分为 O(N)O(N);随后每次 R(1,m) 都沿较大子结点一直下行,即使未交换也不提前退出,比较次数为 Θ(logm)\Theta(\log m)。因此总次数为 Θ(m=2N1logm)=Θ(NlogN)\Theta(\sum_{m=2}^{N-1}\log m)=\Theta(N\log N)