Study Notes: Analysis of Algorithms and Asymptotic Analysis: Recurrences and Running Time
Recurrences and Running Time
An equation or inequality that describes a function in terms of its value on smaller inputs. Example:
Recurrences arise when an algorithm contains recursive calls to itself
The goal is to solve the recurrence: obtain an explicit formula, and bound the recurrence by an expression that involves
Typical complexity targets: , , etc. (focus on asymptotic bounds)
Common Recurrences and Their Theta Bounds
Interpretation: Recursive algorithm that loops through the input to eliminate one item at a time
Recursive algorithm that halves the input in one step
Recursive algorithm that halves the input but must examine every item in the input
Recursive algorithm that splits the input into 2 halves and does a constant amount of other work
Binary Search (Recurrent Algorithm)
Given an ordered array , finds if is in
Algorithm: BINARY-SEARCH(A, lo, hi, x)
if (lo > hi) return FALSE
mid ← ⌊(lo+hi)/2⌋
if return TRUE
if (x < A[mid]) then BINARY-SEARCH(A, lo, mid-1, x)
if (x > A[mid]) then BINARY-SEARCH(A, mid+1, hi, x)
Example A[8] = {1, 2, 3, 4, 5, 7, 9, 11}
lo = 1, hi = 8, x = 7
mid = 4, A[mid] = 4; since x > A[mid], search right half: lo = 5, hi = 8
mid = 6, A[mid] = 9; since x < A[mid], search left half: lo = 5, hi = 5
mid = 5, A[mid] = 5; since x > A[mid], search right half: lo = 6, hi = 5 → NOT FOUND? (illustrative path; actual found at later step if present)
Another example with x = 6 (not in the set):
Similar halving steps show NOT FOUND after narrowing
Analysis of Binary Search
Recurrence:
Solution:
Intuition: At each step, the problem size halves, so the depth of the recursion is , and the work per level is constant
Methods for Solving Recurrences
Iteration method
Substitution method
Recursion-tree method
Master method
The Iteration Method
Convert the recurrence into a summation and bound it using known series
Iterate until the initial condition is reached
Back-substitute to express in terms of and the initial condition
Example 1:
Expand:
Assume
After k steps:
Example 2:
Expand:
Substitute:
Continue:
Pattern after i steps:
Let , after k steps:
The Substitution Method
Steps:
Guess a solution:
Induction goal: for some constants d > 0 and
Induction hypothesis: for all smaller inputs,
Prove the induction goal by using the hypothesis to bound the recurrence
Example: Binary Search (Recurrence )
Guess:
Induction goal: , for some
Induction hypothesis:
Proof:
To ensure , require
Base case must be checked
Example 2:
Guess:
Induction goal:
Induction hypothesis:
Proof:
Require for all large n. One way: choose , since this makes the inequality hold for all sufficiently large n (and in particular for n ≥ 1).
Example 3:
Guess:
Induction goal:
Induction hypothesis:
Proof: if
Changing variables (Renaming) – A trick to simplify
Let , so
Then
Rename:
So
From the previous methods,
Therefore
Idea: transform the recurrence to one you have already solved
The Recursion-Tree Method
Convert the recurrence into a recursion tree: each node represents the cost at a level
The total cost is the sum of costs across levels
Used to guide the guess of the solution
Example 1:
Subproblem size at level :
Number of nodes at level :
Cost per node at level :
Total cost at level :
Depth: levels
Total work:
Example 2:
Subproblem size at level :
Cost per node at level :
Number of nodes at level :
Total cost at level :
Depth:
Total cost: geometric series →
Substitution Method (Substitution-Style Details)
Mastery of the technique: guess a bound and prove by induction using the hypothesis
Key idea: pick a target bound, prove for all sufficiently large n, with appropriate constants
Master Method (Cookbook for T(n) = a T(n/b) + f(n))
Form: where a \ge 1, b > 1 and for some
Compare growth of f(n) with n^{log_b a}
Results:
If a < b^d then
If then
If a > b^d then
This is the standard Master Theorem used to quickly classify many recurrences