Introduction to Algorithms: Recurrences

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

Introduction to Algorithms 6.046J/18.401J
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 2 Prof. Erik Demaine.
Introduction to Algorithms 6.046J
5/5/20151 Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Algorithm : Design & Analysis [4]
Lecture 5COMPSCI.220.FS.T Worst-Case Performance Upper bounds : simple to obtain Lower bounds : a difficult matter... Worst case data may be unlikely.
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.
The master method The master method applies to recurrences of the form T(n) = a T(n/b) + f (n), where a  1, b > 1, and f is asymptotically positive.
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.
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).
Recurrences Part 3. Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
CSE 421 Algorithms Richard Anderson Lecture 11 Recurrences.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
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
MCA 202: Discrete Mathematics Instructor Neelima Gupta
10/13/20151 CS 3343: Analysis of Algorithms Lecture 9: Review for midterm 1 Analysis of quick sort.
1 Computer Algorithms Lecture 7 Master Theorem Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis.
10/25/20151 CS 3343: Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Introduction to Algorithms Chapter 4: Recurrences.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Foundations II: Data Structures and Algorithms
Solving Recurrences with the Substitution Method.
Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner
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.
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.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
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.
Equal costs at all levels
Recursion Ali.
Analysis of Algorithms CS 477/677
Introduction to Algorithms Prof. Charles E. Leiserson
Lecture 11. Master Theorem for analyzing Recursive relations
Divide-and-Conquer 6/30/2018 9:16 AM
Unit 1. Sorting and Divide and Conquer
Chapter 4: Divide and Conquer
Randomized Algorithms
Algorithm : Design & Analysis [4]
Randomized Algorithms
Introduction to Algorithms 6.046J
Divide-and-Conquer 7 2  9 4   2   4   7
Richard Anderson Lecture 11 Recurrences
Recursion-tree method
Ch 4: Recurrences Ming-Te Chi
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Introduction to Algorithms
CS 3343: Analysis of Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Trevor Brown CS 341: Algorithms Trevor Brown
Algorithms CSCI 235, Spring 2019 Lecture 8 Recurrences III
Quicksort Quick sort Correctness of partition - loop invariant
CS200: Algorithm Analysis
Richard Anderson Lecture 12 Recurrences
Algorithms and Data Structures Lecture III
Presentation transcript:

Introduction to Algorithms: Recurrences

CS 421 - Analysis of Algorithms Solving Recurrences Recurrences are like solving integrals, differential equations, etc. Three Common Approaches: Recursion Tree Substitution Master Theorem Next Lecture: Applications of recurrences to divide-and-conquer algorithms. CS 421 - Analysis of Algorithms

Recursion-Tree Method A recursion tree models the costs (time) of a recursive execution of an algorithm. The recursion-tree method can be unreliable, just like any method that uses ellipses (…). The recursion-tree method promotes intuition, however. The recursion tree method is good for generating guesses for the substitution method. CS 421 - Analysis of Algorithms

Example of recursion tree Solve: T(n) = T(n/4) + T(n/2) + n2

Example of Recursion Tree T(n) = T(n/4) + T(n/2) + n2: T(n) CS 421 - Analysis of Algorithms

Example of recursion tree T(n) = T(n/4) + T(n/2) + n2: n2 T(n/4) T(n/2) CS 421 - Analysis of Algorithms

Example of recursion tree T(n) = T(n/4) + T(n/2) + n2: n2 (n/4) 2 (n/2) 2 T(n/16) T(n/8) T(n/8) T(n/4) CS 421 - Analysis of Algorithms

Example of recursion tree T(n) = T(n/4) + T(n/2) + n2: n2 (n/4) 2 (n/2) 2 (n/16) 2 (n/8) 2 (n/8) 2 (n/4) 2 (1) CS 421 - Analysis of Algorithms

Example of recursion tree T(n) = T(n/4) + T(n/2) + n2: n2 n2 (n/4) 2 (n/2) 2 (n/16) 2 (n/8) 2 (n/8) 2 (n/4) 2 (1) CS 421 - Analysis of Algorithms

Example of recursion tree T(n) = T(n/4) + T(n/2) + n2: n2 n2 (n/4) 2 5 n (n/2) 2 2 16 (n/16) 2 (n/8) 2 (n/8) 2 (n/4) 2 (1) CS 421 - Analysis of Algorithms

Example of recursion tree T(n) = T(n/4) + T(n/2) + n2: n2 n2 (n/4) 2 5 n (n/2) 2 2 16 25 n (n/16) 2 (n/8) 2 (n/8) 2 (n/4) 2 2 256 … (1) CS 421 - Analysis of Algorithms

Example of recursion tree T(n) = T(n/4) + T(n/2) + n2: n2 n2 (n/4) 2 5 n (n/2) 2 2 16 25 n (n/16) 2 (n/8) 2 (n/8) 2 (n/4) 2 2 256 Total = 𝑛 2 + 5 16 + ( 5 16 ) 2 + ( 5 16 ) 3 + … (1) CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Geometric Series 𝐹𝑜𝑟 𝑟𝑒𝑎𝑙 𝑥 ≠1, 𝑡ℎ𝑒 𝑠𝑢𝑚𝑚𝑎𝑡𝑖𝑜𝑛: 𝑘=0 𝑛 𝑥 𝑘 =1+𝑥+ 𝑥 2 + …+ 𝑥 𝑛 𝑖𝑠 𝑎 𝑔𝑒𝑜𝑚𝑒𝑡𝑟𝑖𝑐 𝑜𝑟 𝑒𝑥𝑝𝑜𝑛𝑒𝑛𝑡𝑖𝑎𝑙 𝑠𝑒𝑟𝑖𝑒𝑠 𝑎𝑛𝑑 ℎ𝑎𝑠 𝑡ℎ𝑒 𝑣𝑎𝑙𝑢𝑒: 𝑘=0 𝑛 𝑥 𝑘 = 𝑥 𝑛+1 −1 𝑥 −1 CS 421 - Analysis of Algorithms

Geometric Series : Positive Fractions 𝑊ℎ𝑒𝑛 𝑡ℎ𝑒 𝑠𝑢𝑚𝑚𝑎𝑡𝑖𝑜𝑛 𝑖𝑠 𝑖𝑛𝑓𝑖𝑛𝑖𝑡𝑒 𝑎𝑛𝑑 𝑥 <1, 𝑤𝑒 ℎ𝑎𝑣𝑒 𝑎𝑛 𝑖𝑛𝑓𝑖𝑛𝑖𝑡𝑒 𝑑𝑒𝑐𝑟𝑒𝑎𝑠𝑖𝑛𝑔 𝑔𝑒𝑜𝑚𝑒𝑡𝑟𝑖𝑐 𝑠𝑒𝑟𝑖𝑒𝑠: 𝑘=0 ∞ 𝑥 𝑘 = 1 1 − 𝑥 CS 421 - Analysis of Algorithms

Example of recursion tree T(n) = T(n/4) + T(n/2) + n2: n2 n2 (n/4) 2 5 n (n/2) 2 2 16 25 n (n/16) 2 (n/8) 2 (n/8) 2 (n/4) 2 2 256 Total = 𝑛 2 + 16 7 (1) = ( 𝑛 2 ) CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Substitution method The most general method: Guess the form of the solution. Verify by induction. Solve for constants. CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Substitution method EXAMPLE: T(n) = 4T(n/2) + n Make assumption that T(1) = (1). Guess O(n3) . Prove O and  separately. Assume that T(k)  ck3 for k < n . Prove T(n)  cn3 by induction. Solve for any constants. CS 421 - Analysis of Algorithms

Example of substitution T (n)  4T (n / 2)  n  4c(n / 2)3  n  (c / 2)n3  n  cn3  ((c / 2)n3  n) desired – residual  cn3 desired whenever (c/2) ∗ n3 – n  0 For example, if c  2 and n  1. residual CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Example (continued) We must also handle the initial conditions, i.e., ground the induction with base cases. Base: T(n) = (1) for all n < n0, where n0 is a suitable constant. For 1  n < n0, we have “(1)”  cn3, if we pick c big enough. CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Example (continued) We must also handle the initial conditions, i.e., ground the induction with base cases. Base: T(n) = (1) for all n < n0, where n0 is a suitable constant. For 1  n < n0, we have “(1)”  cn3, if we pick c big enough. This bound is not tight! CS 421 - Analysis of Algorithms

A Tighter Upper Bound Prove that T(n) = O(n2).

CS 421 - Analysis of Algorithms A Tighter Upper Bound We shall prove that T(n) = O(n2). Assume that T(k)  ck2 for k < n T (n)  4T (n / 2)  n  4c(n / 2)2  n  cn2  n  O(n2 ) CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms A Tighter Upper Bound We shall prove that T(n) = O(n2). Assume that T(k)  ck2 for k < n T (n)  4T (n / 2)  n  4c(n / 2)2  n  cn2  n  O(n2 ) Wrong! We must prove the I.H. CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms A Tighter Upper Bound We shall prove that T(n) = O(n2). Assume that T(k)  ck2 for k < n T (n)  4T (n / 2)  n  4c(n / 2)2  n  cn2  n  O(n2 )  cn2  (n) [ desired – residual ]  cn2 CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms A Tighter Upper Bound We shall prove that T(n) = O(n2). Assume that T(k)  ck2 for k < n T (n)  4T (n / 2)  n  4c(n / 2)2  n  cn2  n  O(n2 )  cn2  (n) [ desired – residual ] Lose!  cn2 for no choice of c > 0. CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms A Tighter Upper Bound! IDEA: Strengthen the inductive hypothesis. Subtract a low-order term. Inductive hypothesis: T(k)  c1k2 – c2k for k < n. CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms A Tighter Upper Bound! T(k)  c1k2 – c2k for k < n: T(n) = 4T(n/2) + n = 4(c1(n/2)2 – c2(n/2)) + n = c1n2 – 2c2n + n = c1n2 – c2n – (c2n – n)  c1n2 – c2n, if c2  1. CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms A Tighter Upper Bound! T(k)  c1k2 – c2k for k < n: T(n) = 4T(n/2) + n = 4(c1(n/2)2 – c2(n/2)) + n = c1n2 – 2c2n + n = c1n2 – c2n – (c2n – n)  c1n2 – c2n, if c2  1. Pick c1 big enough to handle the initial conditions. CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms The Master Method The Master Method applies to eventually non-decreasing functions that satisfy recurrences of the form: T(n) = a T 𝑛 𝑏 + f (n) for 𝑛= 𝑏 𝑘 , 𝑘=1, 2, … T(1) = c where a  1, b > 1, c > 0. CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Three Common Cases If 𝑓 𝑛 ∈ Θ (𝑛 𝑑 ), where 𝑑≥0, then Case 1: 𝑎 <𝑏 𝑑 Solution: T(n) ∈ ( 𝑛 𝑑 ) . Case 2: 𝑎 =𝑏 𝑑 Solution: T(n) = ( 𝑛 𝑑 log n) . Case 3: 𝑎 >𝑏 𝑑 Solution: T(n) ∈ ( 𝑛 𝑙𝑜𝑔𝑏𝑎 ) . CS 421 - Analysis of Algorithms

Examples : Master Method Example 1: T(n) = 4T 𝑛 2 + n a = 4, b = 2 f (n) ∈ Θ 𝑛 ⇒𝑑=1 CASE 3: 4>21  T(n) = ( 𝑛 𝑙𝑜𝑔𝑏𝑎 ) = ( 𝑛 𝑙𝑜𝑔24 ) = ( 𝑛 2 ) CS 421 - Analysis of Algorithms

Examples : Master Method Example 2: T(n) = 4T 𝑛 2 + n2 a = 4, b = 2 f (n) ∈ Θ 𝑛 2 ⇒𝑑=2 CASE 2: 4= 2 2  T(n) = (nd log n) = (n2 log n) CS 421 - Analysis of Algorithms

Examples : Master Method Example 3: T(n) = 4T 𝑛 2 + n3 a = 4, b = 2 f (n) ∈Θ 𝑛 3 ⇒𝑑=3 CASE 1: 4< 2 3  T(n) = (nd) = (n3) CS 421 - Analysis of Algorithms

Examples : Master Method Example 4: T(n) = 4T 𝑛 2 + n2 log n a = 4, b = 2 f (n) ∈ Θ 𝑛 ⇒𝑑= ? Because n not of the form 𝑏 𝑘 , 𝑘=1, 2, …, Master Method does not apply. CS 421 - Analysis of Algorithms