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) time. Using divide-and-conquer, the running time drops to Θ(nlogn).
Diagram loads as you scroll.
Algorithm Steps
- Step 1: Divide:
Sort points by their x-coordinate. Draw a vertical line x=m through the median point to divide P into equal subsets PL and PR of size ⌈n/2⌉ and ⌊n/2⌋.
- Step 2: Conquer:
Recursively find the closest pair in PL with minimal distance dL, and in PR with minimal distance dR.
Let d=min(dL,dR).
- Step 3: Combine:
A closer pair can only exist if one point is in PL and the other in PR.
Such points must lie within a vertical strip of width 2d centered at x=m (m−d≤x≤m+d).
- Filter all points falling inside the strip and sort them by y-coordinate.
- For each point p in the strip, only points with y-coordinates in [yp,yp+d] need to be checked.
- Geometric Fact: At most 6 points can fit into a d×2d rectangle on the other side of the dividing line without violating the property that all points on each side are at least d 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 x and maintaining a list sorted by y:
T(n)=2T(n/2)+Θ(n)
By the Master Theorem (a=2,b=2,d=1⟹a=bd):
T(n)∈Θ(nlogn)
2. Quickhull (Convex Hull by Divide-and-Conquer)
Inspired by Quick Sort, Quickhull constructs the convex hull of n points in average time Θ(nlogn):
- Identify extreme points with minimum x (p1) and maximum x (pn). The line segment p1pn divides the remaining points into two subsets:
- Upper set S1: Points lying above p1pn.
- Lower set S2: Points lying below p1pn.
- In S1, locate point pmax with the maximum perpendicular distance to p1pn. Point pmax is guaranteed to be an extreme point of the convex hull!
- Points inside the triangle △p1pmaxpn cannot be extreme points and are discarded.
- Recursively repeat for points outside the segments p1pmax and pmaxpn.
- Average Case: Θ(nlogn).
- Worst Case: Θ(n2) (when points lie on a circle and almost no points are discarded).
3. Multiplication of Large Integers (Karatsuba Algorithm)
Multiplying two n-digit integers using pen-and-paper school multiplication takes Θ(n2) single-digit multiplications.
Let A and B be two n-digit integers. Dividing each into two m-digit halves (m=⌊n/2⌋):
A=a1⋅10m+a0
B=b1⋅10m+b0
Direct multiplication yields:
A×B=(a1⋅10m+a0)(b1⋅10m+b0)=(a1b1)102m+(a1b0+a0b1)10m+(a0b0)
This requires 4 multiplications (a1b1, a1b0, a0b1, a0b0), giving recurrence T(n)=4T(n/2)+Θ(n)⟹Θ(n2), which is no faster than brute force!
Karatsuba’s Insight: Reduce to 3 Multiplications
Compute:
- c2=a1×b1
- c0=a0×b0
- c1=(a1+a0)×(b1+b0)−c2−c0
Observe that:
(a1+a0)(b1+b0)−a1b1−a0b0=a1b0+a0b1
Thus, A×B is computed with only 3 multiplications instead of 4:
A×B=c2⋅102m+c1⋅10m+c0
Midterm Case Study: Compute 2134×5687
Using the divide-and-conquer formula for 2134×5687:
- Number of digits n=4⟹m=2.
- A=2134⟹a1=21, a0=34.
- B=5687⟹b1=56, b0=87.
Step 1: Compute c2
c2=a1×b1=21×56=1176
Step 2: Compute c0
c0=a0×b0=34×87=2958
Step 3: Compute c1
(a1+a0)(b1+b0)(a1+a0)(b1+b0)c1=21+34=55=56+87=143=55×143=7865=7865−c2−c0=7865−1176−2958=3731
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
Complexity Analysis
The recurrence for Karatsuba’s algorithm is:
T(n)=3T(n/2)+Θ(n)
Here a=3,b=2,d=1. Since a=3>bd=21=2, Master Theorem Case 3 gives:
T(n)∈Θ(nlog23)≈Θ(n1.585)
This represents a substantial asymptotic speedup over standard Θ(n2) 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 ↓Hide solution ↑
Solution
Let the vertical strip have width 2d, extending distance d to the left and d to the right of the dividing line.
- All points in the left half are at pairwise distance at least d from each other.
- All points in the right half are at pairwise distance at least d from each other.
Consider a rectangle of width d and height d in the right half. The maximum number of points that can be placed in this d×d square such that no two are closer than d is at most 4 (the four corners).
Extending over the candidate band of height d across both halves, at most 6 points can fit within distance d of any candidate point.
Exercise 2
Compute the product 12×34 using Karatsuba’s 3-multiplication method (m=1).
Show solution ↓Hide solution ↑
Solution
A=12⟹a1=1,a0=2.
B=34⟹b1=3,b0=4. m=1.
- c2=a1b1=1×3=3.
- c0=a0b0=2×4=8.
- c1=(1+2)(3+4)−3−8=3×7−11=21−11=10.
Combine:
A×B=c2⋅102+c1⋅101+c0=3(100)+10(10)+8=300+100+8=408Verification: 12×34=408.