divide and conquer - mcgill...

69
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. CLRS 4.3

Upload: phungminh

Post on 30-Mar-2018

226 views

Category:

Documents


3 download

TRANSCRIPT

1

Chapter 5 Divide and Conquer

Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.

CLRS 4.3

2

Divide-and-Conquer

Divide-and-conquer. ■ Break up problem into several parts. ■ Solve each part recursively. ■ Combine solutions to sub-problems into overall solution.

Most common usage. ■ Break up problem of size n into two equal parts of size ½n. ■ Solve two parts recursively. ■ Combine two solutions into overall solution in linear time.

Consequence. ■ Brute force: n2. ■ Divide-and-conquer: n log n. Divide et impera.

Veni, vidi, vici. - Julius Caesar

5.1 Mergesort

Sorting. Given n elements, rearrange in ascending order.

4

Obvious sorting applications. List files in a directory. Organize an MP3 library. List names in a phone book. Display Google PageRank results.

Problems become easier once sorted. Find the median. Find the closest pair. Binary search in a database. Identify statistical outliers. Find duplicates in a mailing list.

Non-obvious sorting applications. Data compression. Computer graphics. Interval scheduling. Computational biology. Minimum spanning tree. Supply chain management. Simulate a system of particles. Book recommendations on Amazon. Load balancing on a parallel computer.. . .

Sorting

5

Mergesort

Mergesort. ■ Divide array into two halves. ■ Recursively sort each half. ■ Merge two halves to make sorted whole.

merge

sort

divide

A L G O R I T H M S

A L G O R I T H M S

A G L O R H I M S T

A G H I L M O R S T

Jon von Neumann (1945)

O(n)

2T(n/2)

O(1)

6

Merging

Merging. Combine two pre-sorted lists into a sorted whole.

How to merge efficiently? ■ Linear number of comparisons. ■ Use temporary array.

Challenge for the bored. In-place merge. [Kronrod, 1969]

A G L O R H I M S T

A G H I

using only a constant amount of extra storage

7

auxiliary array

smallest smallest

A G L O R H I M S T

Merge. ■ Keep track of smallest element in each sorted half. ■ Insert smallest of two elements into auxiliary array. ■ Repeat until done.

A

Merging

8

auxiliary array

smallest smallest

A G L O R H I M S T

A

Merge. ■ Keep track of smallest element in each sorted half. ■ Insert smallest of two elements into auxiliary array. ■ Repeat until done.

G

Merging

9

auxiliary array

smallest smallest

A G L O R H I M S T

A G

Merge. ■ Keep track of smallest element in each sorted half. ■ Insert smallest of two elements into auxiliary array. ■ Repeat until done.

H

Merging

10

auxiliary array

smallest smallest

A G L O R H I M S T

A G H

Merge. ■ Keep track of smallest element in each sorted half. ■ Insert smallest of two elements into auxiliary array. ■ Repeat until done.

I

Merging

11

auxiliary array

smallest smallest

A G L O R H I M S T

A G H I

Merge. ■ Keep track of smallest element in each sorted half. ■ Insert smallest of two elements into auxiliary array. ■ Repeat until done.

L

Merging

12

auxiliary array

smallest smallest

A G L O R H I M S T

A G H I L

Merge. ■ Keep track of smallest element in each sorted half. ■ Insert smallest of two elements into auxiliary array. ■ Repeat until done.

M

Merging

13

auxiliary array

smallest smallest

A G L O R H I M S T

A G H I L M

Merge. ■ Keep track of smallest element in each sorted half. ■ Insert smallest of two elements into auxiliary array. ■ Repeat until done.

O

Merging

14

auxiliary array

smallest smallest

A G L O R H I M S T

A G H I L M O

Merge. ■ Keep track of smallest element in each sorted half. ■ Insert smallest of two elements into auxiliary array. ■ Repeat until done.

R

Merging

15

auxiliary array

first half exhausted smallest

A G L O R H I M S T

A G H I L M O R

Merge. ■ Keep track of smallest element in each sorted half. ■ Insert smallest of two elements into auxiliary array. ■ Repeat until done.

S

Merging

16

auxiliary array

first half exhausted smallest

A G L O R H I M S T

A G H I L M O R S

Merge. ■ Keep track of smallest element in each sorted half. ■ Insert smallest of two elements into auxiliary array. ■ Repeat until done.

T

Merging

17

auxiliary array

first half exhausted

second halfexhausted

A G L O R H I M S T

A G H I L M O R S T

Merge. ■ Keep track of smallest element in each sorted half. ■ Insert smallest of two elements into auxiliary array. ■ Repeat until done.

Merging

Def. T(n) = number of comparisons to mergesort an input of size n.

Mergesort recurrence.

Solution. T(n) ∈ O(n log2 n).

Assorted proofs. We describe several ways to prove this recurrence. Initially we assume n is a power of 2 and replace ≤ with =.

18

A Useful Recurrence Relation

19

Proof by Recursion Tree

T(n)

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

T(n/4)T(n/4)T(n/4) T(n/4)

T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2)

n

T(n / 2k)

2(n/2)

4(n/4)

2k (n / 2k)

n/2 (2)

. . .

. . .log2n

n log2n

Claim. If T(n) satisfies this recurrence, then T(n) = n log2 n.

Pf. (by induction on n) ■ Base case: n = 1. ■ Inductive hypothesis: T(n) = n log2 n. ■ Goal: show that T(2n) = 2n log2 (2n).

20

Proof by Induction

assumes n is a power of 2

Claim. If T(n) satisfies the following recurrence, then T(n) ≤ n ⎡lg n⎤.

Pf. (by induction on n) ■ Base case: n = 1. T(1) = 0 = 1⎡lg 1⎤. ■ Define n1 = ⎣n / 2⎦ , n2 = ⎡n / 2⎤. ■ Induction step: Let n≥2, assume true for 1, 2, ... , n–1.

21

Analysis of Mergesort Recurrence

log2n

5.4 Closest Pair of Points

23

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. O(n log n) easy if points are on a line.

Assumption. No two points have same x coordinate.

to make presentation cleaner

fast closest pair inspired fast algorithms for these problems

24

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

L

Closest Pair of Points

25

Algorithm. ■ Divide: draw vertical line L so that roughly ½n points on each side. ■ Conquer: find closest pair in each side recursively.

12

21

L

Closest Pair of Points

26

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.

12

218

L

seems like Θ(n2)

Closest Pair of Points

27

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

12

21

δ = min(12, 21)

L

Closest Pair of Points

28

Find closest pair with one point in each side, assuming that distance < δ. ■ Observation: only need to consider points within δ of line L.

12

21

δ

L

δ = min(12, 21)

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.

29

12

21

1

2

3

45

6

7L

δ = min(12, 21)

δ

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. ■ Only check distances of those within 11 positions in sorted list!

30

12

21

1

2

3

45

6

7L

δ = min(12, 21)

δ

Closest Pair of Points

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

Claim. If |i – j| ≥ 12, then the distance between si and sj is at least δ. Pf. ■ 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.

31δ

27

2930

31

28

26

25

δ

½δ

2 rows½δ

½δ

39

i

j

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

Closest Pair of Points

32

Smallest-Dist(p1, …, pn) {

if n=2 then return dist(p1,p2)

Compute separation line L such that half the points are on one side and half on the other side.

δ’ = Smallest-Dist(left half) δ’’ = Smallest-Dist(right half) δ = min(δ’,δ’’)

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

Closest Pair of Points

33

Closest-Pair(p1, …, pn) {

if n=2 then return dist(p1,p2),p1,p2

Compute separation line L such that half the points are on one side and half on the other side.

δ’,p’,q’ = Closest-Pair(left half) δ’’,p’’,q’’ = Closest-Pair(right half) δ, p, q = min(δ’, δ’’)(p’,q’,p’’,q’’)

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 neighbours. If any of these distances is less than δ, update δ,p,q.

return δ,p,q. }

O(n log n)

2T(n / 2)

O(n)

O(n log n)

O(n)

Closest Pair of Points

34

Closest Pair of Points: Analysis

Running time.

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

A. Yes. First sort all points according to x coordinate before algo. Don’t sort points in strip from scratch each time. ■ Each recursion returns a list: all points sorted by y coordinate. ■ Sort by merging two pre-sorted lists.

Matrix Multiplication

36

Matrix multiplication. Given two n-by-n matrices A and B, compute C = AB.

Brute force. Θ(n3) arithmetic operations.

Fundamental question. Can we improve upon brute force?

Matrix Multiplication

Divide-and-conquer. ■ Divide: partition A and B into ½n-by-½n blocks. ■ Conquer: multiply 8 ½n-by-½n recursively. ■ Combine: add appropriate products using 4 matrix additions.

37

Matrix Multiplication: Warmup

Key idea. multiply 2-by-2 block matrices with only ⑦ multiplications.

■ 7 multiplications. ■ 18 = 10 + 8 additions (or subtractions).

38

Matrix Multiplication: Key Idea

log2n

39

Strassen: Recursion Tree

n2

7(n2/22)

49(n2/42)

7k (n2/ 4k)

7 lg n (n2/ 4 lg n)

=7 lg n = n lg 7

. . .

. . .

T(n)

T(n/2)

T(n/4)

T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2)

T(n / 2k)

T(n/2)

T(n/4) T(n/4)T(n/4)

T(n/2)

T(n/4) T(n/4)T(n/4)

. . .

. . .

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

774

74

74

773

T(n/4)T(n/4)T(n/4)T(n/4)T(n/4)T(n/4)

≈ .n2 n2 =n2

40

Fast Matrix Multiplication

Fast matrix multiplication. (Strassen, 1969) ■ Divide: partition A and B into ½n-by-½n blocks. ■ Compute: 14 ½n-by-½n matrices via 10 matrix additions. ■ Conquer: multiply 7 ½n-by-½n matrices recursively. ■ Combine: 7 products into 4 terms using 18 matrix additions.

Analysis. ■ Assume n is a power of 2. ■ T(n) = # arithmetic operations.

∈ ∈

41

Fast Matrix Multiplication in Practice

Implementation issues. ■ Sparsity. ■ Caching effects. ■ Numerical stability. ■ Odd matrix dimensions. ■ Crossover to classical algorithm around n = 128.

Common misperception: "Strassen is only a theoretical curiosity." ■ Advanced Computation Group at Apple Computer reports 8x speedup

on G4 Velocity Engine when n ~ 2,500. ■ Range of instances where it's useful is a subject of controversy.

Remark. Can "Strassenize" Ax=b, determinant, eigenvalues, and other matrix ops.

Q. Two 70-by-70 matrices with only 143,640 scalar multiplications? A. Yes! [Pan, 1980]

42

Fast Matrix Multiplication in Theory

Q. Multiply two 2-by-2 matrices with only 7 scalar multiplications? A. Yes! [Strassen, 1969] Θ

� (n log2 7 ) = O(n 2.81)∈

� (n log2 6) = O(n 2.59 )Θ ∈

� (n log3 21) = O(n 2.77 )Θ ∈

� (n log70 143640 ) = O(n 2.80 )Θ ∈

Q. Multiply two 2-by-2 matrices with only 6 scalar multiplications? A. Impossible. [Hopcroft and Kerr, 1971]

Q. Two 3-by-3 matrices with only 21 scalar multiplications? A. Also impossible.

Decimal wars. ■ December, 1979: O(n2.521813). ■ January, 1980: O(n2.521801).

43

Fast Matrix Multiplication in Theory

Best known. O(n2.376) [Coppersmith-Winograd, 1987-2010.]

In 2010, Andrew Stothers gave an improvement to the algorithm O(n2.374). In 2011, Virginia Williams combined a mathematical short-cut from Stothers' paper with her own insights and automated optimization on computers, improving the bound O(n2.3728642). In 2014, François Le Gall simplified the methods of Williams and obtained an improved bound of O(n2.3728639).

Conjecture. O(n2+ε) for any ε > 0.

Caveat. Theoretical improvements to Strassen are progressively less practical (hidden constant gets worse).

CLRS 4.3 Master Theorem

Master Theorem from CLRS 4.3

45

a = (constant) number of sub-instances,b = (constant) size ratio of sub-instances,𝑓(n) = time used for dividing and recombining.

T(n) = aT(n/b) + f(n)

T(n/b2)

. . .T(n / bk)

46

Proof by Recursion Tree

f(n)

af(n/b)

a2 f(n/b2)

ak f(n/bk)

alogb n T(2)

. . .

T(n)

T(n/b)

T(n/b2) T(n/b2)

T(2)

T(n/b2)

T(n/b)

T(n/b2) T(n/b2)T(n/b2)

T(n/b)

T(n/b2) T(n/b2)

T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2)

T(n / bk)

. . .

T(n / bk) T(n / bk) T(n / bk)T(n / bk)T(n / bk)T(n / bk)T(n / bk)

logbn. . .

= T(2) nlogb a

T(n) = ∑ ak f(n/bk)

T(n) = aT(n/b) + f(n)

Solution: T(n) ∈ 𝛩(𝑓(n))

Solution: T(n) ∈ 𝛩(nlogb a)

47

isCase 3: 𝑓(n) ∈ 𝛺(nL) for some constant L > logb aand 𝑓(n) satisfies the regularity condition a𝑓(n/b) ≤ c𝑓(n) for some c<1 and all large n.

Case 1: 𝑓(n) ∈ O(nL) for some constant L < logb a.

Solution: T(n) ∈ 𝛩(nlogb a logk+1 n)

Case 2: 𝑓(n) ∈ 𝛩(nlogb a logk n), for some k ≥ 0.

T(n) = aT(n/b) + f(n)

48

is

Master Theorem

Case 1: 𝑓(n) ∈ O(nL) for some constant L < logb a.

Solution: T(n) ∈ 𝛩(nlogb a)

T(n) = aT(n/b) + f(n)

Solution: T(n) ∈ 𝛩(nlogb a)

49

Master Theorem

Case 1: 𝑓(n) ∈ O(nL) for some constant L < logb a.

T(n) = 5T(n/2) + 𝛩(n2)

Compare nlog2 5 vs. n2.

Since 2 < log2 5 use Case 1

Solution: T(n) ∈ 𝛩(nlog2 5)

50

Master Theorem

Solution: T(n) ∈ 𝛩(nlogb a log n)

Simple Case 2: 𝑓(n) ∈ 𝛩(nlogb a).

T(n) = aT(n/b) + f(n)

51

Master Theorem

Solution: T(n) ∈ 𝛩(nlogb a logk+1 n)

Case 2: 𝑓(n) ∈ 𝛩(nlogb a logk n), for some k ≥ 0.

T(n) = aT(n/b) + f(n)

T(n) = 27T(n/3) + 𝛩(n3 log n)

Compare nlog3 27 vs. n3.

Since 3 = log3 27 use Case 2

Solution: T(n) ∈ 𝛩(n3 log2 n)52

Master Theorem

Solution: T(n) ∈ 𝛩(nlogb a logk+1 n)

Case 2: 𝑓(n) ∈ 𝛩(nlogb a logk n), for some k ≥ 0.

53

Master Theorem

Solution: T(n) ∈ 𝛩(𝑓(n))

Case 3: 𝑓(n) ∈ 𝛺(nL) for some constant L > logb aand 𝑓(n) satisfies the regularity condition a𝑓(n/b) ≤ c𝑓(n) for some c<1 and all large n.

T(n) = aT(n/b) + f(n)

Solution: T(n) ∈ 𝛩(𝑓(n))

Case 3: 𝑓(n) ∈ 𝛺(nL) for some constant L > logb aand 𝑓(n) satisfies the regularity condition a𝑓(n/b) ≤ c𝑓(n) for some c<1 and all large n.

54

Master Theorem

55

Master Theorem

Solution: T(n) ∈ 𝛩(𝑓(n))

Case 3: 𝑓(n) ∈ 𝛺(nL) for some constant L > logb aand 𝑓(n) satisfies the regularity condition a𝑓(n/b) ≤ c𝑓(n) for some c<1 and all large n.

T(n) = 5T(n/2) + 𝛩(n3)

Compare nlog2 5 vs. n3.

Since 3 > log2 5 use Case 3

a𝑓(n/b) = 5(n/2)3 = 5/8 n3 ≤ cn3, for c = 5/8

Solution: T(n) ∈ 𝛩(n3)

56

Master Theorem

T(n) = 27T(n/3) + 𝛩(n3/log n)

Compare nlog3 27 vs. n3.

Since 3 = log3 27 use Case 2

but n3/log n ∈ not 𝛩(n3 log k n) for k ≥ 0

Cannot use Master Method.

Beyond the Master Theorem

Median Finding

59

Median Finding. Given n distinct numbers a1, …, an, find i such that

|{ j : aj<ai }| =⎣n-1 / 2⎦ and |{ j : aj>ai }| =⎡n-1 / 2⎤.

Median Finding

22 31 44 7 12 19 20 35 3 40 27

3 7 12 19 20 22 27 31 35 40 44

9 4 5 6 7 1 11 2 8 10 3

60

Selection. Given n distinct numbers a1, …, an, and index k, find i such that

|{ j : aj<ai }| = k-1 and |{ j : aj>ai }| = n-k.

k=4

Selection

22 31 44 7 12 19 20 35 3 40 27

3 7 12 19 20 22 27 31 35 40 44

9 4 5 6 7 1 11 2 8 10 3

Median( a1, …, an ) = Selection( a1, …, an,⎣n+1 / 2⎦)

Algorithm partition(A, start, stop)

Input: An array A, indices start and stop.

Output: Returns an index j and rearranges the elements of Asuch that for all i<j, A[i] ! A[j] andfor all k>i, A[k] " A[j].

pivot # A[stop]

left # start

right # stop - 1

while left ! right do

while left ! right and A[left] ! pivot) do left # left + 1

while (left ! right and A[right] " pivot) do right # right -1

if (left < right ) then exchange A[left] $ A[right]

exchange A[stop] $ A[left]

return left

Partition (from QuickSort)

j

Example of execution of partition

A = [ 6 3 7 3 2 5 7 5 ] pivot = 5

A = [ 6 3 7 3 2 5 7 5 ] swap 6, 2

A = [ 2 3 7 3 6 5 7 5 ]

A = [ 2 3 7 3 6 5 7 5 ] swap 7,3

A = [ 2 3 3 7 6 5 7 5 ]

A = [ 2 3 3 7 6 5 7 5 ] swap 7,pivot

A = [ 2 3 3 5 6 5 7 7 ]!5 "5

Partition

Partition

63

Selection. Given n distinct numbers a1, …, an, and index k, find i such that

|{ j : aj<ai }| = k-1 and |{ j : aj>ai }| = n-k.

k k-M

Selection from Median

M

Median

Selection( a1, …, a⎣n-1 / 2⎦,k )Selection( a⎣n+3 / 2⎦, …, an, k -⎣n+1 / 2⎦ )

k =⎣n+1 / 2⎦ return k

64

Selection. Given n distinct numbers a1, …, an, and index k, find i such that

|{ j : aj<ai }| = k-1 and |{ j : aj>ai }| = n-k.

m

Selection

Pseudo- Median

Selection( a1, …, am-1,k )Selection( am+1, …, an, k - m )

Partition k = m return k

65

Selection. Given n distinct numbers a1, …, an, and index k, find i such that

|{ j : aj<ai }| = k-1 and |{ j : aj>ai }| = n-k.

3n/10——————————7n/10

Selection

Selection. Given n distinct numbers a1, …, an, and index k, find i such that

|{ j : aj<ai }| = k-1 and |{ j : aj>ai }| = n-k.

T(n) ≤ T(⎡n/5⎤) + Max{ T(⎣3n/10⎦… T(⎡7n/10⎤) } + 𝛩(n)

Solution: T(n) ∈ 𝛩(n)

66

Selection

Selection. Given n distinct numbers a1, …, an, and index k, find i such that

|{ j : aj<ai }| = k-1 and |{ j : aj>ai }| = n-k.

Assuming T(i) ≤ d i for 1 ≤ i ≤ n, 𝛩(n) ≤ cn T(n+1) ≤ T(n+1 /5) + T(7(n+1)/10) + c(n+1) ≤ d(n+1)/5 + 7d(n+1)/10 + c(n+1) = (2d+7d+10c)/10 (n+1) = (9d+10c)/10 (n+1) ≤ d (n+1) as long as (9d+10c)/10 ≤ d, or equivalently 10c ≤ d.

T(n) ≤ T( n/5 ) + T( 7n/10 ) + 𝛩(n) Solution: T(n) ∈ 𝛩(n)

67

Selection

Selection. Given n distinct numbers a1, …, an, and index k, find i such that

|{ j : aj<ai }| = k-1 and |{ j : aj>ai }| = n-k.

example: d=10c, Assuming T(i) ≤ 10c i for 1 ≤ i ≤ n, 𝛩(n) ≤ cn T(n+1) ≤ T(n+1 /5) + T(7/10 (n+1)) + c(n+1) ≤ 10c/5 (n+1) + 7•10c/10 (n+1) + c(n+1) = (2c+7c+c)(n+1) = 10c (n+1)

68

Selection

T(n) ≤ T( n/5 ) + T( 7n/10 ) + 𝛩(n) Solution: T(n) ∈ 𝛩(n)

69

Chapter 5 Divide and Conquer

Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.

CLRS 4.3