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

Post on 17-Aug-2020

7 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Solving recurrences

• Substitution method

• Recursion tree method

• The master method

P. S. Mandal, IITG

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

Recursion tree

P. S. Mandal, IITG

Recursion tree

P. S. Mandal, IITG

Recursion tree

P. S. Mandal, IITG

Recursion tree

P. S. Mandal, IITG

Recursion tree

P. S. Mandal, IITG

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

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

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

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

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

Example

P. S. Mandal, IITG

Idea of master theorem

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

P. S. Mandal, IITG

Idea of master theorem

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

P. S. Mandal, IITG

Idea of master theorem

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

P. S. Mandal, IITG

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)

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

Idea of master theorem

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

P. S. Mandal, IITG

Idea of master theorem

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

P. S. Mandal, IITG

Idea of master theorem

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

P. S. Mandal, IITG

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

Binary search

Find an element in a sorted array:

Divide: Check middle element.

Conquer: Recursively search 1 subarray.

Combine: Trivial.

P. S. Mandal, IITG

Binary search

Find an element in a sorted array:

Divide: Check middle element.

Conquer: Recursively search 1 subarray.

Combine: Trivial.

P. S. Mandal, IITG

Binary search

Find an element in a sorted array:

Divide: Check middle element.

Conquer: Recursively search 1 subarray.

Combine: Trivial.

P. S. Mandal, IITG

Binary search

Find an element in a sorted array:

Divide: Check middle element.

Conquer: Recursively search 1 subarray.

Combine: Trivial.

P. S. Mandal, IITG

Binary search

Find an element in a sorted array:

Divide: Check middle element.

Conquer: Recursively search 1 subarray.

Combine: Trivial.

P. S. Mandal, IITG

Recurrence for binary search

P. S. Mandal, IITG

Powering a number

P. S. Mandal, IITG

top related