Download presentation
Presentation is loading. Please wait.
Published byDale Hawkins Modified over 9 years ago
1
DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis
2
Recurrences The expression: is a recurrence. Recurrence: an equation that describes a function in terms of its value on smaller functions
3
Recurrence Examples
4
Solving Recurrences No general, automatic procedure for solving recurrence relations is known. There are methods for solving specific forms of recurrences Substitution method Iteration method Master method
5
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.
6
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 ?
7
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.
8
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:
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.