|
| 1 | +""" |
| 2 | +BigO Notation |
| 3 | + Big O Notation is used to measure hoe running time or space requirements for your program grow as input size grows |
| 4 | +Order of Magnitude |
| 5 | + A Time Complexity does not tell the exact number of times the code inside a loop is executed, |
| 6 | + but it only shows the order of magnitude. Whether code inside loop is executed for 3n, n+5, n/2 times; |
| 7 | + the time complexity of each code is O(n) |
| 8 | +
|
| 9 | +Rules for BigO Notation |
| 10 | + time = a * n + b |
| 11 | + > Keep fastest growing term |
| 12 | + BigO refers to very large value of n. Hence if you have a function like: |
| 13 | + time = 5*n^2 + 3*n + 20 |
| 14 | + when value of n is very large (b*n + c) become irrelevant |
| 15 | + Example: n = 1000 |
| 16 | + time = 5*1000^2 + 3*1000 + 20 |
| 17 | + time = 5000000 + 3020 |
| 18 | + time ~= 5000000 # terms other than 5*1000^2 contribute negligible |
| 19 | +
|
| 20 | + time = a * n # being b a constant and static |
| 21 | +
|
| 22 | + > Drop constant |
| 23 | + time = n # a being constant |
| 24 | + time = O(n) |
| 25 | +
|
| 26 | +Measuring running time growth (Time Complexity) |
| 27 | +
|
| 28 | + Constant Time Complexity O(1) |
| 29 | + Running time of Constant-time algorithm does not depend on the input size |
| 30 | + time = a |
| 31 | + time = O(1) # applying rules for Big O :: Keep fastest growing term (a) ->->-> Drop Constants(a) |
| 32 | +
|
| 33 | +
|
| 34 | + def foo(a): # time is nearly constant with increase in input size |
| 35 | + size(arr) : 100 -> 0.22 milliseconds |
| 36 | + size(arr) : 1000 -> 0.23 milliseconds |
| 37 | +
|
| 38 | + Logarithmic Time Complexity O(log n) |
| 39 | + A logarithmic algorithm often halves the input size at each step. The running time of such an algorithm is |
| 40 | + logarithmic, because log2 n equals the number of times n must be divided by 2 to get 1 |
| 41 | +
|
| 42 | + Square Root Algorithm O(sqrt(n)) |
| 43 | + A square root algorithm is slower than O(log n) but faster than O(n) A special property of square roots is that |
| 44 | + sqrt(n) = n/(sqrt(n)), so the square root (sqrt(n)) lies, in some sense, in the middle of the input |
| 45 | +
|
| 46 | + Linear Time Complexity O(n) |
| 47 | + A linear algorithm goes through the input a constant number of times |
| 48 | + time = a * n + b |
| 49 | + time = O(n) # applying rules for Big O :: Keep fastest growing term (a*n) ->->-> Drop Constants(a, b) |
| 50 | +
|
| 51 | + def foo(arr): |
| 52 | + size(arr) : 100 -> 0.22 milliseconds |
| 53 | + size(arr) : 1000 -> 2.30 milliseconds |
| 54 | +
|
| 55 | + O(n log n) |
| 56 | + This time complexity often indicates that the algorithm sorts the input, because the time complexity of |
| 57 | + efficient sorting algorithms is O(n logn) |
| 58 | +
|
| 59 | + Quadratic Time Complexity O(n^2) |
| 60 | + A quadratic time complexity often contains two nested loops |
| 61 | + time = a * (n^2) + b |
| 62 | + time = O(n^2) # applying rules for Big O :: Keep fastest growing term (a*n^2) ->->-> Drop Constants(a, b) |
| 63 | + Exception O(n^2) not O(n^2 + n) |
| 64 | + time = a*(n^2) + (b * n) + c |
| 65 | + time = n^2 # applying rules for Big O :: Keep fastest growing term (a*n^2) ->->-> Drop Constants(a, b, c) |
| 66 | +
|
| 67 | + Cubic Time Complexity O(n^3) |
| 68 | + A cubic algorithm often contains three nested loops |
| 69 | +
|
| 70 | + O(2^n) |
| 71 | + This time complexity often indicates that the algorithm iterates through all subsets of the input elements |
| 72 | +
|
| 73 | + O(n!) |
| 74 | + This time complexity often indicates that the algorithm iterates through all permutations of the input elements |
| 75 | +
|
| 76 | + Note: |
| 77 | + An algorithm is polynomial if its time complexity is at most O(nk) where k is a constant. |
| 78 | + All the above time complexities except O(2n) and O(n!) are polynomial |
| 79 | +
|
| 80 | +Given the input size, we can try to guess the required time complexity of the algorithm that solves the problem. |
| 81 | +The following table contains some useful estimates assuming a time limit of one second |
| 82 | +
|
| 83 | + n <= 10 O(n!) |
| 84 | + n <= 20 O(2n) |
| 85 | + n <= 500 O(n3) |
| 86 | + n <= 5000 O(n2) |
| 87 | + n <= 106 O(n logn) or O(n) |
| 88 | + n is large O(1) or O(log n) |
| 89 | +
|
| 90 | +
|
| 91 | +""" |
| 92 | +""" Time Complexity """ |
| 93 | + |
| 94 | +# Linear Time Complexity O(n) |
| 95 | +def squareNum(numbers): |
| 96 | + """ |
| 97 | + Time Complexity: O(n) :: loop will iterate n time linearly |
| 98 | + """ |
| 99 | + sqNum = [] # empty array for squared numbers initialised |
| 100 | + for n in numbers: # loop will iterate n times |
| 101 | + sqNum.append(n*n) |
| 102 | + return sqNum |
| 103 | + |
| 104 | + |
| 105 | +# Constant Time Complexity O(1) |
| 106 | +def findPE(prices, eps, index): |
| 107 | + pe = prices[index] / eps[index] |
| 108 | + return pe |
| 109 | + |
| 110 | + |
| 111 | +# O(n^2) |
| 112 | +def duplicateInArray(numbers): |
| 113 | + """ Running two for loops""" |
| 114 | + duplicate = None |
| 115 | + # (n^2) iterations |
| 116 | + for i in range(len(numbers)): |
| 117 | + for j in range(i+1, len(numbers)): |
| 118 | + if numbers[i] == numbers[j]: |
| 119 | + print(f"{numbers[i]} is duplicate") |
| 120 | + duplicate = numbers[i] |
| 121 | + break |
| 122 | + # n iterations |
| 123 | + for i in range(len(numbers)): |
| 124 | + if numbers[i] == duplicate: |
| 125 | + print(i) |
| 126 | + |
| 127 | + |
| 128 | +""" Space Complexity """ |
| 129 | +def Search(numbers): |
| 130 | + """ |
| 131 | + Search for 10 in list |
| 132 | + Time Complexity: O(n) |
| 133 | + """ |
| 134 | + for i in range(len(numbers)): |
| 135 | + if numbers[i] == 10: |
| 136 | + print(i) |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == '__main__': |
| 142 | + # O(n) |
| 143 | + numbers = [2, 5, 7, 9] |
| 144 | + print(squareNum(numbers)) |
| 145 | + |
| 146 | + # O(1) |
| 147 | + prices = [250, 500, 450] |
| 148 | + eps = [25, 50, 75] |
| 149 | + index = 1 |
| 150 | + print(findPE(prices, eps, index)) |
| 151 | + |
| 152 | + # O(n^2) |
| 153 | + numbers = [2, 4, 6, 8, 14, 9, 4] |
| 154 | + duplicateInArray(numbers) |
| 155 | + |
| 156 | + numbers = [1, 6, 8, 10, 4, 5] |
| 157 | + Search(numbers) |
| 158 | + |
0 commit comments