Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner

Slides:



Advertisements
Similar presentations
5/5/20151 Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Advertisements

Nattee Niparnan. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.
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.
Algorithm : Design & Analysis [4]
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.
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.
COMP2230 Tutorial 2. Mathematical Induction A tool for proving the truth of a statement for some integer “n”. “n” is usually a +ve integer from 1 to infinity.
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.
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).
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
Recurrence Relations Connection to recursive algorithms Techniques for solving them.
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
Introduction to Algorithms Jiafen Liu Sept
Divide-and-Conquer 7 2  9 4   2   4   7
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.
Introduction to Algorithms Chapter 4: Recurrences.
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 UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Recurrences – II. Comp 122, Spring 2004.
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.
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.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
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.
Recursion Ali.
Mathematical Foundations (Solving Recurrence)
Introduction to Algorithms: Recurrences
Chapter 4: Divide and Conquer
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
CS 3343: Analysis of Algorithms
Introduction to Algorithms
Divide and Conquer (Merge Sort)
Trevor Brown CS 341: Algorithms Trevor Brown
Analysis of Algorithms
Algorithms Recurrences.
Quicksort Quick sort Correctness of partition - loop invariant
Presentation transcript:

Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner

Math Background: Review & Beyond Asymptotic notation Math used in asymptotics Recurrences Probabilistic analysis To do: [CLRS] 4 #2

Obtaining Recurrences T(n) = O(1) n=1 T(n) = 4T(n/2) + kn n>1 T’(n) = O(1) n=1 T’(n) = 3T’(n/2) + k’n n>1 Obtained from straightforward reading of algorithms. Introductory multiplication examples: Key observation: Deterministic algorithms lead to recurrences. Determine appropriate metric for the size “n”. Examine how metric changes during recursion/iteration.

Solving for Closed Forms T(n) = O(1) n=1 T(n) = 4T(n/2) + kn n>1 T’(n) = O(1) n=1 T’(n) = 3T’(n/2) + k’n n>1 How? In general, hard. Solutions not always known. Will discuss techniques in a few minutes…

Recurrences’ Base Case T(n) = O(1) n<b T(n) = … nb For constant-sized problems, can bound algorithm by some constant. This constant is irrelevant for asymptote. Often skip writing base case.

? Recurrences T(n) = 4T(n/2) + kn n>1 T’(n) = 3T’(n/2) + k’n n>1 What if n is odd? ? T(n) = 3T(n/2) + T(n/2) + kn n>1 Above more accurate. The difference rarely matters, so usually ignore this detail. Next iteration, n is not integral. Nonsense.

Two Common Forms of Recurrences T(n) = a1T(n-1)+a2T(n-2) + f(n) n>b T(n) = aT(n/b) + f(n) nb Divide-and-conquer: Linear:

Techniques for Solving Recurrences Substitution Recursion Tree Master Method – for divide & conquer Summation – for simple linear Characteristic Equation – for linear

Techniques: Substitution Guess a solution & check it. More detail: Guess the form of the solution, using unknown constants. Use induction to find the constants & verify the solution. Completely dependent on making reasonable guesses.

Substitution Example 1 Guess: T(n) = O(n3). More specifically: T(n) = 4T(n/2) + n n>1 Simplified version of previous example. Guess: T(n) = O(n3). More specifically: T(n)  cn3, for all large enough n.

? Substitution Example 1 Prove by strong induction on n. T(n) = 4T(n/2) + n n>1 Guess: T(n)  cn3 for n>n0 Which means what exactly? ? Prove by strong induction on n. Assume: T(k)  ck3 for k>n0, for k<n. Show: T(n)  cn3 for n>n0.

Awkward. Fortunately, n0=0 works in these examples. Substitution Example 1 T(n) = 4T(n/2) + n n>1 Guess: T(n)  cn3 for n>n0 Assume T(k)  ck3 for k>n0, for k<n. Show T(n)  cn3 for n>n0. Base case, n=n0+1: Awkward. Fortunately, n0=0 works in these examples.

Assume T(k)  ck3, for k<n. Show T(n)  cn3. Substitution Example 1 T(n) = 4T(n/2) + n n>1 Guess: T(n)  cn3 Assume T(k)  ck3, for k<n. Show T(n)  cn3. Base case, n=1: T(n) = 1 Definition. 1  c Choose large enough c for conclusion.

Substitution Example 1 Inductive case, n>1: Guess: T(n) = 4T(n/2) + n n>1 Guess: T(n)  cn3 Assume T(k)  ck3, for k<n. Show T(n)  cn3. While this is O(n3), we’re not done. Need to show c/2  n3 + n  cn3. Fortunately, the constant factor is shrinking, not growing. Inductive case, n>1: T(n) = 4T(n/2) + n Definition.  4c(n/2)3 + n Induction. = c/2  n3 + n Algebra.

Assume T(k)  ck3, for k<n. Show T(n)  cn3. Substitution Example 1 T(n) = 4T(n/2) + n n>1 Guess: T(n)  cn3 Assume T(k)  ck3, for k<n. Show T(n)  cn3. Inductive case, n>1: T(n)  c/2  n3 + n From before. = cn3 - (c/2  n3 - n) Algebra.  cn3 For n>0, if c2.

Proved: T(n)  2n3 for n>0 Substitution Example 1 T(n) = 4T(n/2) + n n>1 Proved: T(n)  2n3 for n>0 Thus, T(n) = O(n3).

Same recurrence, but now try tighter bound. Substitution Example 2 T(n) = 4T(n/2) + n n>1 Guess: T(n) = O(n2). Same recurrence, but now try tighter bound. More specifically: T(n)  cn2 for n>n0.

Substitution Example 2 Guess: T(n) = 4T(n/2) + n n>1 T(n)  cn2 for n>n0 Follow same steps, and we get... Assume T(k)  ck2, for k<n. Show T(n)  cn2. Not  cn2 ! Problem is that the constant isn’t shrinking. T(n) = 4T(n/2) + n  4c(n/2)2 + n = cn2 + n

Substitution Example 2 T(n) = 4T(n/2) + n n>1 Solution: Use a tighter guess & inductive hypothesis. Subtract a lower-order term – a common technique. Guess: T(n)  cn2 - dn for n>0 Assume T(k)  ck2 - dk, for k<n. Show T(n)  cn2 - dn.

Assume T(k)  ck2 - dn, for k<n. Show T(n)  cn2 - dn. Substitution Example 2 T(n) = 4T(n/2) + n n>1 Guess: T(n)  cn2 - dn Assume T(k)  ck2 - dn, for k<n. Show T(n)  cn2 - dn. Base case, n=1: T(n) = 1 Definition. 1  c-d Choosing c, d appropriately.

Assume T(k)  ck2 - dn, for k<n. Show T(n)  cn2 - dn. Substitution Example 2 T(n) = 4T(n/2) + n n>1 Guess: T(n)  cn2 - dn Assume T(k)  ck2 - dn, for k<n. Show T(n)  cn2 - dn. Inductive case, n>1: T(n) = 4T(n/2) + n Definition.  4(c(n/2)2 - d(n/2)) + n Induction. = cn2 - 2dn + n Algebra. = cn2 - dn - (dn - n) Algebra.  cn2 - dn Choosing d1.

Proved: T(n)  2n2 – 1n for n>0 Substitution Example 2 T(n) = 4T(n/2) + n n>1 Proved: T(n)  2n2 – 1n for n>0 Thus, T(n) = O(n2).

Techniques: Recursion Tree Guessing correct answer can be difficult! Need a way to obtain appropriate guess. Unroll recurrence to obtain a summation. Solve or estimate summation. Use solution as a guess in substitution. Math sometimes tricky.

Recursion Tree Example 1 log2 n T(n) = 4T(n/2) + n n>1 How many levels? ? n Cost at this level T(n) n/2 2n Now, turn picture into a summation… n/4 4n … In this example, all terms on a level are the same. Common, but not always true. … 1 4#levels

Recursion Tree Example 1 T(n) = 4T(n/2) + n n>1 Cost at this level T(n) n n n/2 n/2 n/2 n/2 2n n/4 n/4 n/4 n/4 … n/4 n/4 n/4 n/4 4n … … … … … … … … … … 1 4lg n 1 = Θ(n2) T(n) = n + 2n + 4n + … + 2lg n – 1n + 4lg n = n(1 + 2 + 4 + … + 2lg n – 1) + nlg 4

Recursion Tree Example 2 log3/2 n But, not all branches have same depth! T(n) = T(n/3) + T(2n/3) + n n>1 How many levels? ? n Cost at this level T(n) n/3 2n/3 n Makes cost near the leaves hard to calculate. Estimate! … n/9 2n/9 4n/9 n

Recursion Tree Example 2 T(n) = T(n/3) + T(2n/3) + n n>1 Cost at this level T(n) n n Overestimate. Consider all branches to be of max depth. n/3 2n/3 n n/9 2n/9 2n/9 4n/9 n … … … … … T(n)  n (log3/2 n - 1) + n T(n) = O(n log n) 1 1 n … #levels = log3/2 n

Recursion Tree Example 2 T(n) = T(n/3) + T(2n/3) + n n>1 Cost at this level T(n) n n Underestimate. Count the complete levels, & ignore the rest. n/3 2n/3 n n/9 2n/9 2n/9 4n/9 n … … … … … T(n)  n (log3 n – 1) T(n) = Ω(n log n) #levels = log3 n Thus, T(n) = Θ(n log n)

Techniques: Master Method Cookbook solution for many recurrences of the form T(n) = a  T(n/b) + f(n) where a1, b>1, f(n) asymptotically positive First describe its cases, then outline proof.

f(n) = O(nlogb a - ) for some >0  T(n) = Θ(nlogb a) Master Method Case 1 T(n) = a  T(n/b) + f(n) f(n) = O(nlogb a - ) for some >0  T(n) = Θ(nlogb a) T(n) = 7T(n/2) + cn2 a=7, b=2 E.g., Strassen matrix multiplication. cn2 =? O(nlogb a - ) = O(nlog2 7 - )  O(n2.8 - ) Yes, for any   0.8. T(n) = Θ(nlg 7)

f(n) = Θ(nlogb a)  T(n) = Θ(nlogb a lg n) Master Method Case 2 T(n) = a  T(n/b) + f(n) f(n) = Θ(nlogb a)  T(n) = Θ(nlogb a lg n) T(n) = 2T(n/2) + cn a=2, b=2 E.g., mergesort. cn =? Θ(nlogb a) = Θ(nlog2 2) = Θ(n) Yes. T(n) = Θ(n lg n)

Master Method Case 3 T(n) = a  T(n/b) + f(n) f(n) = Ω(nlogb a + ) for some >0 and af(n/b)  cf(n) for some c<1 and all large enough n  T(n) = Θ(f(n)) I.e., is the constant factor shrinking? T(n) = 4T(n/2) + n3 a=4, b=2 n3 =? Ω(nlogb a + ) = Ω(nlog2 4 + ) = Ω(n2 + ) Yes, for any   1. 4(n/2)3 = ½n3 ? cn3 Yes, for any c  ½. T(n) = Θ(n3)

None of previous apply. Master method doesn’t help. Master Method Case 4 T(n) = a  T(n/b) + f(n) None of previous apply. Master method doesn’t help. T(n) = 4T(n/2) + n2/lg n a=4, b=2 Case 1? n2/lg n =? O(nlogb a - ) = O(nlog2 4 - ) = O(n2 - ) = O(n2/n) No, since lg n is asymptotically < n. Thus, n2/lg n is asymptotically > n2/n.

None of previous apply. Master method doesn’t help. Master Method Case 4 T(n) = a  T(n/b) + f(n) None of previous apply. Master method doesn’t help. T(n) = 4T(n/2) + n2/lg n a=4, b=2 Case 2? n2/lg n =? Θ(nlogb a) = Θ(nlog2 4) = Θ(n2) No.

None of previous apply. Master method doesn’t help. Master Method Case 4 T(n) = a  T(n/b) + f(n) None of previous apply. Master method doesn’t help. T(n) = 4T(n/2) + n2/lg n a=4, b=2 Case 3? n2/lg n =? Ω(nlogb a + ) = Ω(nlog2 4 + ) = Ω(n2 + ) No, since 1/lg n is asymptotically < n.

Master Method Proof Outline How many levels? ? T(n) = a  T(n/b) + f(n) logb n Cost at this level T(n) f(n) f(n) n/b … n/b af(n/b) n/b2 … a2f(n/b2) n/b2 … n/b2 … n/b2 … … … … … 1 … 1 a#levels Cases correspond to determining which term dominates & how to compute sum.

For linear recurrences with one recursive term. Technique: Summation For linear recurrences with one recursive term. T(n) = T(n-1) + f(n) T(n) = T(n-2) + f(n) ?

Techniques: Characteristic Equation Applies to linear recurrences Homogenous: an = c1an-1 + c2an-2 + … + ckan-k Nonhomogenous: an = c1an-1 + c2an-2 + … + ckan-k + F(n)