Loading lessons...
Algorithm Analysis, Loop Complexity & Asymptotic Notations
Algorithm Analysis, Loop Complexity & Asymptotic Notations
Algorithm Analysis predicts the resources an algorithm requires as a function of input size n, independent of hardware or compiler differences (A Priori theoretical analysis vs A Posteriori empirical timing).
1. Characteristics & Categories of an Algorithm
An Algorithm is a finite, step-by-step set of instructions to solve a problem. Every valid algorithm must satisfy 5 key characteristics:
- Input: Zero or more externally supplied quantities.
- Output: At least one quantity produced as a result.
- Definiteness: Every instruction is clear and unambiguous.
- Finiteness: Terminates after a finite number of steps for all valid inputs.
- Effectiveness: Every step is basic enough to be carried out in finite time (e.g., with pencil and paper).
2. Space Complexity
Total space S(P) = c + SP(n) consists of:
- Instruction Space: Compiled machine code (fixed).
- Data Space: Constants, simple variables, and arrays/structs.
- Environment Stack Space: Return addresses, parameters, and local variables pushed during function calls and recursion.
2. Time Complexity & Loop Pattern Analysis
We count primitive operations (assignments, comparisons, arithmetic) rather than wall-clock seconds:
| Loop Structure | Iterations | Time Complexity |
|---|---|---|
for (i = 0; i < n; i++) | n | O(n) (Linear) |
for (i = 0; i < n; i += 2) | n / 2 | O(n) (Linear) |
for (i = 1; i <= n; i *= 2) | 2k ≤ n ⇒ k = ⌊log2 n⌋ | O(log n) (Logarithmic) |
| Nested independent loops (0..n, 0..n) | n × n | O(n2) (Quadratic) |
| Outer linear (n) + inner logarithmic (log n) | n log n | O(n log n) (Linearithmic) |
Dependent triangular loop (j = 0; j < i; j++) | (n(n-1) / 2) | O(n2) (Quadratic) |
3. Best, Average & Worst Case
- Best Case (Ω intuition): Minimum steps for a given n (e.g., target at index 0 in Linear Search: O(1)).
- Worst Case (O intuition): Maximum steps over all inputs of size n - gives a strict performance guarantee.
- Average Case: Expected number of steps assuming a probability distribution over all valid inputs.
4. Asymptotic Notations
- Big-O (O - Asymptotic Upper Bound): f(n) = O(g(n)) iff ∃ c > 0, n0 ≥ 0 such that 0 ≤ f(n) ≤ c · g(n) for all n ≥ n0.
- Big-Omega (Ω - Asymptotic Lower Bound): f(n) = Ω(g(n)) iff ∃ c > 0, n0 ≥ 0 such that f(n) ≥ c · g(n) for all n ≥ n0.
- Theta (Θ - Asymptotic Tight Bound): f(n) = Θ(g(n)) iff ∃ c1, c2 > 0, n0 ≥ 0 such that c1 g(n) ≤ f(n) ≤ c2 g(n) for all n ≥ n0.
- Little-o (o) & Little-omega (ω): Strict (non-tight) bounds where the ratio f(n)/g(n) → 0 as n → ∞ (for o), or f(n)/g(n) → ∞ (for ω).
Worked Proofs (Coefficient Sum Method)
- Show 4n + 6 = O(n): For n ≥ 1, 4n + 6 ≤ 4n + 6n = 10n. Choose c = 10, n0 = 1.
- Show 4x2 - 5x + 3 = O(x2): For x ≥ 1, |4x2 - 5x + 3| ≤ 4x2 + 5x2 + 3x2 = 12x2. Choose c = 12, n0 = 1.
- Note: n2 + n = O(n3) is also valid because Big-O is an upper bound (even if not tight).
Growth Rate Hierarchy
5. Algorithm Design Paradigms
- Brute Force: Exhaustively checks candidates; simple but often slow.
- Divide & Conquer: Splits problem into independent subproblems, solves recursively, and merges (Merge Sort, Quick Sort, Binary Search).
- Greedy: Picks the locally optimal choice at each step without backtracking (Kruskal, Prim, Dijkstra). Counterexample: For coins
{1, 7, 10}making15, Greedy picks10 + 1 + 1 + 1 + 1 + 1(6 coins), whereas optimal is7 + 7 + 1(3 coins). - Dynamic Programming: Solves overlapping subproblems once and stores results (Floyd-Warshall, Knapsack).
- Backtracking & Branch and Bound: Explores state-space trees and prunes invalid or suboptimal branches (N-Queens).
Characteristics of an Algorithm (from the notes)
An algorithm is a finite, step-by-step procedure to solve a problem. Every algorithm must be:
| Characteristic | Meaning |
|---|---|
| Unambiguous | Each step and its inputs/outputs have exactly one meaning |
| Input | Zero or more well-defined inputs |
| Output | At least one well-defined output |
| Finiteness | Terminates after a finite number of steps |
| Feasibility | Can be carried out with available resources |
| Independent | Written as steps that do not depend on any programming language |
Categories of algorithms by task: Search (find an item), Sort (arrange in order), Insert (add an item), Update (change an existing item), Delete (remove an item).
Pseudocode and Primitive Operations
Pseudocode is a high-level, language-independent description of an algorithm-more structured than English, less detailed than a program. For analysis we count primitive operations, each assumed to take constant time:
- assigning a value to a variable,
- calling a method / returning from it,
- performing an arithmetic operation,
- comparing two numbers,
- indexing into an array, following a pointer.
Types of Algorithms (Design Strategies)
| Type | Idea | Example |
|---|---|---|
| Simple recursive | Solve the base case directly, recur on a simpler subproblem | Factorial, counting list elements |
| Backtracking | Depth-first search of choices; undo a choice that fails | 8-Queens, maze solving |
| Divide and conquer | Split into independent subproblems, solve, combine | Merge sort, quick sort, binary search |
| Dynamic programming | Remember answers of overlapping subproblems | Fibonacci with a table, 0/1 knapsack |
| Greedy | Take the best-looking choice at each step | Kruskal, Prim, Dijkstra, coin change |
| Branch and bound | Explore a tree of choices, prune branches whose bound is worse than the best found | 0/1 knapsack, travelling salesman |
| Brute force | Try every possibility | Naive string matching, linear search |
| Randomized | Use a random number to make a decision | Randomized quick sort (random pivot) |
Rules for Asymptotic Notation
- Drop constant factors: if f(n) = c · g(n) then f(n) = O(g(n)).
- Drop lower-order terms: in a polynomial of degree d, f(n) = O(nd).
- Sum rule: if T1 = O(f) and T2 = O(g), then T1 + T2 = O(max(f, g)).
- Product rule: T1 · T2 = O(f · g) (nested loops multiply).
- Use the smallest possible class: say 2n is O(n), not O(n2); say 3n + 5 is O(n), not O(3n).
Worked Big-O Proofs (notes' examples)
| f(n) | Big-O | Witnesses (c, n₀) |
|---|---|---|
| 7n - 3 | O(n) | 7n - 3 ≤ 7n for n ≥ 1, so c = 7, n₀ = 1 |
| 50 n log n | O(n log n) | c = 50, n₀ = 1 |
| 8n2 log n + 5n2 + n | O(n2 log n) | ≤ 8n2log n + 5n2log n + n2log n = 14 n2 log n for n ≥ 2; c = 14, n₀ = 2 |
| 5n2 + 3n + 20 | O(n2) | ≤ 5n2 + 3n2 + 20n2 = 28n2 for n ≥ 1; c = 28, n₀ = 1 |
| n(n+1)/2 | O(n2) | = n2/2 + n/2 ≤ n2 for n ≥ 1; c = 1, n₀ = 1 |
Dependent (Triangular) Loops
for (i = 1; i <= n; i++)
for (j = 1; j <= i; j++)
sum++; /* runs 1 + 2 + ... + n times */The inner loop runs i times, so the total is 1 + 2 + … + n = (n(n+1) / 2) = O(n2). A loop whose control variable is multiplied (i = i * 2) runs log2 n times, so a linear loop around it gives O(n log n).
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Define Asymptotic Notations (O, Ω, Θ). Prove that 4n + 6 = O(n) and 4x2 - 5x + 3 = O(x2). (7.5 Marks)
- Write: Formal inequality definitions with constants c, n0, graph/growth hierarchy (1 < log n < n < n log n < n2 < 2n < n!), and step-by-step coefficient-sum proofs.
- Q2. Differentiate between Divide & Conquer, Greedy, and Dynamic Programming with one example each. (5 Marks)
- Write: Subproblem independence (D&C: Merge Sort), local optimal choice without backtracking (Greedy: Kruskal's + coin-change counterexample), overlapping subproblems + memoization (DP: Floyd-Warshall).