-
Problem
-
Goal
- in O(nlgn) time.
-
Solution:
-
Divide
-
Merge: the hard case
- Let d = min{dleft, dright}.
- d would be the answer, except maybe L split a close pair!
-
Region Near L
-
Slab Might Contain All Points!
- Sy might contain all the points, so we can’t just check every pair inside it
-
Theorem
- Suppose Sy = p₁, ... ,pm, if dist( pᵢ,pⱼ ) < d , then
j-i≤ 15
. - In other words, if two points in Sy are close enough in the plane, they are close in the array Sy.
- Suppose Sy = p₁, ... ,pm, if dist( pᵢ,pⱼ ) < d , then
-
Proof 1:
-
Proof 2:
- Suppose 2 points are separated by > 15 indices.
- Then, at least 3 full rows separate them (the packing shown is the smallest possible).
- But the height of 3 rows is > 3d/2, which is > d.
- So the two points are father than d apart.
-
Linear Time Merge
- Therefore, we can scan Sy for pairs of points separated by < d in linear time.
- optimization:
- the condition
if |Px| == 2
can be optimized byif |Px| <=3
- 第三部分merge 可以只考虑 pᵢ 在left array,pⱼ 在right array 的情况
- 这样 只需要考虑 后面的 7个位置
- 更精确的划分,只需要 检查 后面5个位置 ? 5 Proof
- the condition
-
Total Running Time
- Divide set of points in half each time:
- O(log n) depth recursion
- Merge takes O(n) time.
- Recurrence: T(n) ≤ 2T(n/2) + cn
- Same as MergeSort ⇒ O(n log n) time.
- Divide set of points in half each time:
-
-
Improved Algorithm
- Divide the set into two equal sized parts by the line l, and recursively compute the minimal distance in each part.
- Let d be the minimal of the two minimal distances.
- Eliminate points that lie farther than d apart from l
- Sort the remaining points according to their y-coordinates
- Scan the remaining points in the y order and compute the distances of each point to its five neighbors.
- If any of these distances is less than d then update d.