跳到主要内容

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

Author

zephyr, 祭音Myyura

Description

There are two points AA, BB and 2n2^n data points P1,,P2nP_1, \cdots, P_{2^n} in a 2-dimensional Euclidean plane. Assume that the distance ϵ\epsilon between AA and BB, and the distances a1,,a2na_1, \cdots, a_{2^n} between AA and the data points are given. The distances b1,,b2nb_1, \cdots, b_{2^n} between BB and the data points are not given, but a function f(Pi,Pj)f(P_i, P_j) defined below can be used to identify the sign of bibjb_i - b_j for 1i<j2n1 \leq i < j \leq 2^n.

f(Pi,Pj)=sgn(bibj)={1if bibj>00if bibj=01if bibj<0f(P_i, P_j) = \mathrm{sgn}(b_i - b_j) = \begin{cases} 1 & \text{if } b_i - b_j > 0 \\ 0 & \text{if } b_i - b_j = 0 \\ -1 & \text{if } b_i - b_j < 0 \end{cases}

Assume that aiaj>ϵ|a_i - a_j| > \epsilon for any ai,aja_i, a_j (1i<j2n)(1 \leq i < j \leq 2^n).

(1) Show the pseudo-code of an algorithm to sort P1,,P2nP_1, \cdots, P_{2^n} by ascending order of the distances from AA in O(n2n)O(n2^n) worst computational time.

(2) Explain why the worst computational time of the algorithm shown in (1) is O(n2n)O(n2^n).

(3) Prove that if aiaj>2ϵa_i - a_j > 2\epsilon then bi>bjb_i > b_j.

(4) When P1,,P2nP_1, \cdots, P_{2^n} are already sorted by ascending order of the distances from AA, show an algorithm to sort by ascending order of the distances from BB that calls function ff the minimum number of times, and evaluate that number of times.

题目描述

二维欧氏平面内有两点 A,BA,B2n2^n 个数据点 P1,,P2nP_1,\ldots,P_{2^n}。已知 A,BA,B 间距离 ϵ\epsilon,以及 AA 到各数据点的距离 a1,,a2na_1,\ldots,a_{2^n};未知 BB 到各点的距离 b1,,b2nb_1,\ldots,b_{2^n},但可调用

f(Pi,Pj)=sgn(bibj)={1,bibj>0,0,bibj=0,1,bibj<0f(P_i,P_j)=\operatorname{sgn}(b_i-b_j)= \begin{cases} 1,&b_i-b_j>0,\\ 0,&b_i-b_j=0,\\ -1,&b_i-b_j<0 \end{cases}

来比较任意 1i<j2n1\le i<j\le2^n 的两项 bi,bjb_i,b_j。并假设对任意 1i<j2n1\le i<j\le2^n,距离满足

aiaj>ϵ.|a_i-a_j|>\epsilon.
  1. 给出最坏 O(n2n)O(n2^n) 时间内按 aia_i 升序排列 P1,,P2nP_1,\ldots,P_{2^n} 的伪代码。
  2. 说明第 1 问算法为何具有该最坏时间复杂度。
  3. 证明若 aiaj>2ϵa_i-a_j>2\epsilon,则 bi>bjb_i>b_j
  4. 已知各点已按到 AA 的距离升序排列,设计按到 BB 的距离升序排列它们的算法,使对比较函数 ff 的调用次数最少,并精确评价调用次数。

Kai

(1) Pseudo-code to sort P1,,P2nP_1, \cdots, P_{2^n} by ascending order of the distances from AA

def merge_sort(items):                  # each item is (a_i, P_i)
if len(items) <= 1:
return items
mid = len(items) // 2
left = merge_sort(items[:mid])
right = merge_sort(items[mid:])
return merge(left, right)

def merge(left, right):
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i][0] <= right[j][0]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result

items = [(a_1, P_1), (a_2, P_2), ..., (a_{2^n}, P_{2^n})]
sorted_points = [point for _, point in merge_sort(items)]

P.S.: Heapsort can also achieve the same worst-case bound.

(2) Explain the worst computational time of the algorithm shown in (1) is O(n2n)O(n2^n)

The merge sort algorithm has time complexity O(klogk)O(k\log k): merging costs O(k)O(k) per recursion level, and there are O(logk)O(\log k) levels. Here k=2nk=2^n, so the complexity is O(2nlog2n)=O(n2n)O(2^n\log 2^n)=O(n2^n).

(3) Prove that if aiaj>2ϵa_i - a_j > 2\epsilon then bi>bjb_i > b_j

Consider 2 triangles formed by points AA, BB, and PiP_i, PjP_j:

  • ABPi\triangle ABP_i with sides aia_i, bib_i, and ϵ\epsilon

  • ABPj\triangle ABP_j with sides aja_j, bjb_j, and ϵ\epsilon

By the triangle inequality, we have:

bi+ϵaiandai+ϵbib_i + \epsilon \geq a_i \quad \text{and} \quad a_i + \epsilon \geq b_i

Given that aiaj>2ϵa_i - a_j > 2\epsilon, we can write:

bi+ϵai>aj+2ϵbj+ϵb_i + \epsilon \geq a_i > a_j + 2\epsilon \geq b_j + \epsilon

Therefore, bi>bjb_i > b_j.

(4) Algorithm to sort by ascending order of the distances from BB

Given that the points are sorted by aia_i, for any ii (1i2n21 \leq i \leq 2^n-2),

ai+2ai=(ai+2ai+1)+(ai+1ai)>2ϵ.a_{i+2}-a_i=(a_{i+2}-a_{i+1})+(a_{i+1}-a_i)>2\epsilon.

Thus bi+2>bib_{i+2}>b_i by (3).

Therefore, the odd-indexed list L=(P1,P3,,P2n1)L=(P_1,P_3,\ldots,P_{2^n-1}) and the even-indexed list R=(P2,P4,,P2n)R=(P_2,P_4,\ldots,P_{2^n}) are both already sorted by distance from BB. Merge them as follows; compare_B uses one call to ff (reversing its sign when the arguments' original indices are reversed).

def merge_by_B(L, R):
result = []
i = j = 0
while i < len(L) and j < len(R):
if compare_B(L[i], R[j]) <= 0:
result.append(L[i])
i += 1
else:
result.append(R[j])
j += 1
result.extend(L[i:])
result.extend(R[j:])
return result

Evaluation of the number of times the function ff is called

Merging two lists with a total of 2n2^n elements uses at most

2n12^n-1

calls to ff. This is optimal in the worst case: if the two lists alternate in the final order, each of the 2n12^n-1 adjacent cross-list pairs must be compared; otherwise that pair could be interchanged without violating either list's known internal order.

Knowledge

排序算法 算法 复杂度分析 几何 三角不等式

难点解题思路

  • 通过归并排序方法来排序点集 P1,,P2nP_1, \cdots, P_{2^n},利用三角不等式证明点与点之间距离的关系,提供了新的思路来求解几何问题。
  • 利用归并排序和冒泡排序的结合,优化了排序过程中函数调用次数。

解题技巧和信息

  • 对于复杂度分析,归并排序和冒泡排序是常见的有效方法。
  • 使用几何和三角不等式的知识可以帮助解决关于点距离的问题。
  • 在已知一个点集的部分顺序信息时,可以利用该信息减少计算量,优化算法。

重点词汇

  • Sort 排序
  • Merge 归并
  • Triangle inequality 三角不等式
  • Euclidean distance 欧几里得距离
  • Computational complexity 计算复杂度

参考资料

  1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein - Chapter on Sorting and Order Statistics
  2. "Algorithms" by Robert Sedgewick and Kevin Wayne - Chapter on Sorting