Professional Documents
Culture Documents
Divide and Conquer (Closest Pair, Convex Hull, Strassen Matrix Multiply) Demo
Divide and Conquer (Closest Pair, Convex Hull, Strassen Matrix Multiply) Demo
LECTURE 13
Divide and Conquer
• Closest Pair of Points
• Convex Hull
• Strassen Matrix Mult.
Adam Smith
9/24/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Midterm Exam #1
• Willard Building Room 76
• Tuesday night, 8:15pm
9/24/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Review questions
• Find the solution to the recurrence using MT:
T(n)=8T(n/2)+cn.
(Answer: logb(a)=3>1, solution is Θ(n3).)
• Draw the recursion tree for this recurrence.
a. What is its height?
(Answer: h=log n.)
b. What is the number of leaves in the tree?
(Answer: 8h = 8log n = nlog 8= n3.)
9/24/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Review questions: recursion tree
Solve T(n)=8T(n/2)+cn:
cn cn
8
c(n/2) c(n/2) 4cn
8 8
c(n/4) c(n/4) c(n/4) (n/4) 16cn
…
Θ(1) Total = cn(1+4+42+43+…+n2)
= Θ(n3) geometric series
9/24/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Appendix: geometric series
for x ≠ 1
Return to last
slide viewed.
9/24/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Closest Pair of Points
Closest pair. Given n points in the plane, find a pair with smallest
Euclidean distance between them.
Brute force. Check all pairs of points p and q with Θ(n2) comparisons.
L
Closest Pair of Points: First Attempt
L
Closest Pair of Points
Algorithm.
Divide: draw vertical line L so that roughly ½n points on each side.
L
Closest Pair of Points
Algorithm.
Divide: draw vertical line L so that roughly ½n points on each side.
Conquer: find closest pair in each side recursively.
21
16
Closest Pair of Points
Algorithm.
Divide: draw vertical line L so that roughly ½n points on each side.
Conquer: find closest pair in each side recursively.
Combine: find closest pair with one point in each side. seems like Θ(n2)
Return best of 3 solutions.
8
21
16
Closest Pair of Points
Find closest pair with one point in each side, assuming that distance < δ.
21
δ = min(16, 21)
16
Closest Pair of Points
Find closest pair with one point in each side, assuming that distance < δ.
Observation: only need to consider points within δ of line L.
21
δ = min(16, 21)
16
δ
Closest Pair of Points
Find closest pair with one point in each side, assuming that distance < δ.
Observation: only need to consider points within δ of line L.
Sort points in 2δ-strip by their y coordinate.
L
7
5
4 21
δ = min(16, 21)
16 3
δ
Closest Pair of Points
Find closest pair with one point in each side, assuming that distance < δ.
Observation: only need to consider points within δ of line L.
Sort points in 2δ-strip by their y coordinate.
Theorem: Only need to check distances of those
within 11 positions in sorted list!
L
7
5
4 21
δ = min(16, 21)
16 3
δ
Closest Pair of Points
Proof.
No two points lie in same ½δ-by-½δ box. ½δ
Two points at least 2 rows apart 2 rows
30
½δ
have distance ≥ 2(½δ). ▪ 29
i 27
28 ½δ
δ δ
Closest Pair Algorithm
Closest-Pair(p1, …, pn) {
Compute separation line L such that half the points O(n log n)
are on one side and half on the other side.
return δ.
}
Closest Pair of Points: Analysis
Running time.
€
Divide and Conquer in low-dimensional geometry
9/24/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Standard algorithm
for i ← 1 to n
do for j ← 1 to n
do cij ← 0
for k ← 1 to n
do cij ← cij + aik⋅ bkj
9/24/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Divide-and-conquer algorithm
IDEA:
n£n matrix = 2£2 matrix of (n/2)£(n/2) submatrices:
C = A ⋅ B
r = ae + bg recursive
s = af + bh 8 mults of (n/2) £(n/2) submatrices
t = ce + dh ^
4 adds of (n/2) £(n/2) submatrices
u = cf + dg
9/24/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Analysis of D&C algorithm
T(n) = 8 T(n/2) + Θ(n2)
9/24/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Strassen’s idea
• Multiply 2£2 matrices with only 7 recursive mults.
P1 = a ⋅ ( f – h) r = P5 + P4 – P2 + P6
P2 = (a + b) ⋅ h s = P1 + P2
P3 = (c + d) ⋅ e t = P3 + P4
P4 = d ⋅ (g – e) u = P5 + P1 – P3 – P7
P5 = (a + d) ⋅ (e + h) 7 mults, 18 adds/subs.
P6 = (b – d) ⋅ (g + h) Note: No reliance on
P7 = (a – c) ⋅ (e + f ) commutativity of
multiplication!
9/24/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Strassen’s idea
• Multiply 2£2 matrices with only 7 recursive mults.
P1 = a ⋅ ( f – h) r = P5 + P4 – P2 + P6
P2 = (a + b) ⋅ h = (a + d) (e + h)
P3 = (c + d) ⋅ e + d (g – e) – (a + b) h
P4 = d ⋅ (g – e) + (b – d) (g + h)
P5 = (a + d) ⋅ (e + h) = ae + ah + de + dh
P6 = (b – d) ⋅ (g + h) + dg –de – ah – bh
P7 = (a – c) ⋅ (e + f ) + bg + bh – dg – dh
= ae + bg
9/24/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Strassen’s algorithm
1. Divide: Partition A and B into (n/2) x (n/2)
submatrices. Form terms to be multiplied
using + and – .
2. Conquer: Perform 7 multiplications of (n/2) x
(n/2) submatrices recursively.
3. Combine: Form product matrix C using + and –
on (n/2) x (n/2) submatrices.
9/24/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Analysis of Strassen
T(n) = 7 T(n/2) + Θ(n2)
nlogba = nlog27 ≈ n2.81 ⇒ CASE 1 ⇒ T(n) = Θ(nlg 7).
• Number 2.81 may not seem much smaller than 3.
• But the difference is in the exponent.
• The impact on running time is significant.
• Strassen’s algorithm beats the ordinary algorithm
on today’s machines for n ≥ 32 or so.
Best to date (of theoretical interest only): Θ(n2.376).
9/24/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne