Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.

Slides:



Advertisements
Similar presentations
Divide-and-Conquer 7 2  9 4   2   4   7
Advertisements

Recurrences : 1 Chapter 3. Growth of function Chapter 4. Recurrences.
A simple example finding the maximum of a set S of n numbers.
Analysis of Algorithms
5/5/20151 Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
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.
1 Divide-and-Conquer CSC401 – Analysis of Algorithms Lecture Notes 11 Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm Review the.
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.
ADA: 4. Divide/Conquer1 Objective o look at several divide and conquer examples (merge sort, binary search), and 3 approaches for calculating their.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
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.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
Analysis of Recursive Algorithms
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).
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
Recurrences Part 3. Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes.
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.
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
Divide-and-Conquer 7 2  9 4   2   4   7
Analysis of Algorithms
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
10/13/20151 CS 3343: Analysis of Algorithms Lecture 9: Review for midterm 1 Analysis of quick sort.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
1 Recurrences Algorithms Jay Urbain, PhD Credits: Discrete Mathematics and Its Applications, by Kenneth Rosen The Design and Analysis of.
1 Computer Algorithms Lecture 7 Master Theorem Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
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.
10/25/20151 CS 3343: Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 2: Mathematical Foundations.
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 Andreas Klappenecker [based on slides by Prof. Welch]
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.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
Recurrences – II. Comp 122, Spring 2004.
Foundations II: Data Structures and Algorithms
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.
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.
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
Lecture 11. Master Theorem for analyzing Recursive relations
Divide-and-Conquer 6/30/2018 9:16 AM
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Algorithms and Data Structures Lecture III
Divide-and-Conquer 7 2  9 4   2   4   7
Divide and Conquer (Merge Sort)
Divide-and-Conquer 7 2  9 4   2   4   7
Algorithms Recurrences.
Divide-and-Conquer 7 2  9 4   2   4   7
Algorithms and Data Structures Lecture III
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations The master theorem (simple and extended versions) Examples: Merge-Sort and Quick-Sort

Data Structures, Spring 2006 © L. Joskowicz 2 Complexity analysis of an algorithm Two main methods: Direct counting: sum of the individual steps times the number of times executed T(n) = Σ c i t i Best for repeated iterations (loops). Recurrence equation: an equality or inequality describing the function in terms of its behavior on smaller inputs: T(n) = T(n –1) + c; T(1) = 1.  the solution of the equation is T(n) = O(n 2 ). Best for recursive functions and structures.

Data Structures, Spring 2006 © L. Joskowicz 3 Recurrence equations Simplifying assumptions –n is sufficiently large. –T(1) = Θ(1) for sufficiently small n. A value changes the solution of the equation, but usually only by a constant factor, so the order of growth is unchanged! –Choose n according to boundary conditions: n is even (n=2k), a power of two (n=2 k ) where k >0 is an integer. Formulation: Be very careful with the constants! T(n) is not the same as T(n/2)!

Data Structures, Spring 2006 © L. Joskowicz 4 Formulating recurrence equations Consider –in how many sub-problems the problem is split –what is the size of each sub-problem –how much work is required to combine the results of each sub-problem Recursion tree n n/2 n/4 n/2

Data Structures, Spring 2006 © L. Joskowicz 5 Common recurrence equations (1) Factorial: multiply n by (n –1)! T(n) = T(n – 1) + O(1)  O(n) Fibonacci: add fibonacci(n – 1) and fibonacci(n – 2) T(n) = T(n – 1) + T(n – 2)  O(2 n ) Sequential search: see if the first element is the one we are looking for, and if not, recursively call with one element less: T(n) = T(n – 1) + O(1)  O(n) Insertion sort: find the place of the first element in the sorted list, and recursively call with one element less: T(n) = T(n – 1) + O(n)  O(n 2 )

Data Structures, Spring 2006 © L. Joskowicz 6 Common recurrence equations (2) Binary search: see if the root of the tree is the one we are looking for, and if not, recursively call with either the left or right subtree, which has half the elements T(n) = T(n/2) + O(1)  O(lg n) Binary tree traversal: visit all the nodes of a tree by recursively visiting the nodes of the left and right tree: T(n) = 2T(n/2) + O(1)  O(n) Merge Sort: split the list into two equal-sized parts, recursively sort each, and merge the resulting lists: T(n) = 2T(n/2) + O(n)  O(n lg n)

Data Structures, Spring 2006 © L. Joskowicz 7 Solving recurrence equations Three ways to solve recurrence equations: Substitution: guess a bound and use mathematical induction to prove the guess correct. Recursion-tree: convert the recurrence into a tree whose nodes represent the costs at each level and use bounding summations to solve the recurrence. Master method: apply a theorem for recurrences of the form T(n) = aT(n/b) + n c where a, b, c are constants.

Data Structures, Spring 2006 © L. Joskowicz 8 The substitution method The solution to the equation T(n) = 2T(n/2) + n is O(n lg n) for n ≥ 2; assume T(1) = 1 Prove: T(n) ≤ c(n lg n) for c ≥ 2 Base case: T(2) ≤ c 2lg2, which holds for c ≥ 2 since T(2) = 3 General case: Assume that it holds for n/2, that is: T(n/2) ≤ 2(cn/2 lg (n/2)) Substitute into the recurrence relation and prove for n: T(n) ≤ 2(cn/2 lg (n/2) + n ≤ cn lg (n/2) + n ≤ cn lg n – cn lg 2 + n ≤ cn lg n – cn + n ≤ cn lg n for c ≥ 1

Data Structures, Spring 2006 © L. Joskowicz 9 Finding patterns in recurrences (1) Write several elements of the recursion, and see if you can find a pattern. Once you find the pattern, prove it is true by substitution (induction) T(n) = T(n – 1) + n T(n –1) = T(n – 2) + (n –1) T(n –2) = T(n – 3) + (n –2) T(n –3) = T(n – 4) + (n –3) Now substitute: T(n) = T(n – 1) + n = [T(n – 2) + (n –1)] + n = [[T(n – 3) + (n –2)] +(n –1)] + n = [[[T(n – 4) + (n –3)] + (n –2)] +(n –1)] + n = T(n – k) + Σ k i=1 (n –i+1) = T(n – k) + nk – ((k – 1)k)/2

Data Structures, Spring 2006 © L. Joskowicz 10 Finding patterns in recurrences (2) T(n) = T(n – k) + nk – ((k – 1)k)/2 At the end of the recursion, k = n –1 and T(1) = 1, so we get: T(n) = 1 + n 2 – n + n 2 /2 – 3n/2 – 1 = n 2 /2 – n/2 = O(n 2 ) So the guess is that O(n 2 ) is the solution to the recurrence T(n) = T(n – 1) + n

Data Structures, Spring 2006 © L. Joskowicz 11 The master theorem (simple version) Then 1.T(n) = Θ(n c ) when a/b c < 1 (log b a < c) 2.T(n) = Θ(n c log b n) when a/b c = 1 (log b a = c) 3.T(n) = when a/b c > 1 (log b a > c) Let T(n) be defined on non-negative integers by the recurrence: T(n) = aT(n/b) + n c where are constants

Data Structures, Spring 2006 © L. Joskowicz 12 Useful math to know… Logarithms log c (ab) = log c a + log c b log b a n = nlog b a log b (1/a) = – log b a log b a = 1 / log a b Geometric series: See Appendix A in book for many useful tricks!

Data Structures, Spring 2006 © L. Joskowicz 13 Recurrence equation of master theorem T(n) = aT(n/b) + n c T(n/b) = aT(n/b 2 ) + (n/b) c T(n/b 2 ) = aT(n/b 3 ) + (n/b 2 ) c T(n/b 4 ) = aT(n/b 5 ) + (n/b 4 ) c Now substitute: T(n) = aT(n/b) + n c = a[aT(n/b 2 ) + (n/b) c ] + n c = a[a[aT(n/b 3 ) + (n/b 2 ) c ]+ (n/b) c ] + n c = a k T(n/b k ) + n c [1 + a(1/b) c + a 2 (1/b 2 ) c +…a k–1 (1/b k–1 ) c ] k= log b n, is the depth of the recursion

Data Structures, Spring 2006 © L. Joskowicz 14 Recursion-tree for the equation... a2a2 akak a i=0 i=k k= log b n, the depth of the recursion a a i=2 i=1 a

Data Structures, Spring 2006 © L. Joskowicz 15 Master theorem proof The number of comparisons is: which depends on the value of

Data Structures, Spring 2006 © L. Joskowicz 16 Case 1: Master theorem: Case 1 Therefore, T(n) = Θ(n c )

Data Structures, Spring 2006 © L. Joskowicz 17 Case 2: Master theorem: Case 2 Therefore, T(n) = Θ(n c log b n)

Data Structures, Spring 2006 © L. Joskowicz 18 Case 3: Master theorem: Case 3 Therefore,

Data Structures, Spring 2006 © L. Joskowicz 19 Example 1: Merge-Sort The recurrence equation is: T(n) = 2T(n/2) + n Here, a = 2, b = 2, c = 1 Case 2 applies: a/b c = 2/2 1 = 1 Conclusion:

Data Structures, Spring 2006 © L. Joskowicz 20 Solve the recurrence Here a = 9, b = 3, c = 1 Case 3 applies: a/b c = 9/3 1 = 3 > 1 Conclusion: Example 2

Data Structures, Spring 2006 © L. Joskowicz 21 Let be constants, let f (n) be a function, and let T(n) be defined on non-negative integers by the recurrence: where n/b is either or 1.If then 2.If then 3.If and if for some c < 1 and sufficiently large n, then The master theorem (general version)

Data Structures, Spring 2006 © L. Joskowicz 22 Recursion-tree for the equation i=0 k= log b n, the depth of the recursion... i=k i=1 a i=2 a a a2a2 akak a...

Data Structures, Spring 2006 © L. Joskowicz 23 The master theorem – intuition Compares two terms: and f (n) 1.When dominates, the complexity is 2. When f (n) dominates, the complexity is 3.When they are comparable, there is a lg n penalty see book for formal proof!

Data Structures, Spring 2006 © L. Joskowicz 24 Example 1 Solve the recurrence Here a = 1, b = 3/2, f (n) = 1 and Case 2 applies: Conclusion:

Data Structures, Spring 2006 © L. Joskowicz 25 Example 2 Solve the recurrence Here a = 3, b = 4, f (n) = n lg n and Case 3 applies: Conclusion:

Data Structures, Spring 2006 © L. Joskowicz 26 Example 3 Solve the recurrence Here a = 2, b = 2, f (n) = n lg n and None of the three cases apply! Case 3: 2(n/2) lg (n/2) ≤ cn lg n for c < 1 does not hold! Conclusion: the master theorem cannot be used :-(

Data Structures, Spring 2006 © L. Joskowicz 27 Recurrence equations to remember T(n) = T(n – 1) + O(1)  O(n) T(n) = T(n – 1) + O(n)  O(n 2 ) T(n) = 2T(n – 1) + O(1)  O(2 n ) T(n) = T(n/2) + O(1)  O(lg n) T(n) = 2T(n/2) + O(1)  O(n) T(n) = 2T(n/2) + O(n)  O(n lg n)