45
1 Solving recurrences Master Theorem. Slide credit: David Luebke (Virginia)

Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

1

Solving recurrences Master Theorem.

Slide credit: David Luebke (Virginia)

Page 2: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

2

Review: Merge Sort

MergeSort(A, left, right)

if (left < right)

mid = floor((left + right) / 2);

MergeSort(A, left, mid);

MergeSort(A, mid+1, right);

Merge(A, left, mid, right);

// Merge() takes two sorted subarrays of A and

// merges them into a single sorted subarray of A.

// Code for this is in the book. It requires O(n)

// time, and *does* require allocating O(n) space

Page 3: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

3

Review: Analysis of Merge Sort

Statement Effort

So T(n) = Θ(1) when n = 1, and

2T(n/2) + Θ(n) when n > 1

This expression is a recurrence

MergeSort(A, left, right) T(n) if (left < right) Θ(1) mid = floor((left + right) / 2); Θ(1) MergeSort(A, left, mid); T(n/2) MergeSort(A, mid+1, right); T(n/2) Merge(A, left, mid, right); Θ(n)

Page 4: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

4

Recurrences

The expression:

is a recurrence. Recurrence: an equation that describes a function in

terms of its value on smaller functions

>+

=

=1

22

1

)(ncn

nT

nc

nT

Page 5: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

5

Recurrence Examples

>=

−+=

0

0

)1(

0)(

n

n

nscns

>−+=

=0)1(

00)(

nnsn

nns

>+

=

=1

22

1

)(nc

nT

nc

nT

>+

==

1

1

)(

ncnb

naT

nc

nT

Page 6: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

6

Solving Recurrences

Substitution method Iteration method Master method

Page 7: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

7

Solving Recurrences

The substitution method (CLR 4.1) A.k.a. the “making a good guess method” Guess the form of the answer, then use induction

to find the constants and show that solution works Examples:

o T(n) = 2T(n/2) + Θ(n) T(n) = Θ(n lg n)

o T(n) = 2T(n/2) + n ???

Page 8: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

8

Solving Recurrences

The substitution method (CLR 4.1) A.k.a. the “making a good guess method” Guess the form of the answer, then use induction

to find the constants and show that solution works Examples:

o T(n) = 2T(n/2) + Θ(n) T(n) = Θ(n lg n)

o T(n) = 2T(n/2) + n T(n) = Θ(n lg n)o T(n) = 2T(n/2 )+ 17) + n ???

Page 9: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

9

Solving Recurrences

The substitution method (CLR 4.1) A.k.a. the “making a good guess method” Guess the form of the answer, then use induction

to find the constants and show that solution works Examples:

o T(n) = 2T(n/2) + Θ(n) T(n) = Θ(n lg n)

o T(n) = 2T(n/2) + n T(n) = Θ(n lg n)o T(n) = 2T(n/2+ 17) + n Θ(n lg n)

Page 10: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

10

Solving Recurrences

Another option is what the book calls the “iteration method” Expand the recurrence Work some algebra to express as a summation Evaluate the summation

We will show several examples

Page 11: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

11

s(n) =

c + s(n-1)

c + c + s(n-2)

2c + s(n-2)

2c + c + s(n-3)

3c + s(n-3)

kc + s(n-k) = ck + s(n-k)

>−+=

=0)1(

00)(

nnsc

nns

Page 12: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

12

So far for n >= k we have s(n) = ck + s(n-k)

What if k = n? s(n) = cn + s(0) = cn

>−+=

=0)1(

00)(

nnsc

nns

Page 13: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

13

So far for n >= k we have s(n) = ck + s(n-k)

What if k = n? s(n) = cn + s(0) = cn

So

Thus in general s(n) = cn

>−+=

=0)1(

00)(

nnsc

nns

>−+=

=0)1(

00)(

nnsc

nns

Page 14: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

14

s(n)

= n + s(n-1)

= n + n-1 + s(n-2)

= n + n-1 + n-2 + s(n-3)

= n + n-1 + n-2 + n-3 + s(n-4)

= …

= n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k)

>−+=

=0)1(

00)(

nnsn

nns

Page 15: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

15

s(n)

= n + s(n-1)

= n + n-1 + s(n-2)

= n + n-1 + n-2 + s(n-3)

= n + n-1 + n-2 + n-3 + s(n-4)

= …

= n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k)

=

>−+=

=0)1(

00)(

nnsn

nns

)(1

knsin

kni

−+∑+−=

Page 16: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

16

So far for n >= k we have

>−+=

=0)1(

00)(

nnsn

nns

)(1

knsin

kni

−+∑+−=

Page 17: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

17

So far for n >= k we have

What if k = n?

>−+=

=0)1(

00)(

nnsn

nns

)(1

knsin

kni

−+∑+−=

Page 18: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

18

So far for n >= k we have

What if k = n?

>−+=

=0)1(

00)(

nnsn

nns

)(1

knsin

kni

−+∑+−=

2

10)0(

11

+=+=+ ∑∑==

nnisi

n

i

n

i

Page 19: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

19

So far for n >= k we have

What if k = n?

Thus in general

>−+=

=0)1(

00)(

nnsn

nns

)(1

knsin

kni

−+∑+−=

2

10)0(

11

+=+=+ ∑∑==

nnisi

n

i

n

i

2

1)(

+= nnns

Page 20: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

20

T(n) =

2T(n/2) + c

2(2T(n/2/2) + c) + c

22T(n/22) + 3c

22(2T(n/22/2) + c) + 3c

23T(n/23) + 4c + 3c

23T(n/23) + 7c

23(2T(n/23/2) + c) + 7c

24T(n/24) + 15c

2kT(n/2k) + (2k - 1)c

>+

== 1

22

1)( nc

nT

ncnT

Page 21: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

21

So far for n > 2k we have T(n) = 2kT(n/2k) + (2k - 1)c

What if k = lg n? T(n) = 2lg n T(n/2lg n) + (2lg n - 1)c

= n T(n/n) + (n - 1)c

= n T(1) + (n-1)c

= nc + (n-1)c = (2n - 1)c

>+

== 1

22

1)( nc

nT

ncnT

Page 22: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

22

Generalize previous result

More general recurrence than last one Previous example: a=b=2. New feature: result depends on whether a<b,

a=b or a>b !

>+

== 1

1)( ncn

b

naT

ncnT

Page 23: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

23

T(n) =

aT(n/b) + cn

a(aT(n/b/b) + cn/b) + cn

a2T(n/b2) + cna/b + cn

a2T(n/b2) + cn(a/b + 1)

a2(aT(n/b2/b) + cn/b2) + cn(a/b + 1)

a3T(n/b3) + cn(a2/b2) + cn(a/b + 1)

a3T(n/b3) + cn(a2/b2 + a/b + 1)

akT(n/bk) + cn(ak-1/bk-1 + ak-2/bk-2 + … + a2/b2 + a/b + 1)

>+

== 1

1)( ncn

b

naT

ncnT

Page 24: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

24

So we have T(n) = akT(n/bk) + cn(ak-1/bk-1 + ... + a2/b2 + a/b + 1)

For k = logb n n = bk

T(n) = akT(1) + cn(ak-1/bk-1 + ... + a2/b2 + a/b + 1)

= akc + cn(ak-1/bk-1 + ... + a2/b2 + a/b + 1)

= cak + cn(ak-1/bk-1 + ... + a2/b2 + a/b + 1)

= cnak /bk + cn(ak-1/bk-1 + ... + a2/b2 + a/b + 1)

= cn(ak/bk + ... + a2/b2 + a/b + 1)

>+

== 1

1)( ncn

b

naT

ncnT

Page 25: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

25

So with k = logb n T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)

What if a = b? T(n) = cn(k + 1)

= cn(logb n + 1)

= Θ(n log n)

>+

== 1

1)( ncn

b

naT

ncnT

Page 26: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

26

So with k = logb n T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)

What if a < b?

>+

== 1

1)( ncn

b

naT

ncnT

Page 27: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

27

So with k = logb n T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)

What if a < b? Recall that Σ(xk + xk-1 + … + x + 1) = (xk+1 -1)/(x-1)

>+

== 1

1)( ncn

b

naT

ncnT

Page 28: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

28

So with k = logb n T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)

What if a < b? Recall that (xk + xk-1 + … + x + 1) = (xk+1 -1)/(x-1) So:

>+

== 1

1)( ncn

b

naT

ncnT

( )( )

( )( ) baba

ba

ba

ba

b

a

b

a

b

a kk

k

k

k

k

−<

−−=

−−=++++

++

1

1

1

1

1

11

11

1

1

Page 29: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

29

So with k = logb n T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)

What if a < b? Recall that Σ(xk + xk-1 + … + x + 1) = (xk+1 -1)/(x-1) So:

T(n) = cn ·Θ(1) = Θ(n)

>+

== 1

1)( ncn

b

naT

ncnT

( )( )

( )( ) baba

ba

ba

ba

b

a

b

a

b

a kk

k

k

k

k

−<

−−=

−−=++++

++

1

1

1

1

1

11

11

1

1

Page 30: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

30

So with k = logb n T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)

What if a > b?

>+

== 1

1)( ncn

b

naT

ncnT

Page 31: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

31

So with k = logb n T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)

What if a > b?

>+

== 1

1)( ncn

b

naT

ncnT

( )( ) ( )( )k

k

k

k

k

k

baba

ba

b

a

b

a

b

a Θ=−

−=+++++

1

11

1

1

1

Page 32: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

32

So with k = logb n T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)

What if a > b?

T(n) = cn · Θ(ak / bk)

>+

== 1

1)( ncn

b

naT

ncnT

( )( ) ( )( )k

k

k

k

k

k

baba

ba

b

a

b

a

b

a Θ=−

−=+++++

1

11

1

1

1

Page 33: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

33

So with k = logb n T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)

What if a > b?

T(n) = cn · Θ(ak / bk)

= cn · Θ(alog n / blog n) = cn · Θ(alog n / n)

>+

== 1

1)( ncn

b

naT

ncnT

( )( ) ( )( )k

k

k

k

k

k

baba

ba

b

a

b

a

b

a Θ=−

−=+++++

1

11

1

1

1

Page 34: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

34

So with k = logb n T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)

What if a > b?

T(n) = cn · Θ(ak / bk)

= cn · Θ(alog n / blog n) = cn · Θ(alog n / n)

recall logarithm fact: alog n = nlog a

>+

== 1

1)( ncn

b

naT

ncnT

( )( ) ( )( )k

k

k

k

k

k

baba

ba

b

a

b

a

b

a Θ=−

−=+++++

1

11

1

1

1

Page 35: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

35

So with k = logb n

T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)

What if a > b?

T(n) = cn · Θ(ak / bk)

= cn · Θ(alog n / blog n) = cn · Θ(alog n / n)

recall logarithm fact: alog n = nlog a

= cn · Θ(nlog a / n) = Θ(cn · nlog a / n)

>+

== 1

1)( ncn

b

naT

ncnT

( )( ) ( )( )k

k

k

k

k

k

baba

ba

b

a

b

a

b

a Θ=−

−=+++++

1

11

1

1

1

Page 36: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

36

So with k = logb n

T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)

What if a > b?

T(n) = cn · Θ(ak / bk)

= cn · Θ(alog n / blog n) = cn · Θ(alog n / n)

recall logarithm fact: alog n = nlog a

= cn · Θ(nlog a / n) = Θ(cn · nlog a / n)

= Θ(nlog a )

>+

== 1

1)( ncn

b

naT

ncnT

( )( ) ( )( )k

k

k

k

k

k

baba

ba

b

a

b

a

b

a Θ=−

−=+++++

1

11

1

1

1

Page 37: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

37

So…

>+

== 1

1)( ncn

b

naT

ncnT

T n =Θ n ab

Θ n logb n a=b

Θ n logb a ab

Page 38: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

38

The Master Theorem

Given: a divide and conquer algorithm An algorithm that divides the problem of size n

into a subproblems, each of size n/b Let the cost of each stage (i.e., the work to divide

the problem + combine solved subproblems) be described by the function f(n)

Then, the Master Theorem gives us a cookbook for the algorithm’s running time:

Page 39: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

39

The Master Theorem

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

( )

( )

( )

( )

( )

( )

<>

<Ω=

Θ=

=

Θ

Θ

Θ

=

+

1

0

largefor )()/(

AND )(

)(

)(

)(

log)(

log

log

log

log

log

c

nncfbnaf

nnf

nnf

nOnf

nf

nn

n

nT

a

a

a

a

a

b

b

b

b

b

ε

ε

ε

Page 40: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

40

Using The Master Method

T(n) = 9T(n/3) + n a=9, b=3, f(n) = n nlogb a = nlog3 9 = Θ(n2) Since f(n) = O(nlog3 9 - ε), where ε=1, case 1 applies:

Thus the solution is T(n) = Θ(n2)

( ) ( )ε−=Θ= aa bb nOnfnnT loglog )( when )(

Page 41: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

41

Sorting revisited

We have seen algorithms for sorting: INSERTION-SORT, MERGESORT

More generally:

We are given a sequence of items

Each item has a characteristic called sorting key. The values of the sorting key belong to a set on which there exists a total order relationship

Sorting the sequence = arrange its elements such that the sorting keys are in increasing (or decreasing) order

Page 42: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

42

Sorting revisited

Other assumptions:

We shall consider that the sequence is stored in a random access memory (e.g. in an array)

This means that we will discuss about internal sorting

We shall analyze only sorting methods which are in place (the additional space needed for sorting has at most the size of an element/few elements.

Stability: preserves ordering of elements with identical keys

Page 43: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

43

Stability

Example:

Initial configuration:

((Adam,9), (John, 10), (Peter,9), (Victor,8))

Stable sorting :

((John,10),(Adam,9),(Peter,9),(Victor,8))

Unstable sorting :

((John,10), (Peter,9),(Adam,9), (Victor,8))

Page 44: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

44

Page 45: Solving recurrences Master Theorem. - Gabriel Istrategabrielistrate.weebly.com/uploads/2/5/2/6/2526487/curs6.pdf · Solving Recurrences Another option is what the book calls the “iteration

45

Coming up: Sorting methods

(done) INSERTION-SORT, MERGESORT SELECTION-SORT, QUICKSORT HEAPSORT Sorting in linear time Order statistics: median, etc.