Recurrences Part 3. Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes.

Slides:



Advertisements
Similar presentations
한양대학교 정보보호 및 알고리즘 연구실 이재준 담당교수님 : 박희진 교수님
Advertisements

한양대학교 정보보호 및 알고리즘 연구실 이재준 담당교수님 : 박희진 교수님
Recurrences : 1 Chapter 3. Growth of function Chapter 4. Recurrences.
Divide and Conquer (Merge Sort)
Introduction to Algorithms 6.046J
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
CS 253: Algorithms Chapter 4 Divide-and-Conquer Recurrences Master Theorem Credit: Dr. George Bebis.
Updates HW#1 has been delayed until next MONDAY. There were two errors in the assignment Merge sort runs in Θ(n log n). Insertion sort runs in Θ(n2).
4.Recurrences Hsu, Lih-Hsing. Computer Theory Lab. Chapter 4P.2 Recurrences -- Substitution method Recursion-tree method Master method.
Analysis of Algorithms
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
Chapter 4: Solution of recurrence relationships
Recurrence Relations Connection to recursive algorithms Techniques for solving them.
Analysis of Algorithms CS 477/677 Recurrences Instructor: George Bebis (Appendix A, Chapter 4)
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions BIL741: Advanced.
October 1, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Introduction to Algorithms Jiafen Liu Sept
Analysis of Algorithms
Analysis of Algorithms CS 477/677
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
MCA 202: Discrete Mathematics Instructor Neelima Gupta
1 Computer Algorithms Lecture 7 Master Theorem Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
Project 2 due … Project 2 due … Project 2 Project 2.
DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
10/25/20151 CS 3343: Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Chapter 4. Recurrences. Outline Offers three methods for solving recurrences, that is for obtaining asymptotic bounds on the solution In the substitution.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
Recurrences – II. Comp 122, Spring 2004.
Foundations II: Data Structures and Algorithms
ADVANCED ALGORITHMS REVIEW OF ANALYSIS TECHNIQUES (UNIT-1)
Solving Recurrences with the Substitution Method.
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
Divide and Conquer. Recall Divide the problem into a number of sub-problems that are smaller instances of the same problem. Conquer the sub-problems by.
Spring 2015 Lecture 2: Analysis of Algorithms
1Computer Sciences. 2 GROWTH OF FUNCTIONS 3.2 STANDARD NOTATIONS AND COMMON FUNCTIONS.
1Computer Sciences Department. Objectives Recurrences.  Substitution Method,  Recursion-tree method,  Master method.
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 4: Recurrences.
Master Method Some of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
2IL50 Data Structures Spring 2016 Lecture 2: Analysis of Algorithms.
BY Lecturer: Aisha Dawood. A recurrence is a function is defined in terms of:  one or more base cases, and  itself, with smaller arguments. 2.
Chapter 4: Solution of recurrence relationships Techniques: Substitution: proof by induction Tree analysis: graphical representation Master theorem: Recipe.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Recurrences It continues… Jeff Chastine. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. A recurrence.
Equal costs at all levels
Recursion Ali.
Mathematical Foundations (Solving Recurrence)
CS 3343: Analysis of Algorithms
Advance Analysis of Lecture No 9 Institute of Southern Punjab Multan
Introduction to Algorithms 6.046J
Algorithms and Data Structures Lecture III
Ch 4: Recurrences Ming-Te Chi
Divide and Conquer (Merge Sort)
Analysis of Algorithms
Algorithms Recurrences.
Algorithms and Data Structures Lecture III
Presentation transcript:

Recurrences Part 3

Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes a function in terms of its value on smaller inputs

An Example MergeSort T(n) =  (1)if n = 1 2T(n/2) +  (n)if n > 1 =  (nlgn)

Solution Techniques Substitution Method  Guess a bound  Use mathematical induction to prove correct Recursion-Tree Method  Draw a tree whose nodes represent costs at each level of recursion  Use techniques for bounding summations to solve Master Method  Used for recurrences of the form T(n) = aT(n/b) + f(n)

Solution Techniques (cont) In general we can ignore  Floors and ceilings Size of input is usually an integer  Boundary conditions An algorithm runs in constant time on constant- sized input When boundary condition changes –it is usually by a constant factor –it does not affect the order of growth

Substitution Method For upper or lower bounds Substitution Method: 1. Guess the form of the solution 2. Prove using mathematical induction

Substitution Method (cont) Example: Solve T(n) = 2T( n / 2 ) + n  Guess: T(n) =  (nlgn) Since recurrence is similar to MergeSort  Show: T(n)  cnlgn for some c > 0  Assume: it holds for T( n / 2 )  c( n / 2 )lg( n / 2 )  Substitute: this into the original recurrence

Substitution Method (cont) T(n)  2(c( n / 2 )lg( n / 2 )) + n  cnlg ( n / 2 ) + n = cnlgn – cnlg2 + n = cnlgn – cn + n Now we want T(n)  cnlgn  To accomplish this, we want (– cn + n)  0  Thus, n  cn  1  c T(n)  cnlgn for c  1 By identity on page 53. Also note that lg2 = 1. Original equation that we wanted to “show”

Substitution Method (cont) Now check boundary conditions  Assume T(1) = 1 Then T(1) = 1  c(1)lg(1) = 0  OOPS!  We are not constrained to show for n  1, but for n  n 0 Extend boundary conditions T(1) = 1 T(2) = 2T(1) + 2 = 4T(3) = 2T(1) + 3 = 5 T(2)  c2lg2 = 2c T(3)  c3lg3 = 4.755c c  2 c  1.05 For the base cases to hold, any choice of c  2 will suffice Base cases for inductive proof Base case of recurrence

Substitution Method (cont) Making good guesses  Guess similar solutions to similar recurrences T(n) = 2T( n / ) + n Guess that T(n) =  (nlgn) The +42 makes no difference when n is very large You’re still cutting the input in half  Narrow in on solutions using loose upper and lower bounds  (n)   (nlgn)   (n 2 )

Substitution Method (cont) Problem: Lower-order terms may defeat mathematical induction of substitution method T(n) = 2T( n / 2 ) + 1  Guess: T(n) =  (n)  Show: T(n)  cn for some c > 0  Assume: T( n / 2 )  c( n / 2 )

Substitution Method (cont)  Substitute: into original recurrence T(n)  2c( n / 2 ) + 1 = cn + 1  cn  i.e. it is NOT the same as what we were trying to show and we cannot just remove the +1

Substitution Method (cont) Try subtracting a lower-order term  Make a stronger inductive hypothesis  We know that cn – b   (n)  Guess: T(n) =  (n)  Show: T(n)  cn - b where b  0 is some constant  Assume: T( n / 2 )  c( n / 2 ) – b

Substitution Method (cont)  Substitute: into original recurrence T(n) = 2T( n / 2 ) + 1 T(n)  2(c n / 2 - b) + 1 = cn –2b +1  cn – b cn –2b + 1 will be less than cn – b if b  1

Substitution Method (cont) Example: Factorial Fact(n) if n < 1 return 1 else return n * fact(n-1)  T(n) =  (1)if n = 0 T(n-1) +  (1)if n > 0

Substitution Method (cont)  Guess: T(n) =  (n)  Show: T(n)  cn  Assume: T(n-1)  c(n-1)  Substitute: T(n)  c(n-1) +  (1)  = cn – c +  (1)   cn  Boundary conditions: T(1) =  (1)  cn = c  if c   (1) which is true for large enough c

Substitution Method (cont) Example: Fibonacci Fib(n) if n < 2 return n else return Fib(n-1) + Fib(n-2)  T(n) = 1if n < 2 T(n-1) + T(n-2) +  (1)if n  2

Substitution Method (cont)  Guess: T(n) =  (2 n )  Show: T(n)  c2 n  Assume: T(n-1)  c(2 n-1 ) & T(n-2)  c(2 n-2 )  Substitute: T(n)  c(2 n-1 ) + c(2 n-2 ) +  (1)  = ½c2 n + ¼c2 n +  (1)  = ¾c2 n +  (1)   c2 n  Boundary Conditions T(0) = 1  c2 0 = c if c  1  Actually, if ¼c2 n   (1) c  (4  (1))/2 n which holds for sufficiently large n

Recursion-Tree Method Helps to generate a “good guess”  Can be a little mathematically sloppy because:  Then prove using substitution method Can be used as a direct proof if done carefully Helps visualize the recursion

Recursion-Tree Method (cont) Example   Rewrite as: T(n) = 3T(n/4) + cn 2 Here is sloppines s we can tolerate T(n)T(n) T(n/4) cn 2 T(n/4) implied constant coefficient c > 0

Recursion-Tree Method (cont) c(n/4) 2 cn 2 c(n/4) 2 T(n/16)

Recursion-Tree Method (cont) cn 2 c(n/4) 2 c(n/16) 2 T(1) … log 4 n cn 2 3 / 16 cn 2 ( 3 / 16 ) 2 cn 2 Total:  (n 2 )

Recursion-Tree Method (cont) How did we get the total of  (n 2 ) But this is a little messy

Recursion-Tree Method (cont) Use an infinite decreasing geometric series as an upper bound  Again, a little sloppy, but OK for a guess

Recursion-Tree Method (cont) Now use substitution method to check  Guess: T(n) =  (n 2 )  Show: T(n)  dn 2 for some constant d > 0  Assume: T(n/4) = d(n/4) 2

Recursion-Tree Method (cont) Same c as in slide 20 Holds as long as d  (16/13)c

Recursion-Tree Method (cont) Another Example   Find the upper-bound  Again, c will represent  (n)

Recursion-Tree Method (cont) c(n/3)c(2n/3) cn c(n/9)c(2n/9) c(4n/9) T(1) Total:  (nlgn) log 3/2 n cn ? ?

Recursion-Tree Method (cont) Some complications in this example:  The tree is not a complete binary tree  Each level will not contribute a cost of cn Levels toward the bottom contribute less  But we want only a “guess,” so this imprecision is OK  Now check using the Substitution Method

Master Method Solves recurrences of the form  a  1, b > 1 are constants  f(n) is an asymptotically positive function

Master Theorem Let a  1 and b > 1 be constants, let f(n) be a function, and let T(n) be defined on the nonnegative integers by the recurrence where we interpret n/b to mean either  n/b  or  n/b . Then T(n) can be bounded asymptotically as follows.

Master Theorem (cont) 1.If for some constant  > 0, then 2.If then 3.If for some constant  > 0, and if for some constant c < 1 and all sufficiently large n, then

Master Method (cont) Which is larger, f(n) or ? By a factor of or polynomially larger...

Master Method (cont) Example: Case 2

Master Method (cont) Example: Case 1

Master Method (cont) Example: now check that for large n, Case 3

Summary Example Use all three methods: T(n) =  (1)if n  2 2T(n/2) + n 3 if n > 2

Summary Example (cont) Master Method for large n, is Case 3

Summary Example (cont) Substitution Method  Guess: T(n) =  (n 3 )  Show: T(n)  cn 3  Assume: T(n/2)  c(n/2) 3  Substitute:

Summary Example (cont) Substitution Method (cont)  Guess: T(n) =  (n 3 )  Show: T(n)  cn 3  Assume: T(n/2)  c(n/2) 3  Substitute:

Summary Example (cont) Substitution Method (cont)  Boundary conditions:

Summary Example (cont) Substitution Method (cont)  Thus, c must equal 4 / 3  and T(n) =  (n 3 )

Summary Example (cont) Recursion Tree Method n3n3 (n/2) 3 (n/4) 3 T(1) … n3n3 1 / 4 n 3 (1/4)2n3(1/4)2n3 Total:  (n 3 ) (n)(n)......