Algorithms Recurrences CS 8833 Algorithms Recurrences Definition a
- Slides: 37
Algorithms Recurrences CS 8833 Algorithms
Recurrences Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs l Example – recurrence for Merge-Sort l CS 8833 Algorithms
Why Recurrences? The complexity of many interesting algorithms is easily expressed as a recurrence – especially divide and conquer algorithms l The form of the algorithm often yields the form of the recurrence l The complexity of recursive algorithms is readily expressed as a recurrence. l CS 8833 Algorithms
Why solve recurrences? To make it easier to compare the complexity of two algorithms l To make it easier to compare the complexity of the algorithm to standard reference functions. l CS 8833 Algorithms
Example Recurrences for Algorithms l Insertion sort l Linear search of a list CS 8833 Algorithms
Recurrences for Algorithms, continued l Binary search CS 8833 Algorithms
Casual About Some Details l Boundary conditions – – l These are usually constant for small n We sometimes use these to fill in the details of a “rough guess” solution Floors and ceilings – – Usually makes no difference in solution Usually assume n is an “appropriate” integer (i. e. a power of 2) and assume that the function behaves the same way if floors and ceilings were taken into consideration CS 8833 Algorithms
Merge Sort Assumptions l Actual recurrence is: l But we typically assume that n = 2 k where k is an integer and use the simpler recurrence. CS 8833 Algorithms
Methods for Solving Recurrences Substitution (constructive induction) l Iterating the recurrence l Master Theorem l CS 8833 Algorithms
Substitution Method (Constructive Induction) Use mathematical induction to derive an answer l Steps l 1. Guess the form of the solution 2. Use mathematical induction to find constants or show that they can be found and to prove that the answer is correct CS 8833 Algorithms
Substitution l Goal Derive a function of n (or other variables used to express the size of the problem) that is not a recurrence so we can establish an upper and/or lower bound on the recurrence May get an exact solution or may just get upper or lower bounds on the solution CS 8833 Algorithms
Constructive Induction Suppose T includes a parameter n and n is a natural number (positive integer) l Instead of proving directly that T holds for all values of n, prove l – – T holds for a base case b (often n = 1) For every n > b, if T holds for n-1, then T holds for n. » » Assume T holds for n-1 Prove that T holds for n follows from this assumption CS 8833 Algorithms
Example 1 l Given l Prove T(n) O(n 2) – Note that this is the recurrence for insertion sort and we have already shown that this is O(n 2) using other methods CS 8833 Algorithms
Proof for Example 1 Guess that the solution for T(n) is a quadratic equation l Base case T(1) = a + b + c. We need to find values for a, b, and c first. l Assume this solution holds for n-1 l l Now consider the case for n. Begin with the recurrence for T(n) CS 8833 Algorithms
CS 8833 Algorithms
CS 8833 Algorithms
Example 2– Establishing an Upper Bound CS 8833 Algorithms
CS 8833 Algorithms
CS 8833 Algorithms
Ex. 3–Fallacious Argument CS 8833 Algorithms
Ex. 3–Try again CS 8833 Algorithms
Ex. 3–Try again cont. CS 8833 Algorithms
Boundary Conditions Boundary conditions are not usually important because we don’t need an actual c value (if polynomially bounded) l But sometimes it makes a big difference l – – Exponential solutions Suppose we are searching for a solution to: and we find the partial solution CS 8833 Algorithms
Boundary Conditions cont. CS 8833 Algorithms
Boundary Conditions l The solutions to the recurrences below have very different upper bounds CS 8833 Algorithms
Changing Variables CS 8833 Algorithms
Changing Variables continued CS 8833 Algorithms
Iterating the Recurrence The math can be messy with this method l Can sometimes use this method to get an estimate that we can use for the substitution method l CS 8833 Algorithms
Example 4 CS 8833 Algorithms
CS 8833 Algorithms
CS 8833 Algorithms
Recurrence Trees Allow you to visualize the process of iterating the recurrence l Allows you make a good guess for the substitution method l Or to organize the bookkeeping for iterating the recurrence l Example l CS 8833 Algorithms
n 2 T(n/2)2 n 2 (n/2)2 T(n/4) CS 8833 Algorithms (n/2)2 (n/4)2
Counting things CS 8833 Algorithms
Recurrence Tree Example n 2 (n/2)2 lg n (n/4)2 CS 8833 (n/4)2 Algorithms (n/4)2 Total: (n 2)
Another Example CS 8833 Algorithms
n 2 T(n/4) n 2 T(n/2) (n/4)2 n 2 (n/2)2 T(n/16) T(n/8) T(n/4) 5/16 n 2 25/256 n 2=(5/16)2 n 2 Since the values decrease geometrically, the total is at most a constant factor more than the largest term and hence the solution is n 2) CS 8833 Algorithms
- Solving recurrences substitution method
- Master method for solving recurrences
- Master method
- Recurrences
- Defination of algorithm
- Computational thinking algorithms and programming
- Types of algorithm
- List of recursive algorithms
- 1001 design
- Safe patient handling algorithms
- Recursive algorithms
- Types of randomized algorithms
- Process mining algorithms
- Evolutionary algorithms ppt
- Nature-inspired learning algorithms
- Metaheuristic algorithms
- Making good encryption algorithms
- Algorithm analysis examples
- Statistical algorithms
- Professor ajit diwan
- Advantage and disadvantage of greedy algorithm
- Greedy algorithm list
- Snapshot algorithm for fifo channels
- Forrelation
- Fast algorithms for mining association rules
- Dsp programming tutorial
- Distributed algorithms nancy lynch
- How to analyze algorithm
- Association analysis: basic concepts and algorithms
- Virtual web view in web mining
- Fftooo
- Cos 423 princeton
- Algorithms in computer networks
- Data structures and algorithms tutorial
- Lossless compression algorithms in multimedia
- Memory management algorithms
- Raster graphics algorithms
- Summarize the general plan for non-recursive algorithms.