Algorithm : Design & Analysis [4]

Slides:



Advertisements
Similar presentations
Introduction to Algorithms 6.046J
Advertisements

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.
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.
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.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
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.
Recurrence Examples.
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.
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
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.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Solving Second-Order Recursive Relations Lecture 36 ½ Section 8.3 Wed, Apr 19, 2006.
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.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Recursion Algorithm : Design & Analysis [3]. In the last class… Asymptotic growth rate The Sets ,  and  Complexity Class An Example: Maximum Subsequence.
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.
7.2 Solving Linear Recurrence Relations Some of these recurrence relations can be solved using iteration or some other ad hoc technique. However, one important.
Asymptotics and Recurrence Equations Prepared by John Reif, Ph.D. Analysis of Algorithms.
Recurrences – II. Comp 122, Spring 2004.
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.
Master Method Some of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Fundamentals of Algorithm Analysis Algorithm : Design & Analysis [Tutorial - 1]
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
Mathematical Foundations (Solving Recurrence)
Introduction to Algorithms: Recurrences
Modeling with Recurrence Relations
Lecture 11. Master Theorem for analyzing Recursive relations
Design and Analysis of Algorithms
Algorithm : Design & Analysis [4]
Introduction to Algorithms 6.046J
Ch 4: Recurrences Ming-Te Chi
CS 3343: Analysis of Algorithms
Recurrences (Method 4) Alexandra Stefan.
Solving Recurrence Relations
Divide and Conquer (Merge Sort)
At the end of this session, learner will be able to:
Algorithms and Data Structures Lecture III
Presentation transcript:

Algorithm : Design & Analysis [4] Recurrence Equations Algorithm : Design & Analysis [4]

In the last class… Recursive Procedures Analyzing the Recursive Computation. Induction over Recursive Procedures Proving Correctness of Procedures

Recurrence Equations Recursive algorithm and recurrence equation Solution of the Recurrence equations Guess and proving Recursion tree Master theorem Divide-and-conquer

Recurrence Equation: Concept A recurrence equation: defines a function over the natural number n in term of its own value at one or more integers smaller than n Example: Fibonacci numbers Fn=Fn-1+Fn-2 for n2 F0=0, F1=1 Recurrence equation is used to express the cost of recursive procedures.

Linear Homogeneous Relation is called linear homogeneous relation of degree k. Yes No

Characteristic Equation For a linear homogeneous recurrence relation of degree k the polynomial of degree k is called its characteristic equation. The characteristic equation of linear homogeneous recurrence relation of degree 2 is:

Solution of Recurrence Relation If the characteristic equation of the recurrence relation has two distinct roots s1 and s2, then where u and v depend on the initial conditions, is the explicit formula for the sequence. If the equation has a single root s, then, both s1 and s2 in the formula above are replaced by s

Explicit formula for Fibonacci Sequence fn= fn-1+ fn-2 1, 1, 2, 3, 5, 8, 13, 21, 34, ...... Explicit formula for Fibonacci Sequence The characteristic equation is x2-x-1=0, which has roots: Note: (by initial conditions) which results:

Determining the Upper Bound Example: T(n)=2T(n/2) +n Guess T(n)O(n)? T(n)cn, to be proved for c large enough T(n)O(n2)? T(n)cn2, to be proved for c large enough Or maybe, T(n)O(nlogn)? T(n)cnlogn, to be proved for c large enough Try and fail to prove T(n)cn: T(n)=2T(n/2)+n  2c(n/2)+n  2c(n/2)+n = (c+1)n T(n) = 2T(n/2)+n  2(cn/2 lg (n/2))+n  cn lg (n/2)+n = cn lg n – cn log 2 +n = cn lg n – cn + n  cn log n for c1 Note: the proof is invalid for T(1)=1

The recursion tree for T(n)=T(n/2)+T(n/2)+n T(size) nonrecursive cost T(n) n T(n/2) n/2 T(n/2) n/2 T(n/4) n/4 T(n/4) n/4 T(n/4) n/4 T(n/4) n/4 The recursion tree for T(n)=T(n/2)+T(n/2)+n

Recursion Tree Rules Construction of a recursion tree work copy: use auxiliary variable root node expansion of a node: recursive parts: children nonrecursive parts: nonrecursive cost the node with base-case size

Recursion tree equation For any subtree of the recursion tree, size field of root = Σnonrecursive costs of expanded nodes + Σsize fields of incomplete nodes Example: divide-and-conquer: T(n) = bT(n/c) + f(n) After kth expansion:

Evaluation of a Recursion Tree Computing the sum of the nonrecursive costs of all nodes. Level by level through the tree down. Knowledge of the maximum depth of the recursion tree, that is the depth at which the size parameter reduce to a base case.

Work copy: T(k)=T(k/2)+T(k/2)+k Recursion Tree Work copy: T(k)=T(k/2)+T(k/2)+k T(n)=nlgn T(n) n T(n/2) n/2 T(n/2) n/2 n/2d (size 1) T(n/4) n/4 T(n/4) n/4 T(n/4) n/4 T(n/4) n/4 At this level: T(n)=n+2(n/2)+4T(n/4)=2n+4T(n/4)

Recursion Tree for T(n)=3T(n/4)+(n2) Note: Total: O(n2) cn2 cn2 log4n c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 …… …… … T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) Note: Total: O(n2)

Verifying “Guess” by Recursive Tree Inductive hypothesis

Common Recurrence Equation Divide and Conquer T(n) = bT(n/c) + f(n) Chip and Conquer T(n) = T(n - c) + f(n) Chip and Be Conquered T(n) = bT(n - c) + f(n)

Recursion Tree for T(n)=bT(n/c)+f(n) b b Note: Total ? f(n) f(n) f(n/c) f(n/c) f(n/c) b logcn f(n/c2) f(n/c2) f(n/c2) f(n/c2) f(n/c2) f(n/c2) f(n/c2) f(n/c2) f(n/c2) …… …… … T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) Note: Total ?

Solving the Divide-and-Conquer The recursion equation for divide-and-conquer, the general case:T(n)=bT(n/c)+f(n) Observations: Let base-cases occur at depth D(leaf), then n/cD=1, that is D=lg(n)/lg(c) Let the number of leaves of the tree be L, then L=bD, that is L=b(lg(n)/lg(c)). By a little algebra: L=nE, where E=lg(b)/lg(c), called critical exponent.

Divide-and-Conquer: the Solution The recursion tree has depth D=lg(n)/ lg(c), so there are about that many row-sums. The 0th row-sum is f(n), the nonrecursive cost of the root. The Dth row-sum is nE, assuming base cases cost 1, or (nE) in any event. The solution of divide-and-conquer equation is the nonrecursive costs of all nodes in the tree, which is the sum of the row-sums.

T(n)(nE), where E is critical exponent Little Master Theorem Complexity of the divide-and-conquer case 1: row-sums forming a geometric series: T(n)(nE), where E is critical exponent case 2: row-sums remaining about constant: T(n)(f(n)log(n)) case 3: row-sums forming a decreasing geometric series: T(n)(f(n))

Master Theorem Loosening the restrictions on f(n) The positive  is critical, resulting gaps between cases as well Master Theorem Loosening the restrictions on f(n) Case 1: f(n)O(nE-), (>0), then: T(n)(nE) Case 2: f(n)(nE), as all node depth contribute about equally: T(n)(f(n)log(n)) case 3: f(n)(nE+), (>0), and f(n)O(nE+), (), then: T(n)(f(n))

Using Master Theorem

Looking at the Gap T(n)=2T(n/2)+nlgn a=2, b=2, E=1, f(n)=nlgn We have f(n)=(nE), but no >0 satisfies f(n)=(nE+), since lgn grows slower that n for any small positive . So, case 3 doesn’t apply. However, neither case 2 applies.

Proof of the Master Theorem Case 3 as an example (Note: in asymptotic analysis, f(n)(nE+) leads to f(n) is about (nE+), ignoring the coefficients. Decreasing geo. series

Home Assignment pp.143- 3.7 3.8 3.9 3.10