|
| 1 | +/** |
| 2 | + * Big-O is important for analyzing and comparing the efficiencies of algorithms. |
| 3 | + * Big-O Rule |
| 4 | + * |
| 5 | + * 1. Coefficient rule |
| 6 | + * 2. Sum rule |
| 7 | + * 3. Product rule |
| 8 | + * 4. Transitive rule |
| 9 | + * 5. Polynomial rule |
| 10 | + * 6. Log of a power rule |
| 11 | + */ |
| 12 | + |
| 13 | +/** |
| 14 | + * |
| 15 | + * 1. Coefficient Rule. |
| 16 | + * |
| 17 | + * Coefficient simply requires you to ignore any non-input-size-related constants. |
| 18 | + * Coefficients in Big-O are negligible with large input sizes. |
| 19 | + * Any constants are negligible in Big-O notation. |
| 20 | + */ |
| 21 | + |
| 22 | +// Coefficient Rule example 01 |
| 23 | +function coefficientRuleOne(n: number) { |
| 24 | + let count: number = 0; // O(1) |
| 25 | + for (let i = 0; i < n; i++) { |
| 26 | + // O(n) |
| 27 | + count += 1; // O(n) |
| 28 | + } |
| 29 | + return count; // O(1) |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * BIG O Calculation |
| 34 | + * 1 + 1 ==> "O(1)" is 2 times |
| 35 | + * n + n ==> "O(n)" is 2 times. |
| 36 | + * 2*2n |
| 37 | + * O(n) ==> Remove constants/ |
| 38 | + */ |
| 39 | + |
| 40 | +function coefficientRuleOneTwo(limit: number) { |
| 41 | + const a: number = 5; // O(1) |
| 42 | + const b: number = 10; // O(1) |
| 43 | + const c: number = 50; // O(1) |
| 44 | + |
| 45 | + for (let i = 0; i < limit; i++) { |
| 46 | + // O(n) ***TODO:If we include for loop |
| 47 | + let x = i + 1; // O(n) |
| 48 | + let y = i + 1; // O(n) |
| 49 | + let z = i + 1; // O(n) |
| 50 | + } |
| 51 | + |
| 52 | + for (let j = 0; j < limit; j++) { |
| 53 | + // O(n) ***TODO:If we include for loop |
| 54 | + let p = j * 2; // O(n) |
| 55 | + let q = j * 2; // O(n) |
| 56 | + } |
| 57 | + |
| 58 | + const whoAmI = "I don't Know"; // O(1) |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * BIG O Calculation |
| 63 | + * 4 => "O(1)" Four big O |
| 64 | + * 7 => "O(n)" Seven big O n |
| 65 | + * n + n + n + n + n + n + n => "O(n)" |
| 66 | + * 4 + 7n |
| 67 | + * |
| 68 | + * BIG O(4 + 7n) TODO: if we calculate for loop step |
| 69 | + * O(4 + 7n) === O(n) equivalence to O(n) |
| 70 | + * n + n + n + n + n => "O(n)" |
| 71 | + * BIG O(4 + 5n) TODO: no for loop |
| 72 | + * O(4 + 5n) === O(n) equivalence to O(n) |
| 73 | + */ |
| 74 | + |
| 75 | +/** |
| 76 | + * 2. Sum Rule |
| 77 | + * Imagine a master algorithm that involves two other algorithms. |
| 78 | + * The Big-O notation of that master algorithm is simply the sum of the other two Big-O notations. |
| 79 | + */ |
| 80 | + |
| 81 | +function sumRule(boxes: string[], items: number[]) { |
| 82 | + // For boxes iteration |
| 83 | + boxes.forEach(element => { |
| 84 | + // O(n) |
| 85 | + console.log(element); |
| 86 | + }); |
| 87 | + |
| 88 | + // For items iteration |
| 89 | + items.forEach(element => { |
| 90 | + // O(n) |
| 91 | + console.log(element); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * BIG O Calculation |
| 97 | + * O(n + n) |
| 98 | + * O( a + b ) |
| 99 | + */ |
| 100 | + |
| 101 | +/** |
| 102 | + * 3. Product Rule |
| 103 | + * The product rule simply states how Big-Os can be multiplied. |
| 104 | + */ |
| 105 | + |
| 106 | +function productRuleOne(boxes: string | number[]) { |
| 107 | + for (let i = 0; i < boxes.length; i++) { |
| 108 | + // O(n) |
| 109 | + for (let j = 0; j < boxes.length; j++) { |
| 110 | + // O(n) |
| 111 | + console.log(boxes[i], boxes[j]); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +function productRuleTwo(n: number) { |
| 117 | + let count = 0; |
| 118 | + for (let i = 0; i < n; i++) { |
| 119 | + count += 1; // O(n) |
| 120 | + for (let j = 0; j < 5 * n; j++) { |
| 121 | + count += 1; // O(n) |
| 122 | + } |
| 123 | + } |
| 124 | + return count; |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * BIG O Calculation |
| 129 | + * O(n * n) |
| 130 | + * O(n^2) |
| 131 | + * O(n^2) is called Quadratic Time |
| 132 | + */ |
| 133 | + |
| 134 | +/** |
| 135 | + * 4. Polynomial Rule |
| 136 | + * If f(n) is a polynomial of degree k, then f(n) is O(nˆk). |
| 137 | + * The following code block has only one for loop with quadratic time complexity f(n) = nˆ2 because line 4 runs n*n iterations |
| 138 | + */ |
| 139 | + |
| 140 | +function polynomialRule(n: number) { |
| 141 | + let count = 0; |
| 142 | + for (let i = 0; i < n * n; i++) { |
| 143 | + count += 1; |
| 144 | + } |
| 145 | + return count; |
| 146 | +} |
| 147 | + |
| 148 | +// O(n^n) |
| 149 | +// O(n^2) |
| 150 | + |
| 151 | +/** |
| 152 | + * Exercises |
| 153 | + * Calculate the time complexities for each of the exercise code |
| 154 | + */ |
| 155 | + |
| 156 | +// Exercise 01 |
| 157 | + |
| 158 | +function exercisesOne(n: number) { |
| 159 | + for (let i = 0; i < n * 1000; i++) { |
| 160 | + // O(n) |
| 161 | + for (let j = 0; j < n * 2; j++) { |
| 162 | + // O(n) |
| 163 | + console.log(i, j); |
| 164 | + } |
| 165 | + } |
| 166 | +} // O(n^2) |
| 167 | + |
| 168 | +// Exercise 02 |
| 169 | +function exercisesTwo(n: number) { |
| 170 | + for (let i = 0; i < n; i++) { |
| 171 | + // O(n) |
| 172 | + for (let j = 0; j < n; j++) { |
| 173 | + // O(n) |
| 174 | + for (let k = 0; k < n; k++) { |
| 175 | + // O(n) |
| 176 | + for (let l = 0; l < 10; l++) { |
| 177 | + // O(10) constant time iteration |
| 178 | + console.log(i, j, k, l); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +// O(n^3*10) |
| 186 | +// O(n^3) |
| 187 | + |
| 188 | +// Exercise 03 |
| 189 | + |
| 190 | +function exercisesThree(): void { |
| 191 | + for (let i = 0; i < 1000; i++) { |
| 192 | + // O(1000) constant time iteration |
| 193 | + console.log('Hi'); |
| 194 | + } // |
| 195 | +} // O(1) |
| 196 | + |
| 197 | +// Exercise 04 |
| 198 | +function exercisesFour(n: number): void { |
| 199 | + for (let i = 0; i < n * 10; i++) { |
| 200 | + // O(n * 10) |
| 201 | + console.log('Hi'); |
| 202 | + } // |
| 203 | +} // O(n * 10) === O(n) remove constant. |
| 204 | + |
| 205 | +// Exercise 05 |
| 206 | +function exercisesFive(n: number): void { |
| 207 | + for (let i = 0; i < n; i * 2) { |
| 208 | + // O(log2n) |
| 209 | + console.log(i); |
| 210 | + } // |
| 211 | +} // O(log2n) log 2 base n. For a given n, this will operate only log2n times because i is incremented by multiplying by 2 |
| 212 | + |
| 213 | +// Exercise 06 |
| 214 | +function exercisesSix(): void { |
| 215 | + while (true) { |
| 216 | + // O(∞) Infinite loop |
| 217 | + console.log('hello'); |
| 218 | + } |
| 219 | +} // O(∞) |
0 commit comments