25
1 Recurrences Alexandre David B2-206

Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

1

Recurrences

Alexandre DavidB2-206

Page 2: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

2

24-10-06 AA1 2

TodayRecurrencesHow to solve them:

Substitution method.Recursion-tree method.Master method.

Page 3: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

3

24-10-06 AA1 3

RecurrencesA recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs.

E.g. Fibbonacci: Fn=Fn-1+Fn-2.

Methods for solving recurrences:Substitution method.Recursion-tree method.Master method.

Page 4: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

4

24-10-06 AA1 4

The Substitution MethodTwo steps:

Guess the form of the solution.Use induction to find the constants and prove that the solution works.

Problem: To come up with a good guess.Use recursion-trees.Correct the guess.

The name comes from the substitution of the guessed answer for the function (solution) when the induction hypothesis is applied for smaller values (in the induction proof).This can be used to establish lower or upper bounds.

Page 5: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

5

24-10-06 AA1 5

Substitution Method - ExampleUpper bound for T(n)=2T(⎣n/2⎦)+nGuess: T(n)=O(n lgn).By definition of O(…) we have to proveT(n)≤cn lgn for some constant c.Proof (by induction):

Induction hypothesis – prove “next”.Prove formula for first n – or find first n after which the formula holds.

Page 6: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

6

24-10-06 AA1 6

for c≥1

⎣ ⎦ ⎣ ⎦ nnncnT +≤ ))2/lg(2/(2)(

ncnncnncn

ncnncnnncn

lglg

2lglg)2/lg(

≤+−=

+−=+≤

substitution⎣ ⎦ nnTnT += )2/(2)(

?

Page 7: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

7

24-10-06 AA1 7

Substitution MethodImportant:

Assume the solution has some form f(k) up to some k.Prove that it has exactly the same form f(n)for n.

Continue the proof (boundary condition):Assume T(1)=1 (for simplicity).T(1)≤c lg1 – fails. T(2)=4≤4 lg2 – works for c = 4.Boundary condition will often give a constraint on c.

Page 8: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

8

24-10-06 AA1 8

SubtletiesWhat if the guess is almost correct, i.e., it looks like it’s working but the induction hypothesis is not strong enough?Trick: Subtract a lower term.

Page 9: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

9

24-10-06 AA1 9

⎣ ⎦ ⎡ ⎤ 1)2/()2/()( ++= nTnTnTGuess? O(n)

⎣ ⎦ ⎡ ⎤ 1)2/()2/()( ++≤ ncncnT1+= cn OOPS

Page 10: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

10

24-10-06 AA1 10

for b≥1

⎣ ⎦ ⎡ ⎤ 1)2/()2/()( ++= nTnTnTGuess? O(n) but induction with T(n)≤cn-b.

⎣ ⎦ ⎡ ⎤ 1)2/()2/()( +−+−≤ bncbncnT

bcnbcn

−≤+−= 12?

Page 11: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

11

24-10-06 AA1 11

Recursion-Tree MethodEach node represents the cost of a single sub-problem.Useful when it describes the running time of a divide-and-conquer algorithm.Used to generate a good guess or as a direct proof of a solution to a recurrence.Example: T(n)=3T(n/4)+Θ(n2).

What does it mean?Construct recursion tree to obtain a guess.Use the substitution method for the proof.

?

Page 12: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

12

24-10-06 AA1 12

T(n) = cn2

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

+

=c(n/4)2

+T(n/16) T(n/16)T(n/16)

=c(n/4)2 =c(n/4)2

T(n/16) T(n/16)T(n/16)

T(n/16) T(n/16)T(n/16)

+ +

=c(n/16)2

+

T(n/64) T(n/64)T(n/64)

Page 13: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

13

24-10-06 AA1 13

cn2

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

c(n/16)2c(n/16)2 c(n/16)2

c(n/16)2c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2

c(n/16)2

T(n/64) T(n/64)T(n/64)

Page 14: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

14

24-10-06 AA1 14

2cn

2

4⎟⎠⎞

⎜⎝⎛ nc

2

4⎟⎠⎞

⎜⎝⎛ nc

2

4⎟⎠⎞

⎜⎝⎛ nc

2

4⎟⎠⎞

⎜⎝⎛

i

nc2

4⎟⎠⎞

⎜⎝⎛

i

nc2

4⎟⎠⎞

⎜⎝⎛

i

nc2

4⎟⎠⎞

⎜⎝⎛

i

nc2

4⎟⎠⎞

⎜⎝⎛

i

nc2

4⎟⎠⎞

⎜⎝⎛

i

nc2

4⎟⎠⎞

⎜⎝⎛

i

nc2

4⎟⎠⎞

⎜⎝⎛

i

nc2

4⎟⎠⎞

⎜⎝⎛

i

nc

i=0

i=1

i=2

?T(1)

ninn ii 4log41

4=⇔=⇔=

log4n+1 levels

level k has 3k terms

last level: Θ(3log4n)

Page 15: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

15

24-10-06 AA1 15

2cn

2

4⎟⎠⎞

⎜⎝⎛ nc

2

4⎟⎠⎞

⎜⎝⎛ nc

2

4⎟⎠⎞

⎜⎝⎛ nc

2

4⎟⎠⎞

⎜⎝⎛

i

nc2

4⎟⎠⎞

⎜⎝⎛

i

nc2

4⎟⎠⎞

⎜⎝⎛

i

nc2

4⎟⎠⎞

⎜⎝⎛

i

nc2

4⎟⎠⎞

⎜⎝⎛

i

nc2

4⎟⎠⎞

⎜⎝⎛

i

nc2

4⎟⎠⎞

⎜⎝⎛

i

nc2

4⎟⎠⎞

⎜⎝⎛

i

nc2

4⎟⎠⎞

⎜⎝⎛

i

nc

i=0

i=1

i=2

)(log)3)(log(log)3(log:3

3log444

log4

3loglog

44

44

nnn

n

n

==

=

2cn

2

163 cn

224

3 cni

⎟⎠⎞

⎜⎝⎛

Θ(3log4n)…T(1)…

=Cn2+Θ(nlog43)=O(n2)

Page 16: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

16

24-10-06 AA1 16

The Master MethodApply to recurrences of the formT(n)=aT(n/b)+f(n)where a≥1 and b>1 are constants and f(n) is asymptotically positive.

Don’t re-invent the wheel every time.General solved equations for different cases.Intuition: Compare f(n) to nlogba.

Polynomially larger/smaller (by a factor nε).

Page 17: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

17

24-10-06 AA1 17

T(n)=aT(n/b)+f(n)T(n) is typically time to solve problem of size n.An algorithm divides the problem of size ninto a sub-problems of size n/b,then combines the results, which costs f(n).Note 1: We omit the detail of floor/ceil.Note 2: All the cases are not covered ⇒the master method does not solve all possible cases.

Understand what this recurrence means.

Page 18: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

18

24-10-06 AA1 18

The Master TheoremIf for some ε>0 then

If then

If for some ε>0and iffor some c<1 then

)()( log ε−= abnOnf

)()( log abnnf Θ=

)()( log ε+Ω= abnnf

)()( log abnnT Θ=

)lg)(()( nnfnT Θ=

))(()( nfnT Θ=

)()/( ncfbnaf ≤

f(n) “smaller” than nlogba ⇒

f(n) “same” as nlogba ⇒

f(n) “larger” than nlogba ⇒

regularity condition

Page 19: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

19

24-10-06 AA1 19

ExampleT(n)=9T(n/3)+n.a=9, b=3, f(n)=n.Case 1 with ε=1:

We conclude T(n)=Θ(n2).

29loglog 3 nnn ab ==

)()()( 12log −− == nOnOnf ab ε

Page 20: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

20

24-10-06 AA1 20

ExampleT(n)=T(2n/3)+1.a=1, b=2/3, f(n)=1.Case 2:

We conclude T(n)=Θ( lgn).

11loglog 2/3 == nn ab

)1()()( log Θ=Θ= abnnf

Page 21: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

21

24-10-06 AA1 21

ExampleT(n)=3T(n/4)+n lgn.a=3, b=4, f(n)=n lgn.

Case 3 with ε=0.1 + check regularity:

(c=3/4).We conclude T(n)=Θ(n lgn).

)( 793.03loglog 4 nOnn ab ==

nnncfnnbnafnnnf ab

lg)4/3()()4/lg()4/(3)/()()()( 1.0793.0log

=≤=Ω=Ω= ++ε

Page 22: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

22

24-10-06 AA1 22

ExampleT(n)=2T(n/2)+n lgn.a=2, b=2, f(n)=n lgn.Case 3?Problem: f(n)=n lg(n) not polynomiallylarger than n: no ε>0 s.t. n lgn=Ω(n1+ε).We cannot apply the theorem.

nn ab =log

Page 23: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

23

24-10-06 AA1 23

Master Theorem: Proof IdeaProof for a sub-domain (to simplify):n=1,b,b2,…

Compute the cost with a recursion tree (lemma 4.2):leaves + tree =

Bound the 2nd term with 3 cases (lemma 4.3).Evaluate the sum asymptotically using lemma 4.3.Extend the proof for any n.

∑ −

=+Θ

1log

0log )/()( n

jjja bb bnfan

Page 24: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

24

24-10-06 AA1 24

)(nf

⎟⎠⎞

⎜⎝⎛

bnf ⎟

⎠⎞

⎜⎝⎛

bnf ⎟

⎠⎞

⎜⎝⎛

bnf

⎟⎠⎞

⎜⎝⎛

ibnf ⎟

⎠⎞

⎜⎝⎛

ibnf ⎟

⎠⎞

⎜⎝⎛

ibnf ⎟

⎠⎞

⎜⎝⎛

ibnf⎟

⎠⎞

⎜⎝⎛

ibnf ⎟

⎠⎞

⎜⎝⎛

ibnf ⎟

⎠⎞

⎜⎝⎛

ibnf⎟

⎠⎞

⎜⎝⎛

ibnf ⎟

⎠⎞

⎜⎝⎛

ibnf

i=0

i=1

i=2

)(nf

)(naf

⎟⎠⎞

⎜⎝⎛

ii

bnfa

Θ(nlogba)…Θ(1)…

a× a× a×

∑ −

=+Θ

1log

0log )/()( n

jjja bb bnfan

Page 25: Recurrences - Aalborg Universitetpeople.cs.aau.dk/~adavid/teaching/AA/AA1-06/03-Recurrences.pdf · Recurrences A recurrence is an equation or inequality that describes a function

25

24-10-06 AA1 25

Lemma 4.3Bound the sum term.Proof not difficult, only technical.

Idea: Use hypothesis, substitute, and compute the sum.