Recurrences – II. Comp 122, Spring 2004.

Slides:



Advertisements
Similar presentations
Recurrences : 1 Chapter 3. Growth of function Chapter 4. Recurrences.
Advertisements

Master Theorem Chen Dan Dong Feb. 19, 2013
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)
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.
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.
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.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Analysis of Recursive Algorithms
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
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).
Recurrence Relations Reading Material –Chapter 2 as a whole, but in particular Section 2.8 –Chapter 4 from Cormen’s Book.
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
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.
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.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
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.
Chapter 4. Recurrences. Outline Offers three methods for solving recurrences, that is for obtaining asymptotic bounds on the solution In the substitution.
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.
ADVANCED ALGORITHMS REVIEW OF ANALYSIS TECHNIQUES (UNIT-1)
Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner
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
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.
Lecture #3 Analysis of Recursive Algorithms
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)
Lecture 11. Master Theorem for analyzing Recursive relations
Chapter 4: Divide and Conquer
Algorithm : Design & Analysis [4]
Algorithm Analysis (for Divide-and-Conquer problems)
T(n) = aT(n/b) + cn = a(aT(n/b/b) + cn/b) + cn 2
CS 3343: Analysis of Algorithms
Advance Analysis of Lecture No 9 Institute of Southern Punjab Multan
CS 3343: Analysis of Algorithms
Divide and Conquer (Merge Sort)
Using The Master Method Case 1
Analysis of Algorithms
At the end of this session, learner will be able to:
Introduction To Algorithms
Algorithms Recurrences.
MASTER THEOREM.
CS200: Algorithm Analysis
Presentation transcript:

Recurrences – II. Comp 122, Spring 2004

The Master Method Based on the Master theorem. “Cookbook” approach for solving recurrences of the form T(n) = aT(n/b) + f(n) a  1, b > 1 are constants. f(n) is asymptotically positive. n/b may not be an integer, but we ignore floors and ceilings. Why? Requires memorization of three cases. Comp 122

The Master Theorem Theorem 4.1 Let a  1 and b > 1 be constants, let f(n) be a function, and Let T(n) be defined on nonnegative integers by the recurrence T(n) = aT(n/b) + f(n), where we can replace n/b by n/b or n/b. T(n) can be bounded asymptotically in three cases: If f(n) = O(nlogba–) for some constant  > 0, then T(n) = (nlogba). If f(n) = (nlogba), then T(n) = (nlogbalg n). If f(n) = (nlogba+) for some constant  > 0, and if, for some constant c < 1 and all sufficiently large n, we have a·f(n/b)  c f(n), then T(n) = (f(n)). Comp 122

Recursion tree view f(n) f(n/b) (nlogba) Total: f(n) a … af(n/b) Θ(1) f(n) f(n/b) f(n/b2) a … f(n) af(n/b) a2f(n/b2) (nlogba) Total: Comp 122

The Master Theorem Theorem 4.1 Let a  1 and b > 1 be constants, let f(n) be a function, and Let T(n) be defined on nonnegative integers by the recurrence T(n) = aT(n/b) + f(n), where we can replace n/b by n/b or n/b. T(n) can be bounded asymptotically in three cases: If f(n) = O(nlogba–) for some constant  > 0, then T(n) = (nlogba). If f(n) = (nlogba), then T(n) = (nlogbalg n). If f(n) = (nlogba+) for some constant  > 0, and if, for some constant c < 1 and all sufficiently large n, we have a·f(n/b)  c f(n), then T(n) = (f(n)). Comp 122

Master Method – Examples T(n) = 16T(n/4)+n a = 16, b = 4, nlogba = nlog416 = n2. f(n) = n = O(nlogba-) = O(n2- ), where  = 1  Case 1. Hence, T(n) = (nlogba ) = (n2). T(n) = T(3n/7) + 1 a = 1, b=7/3, and nlogba = nlog 7/3 1 = n0 = 1 f(n) = 1 = (nlogba)  Case 2. Therefore, T(n) = (nlogba lg n) = (lg n) Comp 122

Master Method – Examples T(n) = 3T(n/4) + n lg n a = 3, b=4, thus nlogba = nlog43 = O(n0.793) f(n) = n lg n = (nlog43 +  ) where   0.2  Case 3. Therefore, T(n) = (f(n)) = (n lg n). T(n) = 2T(n/2) + n lg n a = 2, b=2, f(n) = n lg n, and nlogba = nlog22 = n f(n) is asymptotically larger than nlogba, but not polynomially larger. The ratio lg n is asymptotically less than n for any positive . Thus, the Master Theorem doesn’t apply here. Comp 122

Master Theorem – What it means? Case 1: If f(n) = O(nlogba–) for some constant  > 0, then T(n) = (nlogba). nlogba = alogbn : Number of leaves in the recursion tree. f(n) = O(nlogba–)  Sum of the cost of the nodes at each internal level asymptotically smaller than the cost of leaves by a polynomial factor. Cost of the problem dominated by leaves, hence cost is (nlogba). Comp 122

Master Theorem – What it means? Case 2: If f(n) = (nlogba), then T(n) = (nlogbalg n). nlogba = alogbn : Number of leaves in the recursion tree. f(n) = (nlogba)  Sum of the cost of the nodes at each level asymptotically the same as the cost of leaves. There are (lg n) levels. Hence, total cost is (nlogba lg n). Comp 122

Master Theorem – What it means? Case 3: If f(n) = (nlogba+) for some constant  > 0, and if, for some constant c < 1 and all sufficiently large n, we have a·f(n/b)  c f(n), then T(n) = (f(n)). nlogba = alogbn : Number of leaves in the recursion tree. f(n) = (nlogba+)  Cost is dominated by the root. Cost of the root is asymptotically larger than the sum of the cost of the leaves by a polynomial factor. Hence, cost is (f(n)). Comp 122

Master Theorem – Proof for exact powers Proof when n is an exact power of b. Three steps. Reduce the problem of solving the recurrence to the problem of evaluating an expression that contains a summation. Determine bounds on the summation. Combine 1 and 2. Comp 122

Proof for exact powers – Step 1 Lemma 4.2 Let a  1 and b > 1 be constants, and let f(n) be a nonnegative function defined on exact powers of b. Define T(n) on exact powers of b by the recurrence T(n) = (1) if n = 1, aT(n/b) + f(n) if n = bi, i is a +ve integer. Then (4.6) Comp 122

Proof of Lemma 4.2 f(n) f(n/b) (nlogba) Total: f(n) a … af(n/b) Θ(1) f(n) f(n/b) f(n/b2) a … f(n) af(n/b) a2f(n/b2) (nlogba) Total: Comp 122

Proof of Lemma 4.2 – Contd. Cost of the root – f(n) Number of children of the root = Number of nodes at distance 1 from the root = a. Problem size at depth 1 = Original Size/b = n/b. Cost of nodes at depth 1 = f(n/b). Each node at depth 1 has a children. Hence, number of nodes at depth 2 = # of nodes at depth 1  # of children per depth 1 node, = a  a = a2 Size of problems at depth 2 = ((Problem size at depth 1)/b) = n/b/b = n/b2. Cost of problems at depth 2 = f(n/b2). Comp 122

Proof of Lemma 4.2 – Contd. Continuing in the same way, number of nodes at depth j = aj Size of problems at depth j = n/bj. Cost of problems at depth j = f(n/bj). Problem size reduces to 1 at leaves. Let x be the depth of leaves. Then x is given by n/bx =1 Hence, depth of leaf level is logbn. number of leaves = number of nodes at level logbn = alogbn = nlogba. Comp 122

Proof of Lemma 4.2 – Contd. Cost of a leaf node = (1). So, total cost of all leaf nodes = (nlogba). Total cost of internal nodes = Sum of total cost of internal nodes at all levels (from depth 0 (root) to depth logbn – 1). = Total problem cost = Cost of leaves + Cost of internal nodes = (4.2 a) (4.2 b) (from 4.2 a and 4.2 b) Comp 122

Step 2 – Bounding the Summation in Eq. (4.6) Lemma 4.3 Let a  1 and b > 1 be constants, and let f(n) be a nonnegative function defined on exact powers of b. A function g(n) defined over exact powers of b by can be bounded asymptotically for exact powers of b as follows. If f(n) = O(nlogba–) for some constant  > 0, then g(n) = O(nlogba). If f(n) = (nlogba), then g(n) = (nlogbalg n). If a f(n/b)  c f(n) for some constant c < 1 and all n  b, then g(n) = (f(n)). Comp 122

Proof of Lemma 4.3 Case 1 f(n) = O(nlogba–)  f(n/bj) = O((n/bj)logba–) Factoring out terms and simplifying the summation within O-notation leaves an increasing geometric series. Comp 122

Proof of Lemma 4.3 – Contd. ;because  and b are constants. Comp 122

Proof of Lemma 4.3 – Contd. Case 2 f(n) = (nlogba)  f(n/bj) = ((n/bj)logba) Factoring out terms and simplifying the summation within -notation leaves a constant series. Comp 122

Proof of Lemma 4.3 – Contd. Case 2 – Contd. Comp 122

Proof of Lemma 4.3 – Contd. Case 3 f(n) is nonnegative, by definition. a (number of subproblems) and b (factor by which the problem size is reduced at each step) are nonnegative. Hence, each term in the above expression for g(n) is nonnegative. Also, g(n) contains f(n). Hence g(n) = (f(n)), for exact powers of b. Comp 122

Proof of Lemma 4.3 – Contd. Case 3 – Contd. By assumption, a f(n/b)  c f(n), for c < 1 and all n  b.  f(n/b)  (c/a) f(n). Iterating j times, f(n/bj)  (c/a)j f(n).  aj f(n/b)  cj f(n). Comp 122

Proof of Lemma 4.3 – Contd. Case 3 – Contd. Substituting aj f(n/b)  cj f(n) and simplifying yields a decreasing geometric series since c < 1. Thus, g(n) = O(f(n)) and g(n) = (f(n)) (proved earlier).  g(n) = (f(n)). Comp 122

Master Theorem – Proof – Step 3 Lemma 4.4 Let a  1 and b > 1 be constants, and let f(n) be a nonnegative function defined on exact powers of b. Define T(n) on exact powers of b by the recurrence T(n) = (1) if n = 1, T(n) = aT(n/b) + f(n) if n = bi, i is a +ve integer. Then T(n) can be bounded asymptotically for exact powers of b as follows. If f(n) = O(nlogba–) for some constant  > 0, then T(n) = (nlogba). If f(n) = (nlogba), then T(n) = (nlogbalg n). If f(n) = (nlogba+) for some constant  > 0, and af(n/b)  c f(n) for some constant c < 1 and large n, then T(n) = (f(n)). Comp 122

Lemma 4.4 – Proof By Lemma 4.2, Case 1: Case 2: Case 3: Bounds obtained for all 3 cases in Lemma 4.3. Use them. Case 1: Why? Case 2: Case 3: Comp 122

Proof for when n is not an exact power of b To complete the proof for Master Theorem in general, Extend analysis to cases where floors and ceilings occur in the recurrence. I.e., consider recurrences of the form and Go through Sec. 4.4.2 in the text. Comp 122