solving recurrences substitution method recursion tree method the master method · 2012. 1. 4. ·...

29
Solving recurrences Substitution method Recursion tree method The master method P. S. Mandal, IITG

Upload: others

Post on 17-Aug-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Solving recurrences

• Substitution method

• Recursion tree method

• The master method

P. S. Mandal, IITG

Page 2: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Substitution method

Few Exercises:

1. T(n) = T(⌊n/2⌋) + T(⌈n/2⌉) +O(n)

2. T(n) =2T(sqrt(n)) +lg n

3. T(n) = T(2n/3) + T(n/3) + O(n)

4. T(n) = 3T (⌊n/4⌋) + Θ(n2)

P. S. Mandal, IITG

Page 3: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Recursion tree

P. S. Mandal, IITG

Page 4: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Recursion tree

P. S. Mandal, IITG

Page 5: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Recursion tree

P. S. Mandal, IITG

Page 6: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Recursion tree

P. S. Mandal, IITG

Page 7: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Recursion tree

P. S. Mandal, IITG

Page 8: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Compression

• Θ(nlgn) grows more slowly than Θ(n2).

• Therefore, merge sort asymptotically beats insertion sort in the worst case.

• In practice, merge sort beats insertion sort for n> 30 or so.

• Go test it out for yourself!

P. S. Mandal, IITG

Page 9: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

The master method

The master method applies to recurrences of the form

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

where a≥1, b> 1, and f is asymptotically positive.

P. S. Mandal, IITG

Page 10: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Three common cases

T(n) = aT(n/b) + f(n) where a≥1, b> 1

Compare f(n) with nlogba :

1. If f(n) = O(nlogba – ε) for some constant ε> 0.

– f(n) grows polynomially slower than nlogba

(by an nε factor).

• Solution: T(n) = Θ(nlogba).

P. S. Mandal, IITG

Page 11: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Three common cases

T(n) = aT(n/b) + f(n) where a≥1, b> 1

2. If f(n) = Θ(nlogba lgkn) for some constant k≥0.

– f(n) and nlogba grow at similar rates.

Solution: T(n) = Θ(nlogba lgk+1n).

P. S. Mandal, IITG

Page 12: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Three common cases

T(n) = aT(n/b) + f(n) where a≥1, b> 1 Compare f(n)with nlogba : 3. If f(n) = Ω(nlogba + ε) for some constant ε> 0.

– f(n) grows polynomially faster than nlogba

(by an nε factor), and – f(n) satisfies the regularity condition that

a f(n/b) ≤ cf(n) for some constant c < 1.

• Solution: T(n) = Θ(f(n)).

P. S. Mandal, IITG

Page 13: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Example

P. S. Mandal, IITG

Page 14: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Idea of master theorem

T(n) = aT(n/b) + f(n) where a≥1, b> 1

P. S. Mandal, IITG

Page 15: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Idea of master theorem

T(n) = aT(n/b) + f(n) where a≥1, b> 1

P. S. Mandal, IITG

Page 16: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Idea of master theorem

T(n) = aT(n/b) + f(n) where a≥1, b> 1

P. S. Mandal, IITG

Page 17: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Idea of master theorem

T(n) = aT(n/b) + f(n) where a≥1, b> 1

P. S. Mandal, IITG T(n)= Θ(nlogba) + j=0 logbn - 1 aj f(n/bj)

Page 18: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

T(n)= Θ(nlogba) + j=0 logbn - 1 aj f(n/bj)

T(n) = Θ(nlogba) or

= Θ(nlogba lgk+1n) or

= Θ(f(n))

Proof take an exercise.

P. S. Mandal, IITG

Page 19: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Idea of master theorem

T(n) = aT(n/b) + f(n) where a≥1, b> 1

P. S. Mandal, IITG

Page 20: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Idea of master theorem

T(n) = aT(n/b) + f(n) where a≥1, b> 1

P. S. Mandal, IITG

Page 21: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Idea of master theorem

T(n) = aT(n/b) + f(n) where a≥1, b> 1

P. S. Mandal, IITG

Page 22: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Exercises

• T(n) = 2T(n/2) + n3

• T(n) = T(9n/10) + n

• T(n) = 16T(n/4) + n2

• T (n) = 7T(n/3) + n2

• T(n) = 7T(n/2) + n2

• Assume T(n) is constant for n ≤ 2.

P. S. Mandal, IITG

Page 23: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Binary search

Find an element in a sorted array:

Divide: Check middle element.

Conquer: Recursively search 1 subarray.

Combine: Trivial.

P. S. Mandal, IITG

Page 24: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Binary search

Find an element in a sorted array:

Divide: Check middle element.

Conquer: Recursively search 1 subarray.

Combine: Trivial.

P. S. Mandal, IITG

Page 25: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Binary search

Find an element in a sorted array:

Divide: Check middle element.

Conquer: Recursively search 1 subarray.

Combine: Trivial.

P. S. Mandal, IITG

Page 26: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Binary search

Find an element in a sorted array:

Divide: Check middle element.

Conquer: Recursively search 1 subarray.

Combine: Trivial.

P. S. Mandal, IITG

Page 27: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Binary search

Find an element in a sorted array:

Divide: Check middle element.

Conquer: Recursively search 1 subarray.

Combine: Trivial.

P. S. Mandal, IITG

Page 28: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Recurrence for binary search

P. S. Mandal, IITG

Page 29: Solving recurrences Substitution method Recursion tree method The master method · 2012. 1. 4. · The master method The master method applies to recurrences of the form T(n) = aT(n/b)

Powering a number

P. S. Mandal, IITG