+ All Categories
Home > Documents > 4.Divide-and-Conquer

4.Divide-and-Conquer

Date post: 01-Jan-2016
Category:
Upload: oprah-patterson
View: 15 times
Download: 0 times
Share this document with a friend
Description:
4.Divide-and-Conquer. Hsu, Lih-Hsing. Instruction. Divide Conquer Combine. Recurrences --. if n=1 , if n > 1. Substitution method Recursion-tree method Master method - PowerPoint PPT Presentation
50
4.Divide-and-Conquer Hsu, Lih-Hsing
Transcript

4.Divide-and-Conquer

Hsu, Lih-Hsing

Chapter 4 P.2

Computer Theory Lab.

Instruction Divide Conquer Combine

Chapter 4 P.3

Computer Theory Lab.

Recurrences -- if n=1 ,

if n > 1. Substitution method Recursion-tree method Master method

T(n)=aT(n /b)+f (n), where a≧1, b > 1, and f (n) is a given function.

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

)()2/(2

)1()(

nnTnT

Chapter 4 P.4

Computer Theory Lab.

Technicalities in recurrences Boundary conditions represent anoth

er class of details that we typically ignore.

When we state and solve recurrences, we often omit floors, ceilings, and boundary conditions.

Chapter 4 P.5

Computer Theory Lab.

4.1 The maximum-subarray problem

Chapter 4 P.6

Computer Theory Lab.

Chapter 4 P.7

Computer Theory Lab.

A brute-force solution Take time.Can we do better?

A transformation Maximum subarray

)( 2n

Chapter 4 P.8

Computer Theory Lab.

Chapter 4 P.9

Computer Theory Lab.

FIND-MAX-CROSSING-SUBARRAY(A,low,mid,high)

1 left-sun = –∞2 sum = 03 for I = mid downto low4 sum = sum + A[i]5 if sum > left-sum6 left-sum = sum7 max-left = I8 right-sun = –∞

Chapter 4 P.10

Computer Theory Lab.

9 sum = 010 for j = mid + 1 to high11 sum = sum + A[j]12 if sum > right-sum13 right-sum = sum14 max-right = j15 return(max-left,max-right,left-sum

+ right-sum)

Chapter 4 P.11

Computer Theory Lab.

FIND-MAXIMUM-SUBARRAY(A,low,high)1 if high == low //base case:only one elem

ent2 return(low,high,A[low])3 else mid =4 (left-low,left-high,left-sum) = FIND-MAXI

MUM-SUBBARY(A,low,mid)5 (right-low,right-high,right-sum)= FIND-

MAXIMUM-SUBBARY (A,mid+1,high)

2/highlow

Chapter 4 P.12

Computer Theory Lab.

6 (cross-low,cross-high,cross-sum) = FIND-MAX-CROSSING-SUBARRAY (A,low,mid,high)

7 if left-sum≧right-sum and left-sum ≧ cross-sum

8 return(left-low,left-high,left-sum)9 elseif right-sum≧left-sum and right-sum

≧ cross-sum10 return(right-low,right-high,right-sum)11 else return(cross-low,cross-high,cross-su

m)

Chapter 4 P.13

Computer Theory Lab.

Analyzing the divide-and conquer algorithm

if n=1 if n > 1

)()2/(2

)1()(

nnTnT

Chapter 4 P.14

Computer Theory Lab.

4.2 Strassen`s algorithm for matrix multiplication If A = (aij) and B = (bij) are square n×n

matrices. C = A‧B Cij =

n

kkjik ba

1

Chapter 4 P.15

Computer Theory Lab.

SQUARE-MATRIX-MULTIPLY(A, B)1 n = A.rows2 let C be a new n×n matrix3 for i = 1 to n4 for j = 1 to n5 cij=06 for k = 1 to n7 cij = cij + aik‧bkj

8 return C

Chapter 4 P.16

Computer Theory Lab.

A simple divide-and-conquer algorithm , ,

= ‧ C11 = A11‧B11 + A12‧B21 ,

C12 = A11‧B12 + A12‧B22 ,C21 = A21‧B11 + A22‧B21 ,C22 = A21‧B12 + A22‧B22 。

2221

1211

AA

AA

2221

1211

BB

BBB

2221

1211

AA

AAA

2221

1211

CC

CCC

2221

1211

CC

CC

2221

1211

BB

BB

Chapter 4 P.17

Computer Theory Lab.

SQUARE-MATRIX-MULTIPLY-RECURSIVE(A, B)1 n=A.rows2 let C be a new n×n matrix3 if n == 14 c11=a11‧b115 else partition A, B, and C as in equations6 c11=SQUARE-MATRIX-MULTIPLY-RECURSIVE

(A11, b11) + SQUARE-MATRIX-MULTIPLY- RECURSIVE (A12, B21)

7 c12=SQUARE-MATRIX-MULTIPLY-RECURSIVE (A11, b12) + SQUARE-MATRIX-MULTIP

LY- RECURSIVE (A12, B22)

Chapter 4 P.18

Computer Theory Lab.

8 c21=SQUARE-MATRIX-MULTIPLY-RECURSIVE (A21, b11) + SQUARE-MATRIX-M

ULTIPLY- RECURSIVE (A22, B21)9 c22=SQUARE-MATRIX-MULTIPLY-RECURSIVE

(A21, b12) + SQUARE-MATRIX-MULTIPLY- RECURSIVE (A22, B22)

10 return C

if n = 1, if n > 1.

)()2/(8

)1()( 2nnTnT

Chapter 4 P.19

Computer Theory Lab.

Strassen`s method(1/2)1 Divide the input matrices A and B and out

put matrix C into n/2×n/2 submatrices. This step takes time by index calculation.

2 Creat 10 matrices S1, S2,…,S10, each of which is n/2×n/2 and is the sum or difference of two matrices created step 1. We can creat all 10 matrices in time.

)1(

)( 2n

Chapter 4 P.20

Computer Theory Lab.

Strassen`s method(2/2)3 Using the submatrices created in step 1 a

nd the 10 matrices created in step 2, recursively compute seven matrix products P1,P2,…,P7. Each matrix Pi is n/2 × n/2.

4 Compute the desired submatrices C11,C12,C21,C22 of the result matrix C by adding and subtracting various combinations of the Pi matrices. We can compute all four submatrices in time.

)( 2n

Chapter 4 P.21

Computer Theory Lab.

Strassen`s algorithm(1/7) if n = 1, if n > 1.

)()2/(7

)1()( 2nnTnT

Chapter 4 P.22

Computer Theory Lab.

Strassen`s algorithm(2/7) S1 = B12 - B22 S2 = A11 + A12 S3 = A21 + A22 S4 = B21 - B11 S5 = A11 + A22 S6 = B11 + B22 S7 = A12 - A22 S8 = B21 + B22 S9 = A11 - A21 S10 = B11 + B12

Chapter 4 P.23

Computer Theory Lab.

Strassen`s algorithm(3/7) P1 = A11 . S1 = A11 . B12 - A11 . B22 ,

P2 = S2 . B22 = A11 . B22 - A12 . B22 ,P3 = S3 . B11 = A21 . B11 - A22 . B11 ,P4 = A22 . S4 = A22 . B21 - A22 . B11 ,P5=S5 . S6=A11 . B11 + A11 . B22 + A22 . B11 +

A22 . B22

P6=S7 . S8=A12 . B21 + A12 . B22 - A22 . B21 -A22 . B22

P7=S9 . S10=A11 . B11 + A11 . B12 - A21 . B11 -A21 . B12

Chapter 4 P.24

Computer Theory Lab.

Strassen`s algorithm(4/7) We start with

C11 = P5 + P4 - P2 + P6

A11 . B11 + A11 . B22 + A22 . B11 + A22 . B22

- A22 . B11 + A22 . B21

- A11 . B22 - A12 . B22

- A22 . B22 - A22 . B21 + A12 . B22 + A12 . B21

___________________________________________________________________________________________

A11 . B11 + A12 . B21

Chapter 4 P.25

Computer Theory Lab.

Strassen`s algorithm(5/7) Similarly, we set

C12 = P1 + P2

A11 . B12 - A11 . B22 + A11 . B22 + A12 . B22

A11 . B12 + A12 . B22

Chapter 4 P.26

Computer Theory Lab.

Strassen`s algorithm(6/7) Setting

C21 = P3 + P4

A21 . B11 + A22 . B11

- A22 . B11 + A22 . B21

A21 . B11 + A22 . B21

Chapter 4 P.27

Computer Theory Lab.

Strassen`s algorithm(7/7) Finally, we set

C22 = P5 + P1 - P3 - P73

A11 . B11 + A11 . B22 + A22 . B11 + A22 . B22

- A11 . B22 + A11 . B12

- A22 . B11 - A21 . B11

- A11 . B11 - A11 . B12 + A21 . B11 + A21 .B12

A22 . B22 + A21 . B12

Chapter 4 P.28

Computer Theory Lab.

4.3 The substitution method : Mathematical induction The substitution method for

solving recurrence entails two steps:

1. Guess the form of the solution. 2. Use mathematical induction to find

the constants and show that the solution works.

Chapter 4 P.29

Computer Theory Lab.

Example

(We may omit the initial condition later.)

Guess  Assume

T n T n n

T

( ) ( / )

( )

2 2

1 1

)log()( nnOnT

T n c n n( / ) / log /2 2 2

Chapter 4 P.30

Computer Theory Lab.

Initial condition However,

T n c n n n cnn

n

cn n cn n cn n c .

( ) / log / ) log

log log log )

2( 2 22

2 1 (if

)(01log)1(1 cnT

4 2 2 4 T cn c( ) log ( )if

Chapter 4 P.31

Computer Theory Lab.

Making a good guess

We guess

Making guess provides loose upper bound and lower bound. Then improve the gap.

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

T n O n n( ) ( log )

Chapter 4 P.32

Computer Theory Lab.

Subtleties

Guess Assume

 However, assume

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

T n O n( ) ( )

T n cn( )

cncnncncnT 112/2/)(

dcnnT )(

)1 Choose(12

1)2/()2/()(

ddcndcn

dncdncnT

Chapter 4 P.33

Computer Theory Lab.

Exercise 4.3-6

Show that the solution to T(n) = 2T(n2 + 17) + n is O(n lg n)

Solution:

assume a > 0, b > 0, c > 0 and T(n) ≦ an lg n – blg n - c

T(n) ≦ 2[(n2 + 17)lg(

n2 +17) - blg(

n2 + 17)-c]+n

≦ (an + 34a)lg(n2 +17) - 2blg(

n2 +17) - 2c + n

≦ anlg(n2 +17) + anlg21/a + (34a-2b)lg(

n2 +17) - 2c

≦ anlg(n) 21/a+ (34a-2b)lg(n) - 2c

Chapter 4 P.34

Computer Theory Lab.

anlg(n) 21/a+ (34a-2b)lg(n) - 2c

n≧ n2 +17,n≧ 34

n≧ (n2 +17) 21/a ∵, 21/2≦ 1.5∴ n ≧ 12

34a-2b≦ -b,b ≧ 34a

c > 0,-c > -2c

T(n) ≦ anlgn - blgn - c,T(n) ≦ anlgn

T(n) = O(nlgn)

Chapter 4 P.35

Computer Theory Lab.

Avoiding pitfalls

Assume Hence  

(WRONG!) You cannot find such a c.

T n T n n

T

( ) ( / )

( )

2 2

1 1

T n O n( ) ( )

T n cn( )

constant) a is Since(

)()2/(2)(

c

nOncnnncnT

Chapter 4 P.36

Computer Theory Lab.

Changing variables nnTnT lg)(2)(

Let m nlg .

T T mm m( ) ( )/2 2 2 2

Then S m S m m( ) ( / ) 2 2 .

S m O m m

T n T S m O m m

O n n

m

( ) ( lg )

( ) ( ) ( ) ( lg )

(lg lg lg )

2

Chapter 4 P.37

Computer Theory Lab.

4.4 the Recursion-tree method

)()4/(3)( 2nnTnT

Chapter 4 P.38

Computer Theory Lab.

Chapter 4 P.39

Computer Theory Lab.

).(1)16/3(

1)16/3(

16

3

)(16

3...

16

3

16

3)(

3log2log

1log

0

3log2

3log21log

22

22

4

4

4

4

4

4

ncn

ncn

ncncncncnnT

n

n

i

i

n

The cost of the entire tree

Chapter 4 P.40

Computer Theory Lab.

)(

)(13

16

)16/3(1

1

16

3

16

3)(

2

3log2

3log2

3log2

0

1log

0

3log2

4

4

4

4

4

nO

ncn

ncn

ncn

ncnnT

i

i

n

i

i

Chapter 4 P.41

Computer Theory Lab.

substitution methodWe want to Show that T(n) ≤ dn2 for some

constant d > 0. using the same constant c > 0 as before, we have

Where the last step holds as long as d (16/13)c.

,

16

3

)4/(3

4/3

)4/(3)(

2

22

22

22

2

dn     

cndn     

cnnd     

cnnd     

cnnT nT

Chapter 4 P.42

Computer Theory Lab.

cnnTnTnT )3/2()3/()(

Chapter 4 P.43

Computer Theory Lab.

substitution method

,lg

)3/23(lglg

)2lg)3/2(3lg)3/2(3lg)3/((lg

))2/3lg()3/2(3lg)3/((lg

))2/3lg()3/2(lg)3/2(()3lg)3/(lg)3/((

)3/2lg()3/2()3/lg()3/(

)3/2()3/()(

ndn     

cndnndn     

cnnnndndn     

cnnndndn     

cnndnndndnnd     

cnnndnnd     

cnnTnTnT

as long as d c/(lg3 – (2/3)).

Chapter 4 P.44

Computer Theory Lab.

4.5 The master method Theorem 4.1 (((MMMaaasssttteeerrr ttthhheeeooorrreeemmm)))

Let a 1 and b 1 be constants, let f n( ) be a function, and T n( )

be defined on the nonnegative integers by the recurrence T n aT n b f n( ) ( / ) ( )

where we interpret n b/ mean either n b/ or n b/ .

1. If f n O n b a( ) ( )log for some constant 0 , then T n n b a( ) ( )log .

2. If )()( log abnnf , then T n n nb a( ) ( log )log .

3. If )()( log abnnf for some constant 0 , and if )()/( ncfbnaf for

some constant c 1 and all sufficiently large n, then T n f n( ) ( ( )) .

Proof. (In section 4.6 by recursive tree)

Chapter 4 P.45

Computer Theory Lab.

T n T n n( ) ( / ) 9 3

a b f n n

n n f n O n

9 3

3 39 2 9 1

, , ( )

, ( ) ( )log log

Case T n n1 2 ( ) ( )

T n T n( ) ( / ) 2 3 1

a b f n

n n f n

1, 3 2 1

13 21 0

/ , ( )

( ),log /

Case T n n2 ( ) (log )

Chapter 4 P.46

Computer Theory Lab.

T n T n n n( ) ( / ) log 3 4

)()(),(

log)(,4,33log793.03log 44

nnfnOn

nnnfba

Case

af(n/b)= (n) (

n)

nn=cf(n)

c= n

T n n n

3

34 4

34

34

Check

for and sufficiently large

log log

,

( ) ( log )

Chapter 4 P.47

Computer Theory Lab.

Note that the three cases do not cover all the possibilities for f(n). There is a gap between cases 1 and 2 when f(n) is smaller than but not polynomially smaller. Similarly, there is a gap between cases 2 and 3 when f(n) is larger than but not polynomially larger.

abnlog

abnlog

Chapter 4 P.48

Computer Theory Lab.

The master method does not apply to the recurrence

even though it has the proper form: a = 2, b=2, f(n)= n lgn, and It might seem that case 3 should apply, since f(n)= n lgn is asymptotically larger than

The problem is that it is not polynomially larger.

,lg)2/(2)( nnnTnT

.log nn ab

.log nn ab

Chapter 4 P.49

Computer Theory Lab.

Exercise 4.3-9Solve the recurrence T(n)=3T( ) + logn by making a change of variables. Your solution should be asymptotically tight. Do not worry about whether values are integral.

n

Chapter 4 P.50

Computer Theory Lab.

Exercise 4.5-1Use the master method to give tight asympttotic bounds for the following recurrences.a. T(n) = 2T(n/4)+1.b. T(n) = 2T(n/4)+ .c. T(n) = 2T(n/4)+n.d. T(n) = 2T(n/4)+n2.

n


Recommended