solving recurrences master theorem. - gabriel...

Post on 19-Jul-2020

13 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Solving recurrences Master Theorem.

Slide credit: David Luebke (Virginia)

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

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)

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

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

6

Solving Recurrences

Substitution method Iteration method Master method

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 ???

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 ???

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)

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

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

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

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

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

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

−+∑+−=

16

So far for n >= k we have

>−+=

=0)1(

00)(

nnsn

nns

)(1

knsin

kni

−+∑+−=

17

So far for n >= k we have

What if k = n?

>−+=

=0)1(

00)(

nnsn

nns

)(1

knsin

kni

−+∑+−=

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

37

So…

>+

== 1

1)( ncn

b

naT

ncnT

T n =Θ n ab

Θ n logb n a=b

Θ n logb a ab

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:

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

ε

ε

ε

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

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

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

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

44

45

Coming up: Sorting methods

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

top related