dr. naveed ahmad department of computer science university of peshawar lecture-5 advance algorithm...
TRANSCRIPT
![Page 1: DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis](https://reader035.vdocuments.mx/reader035/viewer/2022072015/56649ed85503460f94be6da2/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022072015/56649ed85503460f94be6da2/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022072015/56649ed85503460f94be6da2/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022072015/56649ed85503460f94be6da2/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022072015/56649ed85503460f94be6da2/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022072015/56649ed85503460f94be6da2/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022072015/56649ed85503460f94be6da2/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022072015/56649ed85503460f94be6da2/html5/thumbnails/8.jpg)
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: