Unit 1: Behaviour Analysis of Algorithms
1. Architectural Foundations of Algorithmic Behaviour
The Random Access Machine (RAM) Model
In computational complexity and competitive coding, algorithmic performance is analyzed against an idealized hardware model termed the Random Access Machine (RAM):
- Memory consists of an unbounded array of uniform memory cells, each capable of holding an integer or pointer of standard word size ( bits, typically 32 or 64 bits).
- Basic scalar operations execute in strictly constant time:
- Arithmetic operations: addition, subtraction, multiplication, integer division, modulo (
+,-,*,/,%). - Bitwise operations: shifts, bitwise AND, OR, XOR (
<<,>>,&,|,^,~). - Pointer dereferencing, indexing, and assignment.
- Arithmetic operations: addition, subtraction, multiplication, integer division, modulo (
- Operations on arbitrary-precision numbers (strings or big-integers with digits) are not ; they scale as or .
Mathematical Limit Hierarchy
The relative asymptotic growth rate of two complexity functions and as input size is rigorously evaluated using L'Hôpital's rule and mathematical limits:
- : grows strictly slower than ().
- : and have identical asymptotic rates of growth ().
- : grows strictly faster than ().
2. Asymptotic Notations: Formal Definitions and Set Theory
Asymptotic Upper Bound: Big-O ()
if and only if there exist positive constants and such that:
Big-O provides a worst-case asymptotic ceiling on resource growth.
Asymptotic Lower Bound: Big-Omega ()
if and only if there exist positive constants and such that:
Big-Omega represents an asymptotic floor beneath which the function cannot drop for large .
Asymptotically Tight Bound: Big-Theta ()
if and only if and . Formally:
Strict Asymptotic Bounds: Little-o () and Little-omega ()
- Little-o (): means for every positive constant , there exists such that for all . Equivalently, .
- Little-omega (): means .
3. The Canonical Complexity Class Hierarchy
Ranked from slowest growing (most efficient) to fastest exploding:
- Constant : Hash table lookup, stack push/pop, array indexing.
- Double Logarithmic : Interpolation search, van Emde Boas trees.
- Logarithmic : Binary search, balanced BST operations, binary exponentiation.
- Square Root : Trial division primality testing, Mo's algorithm, square root decomposition.
- Linear : Two pointers, Kadane's algorithm, prefix sum construction, linear sieve.
- Linearithmic : Merge sort, quicksort (average), segment tree construction.
- Polynomial : Nested grid iterations, Floyd-Warshall, matrix multiplication.
- Exponential & Factorial : Subsets, bitmask DP, permutations, exhaustive brute force.
4. Analysis Paradigms: Worst, Average, Best, and Amortized
Input-Dependent Behaviors
- Worst-Case Analysis : The maximum number of basic operations taken over all possible inputs of size . This is the standard metric in competitive programming because contest judges deliberately submit adversarial test cases.
- Average-Case Analysis : The expected value over a probabilistic distribution of inputs. For example, randomized Quicksort runs in expected time.
- Best-Case Analysis : The minimum number of steps taken over any input of size (e.g., for insertion sort on an already sorted array).
Amortized Analysis Techniques
Amortized analysis averages the running time of a sequence of operations over time, guaranteeing an average cost per operation even if individual steps are occasionally expensive:
- Aggregate Method: Show that a sequence of operations takes total time in the worst case; the amortized cost per operation is .
- Accounting (Banker's) Method: Charge artificial credits (amortized cost) to cheap operations. These stored credits pay for subsequent expensive operations.
- Potential Method (Physicist's Method): Define a potential function mapping the data structure state to a non-negative real number (). The amortized cost of the -th operation is:
Summing over operations: .
5. Practical Competitive Programming Budgets and Scaling Rules
The Operations Per Second Benchmark
Standard online judge CPU cores (Codeforces, AtCoder, CSES, LeetCode) execute roughly simple integer/bitwise operations per second:
- Time Limit = second operations.
- Time Limit = seconds operations.
Input Constraint-to-Algorithm Mapping Table
| Constraint on | Target Complexity Bound | Typical Algorithmic Approaches |
|---|---|---|
| Permutations, Travelling Salesperson (TSP) via Bitmask DP | ||
| Subset generation, meet-in-the-middle, bitmask dynamic programming | ||
| 4-nested loops, Gaussian elimination, dynamic programming on quadruplets | ||
| Floyd-Warshall all-pairs shortest paths, cubic interval DP, matrix multiplication | ||
| All-pairs comparison, insertion sort, quadratic DP, grid BFS | ||
| Sorting, Segment Trees, Fenwick Trees (BIT), divide-and-conquer | ||
| Two pointers, monotonic stacks, prefix sums, linear sieves | ||
| Sieve to , binary search, Euclidean GCD, matrix exponentiation | ||
| Fast exponentiation, Miller-Rabin test, closed-form formulas |
Memory Budgeting and Stack Overflow Prevention
- Memory Limit: Typically MB.
- .
- A 32-bit
inttakes 4 bytes. An array of integers takes MB. - An array of size integers consumes MB, triggering Memory Limit Exceeded (MLE).
- Call Stack Limit: Linux platforms allocate an 8 MB default stack. Recursive depth exceeding nested frames triggers Runtime Error / Stack Overflow (SIGSEGV). Deep recursions (DFS on trees/graphs) must be converted to iterative stacks or use custom stack pragmas.