跳到主要内容

東京大学 新領域創成科学研究科 メディカル情報生命専攻 2019年8月実施 問題7

Author

zephyr

Description

Let A={a1,a2,,aN}A = \{a_1, a_2, \ldots, a_N\} (1a1,a2,,aNW,aiaj if ij,1NW)(1 \leq a_1, a_2, \ldots, a_N \leq W, a_i \neq a_j \text{ if } i \neq j, 1 \leq N \leq W) be an array of size NN, whose elements are different integers ranging from 11 to WW. Let S(A)S(A) be the number of all possible distinct arrays each of which can be created by iteratively applying the following operation to AA, 00 or more times.

Operation: choose arbitrary ii that satisfies 1iN11 \leq i \leq N - 1 and ai+ai+1<Wa_i + a_{i+1} < W, and swap the values of aia_i and ai+1a_{i+1}.

We denote the largest number in array AA by aha_h, and the smallest number by ala_l.

(1) Let A={4,1,10,3,2}A = \{4, 1, 10, 3, 2\}. Show S(A)S(A) for W=11,12W = 11, 12, respectively.

(2) When ah+al<Wa_h + a_l < W, prove S(A)=NS(Aal)S(A) = N \cdot S(A - a_l). Here, AalA - a_l denotes an array that is array AA with element ala_l removed.

(3) When ah+alWa_h + a_l \geq W, prove S(A)=S({a1,a2,,ah1})S({ah+1,ah+2,,aN})S(A) = S(\{a_1, a_2, \ldots, a_{h-1}\}) \cdot S(\{a_{h+1}, a_{h+2}, \ldots, a_N\}). Here, we define S()=1S(\emptyset) = 1.

(4) Show an algorithm that computes S(A)S(A) from AA using (2) and (3).

(5) Let W=NW = N. We define T(A)T(A) as the worst time complexity of the algorithm you showed in (4). Write T(A)T(A) in terms of NN, and prove it. You can assume that addition, comparison, copy of two elements take unit time.

(6) Let W=NW = N. Assuming that the N!N! possible permutations have equal probability of being generated as the input array, write the average time complexity of the algorithm you showed in (4).


A={a1,a2,,aN}A = \{a_1, a_2, \ldots, a_N\} (1a1,a2,,aNW,aiaj 当 ij,1NW)(1 \leq a_1, a_2, \ldots, a_N \leq W, a_i \neq a_j \text{ 当 } i \neq j, 1 \leq N \leq W) 为一个大小为 NN 的数组,其元素为 11WW 范围内的不同整数。设 S(A)S(A) 为可以通过对 AA 进行 00 次或多次以下操作创建的所有可能不同数组的数量。

操作:选择满足 1iN11 \leq i \leq N - 1ai+ai+1<Wa_i + a_{i+1} < W 的任意 ii,交换 aia_iai+1a_{i+1} 的值。

我们将数组 AA 中的最大数记为 aha_h,最小数记为 ala_l

(1) 设 A={4,1,10,3,2}A = \{4, 1, 10, 3, 2\}。分别显示 W=11,12W = 11, 12 时的 S(A)S(A)

(2) 当 ah+al<Wa_h + a_l < W 时,证明 S(A)=NS(Aal)S(A) = N \cdot S(A - a_l)。这里,AalA - a_l 表示从数组 AA 中移除元素 ala_l 后的数组。

(3) 当 ah+alWa_h + a_l \geq W 时,证明 S(A)=S({a1,a2,,ah1})S({ah+1,ah+2,,aN})S(A) = S(\{a_1, a_2, \ldots, a_{h-1}\}) \cdot S(\{a_{h+1}, a_{h+2}, \ldots, a_N\})。这里,我们定义 S()=1S(\emptyset) = 1

(4) 使用 (2) 和 (3) 展示一个从 AA 计算 S(A)S(A) 的算法。

(5) 设 W=NW = N。我们定义 T(A)T(A) 为你在 (4) 中展示的算法的最坏时间复杂度。用 NN 表示 T(A)T(A),并证明它。你可以假设两个元素的加法、比较、复制操作的时间为一个单位时间。

(6) 设 W=NW = N。假设 N!N! 个可能的排列作为输入数组被生成的概率相等,写出你在 (4) 中展示的算法的平均时间复杂度。

Kai

(1)

Given A={4,1,10,3,2}A = \{4, 1, 10, 3, 2\}:

For W=11W = 11

To find S(A)S(A) for W=11W = 11, we need to consider the operation condition ai+ai+1<Wa_i + a_{i+1} < W. We start with the initial array and systematically apply all valid swaps.

Initial array: {4,1,10,3,2}\{4, 1, 10, 3, 2\}

We need to check all possible adjacent swaps under the condition ai+ai+1<11a_i + a_{i+1} < 11:

Swap 44 and 11 (since 4+1=5<114 + 1 = 5 < 11):

{1,4,10,3,2} \{1, 4, 10, 3, 2\}

Swap 11 and 44 back (since 1+4=5<111 + 4 = 5 < 11):

{4,1,10,3,2} \{4, 1, 10, 3, 2\}

Swap 1010 and 33 (since 10+3=131110 + 3 = 13 \not< 11):

No swap \text{No swap}

Swap 33 and 22 (since 3+2=5<113 + 2 = 5 < 11):

{4,1,10,2,3} \{4, 1, 10, 2, 3\}

All valid permutations for W=11W = 11 are:

  1. {4,1,10,3,2}\{4, 1, 10, 3, 2\}
  2. {1,4,10,3,2}\{1, 4, 10, 3, 2\}
  3. {4,1,10,2,3}\{4, 1, 10, 2, 3\}
  4. {1,4,10,2,3}\{1, 4, 10, 2, 3\}

Therefore, for W=11W = 11:

S(A)=4S(A) = 4

For W=12W = 12

For W=12W = 12, we need to explore more possible swaps as the condition is more lenient.

Initial array: {4,1,10,3,2}\{4, 1, 10, 3, 2\}

We start with the initial array and apply all valid swaps:

Swap 44 and 11 (since 4+1=5<124 + 1 = 5 < 12):

{1,4,10,3,2} \{1, 4, 10, 3, 2\}

Swap 11 and 1010 (since 1+10=11<121 + 10 = 11 < 12):

{4,10,1,3,2} \{4, 10, 1, 3, 2\}

Swap 33 and 22 (since 3+2=5<123 + 2 = 5 < 12)

{4,1,10,2,3}\{4, 1, 10, 2, 3\}

Now explore permutations of {1,4,10,3,2}\{1, 4, 10, 3, 2\}:

Swap 33 and 22 (since 3+2=5<123+2=5<12):

{1,4,10,2,3}\{1, 4, 10, 2, 3\}

Now explore permutations of {4,10,1,3,2}\{4, 10, 1, 3, 2\}:

Swap 11 and 33 (since 1+3=4<121 + 3 = 4 < 12):

{4,10,3,1,2} \{4, 10, 3, 1, 2\}

Swap 33 and 22 (since 3+2=5<123 + 2 = 5 < 12):

{4,10,1,2,3} \{4, 10, 1, 2, 3\}

Now explore permutations of {4,10,3,1,2}\{4, 10, 3, 1, 2\}:

Swap 11 and 22 (since 1+2=3<121+2=3<12):

{4,10,3,2,1}\{4, 10, 3, 2, 1\}

Now explore permutations of {4,10,1,3,2}\{4, 10, 1, 3, 2\}:

Similarly, we get:

{4,10,2,3,1},{4,10,2,1,3}\{4, 10, 2, 3, 1\}, \{4, 10, 2, 1, 3\}

Thus, the valid permutations for W=12W = 12 are:

  1. {4,1,10,3,2}\{4, 1, 10, 3, 2\}
  2. {1,4,10,3,2}\{1, 4, 10, 3, 2\}
  3. {4,1,10,2,3}\{4, 1, 10, 2, 3\}
  4. {1,4,10,2,3}\{1, 4, 10, 2, 3\}
  5. {4,10,1,3,2}\{4, 10, 1, 3, 2\}
  6. {4,10,1,2,3}\{4, 10, 1, 2, 3\}
  7. {4,10,2,3,1}\{4, 10, 2, 3, 1\}
  8. {4,10,2,1,3}\{4, 10, 2, 1, 3\}
  9. {4,10,3,1,2}\{4, 10, 3, 1, 2\}
  10. {4,10,3,2,1}\{4, 10, 3, 2, 1\}

Therefore, for W=12W = 12:

S(A)=10S(A) = 10

(2)

When ah+al<Wa_h + a_l < W, prove S(A)=NS(Aal)S(A) = N \cdot S(A - a_l).

Given ah+al<Wa_h + a_l < W, the smallest number ala_l can be swapped with any adjacent element and can move to any position in the array.

Therefore, the number of permutations of AA is equal to the number of permutations of AalA - a_l, multiplied by the number of possible positions for ala_l, which is NN. Hence, we have:

S(A)=NS(Aal) S(A) = N \cdot S(A - a_l)

(3)

When ah+alWa_h + a_l \geq W, prove S(A)=S({a1,a2,,ah1})S({ah+1,ah+2,,aN})S(A) = S(\{a_1, a_2, \ldots, a_{h-1}\}) \cdot S(\{a_{h+1}, a_{h+2}, \ldots, a_N\}).

If ah+alWa_h + a_l \geq W, then aha_h and ala_l , or aha_h and any other element of the array, cannot be swapped, effectively dividing AA into two independent subarrays. Thus, the total number of distinct arrays is the product of the number of distinct arrays of each subarray:

S(A)=S({a1,a2,,ah1})S({ah+1,ah+2,,aN}) S(A) = S(\{a_1, a_2, \ldots, a_{h-1}\}) \cdot S(\{a_{h+1}, a_{h+2}, \ldots, a_N\})

(4)

Algorithm to compute S(A)S(A):

def compute_S(A, W):
if len(A) == 0:
return 1

# O(N)
a_h = max(A)
a_l = min(A)

if a_h + a_l < W:
# O(N)
A_minus_a_l = [x for x in A if x != a_l]
return len(A) * compute_S(A_minus_a_l, W)
# T(N-1)
else:
index_h = A.index(a_h)
left_part = A[:index_h]
right_part = A[index_h+1:]
return compute_S(left_part, W) * compute_S(right_part, W)
# 2*T(N/2)

# Example
A = [4, 1, 10, 3, 2]
W = 11
print(compute_S(A, W)) # Output: 4
W = 12
print(compute_S(A, W)) # Output: 10

(5)

Given that the worst-case scenario involves removing the smallest element each time, the time complexity can be analyzed as follows:

  1. Initial size: NN
  2. Operation: Finding the smallest element (which takes O(N)O(N) time) and removing it, reducing the problem size by 1.
  3. Recurrence relation: The total time complexity is the sum of the times taken for each step as we reduce the size of the array from NN to 0.

The time complexity can be expressed as:

T(N)=N+(N1)+(N2)++1 T(N) = N + (N-1) + (N-2) + \ldots + 1

This is the sum of the first NN natural numbers:

T(N)=i=1Ni=N(N+1)2 T(N) = \sum_{i=1}^{N} i = \frac{N(N+1)}{2}

Therefore, the worst-case time complexity is:

T(N)=O(N2) T(N) = O(N^2)

(6)

Assuming that each of the N!N! permutations of the array is equally likely, and given that the average scenario does not always involve removing the smallest element each time, the time complexity will be different.

In the average case, the algorithm will involve both scenarios of removing the smallest element and splitting the array. However, the average number of operations will not always hit the worst-case scenario.

Considering the balanced approach where the split operation happens frequently, the recurrence relation can be described more favorably compared to the worst-case:

Tavg(N)=2T(N/2)+O(N) T_{\text{avg}}(N) = 2T(N/2) + O(N)

Using the Master Theorem for a=2a = 2, b=2b = 2, and f(N)=O(N)f(N) = O(N):

Tavg(N)=O(NlogN) T_{\text{avg}}(N) = O(N \log N)

Thus, the average time complexity is:

Tavg(N)=O(NlogN) T_{\text{avg}}(N) = O(N \log N)

Knowledge

组合计数 递归 复杂度分析 主定理

解题技巧和信息

  1. Recursive Reduction: Understand how reducing a problem by removing an element or splitting the array affects complexity.
  2. Complexity Analysis: Sum of first NN numbers is N(N+1)2\frac{N(N+1)}{2}.
  3. Master Theorem: Useful for solving recurrence relations in divide-and-conquer algorithms.

重点词汇

  • worst-case 最坏情况
  • average-case 平均情况
  • time complexity 时间复杂度
  • recurrence relation 递推关系

参考资料

  1. Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein, Chapter 4.