|
| 1 | +/* |
| 2 | + Session : Time Complexity analysis |
| 3 | + By : Muhammad Magdi |
| 4 | + On : 20/08/2017 |
| 5 | +*/ |
| 6 | + |
| 7 | +* What's time Complexity? |
| 8 | + How does the running time change as the size of input change. |
| 9 | + |
| 10 | +* Running time depends on: |
| 11 | + 1- Processor. |
| 12 | + 2- Read/Write speed to memory. |
| 13 | + 3- 32 bit VS 64 bit. |
| 14 | + 4- INPUT. |
| 15 | + |
| 16 | +* What are the cases your code may face? |
| 17 | + 1- Best case. |
| 18 | + 2- Worst case. |
| 19 | + 3- Average case. |
| 20 | + |
| 21 | +* The notations to represent your code's Complexity: |
| 22 | + 1- Big O Notation ----> the upper bound of the time. |
| 23 | + 2- Omega Notation ----> the lower bound of the time. |
| 24 | + 3- Theta Notation ----> the bound itself. (Calculated mathematically) |
| 25 | + |
| 26 | +* Rules: |
| 27 | + 1- Running time is the sum of running times of all consecutive blocks. |
| 28 | + 2- Nested loops are multiplied. |
| 29 | + In general -> Nested repetitive Blocks are multiplied. |
| 30 | + 3- In Conditional statements pick the "Worst case" one. |
| 31 | + 4- Drop Constants (addition, subtraction, multiplication or division). |
| 32 | + 5- Drop all lower order terms. |
| 33 | + |
| 34 | + |
| 35 | +* Some useful Observations: |
| 36 | + Big O Name Max n |
| 37 | +------------------------------------------------------------------------------------------- |
| 38 | + O(1) ----> Constant ----> 1e18 ----> Math, Observation |
| 39 | + O(Log(n)) ----> Logarithmic ----> 1e18 ----> Binary Search (lower -upper- bound) |
| 40 | + O(n) ----> Linear ----> 1e8 ----> one loop |
| 41 | + O(n*Log(n)) ----> LogLinear ----> 4e5 ----> Sorting, loop + binary search |
| 42 | + O(n^2) ----> Quadratic ----> 1e4 ----> nested loop |
| 43 | + O(2^n) ----> Exponential ----> 25 ----> Bitmasks, finding all possible answers |
| 44 | + O(n!) ----> factorial ----> 11 ----> finding all permutations |
| 45 | + |
| 46 | + |
| 47 | +int calcSum(int a, int b){ |
| 48 | + int sum = a+b; |
| 49 | + return sum; |
| 50 | +} |
| 51 | + |
| 52 | +double calcAverage(int a, int b){ |
| 53 | + double avg = (a+b)/2.0; |
| 54 | + return avg; |
| 55 | +} |
| 56 | + |
| 57 | +bool isAlphabit(char x){ |
| 58 | + return (x>='A' && x<='Z' || x>='a' && x<='z'); |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +double sumHarmonicSeries(int n){ |
| 64 | + double sum = 0; |
| 65 | + for(int i = 1 ; i <= n ; ++i){ |
| 66 | + sum += (1.0/i); |
| 67 | + } |
| 68 | + return sum; |
| 69 | +} |
| 70 | + |
| 71 | +long long calcSumSegment(int a, int b){ |
| 72 | + long long sum = 0; |
| 73 | + for(int i = a ; i<=b ; ++i) |
| 74 | + sum += i; |
| 75 | + return sum; |
| 76 | +} |
| 77 | + |
| 78 | +int fact(int n){ |
| 79 | + if(!n || n==1) return 1; |
| 80 | + return n*fact(n-1); |
| 81 | +} |
| 82 | + |
| 83 | +int power1(int base, int power){ |
| 84 | + if(!power) return 1; |
| 85 | + return base*power1(base, power-1); |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +bool binarySearch(int val){ |
| 91 | + int lo = 0, hi = n, mid; |
| 92 | + while(lo<hi){ |
| 93 | + mid = ((lo+hi)>>1); |
| 94 | + if(A[mid] == val) return 1; |
| 95 | + if(A[mid] < val) |
| 96 | + lo = mid+1; |
| 97 | + else |
| 98 | + hi = mid-1; |
| 99 | + } |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | +int calcLog(int n){ |
| 104 | + int ret = 0; |
| 105 | + while(n > 1){ |
| 106 | + ++ret; |
| 107 | + n /= 2; |
| 108 | + } |
| 109 | + return ret; |
| 110 | +} |
| 111 | + |
| 112 | +int power2(int base, int power){ |
| 113 | + if(!power) return 1; |
| 114 | + int sub = power2(base, power>>1); |
| 115 | + return (power&1? sub*sub*base : sub*sub); |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +for(int i = 0 ; i < (1<<n) ; ++i){ |
| 121 | + //some O(1) operations |
| 122 | +} |
| 123 | +/* |
| 124 | +8 4 2 1 |
| 125 | +0 0 0 1 |
| 126 | +0 0 1 0 |
| 127 | +0 1 0 0 |
| 128 | +1 0 0 0 |
| 129 | + |
| 130 | +1<<0 = 2^0 = 1 |
| 131 | +1<<1 = 2^1 = 2 |
| 132 | +1<<2 = 2^2 = 4 |
| 133 | +1<<n = 2^n |
| 134 | +*/ |
| 135 | + |
| 136 | +int fib(int n){ |
| 137 | + if(!n || n==1) return n; |
| 138 | + return fib(n-1)+fib(n-2); |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +for(int i = 0 ; i < (1<<n) ; ++i){ |
| 144 | + for(int i = 0 ; i < n ; ++i){ |
| 145 | + //some constant order statements go here |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +void searchArray(){ |
| 150 | + for(int i = 0 ; i < n ; ++i){ |
| 151 | + if(binarySearch(B[i])) |
| 152 | + puts("Found"); |
| 153 | + else |
| 154 | + puts("Not Found"); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +bool isComp[1008]; |
| 159 | +void sieve(){ |
| 160 | + isComp[0] = isComp[1] = 1; |
| 161 | + for(int i = 2 ; i < n ; ++i){ |
| 162 | + if(!isComp[i]){ |
| 163 | + for(int j = i ; j < n ; j+=i) |
| 164 | + isComp[i] = 1; |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | +void printPermutations(string s){ |
| 172 | + sort(s.begin(), s.end()); |
| 173 | + do { |
| 174 | + cout << s << endl; |
| 175 | + } while(next_permutation(s.begin(), s.end())); |
| 176 | +} |
0 commit comments