Introduction to Algorithms 6.046J

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.
©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 6 Prof. Erik Demaine.
Divide-and-Conquer CIS 606 Spring 2010.
Recurrences : 1 Chapter 3. Growth of function Chapter 4. Recurrences.
Introduction to Algorithms 6.046J Lecture 1 Prof. Shafi Goldwasser Prof. Erik Demaine.
A simple example finding the maximum of a set S of n numbers.
5/5/20151 Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Introduction to Algorithms Jiafen Liu Sept
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
Algorithm : Design & Analysis [4]
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.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Recurrence Relations Reading Material –Chapter 2 as a whole, but in particular Section 2.8 –Chapter 4 from Cormen’s Book.
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.
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
CSE 421 Algorithms Richard Anderson Lecture 11 Recurrences.
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.
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.
Recursion Algorithm : Design & Analysis [3]. In the last class… Asymptotic growth rate The Sets ,  and  Complexity Class An Example: Maximum Subsequence.
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.
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 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.
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
Introduction to Algorithms
Introduction to Algorithms: Recurrences
Lecture 11. Master Theorem for analyzing Recursive relations
Divide-and-Conquer 6/30/2018 9:16 AM
Algorithm : Design & Analysis [4]
Analysis of Algorithms
Introduction to Algorithms 6.046J
Algorithms and Data Structures Lecture III
Divide-and-Conquer 7 2  9 4   2   4   7
Recursion-tree method
CS 3343: Analysis of Algorithms
Introduction to Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Algorithms and Data Structures Lecture III
Presentation transcript:

Introduction to Algorithms 6.046J Lecture 2 Prof. Shafi Goldwasser

Solving recurrences The analysis of integer multiplication from Lecture 1 required us to solve a recurrence. Recurrences are a major tool for analysis of algorithms Today: Learn a few methods. Lecture 3: Divide and Conquer algorithms which are analyzable by recurrences.

Recall: Integer Multiplication Let X = A B and Y = C D where A,B,C and D are n/2 bit integers Simple Method: XY = (2n/2A+B)(2n/2C+D) Running Time Recurrence T(n) < 4T(n/2) + 100n How do we solve it?

Substitution method The most general method: Guess the form of the solution. Verify by induction. Solve for constants. Example: T(n) = 4T(n/2) + 100n [Assume that T(1) = Q(1).] Guess O(n3) . (Prove O and W separately.) Assume that T(k) £ ck3 for k < n . Prove T(n) £ cn3 by induction.

Example of substitution ( n ) = 4 T ( n / 2 ) + 100n £ 4 c ( n / 2 ) 3 + 100n = ( c / 2 ) n 3 + 100n = cn 3 - (( c / 2 ) n 3 - 100n ) desired – residual desired £ cn 3 whenever (c/2)n3 – 100n ³ 0, for example, if c ³ 200 and n ³ 1. residual

Example (continued) We must also handle the initial conditions, that is, ground the induction with base cases. Base: T(n) = Q(1) for all n < n0, where n0 is a suitable constant. For 1 £ n < n0, we have “Q(1)” £ cn3, if we pick c big enough. This bound is not tight!

A tighter upper bound? We shall prove that T(n) = O(n2). Assume that T(k) £ ck2 for k < n: ) 2 / ( 4 100n cn n T + £ = 2 £ cn for no choice of c > 0. Lose!

A tighter upper bound! IDEA: Strengthen the inductive hypothesis. Subtract a low-order term. Inductive hypothesis: T(k) £ c1k2 – c2k for k < n. T ( n ) = 4 T ( n / 2 ) + 100n £ 4 ( c ( n / 2 ) 2 - c ( n / 2 )) + 100n 1 2 = c n 2 - 2 c n + 100n 1 2 = c n 2 - c n - ( c n - ) 100n 1 2 2 £ c n 2 - c n if c2 > 100. 1 2 Pick c1 big enough to handle the initial conditions.

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

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

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

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

Example of recursion tree Solve 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)

Example of recursion tree Solve 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 … Q(1)

Example of recursion tree Solve 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 … Q(1)

Example of recursion tree Solve 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 … Q(1)

Example of recursion tree Solve 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 … … Q(1)

Example of recursion tree Solve 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 … … Q(1) Total = = Q(n2) geometric series

Appendix: geometric series for x ¹ 1 for |x| < 1 Return to last slide viewed.

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.

Idea of master theorem Recursion tree: f (n) f (n) a f (n/b) … h = logbn a … f (n/b) f (n/b) f (n/b) a … f (n/b2) f (n/b2) f (n/b2) … #leaves = ah = alogbn = nlogba nlogbaT (1) T (1)

Three common cases Compare f (n) with nlogba: f (n) = O(nlogba – e) for some constant e > 0. f (n) grows polynomially slower than nlogba (by an ne factor). Solution: T(n) = Q(nlogba) .

Idea of master theorem Recursion tree: f (n) f (n) a … f (n/b) f (n/b) h = logbn … a2 f (n/b2) f (n/b2) f (n/b2) f (n/b2) … … CASE 1: The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight. nlogbaT (1) T (1) Q(nlogba)

Three common cases Compare f (n) with nlogba: f (n) = Q(nlogba lgkn) for some constant k ³ 0. f (n) and nlogba grow at similar rates. Solution: T(n) = Q(nlogba lgk+1n) .

Idea of master theorem Recursion tree: f (n) f (n) a … f (n/b) f (n/b) h = logbn … a2 f (n/b2) f (n/b2) f (n/b2) f (n/b2) … … CASE 2: (k = 0) The weight is approximately the same on each of the logbn levels. nlogbaT (1) T (1) Q(nlogbalg n)

Three common cases (cont.) Compare f (n) with nlogba: f (n) = W(nlogba + e) for some constant e > 0. f (n) grows polynomially faster than nlogba (by an ne factor), and f (n) satisfies the regularity condition that a f (n/b) £ c f (n) for some constant c < 1. Solution: T(n) = Q( f (n) ) .

Idea of master theorem Recursion tree: f (n) f (n) a … f (n/b) f (n/b) h = logbn … a2 f (n/b2) f (n/b2) f (n/b2) f (n/b2) … … CASE 3: The weight decreases geometrically from the root to the leaves. The root holds a constant fraction of the total weight. nlogbaT (1) T (1) Q( f (n))

Examples Ex. T(n) = 4T(n/2) + n a = 4, b = 2  nlogba = n2; f (n) = n. CASE 1: f (n) = O(n2 – e) for e = 1.  T(n) = Q(n2). Ex. T(n) = 4T(n/2) + n2 a = 4, b = 2  nlogba = n2; f (n) = n2. CASE 2: f (n) = Q(n2lg0n), that is, k = 0.  T(n) = Q(n2lg n).

Examples Ex. T(n) = 4T(n/2) + n3 a = 4, b = 2  nlogba = n2; f (n) = n3. CASE 3: f (n) = W(n2 + e) for e = 1 and 4(cn/2)3 £ cn3 (reg. cond.) for c = 1/2.  T(n) = Q(n3). Ex. T(n) = 4T(n/2) + n2/lg n a = 4, b = 2  nlogba = n2; f (n) = n2/lg n. Master method does not apply. In particular, for every constant e > 0, we have ne = w(lg n).

Conclusion Next time: applying the master method. For proof of master theorem, goto section