Algorithmic Analysis Notation
📊 Asymptotic Notation and Recurrence Relations
This lecture delves into the mathematical underpinnings of algorithm analysis, focusing on asymptotic notation for rigorous definition and methods for solving recurrence relations, which are crucial for analyzing recursive algorithms like Merge Sort.
📈 Asymptotic Notation: Defining Bounds
Asymptotic notation provides a way to describe the limiting behavior of functions, particularly the running time or space complexity of algorithms, as input size approaches infinity. It allows us to ignore constant factors and lower-order terms, focusing on the dominant growth rate.
🟠 Big O-Notation (O)
Big O-notation defines an upper bound for a function's growth.
Definition: f(n)=O(g(n)) means there exist positive constants c and n0 such that 0≤f(n)≤c⋅g(n) for all n≥n0.
Intuition: f(n) grows no faster than g(n), or f(n) is "less than or equal to" g(n) asymptotically.
Example: 2n2=O(n3) because for sufficiently large n, 2n2 will always be less than or equal to c⋅n3 for a suitable constant c.
Asymmetry: The equality f(n)=O(g(n)) is asymmetric. 2n2=O(n3) is true, but n3=O(2n2) is false. This is because the notation implies "is a member of the set of functions bounded by".
More formally, f(n)∈O(g(n)), meaning f(n) is in the set of functions that are upper-bounded by g(n) up to a constant factor.
Usage as a Macro: Big O can be used within expressions to denote an error bound or a general function.
Example: f(n)=n3+O(n2) means f(n) is roughly n3, plus some lower-order terms that are bounded by O(n2). This implies there exists a function h(n) such that h(n)=O(n2) and f(n)=n3+h(n).
This allows for more refined statements about a function's behavior than just stating f(n)=O(n3).
Left-hand Side Usage: When O(g(n)) appears on the left-hand side, it implies "for any expansion of the macro on the left, there is an expansion on the right such that equality holds."
Example: n2+O(n)=O(n2) means that any function f(n) that is n2 plus an O(n) term is also O(n2). This is intuitive but highlights the asymmetry.
📉 Big Omega-Notation (Ω)
Big Omega-notation defines a lower bound for a function's growth.
Definition: f(n)=Ω(g(n)) means there exist positive constants c and n0 such that 0≤c⋅g(n)≤f(n) for all n≥n0.
Intuition: f(n) grows at least as fast as g(n), or f(n) is "greater than or equal to" g(n) asymptotically.
Example: n=Ω(lgn) (log base 2 of n) because for sufficiently large n, n will always be greater than or equal to c⋅lgn for a suitable constant c.
⚖️ Big Theta-Notation (Θ)
Big Theta-notation defines a tight bound, indicating that a function's growth rate is exactly the same as another function, up to constant factors.
Definition: f(n)=Θ(g(n)) means f(n)=O(g(n)) AND f(n)=Ω(g(n)).
Intuition: f(n) grows at the same rate as g(n), or f(n) is "equal to" g(n) asymptotically, ignoring constant factors and lower-order terms.
Example: n2=Θ(2n2) because they differ only by a constant factor. Also, n2+O(n)=Θ(n2).
💨 Little o-Notation (o) and Little Omega-Notation (ω)
These notations define strict bounds.
Little o-notation: f(n)=o(g(n)) means f(n) grows strictly slower than g(n).
Definition: For every constant c>0, there exists an n0 such that 0≤f(n)<c⋅g(n) for all n≥n0.
Intuition: f(n) is "strictly less than" g(n) asymptotically.
Example: n2=o(n3).
Little omega-notation: f(n)=ω(g(n)) means f(n) grows strictly faster than g(n).
Definition: For every constant c>0, there exists an n0 such that 0≤c⋅g(n)<f(n) for all n≥n0.
Intuition: f(n) is "strictly greater than" g(n) asymptotically.
Example: n3=ω(n2).
There is no "little theta" as strict equality doesn't have an asymptotic analog.
🌳 Solving Recurrences: Three Methods
Solving recurrence relations is fundamental for analyzing the running time of recursive algorithms. There isn't a single universal method, but rather a set of techniques.
1. 🔍 Substitution Method
The substitution method involves guessing a solution and then proving its correctness by mathematical induction.
Steps:
Guess the form of the solution (e.g., O(n2), Θ(nlgn)). Often, you don't need to guess the exact constant factors.
Verify by induction: Substitute the guessed solution (expanded with constants, e.g., cn2) into the recurrence and prove that it holds for all n≥n0 and for an appropriate choice of constants.
Solve for constants: Determine the specific constants (c, n0) that make the induction work.
Caution: Avoid using Big O-notation directly within the inductive proof, as the implicit constants can change at each step, leading to invalid proofs. Always expand to explicit constants (e.g., T(n)≤cn2).
Example Recurrence: T(n)=4T(n/2)+n
Guess 1 (Upper Bound): T(n)=O(n3)
Inductive Hypothesis: Assume T(k)≤ck3 for all k<n.
Proof for T(n): T(n)=4T(n/2)+n T(n)≤4c(n/2)3+n (by inductive hypothesis) T(n)≤4c(n3/8)+n T(n)≤21cn3+n
To show T(n)≤cn3, we need 21cn3+n≤cn3.
This simplifies to n≤21cn3, or 1≤21cn2.
This holds for c≥2 and n≥1.
Base Case: For T(1)=constant, we need T(1)≤c(1)3, which means c must be at least T(1). This is satisfied if we choose c large enough.
Result: T(n)=O(n3) is a valid upper bound, but not tight.
Guess 2 (Attempting Tight Upper Bound): T(n)=O(n2)
Inductive Hypothesis: Assume T(k)≤ck2 for all k<n.
Proof for T(n): T(n)=4T(n/2)+n T(n)≤4c(n/2)2+n T(n)≤4c(n2/4)+n T(n)≤cn2+n
To show T(n)≤cn2, we need cn2+n≤cn2.
This simplifies to n≤0, which is impossible for n≥1.
Problem: The "+ n" term prevents the direct inductive step from working. This indicates the induction hypothesis is too weak.
Guess 3 (Strengthened Hypothesis for Tight Upper Bound): T(n)=O(n2) by assuming T(k)≤c1k2−c2k
Intuition: Subtracting a lower-order term (like c2k) can sometimes "absorb" the problematic additive terms in the recurrence.
Inductive Hypothesis: Assume T(k)≤c1k2−c2k for all k<n.
Proof for T(n): T(n)=4T(n/2)+n T(n)≤4[c1(n/2)2−c2(n/2)]+n T(n)≤4[c1n2/4−c2n/2]+n T(n)≤c1n2−2c2n+n T(n)≤c1n2−(2c2−1)n
To show T(n)≤c1n2−c2n, we need c1n2−(2c2−1)n≤c1n2−c2n.
This simplifies to −(2c2−1)n≤−c2n, or (2c2−1)n≥c2n.
Dividing by n (assuming n>0): 2c2−1≥c2.
This yields c2≥1.
Base Case: We need T(1)≤c1(1)2−c2(1). If T(1) is some constant, we can choose c1 sufficiently large (e.g., c1≥T(1)+c2) to satisfy this.
Result: By choosing c2≥1 and c1 sufficiently large, T(n)=O(n2) is proven.
2. 🌲 Recursion-Tree Method
The recursion-tree method involves drawing a tree that represents the recursive calls and the work done at each level, then summing the costs level by level.
Process:
Draw the tree: Each node represents the cost of a subproblem. The root is the original problem. Children nodes represent recursive calls.
Determine cost at each level: Sum the non-recursive work at each level of the tree.
Determine tree height: Calculate the number of levels until the base case is reached.
Determine number of leaves: Count the number of base case problems.
Sum all costs: Add up the costs of all levels (including leaves) to get the total running time.
Non-Rigorous Aspect: This method often involves "..." (dot, dot, dots), making it less formal. It's excellent for guessing the solution, which can then be formally proven with the substitution method.
Example Recurrence: T(n)=T(n/4)+T(n/2)+n2
Level 0 (Root): Cost = n2
Level 1: Two recursive calls: T(n/4) and T(n/2). Costs = (n/4)2+(n/2)2=n2/16+n2/4=(1/16+4/16)n2=(5/16)n2.
Level 2:
From T(n/4): T(n/16) and T(n/8). Costs = (n/16)2+(n/8)2=n2/256+n2/64=(1/256+4/256)n2=(5/256)n2.
From T(n/2): T(n/8) and T(n/4). Costs = (n/8)2+(n/4)2=n2/64+n2/16=(4/256+16/256)n2=(20/256)n2.
Total Level 2 cost = (5/256+20/256)n2=(25/256)n2.
Observation: The costs at each level form a geometric series: n2, (5/16)n2, (25/256)n2, ...
Summing the series: T(n)=n2(1+5/16+25/256+…). Since the common ratio (5/16) is less than 1, this is a decreasing geometric series. The sum is dominated by the first term.
Result: T(n)=Θ(n2).
3. 🧙 Master Method (Master Theorem)
The master method provides a direct solution for recurrences of a specific form. It's more precise and formal than recursion trees but is restricted to certain types of recurrences.
General Form: T(n)=aT(n/b)+f(n)
a≥1: Number of subproblems.
b>1: Factor by which subproblem size is reduced.
f(n): Cost of dividing the problem and combining solutions (non-recursive work).
f(n) must be asymptotically positive (i.e., f(n)>0 for sufficiently large n).
Key Comparison: The method compares f(n) with nlogba.
Three Cases:
Case 1: f(n) is polynomially smaller than nlogba
Condition: f(n)=O(nlogba−ϵ) for some constant ϵ>0.
Solution: T(n)=Θ(nlogba).
Intuition: The cost is dominated by the work done at the leaves of the recursion tree.
Case 2: f(n) is asymptotically equal to nlogba (up to polylogarithmic factors)
Condition: f(n)=Θ(nlogbalgkn) for some constant k≥0.
Solution: T(n)=Θ(nlogbalgk+1n).
Intuition: The cost is roughly evenly distributed across all levels of the recursion tree. The lgk+1n factor comes from multiplying the work per level by the number of levels (which is proportional to lgn).
Case 3: f(n) is polynomially larger than nlogba
Condition: f(n)=Ω(nlogba+ϵ) for some constant ϵ>0.
Regularity Condition: Additionally, there must exist a constant c<1 such that af(n/b)≤cf(n) for all sufficiently large n. This ensures that the non-recursive work decreases fast enough at each level.
Solution: T(n)=Θ(f(n)).
Intuition: The cost is dominated by the work done at the root (top level) of the recursion tree.
Examples:
T(n)=4T(n/2)+n
a=4,b=2,f(n)=n.
Compare f(n)=n with nlog24=n2.
n=O(n2−ϵ) for ϵ=1. This is Case 1.
Solution: T(n)=Θ(n2).
T(n)=4T(n/2)+n2
a=4,b=2,f(n)=n2.
Compare f(n)=n2 with nlog24=n2.
n2=Θ(n2lg0n) (here k=0). This is Case 2.
Solution: T(n)=Θ(n2lg0+1n)=Θ(n2lgn).
T(n)=4T(n/2)+n3
a=4,b=2,f(n)=n3.
Compare f(n)=n3 with nlog24=n2.
n3=Ω(n2+ϵ) for ϵ=1. This is Case 3.
Check regularity condition: af(n/b)=4(n/2)3=4(n3/8)=21n3.
Is 4f(n/2)≤cf(n)? Is 21n3≤cn3? Yes, for c=1/2<1.
Solution: T(n)=Θ(n3).
T(n)=4T(n/2)+n2/lgn
a=4,b=2,f(n)=n2/lgn.
Compare f(n)=n2/lgn with nlog24=n2.
f(n) is neither polynomially smaller nor polynomially larger, nor Θ(n2lgkn). It falls in the "gap" between Case 1 and Case 2. The Master Method does not apply.
💡 Intuition Behind the Master Method (Recursion Tree Sketch)
The height of the recursion tree is logbn.
The number of leaves is alogbn=nlogba.
Cost at levels:
Root: f(n)
Level 1: a⋅f(n/b)
Level 2: a2⋅f(n/b2)
...
Level k: ak⋅f(n/bk)
Leaves: nlogba (each contributes a constant amount).
Case 1 (Leaf-dominated): If f(n) is polynomially smaller than nlogba, the sum of costs forms a geometrically increasing series, dominated by the last term (leaves).
Case 3 (Root-dominated): If f(n) is polynomially larger than nlogba (and satisfies the regularity condition), the sum of costs forms a geometrically decreasing series, dominated by the first term (root).
Case 2 (Evenly distributed): If f(n) is asymptotically equal to nlogba (up to polylog factors), the cost is roughly the same at all levels. The total cost is then approximately (cost per level) × (number of levels), resulting in the extra lgn factor.

Ask anything
We've read this whole study set, so questions about it get the best answers.