algorithm design and analysis - penn state college of...

27
9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis LECTURE 13 Divide and Conquer Closest Pair of Points Convex Hull Strassen Matrix Mult.

Upload: others

Post on 06-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Adam Smith

Algorithm Design and Analysis

LECTURE 13 Divide and Conquer • Closest Pair of Points • Convex Hull • Strassen Matrix Mult.

Page 2: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

Midterm Exam #1

•  Willard Building Room 76 •  Tuesday night, 8:15pm

•  You may bring: one (1) double-sided, hand-written 8.5” x 11” sheet of notes on colored paper – Hint: use its preparation as a study aid

9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Page 3: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

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.)

Page 4: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

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:

c(n/4) c(n/4) c(n/4) (n/4)

c(n/2)

Θ(1)

Total = cn(1+4+42+43+…+n2) = Θ(n3)

cn

c(n/2)

geometric series

8 cn

8 8 4cn

16cn

Page 5: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Appendix: geometric series

for |x| < 1

for x ≠ 1

Return to last slide viewed.

Page 6: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

Closest Pair of Points

Closest pair. Given n points in the plane, find a pair with smallest Euclidean distance between them.

Fundamental geometric primitive.   Graphics, computer vision, geographic information systems,

molecular modeling, air traffic control.   Special case of nearest neighbor, Euclidean MST, Voronoi.

Brute force. Check all pairs of points p and q with Θ(n2) comparisons.

1-D version (points on a line): O(n log n) easy via sorting

Assumption. No two points have same x coordinate.

to make presentation cleaner

fast closest pair inspired fast algorithms for these problems

Page 7: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

Closest Pair of Points: First Attempt

Divide. Sub-divide region into 4 quadrants.

L

Page 8: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

Closest Pair of Points: First Attempt

Divide. Sub-divide region into 4 quadrants. Obstacle. Impossible to ensure n/4 points in each piece.

L

Page 9: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

Closest Pair of Points

Algorithm.   Divide: draw vertical line L so that roughly ½n points on each side.

L

Page 10: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

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.

16

21

L

Page 11: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

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.   Return best of 3 solutions.

16

21 8

L

seems like Θ(n2)

Page 12: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

Closest Pair of Points

Find closest pair with one point in each side, assuming that distance < δ.

16

21

δ = min(16, 21)

L

Page 13: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

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.

16

21

δ

L

δ = min(16, 21)

Page 14: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

16

21

1 2

3

4 5 6

7

δ

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

δ = min(16, 21)

Page 15: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

16

21

1 2

3

4 5 6

7

δ

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

δ = min(16, 21)

Page 16: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

Closest Pair of Points

Def. Let si be the point in the 2δ-strip, with the ith smallest y-coordinate.

Claim. If |i – j| ≥ 12, then the distance between si and sj is at least δ.

Proof.   No two points lie in same ½δ-by-½δ box.   Two points at least 2 rows apart

have distance ≥ 2(½δ). ▪

Fact. Still true if we replace 12 with 7.

δ

27

29 30

31

28

26

25

δ

½δ

2 rows ½δ

½δ

39

i

j

Page 17: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

Closest Pair Algorithm

Closest-Pair(p1, …, pn) { Compute separation line L such that half the points are on one side and half on the other side.

δ1 = Closest-Pair(left half) δ2 = Closest-Pair(right half) δ = min(δ1, δ2)

Delete all points further than δ from separation line L

Sort remaining points by y-coordinate.

Scan points in y-order and compare distance between each point and next 11 neighbors. If any of these distances is less than δ, update δ.

return δ. }

O(n log n)

2T(n / 2)

O(n)

O(n log n)

O(n)

Page 18: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

Closest Pair of Points: Analysis

Running time.

Q.  Can we achieve O(n log n)?

A. Yes. Don't sort points in strip from scratch each time.   Sort entire point set by x-coordinate only once   Each recursive call takes as input a set of points sorted by

x coordinates and returns the same points sorted by y coordinate (together with the closest pair)

  Create new y-sorted list by merging two output from recursive calls

T(n) ≤ 2T n /2( ) + O(n) ⇒ T(n) = O(n logn)

Totaltime(n) =O(n log(n)) + T(n)

Page 19: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

Divide and Conquer in low-dimensional geometry

  Powerful technique for low-dimensional geometric problems   Intuition: points in different parts of the plane don’t

interfere too much   Example: convex hull in O(n log (n)) time a la MergeSort

1.  Convex-Hull(left-half) 2.  Convex-Hull(right-half) 3.  Merge (see Cormen et al., Chap 33)

T(n/2) T(n/2)

Θ(n)

Page 20: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Matrix multiplication

Input: A = [aij], B = [bij]. Output: C = [cij] = A⋅ B. i, j = 1, 2,… , n.

Page 21: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

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

Running time = Θ(n3)

Page 22: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Divide-and-conquer algorithm

n£n matrix = 2£2 matrix of (n/2)£(n/2) submatrices: IDEA:

C = A ⋅ B r = ae + bg s = af + bh t = ce + dh u = cf + dg

8 mults of (n/2) £(n/2) submatrices 4 adds of (n/2) £(n/2) submatrices ^

recursive

Page 23: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Analysis of D&C algorithm

nlogba = nlog28 = n3 ⇒ CASE 1 ⇒ T(n) = Θ(n3).

No better than the ordinary algorithm.

# submatrices submatrix size

work adding submatrices

T(n) = 8 T(n/2) + Θ(n2)

Page 24: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

7 mults, 18 adds/subs. Note: No reliance on commutativity of multiplication!

Strassen’s idea • Multiply 2£2 matrices with only 7 recursive mults.

P1 = a ⋅ ( f – h) P2 = (a + b) ⋅ h P3 = (c + d) ⋅ e P4 = d ⋅ (g – e) P5 = (a + d) ⋅ (e + h) P6 = (b – d) ⋅ (g + h) P7 = (a – c) ⋅ (e + f )

r = P5 + P4 – P2 + P6 s = P1 + P2 t = P3 + P4 u = P5 + P1 – P3 – P7

Page 25: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

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) P2 = (a + b) ⋅ h P3 = (c + d) ⋅ e P4 = d ⋅ (g – e) P5 = (a + d) ⋅ (e + h) P6 = (b – d) ⋅ (g + h) P7 = (a – c) ⋅ (e + f )

r = P5 + P4 – P2 + P6 = (a + d) (e + h) + d (g – e) – (a + b) h + (b – d) (g + h) = ae + ah + de + dh + dg –de – ah – bh + bg + bh – dg – dh = ae + bg

Page 26: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

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.

T(n) = 7 T(n/2) + Θ(n2)

Page 27: Algorithm Design and Analysis - Penn State College of ...ads22/courses/cse565/F08/www/lec-notes/CSE565-F08-Lec-13.pdfA. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

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).

Best to date (of theoretical interest only): Θ(n2.376).

• 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.