CS200: Algorithm Analysis

Slides:



Advertisements
Similar presentations
Introduction to Algorithms 6.046J
Advertisements

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
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.
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.
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.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Instructor: Paul Beame TA: Gidon Shavit.
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).
Recurrences Part 3. Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes.
Recurrence Examples.
Analysis of Algorithms
Chapter 4: Solution of recurrence relationships
Analysis of Algorithms CS 477/677 Recurrences Instructor: George Bebis (Appendix A, Chapter 4)
October 1, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
1 Computer Algorithms Lecture 7 Master Theorem Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
10/25/20151 CS 3343: Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
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.
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.
1Computer Sciences. 2 GROWTH OF FUNCTIONS 3.2 STANDARD NOTATIONS AND COMMON FUNCTIONS.
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.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4.
Equal costs at all levels
Introduction to Algorithms
Recursion Ali.
Mathematical Foundations (Solving Recurrence)
Introduction to Algorithms: Recurrences
CS 3343: Analysis of Algorithms
Lecture 11. Master Theorem for analyzing Recursive relations
Divide-and-Conquer 6/30/2018 9:16 AM
Chapter 4: Divide and Conquer
Analysis of Algorithms
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
CS 3343: Analysis of Algorithms
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
Divide-and-Conquer 7 2  9 4   2   4   7
Recursion-tree method
CS 3343: Analysis of Algorithms
Introduction to Algorithms
CS 3343: Analysis of Algorithms
Using The Master Method Case 1
Divide-and-Conquer 7 2  9 4   2   4   7
Analysis of Algorithms
At the end of this session, learner will be able to:
Algorithms Recurrences.
Divide-and-Conquer 7 2  9 4   2   4   7
CS200: Algorithm Analysis
Algorithms and Data Structures Lecture III
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

CS200: Algorithm Analysis

ITERATION METHOD FOR SOLVING RECURRENCES This technique requires converting the recurrence to a summation and finding the closed form of the summation. This method is most easy to see with recurrence trees. If I ask you to solve a recurrence using iteration it should be possible to see the pattern by building the recurrence tree and without resorting to the following rather messy technique. Note that this method is not covered in text.

Expand recurrence until a pattern is found: ex. T(n) = n + 4T(n/2) = n + 4(n/2 + 4T(n/4)) = n +4(n/2 + 4((n/4)+4T(n/8))) = n + 2n + 4n + 8n +16n + .....+ ? Question is, how do we get base case of iteration? Base case of iteration occurs when n = 1 or for T(1). Using above example,

ex. n = 64 for T(n) = n + 4T(n/2) = 64 +4(32 + 4(16 + 4(8 + 4(4 + 4(2 +4(1)))))) 1 2 3 4 5 6 Notice that it requires 6 (+1) iterations to get to the base case of T(1), for n = 64, also notice that the base case = 46T(1) = 4lognT(1). thus the iteration results in = n + 2n + 4n + 8n +16n + .....+ 4lognT(1). = n ( S log n-1 2k) + Q (n2) k=0

n ( S log n-1 2k) + Q (n2) k=0 The n2 term results from the following log identity: a log b = b log a thus 4 log n = n log 4 = n2 The above summation is a geometric series: ( S t xk) = (xt+1 -1)/ (x-1) for x > 1

( S t xk) = (xt+1 -1)/ (x-1) for x > 1 k=0 So by substitution of 2 for x and log n for t, we get: ( S t xk) = (xt+1 -1)/ (x-1) for x > 1 k=0 T(n) = n ( S log n-1 2k) + Q (n2) = n(2log n -1/ (2-1)) + Q (n2) = n(n-1) + Q (n2) = Q (n2) You must know geometric and arithmetic series. The iteration method can be used to generate a guess for the substitution method.

MASTER METHOD (MM) FOR SOLVING RECURRENCES This method is not as general as substitution or iteration and can only be used for recurrences of the following form: T(n) = a T(n/b) + f(n) where a >= 1, b > 1 and f(n) > 0 Advantage is that it is a cookbook technique that can be easily applied to many recurrences. Look at general recursion tree from text. Explain intuition behind Master Method. Text proves Master Method (not discussed in class). Almost all recurrences in this course can be solved using MM.

General Recurrence Tree used in MASTER METHOD Case 3 Case2 Case 1 Note: Leaf work is alogbn(n/blogbn) alogbn = nlogba by log identity [a log b = b log a] and (n/blogbn) simplifies to (n/n) thus work at leaves is nlogba

Master Method Cases In this presentation, each case is stated as a ratio instead of a bound on f (as in text). This is easier to understand (I think so and it is recommended by the author of the book) and both presentations are equivalent. CASE1 (work at leaves)/(work at root): nlogba / f(n) = W(ne) for some constant e > 0. This specifies that nlogba is polynomially larger that f(n). Building a recursion tree for this case shows that the weight at each level increases geometrically and that each leaf contains a constant fraction of the total weight. Therefore T(n) = Q (n logba ).

Stated more intuitively : The leaf nodes of the recursion tree are where most of the work is done (by a polynomial order of magnitude). This means that the work done at the other tree levels does not contribute to growth rate as n approaches infinity. We can state case 1 as (work at root)/(work at leaves) so it has the same form as Case 2 and 3. f(n) / nlogba = O(n-e) for some constant e > 0. Ex. T(n) = 9T(n/3) + n, a = 9, b= 3, f(n) = n n / nlog39 = n/ n2 = O(n-1) , where e = 1, c =1, n0 =1 Thus T(n) = Q(nlog39 ) = Q(n2)

CASE2: f(n) / n logba = Q(logk n) for some constant k >= 0. This specifies that f(n) and n logba are the same within a polylogarithmic factor. The recursion tree for this case shows that the weight for each level decreases or remains constant. This means that we must incorporate each levels' work into the solution for our recurrence. Thus T(n) = Q(n logba * log k+1n). ex. T(n) = T(2n/3) + 1, a = 1, b = 3/2, f(n) = 1 1/n log3/21 = 1/n0 = 1/1 = log0n for k = 0, c1 =1, c2 = ½, n0 =1 Thus T(n) = Q(nlog3/21 * log1n) = Q(log n)

CASE3: f(n) / nlogba = W(ne) for some constant e > 0 (and af(n/b) <= cf(n) for some constant c < 1). This specifies that f(n) is polynomially larger than nlogba. The recursion tree for this case shows that the weight at each level is geometrically decreasing and the root contains a constant fraction of the total weight. Thus as n approaches infinity, the work done at the root defines growth rate. Thus T(n) = Q(f(n)). Note that the regularity condition must be proven.

ex. T(n) = 3T(n/4) + nlog n, a = 3, b = 4, f(n) =nlog n, nlog n / nlog43 = nlog n / n.793 = n.2 log n/1 for e  .2, c=1, n0 =1 and 3(n/4 log(n/4)) ≤cn log n for c = 3/4 Thus T(n) = Q(n log n). Now try some examples on your own (identify case and solve) 1. Mergesort T(n) = 2T(n/2) + n 2. Binsearch T(n) = T(n/2) + 1 3. Straussens T(n) = 7T(n/2) + n2 4. Mystery T(n) = 4T(n/2) + n3

Summary Iteration Method Master Method Three cases To solve, use ratio of root work to leaf work, simplify ratio, and specify one of 3 cases by proving it fits the bound of the case.