dr. naveed ahmad department of computer science university of peshawar lecture-5 advance algorithm...

Post on 02-Jan-2016

214 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

DR. NAV EED A HM A DDEPA RT M EN T OF COM P UT ER SCIENCE

UNIV ERSIT Y OF P ESHAWA R

LECT URE - 5

Advance Algorithm Analysis

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

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

Solving Recurrences

No general, automatic procedure for solving recurrence relations is known.

There are methods for solving specific forms of recurrences

Substitution methodIteration methodMaster method

Substitution method

Guess the form of the solution .

Use mathematical induction to find the constants and show that the solution works .

The substitution method can be used to establish either upper or lower bounds on a recurrence.

substitution method

“Making a good guess” method Guess the form of the answer, then use induction

to find the constants and show that solution works

Examples: T(n) = 2T(n/2) + (n) T(n) = (n lg n) T(n) = 2T(n/2) + n T(n) = (n lg n) T(n) = 2T(n/2 )+ 17) + n ??? T(n)= 4T(n/2)+n ?

An example (Substitution method )

T(n) = 2T(floor(n/2) ) +n We guess that the solution is T(n)=0(n lg n). i.e. to show that T(n) ≤ cn lg n , for some

constant c> 0 and n ≥ m.

Assume that this bound holds for [n/2]. So , we get

T(n) ≤ 2(c floor (n/2) lg(floor(n/2))) + n ≤ cn lg(n/2) + n

= cn lg n – cn lg 2 + n = cn lg n – cn + n ≤ cn lg n where , the last step holds as long as c≥

1.

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:

top related