Recurrences David Kauchak cs161 Summer 2009. Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:

Slides:



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

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.
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.
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.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
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.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
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
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
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.
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.
CompSci 102 Discrete Math for Computer Science April 17, 2012 Prof. Rodger.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 3.
Chapter 4. Recurrences. Outline Offers three methods for solving recurrences, that is for obtaining asymptotic bounds on the solution In the substitution.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
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.
1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences.
Foundations II: Data Structures and Algorithms
Solving Recurrences with the Substitution Method.
Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner
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.
1Computer Sciences. 2 GROWTH OF FUNCTIONS 3.2 STANDARD NOTATIONS AND COMMON FUNCTIONS.
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.
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
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.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
Recurrences It continues… Jeff Chastine. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. A recurrence.
Mathematical Foundations (Solving Recurrence)
Analysis of Algorithms CS 477/677
Introduction to Algorithms: Recurrences
Lecture 11. Master Theorem for analyzing Recursive relations
Divide-and-Conquer 6/30/2018 9:16 AM
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Advance Analysis of Lecture No 9 Institute of Southern Punjab Multan
Algorithms and Data Structures Lecture III
Divide-and-Conquer 7 2  9 4   2   4   7
CS 3343: Analysis of Algorithms
CSE 2010: Algorithms and Data Structures
Divide and Conquer (Merge Sort)
Divide-and-Conquer 7 2  9 4   2   4   7
Trevor Brown CS 341: Algorithms Trevor Brown
Analysis of Algorithms
David Kauchak cs161 Summer 2009
Algorithms Recurrences.
Algorithms CSCI 235, Spring 2019 Lecture 6 Recurrences
Divide-and-Conquer 7 2  9 4   2   4   7
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

Recurrences David Kauchak cs161 Summer 2009

Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder: office hours today in Gates 195

Recurrence A function that is defined with respect to itself on smaller inputs

Why are we interested in recurrences? Computational cost of divide and conquer algorithms a subproblems of size n/b D(n) the cost of dividing the data C(n) the cost of recombining the subproblem solutions In general, the runtimes of most recursive algorithms can be expressed as recurrences

The challenge Recurrences are often easy to define because they mimic the structure of the program But… they do not directly express the computational cost, i.e. n, n 2, … We want to remove self-recurrence and find a more understandable form for the function

Three approaches Substitution method: when we have a good guess of the solution, we prove that it’s correct Recursion-tree method: If we don’t have a good guess of the solution, the recursion tree can help. Then solve with substitution method. Master method: Provides solutions for recurrences of the form:

Substitution method Guess the form of the solution Then prove it’s correct by induction Halves the input and does a constant Guess?

Substitution method Guess the form of the solution Then prove it’s correct by induction Halves the input and does a constant Similar to binary search: Guess: O(log 2 n)

Assume T(k) = O(log 2 k) for all k < n Show that T(n) = O(log 2 n) Given that T(n/2) = O(log 2 n), then T(n/2) ≤ c log 2 (n/2)

To prove that T(n) = O(log 2 n) we need to identify the appropriate constants: if c ≥ d i.e. some constant c such that T(n) ≤ c log 2 n residual

Base case? For an inductive proof we need to show two things: Assuming it’s true for k < n show it’s true for n Show that it holds for some base case What is the base case in our situation?

Guess the solution? At each iteration, does a linear amount of work (i.e. iterate over the data) and reduces the size by one at each step O(n 2 ) Assume T(k) = O(k 2 ) for all k < n again, this implies that T(n-1) = c(n-1) 2 Show that T(n) = O(n 2 ), i.e. T(n) ≤ cn 2

if residual which holds for any c ≥1 for n ≥1

Guess the solution? Recurses into 2 sub-problems that are half the size and performs some operation on all the elements O(n log n) What if we guess wrong, e.g. O(n 2 )? Assume T(k) = O(k 2 ) for all k < n again, this implies that T(n/2) = c(n/2) 2 Show that T(n) = O(n 2 )

residual if overkill?

What if we guess wrong, e.g. O(n)? Assume T(k) = O(k) for all k < n again, this implies that T(n/2) ≤ c Show that T(n) = O(n) factor of n so we can just roll it in?

What if we guess wrong, e.g. O(n)? Assume T(k) = O(k) for all k < n again, this implies that T(n/2) = c(n/2) Show that T(n) = O(n) factor of n so we can just roll it in? Must prove the exact form! cn+n ≤ cn ??

Prove T(n) = O(n log 2 n) Assume T(k) = O(k log 2 k) for all k < n again, this implies that T(k) = ck log 2 k Show that T(n) = O(n log 2 n) residual if cn ≥ n, c > 1

Changing variables Guesses? We can do a variable change: let m = log n (or n = 2 m ) Now, let S(m)=T(2 m )

Changing variables Guess? substituting m=log n

Recursion Tree Guessing the answer can be difficult The recursion tree approach Draw out the cost of the tree at each level of recursion Sum up the cost of the levels of the tree Find the cost of each level with respect to the depth Figure out the depth of the tree Figure out (or bound) the number of leaves Verify your answer using the substitution method

cn 2 T(n/4 ) cn 2 cost

cn 2 cost 3/16cn 2

cn 2 cost 3/16cn 2 (3/16) 2 cn 2 What is the cost at each level?

What is the depth of the tree? At each level, the size of the data is divided by 4

cn 2 How many leaves are there?

How many leaves? How many leaves are there in a complete ternary tree of depth d?

Total cost let x = 3/16 ?

Total cost

Verify solution using substitution Assume T(k) = O(k 2 ) for all k < n Show that T(n) = O(n 2 ) Given that T(n/4) = O((n/4) 2 ), then T(n/4) ≤ c(n/4) 2

To prove that Show that T(n) = O(n 2 ) we need to identify the appropriate constants: if i.e. some constant c such that T(n) ≤ cn 2

Master Method Provides solutions to the recurrences of the form:

a = b = f(n) = 16 4 n Case 1:Θ(n2)Θ(n2)

a = b = f(n) = 1 2 2n2n Case 3?

T(n) = Θ(2 n ) Let c = 1/2

a = b = f(n) = 2 2 n Case 2:Θ(n log n)

a = b = f(n) = 16 4 n! Case 3?

T(n) = Θ(n!) Let c = 1/2 therefore,

a = b = f(n) = 2 logn Case 1:Θ( )

a = b = f(n) = 4 2 n Case 1:Θ(n2)Θ(n2)

Why does the master method work? a a a a

What is the depth of the tree? At each level, the size of the data is divided by b

How many leaves? How many leaves are there in a complete a-ary tree of depth d?

Total cost Case 1: cost is dominated by the cost of the leaves

Total cost Case 2: cost is evenly distributed across tree As we saw with mergesort, log n levels to the tree and at each level f(n) work

Total cost Case 3: cost is dominated by the cost of the root a

Don’t shoot the messenger Why do we care about substitution method and recurrence tree method? Master method is much easier. Some recurrences don’t fit the mold

Other forms of the master method

Recurrences

Practice problem You are given an infinitely long array, where the first n elements contain data and the remaining elements do not. At any given position, you can test whether that element contains data or not. How can you determine n?