About

Closest-Pair, Quickhull & Karatsuba Multiplication

Divide-and-Conquer Applications: Closest-Pair, Quickhull, and Large Integer Multiplication

Divide-and-conquer is applied to transform quadratic algorithms into efficient sub-quadratic or near-linear solutions.


1. Divide-and-Conquer Closest-Pair Algorithm

In Chapter 3, the brute-force closest-pair algorithm took Θ(n2)\Theta(n^2) time. Using divide-and-conquer, the running time drops to Θ(nlog⁡n)\Theta(n \log n).

Diagram loads as you scroll.

Algorithm Steps

  1. Step 1: Divide: Sort points by their xx-coordinate. Draw a vertical line x=mx = m through the median point to divide PP into equal subsets PLP_L and PRP_R of size ⌈n/2⌉\lceil n/2 \rceil and ⌊n/2⌋\lfloor n/2 \rfloor.
  2. Step 2: Conquer: Recursively find the closest pair in PLP_L with minimal distance dLd_L, and in PRP_R with minimal distance dRd_R. Let d=min⁡(dL,dR)d = \min(d_L, d_R).
  3. Step 3: Combine: A closer pair can only exist if one point is in PLP_L and the other in PRP_R. Such points must lie within a vertical strip of width 2d2d centered at x=mx = m (m−d≤x≤m+dm - d \le x \le m + d).
    • Filter all points falling inside the strip and sort them by yy-coordinate.
    • For each point pp in the strip, only points with yy-coordinates in [yp,yp+d][y_p, y_p + d] need to be checked.
    • Geometric Fact: At most 6 points can fit into a d×2dd \times 2d rectangle on the other side of the dividing line without violating the property that all points on each side are at least dd apart!
    • Hence, each point in the strip is compared against at most 5 to 7 neighboring points.

Complexity Analysis

By pre-sorting the points by xx and maintaining a list sorted by yy:

T(n)=2T(n/2)+Θ(n)T(n) = 2 T(n / 2) + \Theta(n)

By the Master Theorem (a=2,b=2,d=1  ⟹  a=bda = 2, b = 2, d = 1 \implies a = b^d):

T(n)∈Θ(nlog⁡n)T(n) \in \Theta(n \log n)

2. Quickhull (Convex Hull by Divide-and-Conquer)

Inspired by Quick Sort, Quickhull constructs the convex hull of nn points in average time Θ(nlog⁡n)\Theta(n \log n):

  1. Identify extreme points with minimum xx (p1p_1) and maximum xx (pnp_n). The line segment p1pn‾\overline{p_1 p_n} divides the remaining points into two subsets:
    • Upper set S1S_1: Points lying above p1pn‾\overline{p_1 p_n}.
    • Lower set S2S_2: Points lying below p1pn‾\overline{p_1 p_n}.
  2. In S1S_1, locate point pmax⁡p_{\max} with the maximum perpendicular distance to p1pn‾\overline{p_1 p_n}. Point pmax⁡p_{\max} is guaranteed to be an extreme point of the convex hull!
  3. Points inside the triangle △p1pmax⁡pn\triangle p_1 p_{\max} p_n cannot be extreme points and are discarded.
  4. Recursively repeat for points outside the segments p1pmax⁡‾\overline{p_1 p_{\max}} and pmax⁡pn‾\overline{p_{\max} p_n}.

3. Multiplication of Large Integers (Karatsuba Algorithm)

Multiplying two nn-digit integers using pen-and-paper school multiplication takes Θ(n2)\Theta(n^2) single-digit multiplications.

Let AA and BB be two nn-digit integers. Dividing each into two mm-digit halves (m=⌊n/2⌋m = \lfloor n/2 \rfloor):

A=a1⋅10m+a0A = a_1 \cdot 10^m + a_0 B=b1⋅10m+b0B = b_1 \cdot 10^m + b_0

Direct multiplication yields:

A×B=(a1⋅10m+a0)(b1⋅10m+b0)=(a1b1)102m+(a1b0+a0b1)10m+(a0b0)A \times B = (a_1 \cdot 10^m + a_0)(b_1 \cdot 10^m + b_0) = (a_1 b_1) 10^{2m} + (a_1 b_0 + a_0 b_1) 10^m + (a_0 b_0)

This requires 4 multiplications (a1b1a_1 b_1, a1b0a_1 b_0, a0b1a_0 b_1, a0b0a_0 b_0), giving recurrence T(n)=4T(n/2)+Θ(n)  ⟹  Θ(n2)T(n) = 4T(n/2) + \Theta(n) \implies \Theta(n^2), which is no faster than brute force!

Karatsuba’s Insight: Reduce to 3 Multiplications

Compute:

  1. c2=a1×b1c_2 = a_1 \times b_1
  2. c0=a0×b0c_0 = a_0 \times b_0
  3. c1=(a1+a0)×(b1+b0)−c2−c0c_1 = (a_1 + a_0) \times (b_1 + b_0) - c_2 - c_0

Observe that:

(a1+a0)(b1+b0)−a1b1−a0b0=a1b0+a0b1(a_1 + a_0)(b_1 + b_0) - a_1 b_1 - a_0 b_0 = a_1 b_0 + a_0 b_1

Thus, A×BA \times B is computed with only 3 multiplications instead of 4:

A×B=c2⋅102m+c1⋅10m+c0A \times B = c_2 \cdot 10^{2m} + c_1 \cdot 10^m + c_0

Midterm Case Study: Compute 2134×56872134 \times 5687

Using the divide-and-conquer formula for 2134×56872134 \times 5687:

Step 1: Compute c2c_2

c2=a1×b1=21×56=1176c_2 = a_1 \times b_1 = 21 \times 56 = 1176

Step 2: Compute c0c_0

c0=a0×b0=34×87=2958c_0 = a_0 \times b_0 = 34 \times 87 = 2958

Step 3: Compute c1c_1

(a1+a0)=21+34=55(b1+b0)=56+87=143(a1+a0)(b1+b0)=55×143=7865c1=7865−c2−c0=7865−1176−2958=3731\begin{aligned} (a_1 + a_0) &= 21 + 34 = 55 \\ (b_1 + b_0) &= 56 + 87 = 143 \\ (a_1 + a_0)(b_1 + b_0) &= 55 \times 143 = 7865 \\ c_1 &= 7865 - c_2 - c_0 = 7865 - 1176 - 2958 = 3731 \end{aligned}

Step 4: Combine the Products

A×B=c2⋅104+c1⋅102+c0=1176×104+3731×102+2958=11,760,000+373,100+2,958=12,136,058\begin{aligned} A \times B &= c_2 \cdot 10^4 + c_1 \cdot 10^2 + c_0 \\ &= 1176 \times 10^4 + 3731 \times 10^2 + 2958 \\ &= 11{,}760{,}000 + 373{,}100 + 2{,}958 \\ &= \mathbf{12{,}136{,}058} \end{aligned}

Complexity Analysis

The recurrence for Karatsuba’s algorithm is:

T(n)=3T(n/2)+Θ(n)T(n) = 3 T(n / 2) + \Theta(n)

Here a=3,b=2,d=1a = 3, b = 2, d = 1. Since a=3>bd=21=2a = 3 > b^d = 2^1 = 2, Master Theorem Case 3 gives:

T(n)∈Θ(nlog⁡23)≈Θ(n1.585)T(n) \in \Theta(n^{\log_2 3}) \approx \Theta(n^{1.585})

This represents a substantial asymptotic speedup over standard Θ(n2)\Theta(n^2) multiplication.


Exercises

Exercise 1

In the divide-and-conquer closest-pair algorithm, why are at most 6 points inspected in the opposite half of the strip for each point?

Show solution ↓
Solution

Let the vertical strip have width 2d2d, extending distance dd to the left and dd to the right of the dividing line.

  • All points in the left half are at pairwise distance at least dd from each other.
  • All points in the right half are at pairwise distance at least dd from each other.

Consider a rectangle of width dd and height dd in the right half. The maximum number of points that can be placed in this d×dd \times d square such that no two are closer than dd is at most 4 (the four corners). Extending over the candidate band of height dd across both halves, at most 6 points can fit within distance dd of any candidate point.

Exercise 2

Compute the product 12×3412 \times 34 using Karatsuba’s 3-multiplication method (m=1m = 1).

Show solution ↓
Solution

A=12  ⟹  a1=1,a0=2A = 12 \implies a_1 = 1, a_0 = 2. B=34  ⟹  b1=3,b0=4B = 34 \implies b_1 = 3, b_0 = 4. m=1m = 1.

  1. c2=a1b1=1×3=3c_2 = a_1 b_1 = 1 \times 3 = 3.
  2. c0=a0b0=2×4=8c_0 = a_0 b_0 = 2 \times 4 = 8.
  3. c1=(1+2)(3+4)−3−8=3×7−11=21−11=10c_1 = (1 + 2)(3 + 4) - 3 - 8 = 3 \times 7 - 11 = 21 - 11 = 10.

Combine:

A×B=c2⋅102+c1⋅101+c0=3(100)+10(10)+8=300+100+8=408A \times B = c_2 \cdot 10^2 + c_1 \cdot 10^1 + c_0 = 3(100) + 10(10) + 8 = 300 + 100 + 8 = 408

Verification: 12×34=40812 \times 34 = 408.