


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



2 1. The Core Concepts 3 What is Big O? 4 Big O notation describes the growth rate of an algorithm's time or space requirements as the input size (nn 5 n) grows. 6 7 It does NOT measure exact seconds or bytes. It measures how the algorithm scales. 8 Hardware Independent: An O(n)O(n) 9 O(n) algorithm on a supercomputer and a potato PC both scale at the same rate. 10 11 Time vs. Space Complexity: 12 13 Time Complexity: How the runtime grows as input grows. 14 Space Complexity: How the extra memory (auxiliary space) grows as input grows. (Note: We usually don't count the space taken by the input itself, just the extra space the algorithm needs). 15 16 The "Cases" of Complexity: 17 18 Worst-Case (Big O): The maximum number of operations. This is the default when people say "Big O". 19 Best-Case (Big Omega - Ω\Omega 20 Ω): The minimum number of operations. 21 Average-Case (Big Theta - Θ\Theta 22 Θ): The expected number of operations over all possible inputs. 23 24 2. The Big O Hierarchy (Fastest to Slowest) 25 Memorize this order. If you are comparing two algorithms, the one lower on this list is better. 26 27 O(1)O(1) 28 O(1) - Constant: 29 Meaning: Takes the exact same time regardless of input size. 30 Example: Accessing an array index arr[5], pushing/popping from a stack. 31 O(logn)O(\log n) 32 O(logn) - Logarithmic: 33 Meaning: The problem size is cut in half at each step. Extremely efficient. 34 Example: Binary Search. (If you have 1,000,000 items, it takes at most ~20 steps to find the target). 35 O(n)O(n) 36 O(n) - Linear: 37 Meaning: Time grows directly proportional to input size. 38 Example: A simple for loop iterating through an array. 39 O(nlogn)O(n \log n) 40 O(nlogn) - Linearithmic: 41 Meaning: Slightly slower than linear. This is the mathematical limit for comparison-based sorting. 42 Example: Merge Sort, Quick Sort, Heap Sort. 43 O(n2)O(n^2) 44 O(n2) - Quadratic: 45 Meaning: Time grows proportionally to the square of the input size. 46 Example: Nested loops (a loop inside a loop). Bubble Sort, Insertion Sort. 47 O(2n)O(2^n) 48 O(2n) - Exponential: 49 Meaning: Operations double with every addition to the input. Terrible for large inputs. 50 Example: Recursive Fibonacci (without memoization), generating all subsets of a set. 51 O(n!)O(n!) 52 O(n!) - Factorial: 53 Meaning: The worst possible scaling. 54 Example: Brute-forcing the Traveling Salesperson Problem (calculating every possible permutation). 55 56 3. The 4 Rules for Calculating Big O 57 When looking at a piece of code, use these rules to simplify the math: 58 Rule 1: Drop the Constants 59 Big O cares about the curve, not the exact multiplier.
61 O(2n)→O(n)O(2n) \rightarrow \mathbf{O(n)} 62 O(2n)→O(n) 63 O(500)→O(1)O(500) \rightarrow \mathbf{O(1)} 64 O(500)→O(1) 65 O(n2+5n)→O(n2)O(n^2 + 5n) \rightarrow \mathbf{O(n^2)} 66 O(n2+5n)→O(n2) 67 68 Rule 2: Drop Non-Dominant Terms 69 If an algorithm has multiple steps, keep only the largest/most dominant term. 70 71 Code has a loop of nn 72 n, then a nested loop of n2n^ 73 n2. Total = O(n2+n)→O(n2)O(n^2 + n) \rightarrow \mathbf{O(n^2)} 74 O(n2+n)→O(n2). 75 76 Rule 3: Sequential Steps = ADD 77 If you do one thing, then do another thing, you add their complexities. 78 79 Loop A (size n) + Loop B (size m) = O(n+m)\mathbf{O(n + m)} 80 O(n+m) 81 82 Rule 4: Nested Steps = MULTIPLY 83 If you do a thing inside another thing, you multiply their complexities. 84 85 Loop A (size n) containing Loop B (size m) = O(n×m)\mathbf{O(n \times m)} 86 O(n×m) (or O(n2)O(n^2) 87 O(n2) if both are size nn 88 n). 89 90 4. Sorting Algorithms Cheat Sheet 91 Sorting is heavily tested. Know the Time and Space complexities for these: 92 Algorithm 93 94 Best Case 95 96 Average Case 97 98 Worst Case 99 100 Space 101 102 Stable? 103 Bubble Sort 104 105 O(n)O(n) 106 O(n) 107 108 O(n2)O(n^2) 109 O(n2) 110 111 O(n2)O(n^2) 112 O(n2) 113 114 O(1)O(1) 115 O(1) 116 117 Yes 118 Insertion Sort 119 120 O(n)O(n) 121 O(n) 122 123 O(n2)O(n^2) 124 O(n2) 125 126 O(n2)O(n^2) 127 O(n2) 128
196 O(n2) happens if you pick a terrible pivot (like the smallest/largest element every time). Randomized pivots prevent this. 197 198 5. Searching & Traversal Algorithms 199 Searching: 200 201 Linear Search: O(n)O(n) 202 O(n) time, O(1)O(1) 203 O(1) space. (Just check every item). 204 Binary Search: O(logn)O(\log n) 205 O(logn) time, O(1)O(1) 206 O(1) space. (Requires a sorted array). Checks the middle, eliminates half the data, repeats. 207 208 Tree/Graph Traversals: 209 210 Breadth-First Search (BFS): Explores level by level. Uses a Queue. Time: O(V+E)O(V + E) 211 O(V+E) (Vertices + Edges). Space: O(V)O(V) 212 O(V) for the queue. 213 Depth-First Search (DFS): Explores as deep as possible down one branch before backtracking. Uses a Stack (or Recursion). Time: O(V+E)O(V + E) 214 O(V+E). Space: O(V)O(V) 215 O(V) for the call stack. 216 217 6. Advanced Concepts (Pro-Tips for Interviews) 218 A. Amortized Analysis (The "Dynamic Array" Trick) 219 If you use a dynamic array (like Python list or Java ArrayList), the .append() or .push() operation is usually O(1)O(1) 220 O(1). 221 222 Why? Occasionally, the array runs out of space and must allocate a new, larger array and copy everything over (which takes O(n)O(n) 223 O(n)). 224 However, because this O(n)O(n) 225 O(n) event happens very rarely, the amortized (average) time is considered O(1)O(1) 226 O(1). 227 228 B. Recursion & Space Complexity 229 People often forget that recursion takes up Space Complexity on the Call Stack! 230 231 If you write a recursive function that goes nn 232 n levels deep, the Space Complexity is O(n)O(n) 233 O(n) because the system must keep nn 234 n function frames in memory simultaneously. 235 236 C. The "Recursion Tree" Trick for Time Complexity 237 If an algorithm branches out recursively (like calculating Fibonacci), draw a tree of the recursive calls. 238 239 Formula: Time=O(branchesdepth)\mathbf{Time = O(branches^{depth})} 240 Time=O(branchesdepth) 241 Example: Fibonacci recursion branches 2 ways, and the depth is nn 242 n. Therefore, Time = O(2n)O(2^n) 243 O(2n). 244 245 D. Hash Maps are your Best Friend 246 If you are trying to optimize an O(n2)O(n^2) 247 O(n2) algorithm (usually a nested loop looking for pairs/sums), ask yourself: "Can I use a Hash Map (Dictionary) to trade Space for Time?" 248 249 Storing previously seen values in a Hash Map allows O(1)O(1) 250 O(1) lookups, often dropping an O(n2)O(n^2) 251 O(n2) algorithm down to O(n)O(n) 252 O(n) time (at the cost of O(n)O(n) 253 O(n) space).