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

8
DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis

Upload: dale-hawkins

Post on 02-Jan-2016

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis

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

Page 2: DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-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

Page 3: DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis

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

Page 4: DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis

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

Page 5: DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis

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.

Page 6: DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis

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 ?

Page 7: DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis

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.

Page 8: DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis

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: