├── .gitignore ├── README.md ├── constant.py ├── examples ├── example1.js ├── example2.js ├── example3.js ├── example4.js ├── example5.js ├── example6.js └── example7.js ├── exponential.py ├── factoial_problem.py ├── graphs ├── constant_vs_linear.png ├── linear_vs_logarithmic.png ├── linear_vs_logarithmic_vs_loglinear.png ├── linear_vs_polynomial.png └── polynomial_vs_exponential_2.png ├── images ├── big_o_explanation.png ├── big_o_graph.png ├── example5_and_example6.jpg ├── example7.jpg ├── exponential.HEIC ├── factorial.HEIC ├── how_to_simplify.png └── simplifying.png ├── lecture_notes ├── constant.png ├── exponential.png ├── factorial.png ├── linear.png ├── loglinear_graph.png ├── logrithmic_1.png ├── logrithmic_2.png ├── polynomials.png ├── simplify_product.png └── simplify_sums.png ├── linear.py ├── logarithmic.py ├── loglinear.py └── polynomial.py /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Algorithms and Complexity { `Big O` } 3 | 4 | Time Complexity - a measure of how fast an algorithm runs. Time complexity is a central concept in the field of algorithms and in coding interviews. It's expressed using Big O Notation. 5 | 6 | Space Complexity - a measure of how much auxillary memory an algorithm takes up. Space complexity is a central concept in the field of algorithms and in coding interviews. Also expressed using Big O Notation. 7 | 8 | -------------------------------------------------------------------------------- /constant.py: -------------------------------------------------------------------------------- 1 | # Constant - O(1) 2 | 3 | def constant_1(n): 4 | return n * 2 + 1 5 | # T(2) => O(1) 6 | 7 | def constant_2(n): 8 | for i in range(0, 1000): 9 | print(i) 10 | # T(100) => O(1) 11 | 12 | 13 | ```js 14 | function constant_2(n) { 15 | for (let i = 1; i <= 100, i++) { 16 | console.log(i) 17 | } 18 | } 19 | 20 | function constant_1(n) { 21 | return n * 2 + 1 22 | } 23 | 24 | ``` 25 | -------------------------------------------------------------------------------- /examples/example1.js: -------------------------------------------------------------------------------- 1 | function example1(array) { 2 | for (let i = 0; i <= 20; i++) { 3 | for (let i = 0; i < array.length; i++) { 4 | // do something 5 | } 6 | } 7 | } 8 | 9 | // Time Complexity ? 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | // T (20n) => O(n) -------------------------------------------------------------------------------- /examples/example2.js: -------------------------------------------------------------------------------- 1 | function example2(array) { 2 | for (let i = 0; i <= array.length; i++) { 3 | for (let i = 0; i < array.length; i++) { 4 | // do work 5 | } 6 | } 7 | 8 | for(let k = 0; k < array.length; k++) { 9 | // do more work 10 | } 11 | } 12 | 13 | // Time Complexity ? 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | // T(n^2 + n) => O(n^2) -------------------------------------------------------------------------------- /examples/example3.js: -------------------------------------------------------------------------------- 1 | function example3(n) { 2 | for (let i = 0; i < (n / 2); i++) { 3 | // make some money 4 | } 5 | } 6 | 7 | // Big O ? 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | // T(n/2) -> T((1/2)*n) -> O(n) -------------------------------------------------------------------------------- /examples/example4.js: -------------------------------------------------------------------------------- 1 | function example4(n) { 2 | if (n === 0) return; 3 | 4 | for (let i = 1; i <= 23; i++) { 5 | // let's go!!! 6 | } 7 | 8 | example4(n - 1) 9 | } 10 | 11 | // Example with 5 12 | // Recursive chain 13 | /* 14 | 5 15 | | 16 | 4 17 | | 18 | 3 19 | | 20 | 2 21 | | 22 | 1 23 | | 24 | 0 25 | */ 26 | 27 | // For every single recursive call, we have a loop 28 | 29 | 30 | 31 | 32 | 33 | 34 | // T(23n) -> O(n) 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /examples/example5.js: -------------------------------------------------------------------------------- 1 | function example5(n) { 2 | if (n <= 1) return; 3 | 4 | example5(n - 1); 5 | example5(n - 1); 6 | example5(n - 1); 7 | example5(n - 1); 8 | } 9 | 10 | // Example with 5 11 | // Viual on images/example5.jpg 12 | 13 | 14 | // Big O 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | // O(4^n) -------------------------------------------------------------------------------- /examples/example6.js: -------------------------------------------------------------------------------- 1 | function example6(n) { 2 | if (n <= 1) return; 3 | for (let i = 1; i <= 4; i++) { 4 | example6(n - 1) 5 | } 6 | } 7 | 8 | // Example with 5 9 | 10 | 11 | // 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | // notice similar, if not the same as example5.js 31 | // O(4^n) 32 | -------------------------------------------------------------------------------- /examples/example7.js: -------------------------------------------------------------------------------- 1 | // Keep in mind that this input is a string 2 | function example7(str) { 3 | if (str.length <= 1) return; 4 | 5 | let midIdx = Math.floor(str.length / 2); 6 | let left = str.slice(0, midIdx); 7 | let right = str.slice(midIdx); 8 | 9 | example7(left); 10 | example7(right); 11 | } 12 | 13 | // Big O? 14 | 15 | // When consider Big O, we should bare in mind the Big O for any hidden methods 16 | // of methods used in the logic. 17 | 18 | // Math.floor() is a constant operation 19 | // str.slice() is a linear because you don't know the length of the string making it (n) 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | // O(nlog(n)) 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /exponential.py: -------------------------------------------------------------------------------- 1 | # Exponential - O(2^n) 2 | def exponential_2n(n): 3 | if (n == 1): 4 | return 5 | exponential_2n(n - 1) 6 | exponential_2n(n - 1) 7 | 8 | # If we pass in 4, we are going to double our calls every single time 9 | 10 | # Exponential - O(3^n) 11 | def exponential_3n(n): 12 | if (n == 1): 13 | return 14 | exponential_2n(n - 1) 15 | exponential_2n(n - 1) 16 | exponential_2n(n - 1) 17 | # If we pass in 4, we are going to triple our calls every single time 18 | 19 | # Exponential - O(2^n) 20 | # function exponential_2n(n) { 21 | # if (n === 1) return; 22 | # exponential_2n(n - 1); 23 | # exponential_2n(n - 1); 24 | # } 25 | 26 | # Exponential - O(3^n) 27 | # function exponential_3n(n) { 28 | # if (n === 1) return; 29 | # exponential_2n(n - 1); 30 | # exponential_2n(n - 1); 31 | # exponential_2n(n - 1); 32 | # } -------------------------------------------------------------------------------- /factoial_problem.py: -------------------------------------------------------------------------------- 1 | # Factorial 2 | # DISCLAIMER: Not showing factorial solution, just an example of 3 | # what a factorial problem would look like 4 | 5 | def factorial_problem(n): 6 | if n == 1: 7 | return 8 | for i in range(1, n): 9 | factorial_problem(n - 1) 10 | 11 | # The number of times that we branch is depended on size of input 12 | # ref Graph 13 | 14 | # Javascript 15 | ```js 16 | function factorialProblem(n) { 17 | if (n === 1) return; 18 | 19 | for (let i = 1; i <= n; i++) { 20 | factorialProblem(n - 1) 21 | } 22 | } 23 | ``` 24 | -------------------------------------------------------------------------------- /graphs/constant_vs_linear.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/graphs/constant_vs_linear.png -------------------------------------------------------------------------------- /graphs/linear_vs_logarithmic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/graphs/linear_vs_logarithmic.png -------------------------------------------------------------------------------- /graphs/linear_vs_logarithmic_vs_loglinear.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/graphs/linear_vs_logarithmic_vs_loglinear.png -------------------------------------------------------------------------------- /graphs/linear_vs_polynomial.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/graphs/linear_vs_polynomial.png -------------------------------------------------------------------------------- /graphs/polynomial_vs_exponential_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/graphs/polynomial_vs_exponential_2.png -------------------------------------------------------------------------------- /images/big_o_explanation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/images/big_o_explanation.png -------------------------------------------------------------------------------- /images/big_o_graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/images/big_o_graph.png -------------------------------------------------------------------------------- /images/example5_and_example6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/images/example5_and_example6.jpg -------------------------------------------------------------------------------- /images/example7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/images/example7.jpg -------------------------------------------------------------------------------- /images/exponential.HEIC: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/images/exponential.HEIC -------------------------------------------------------------------------------- /images/factorial.HEIC: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/images/factorial.HEIC -------------------------------------------------------------------------------- /images/how_to_simplify.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/images/how_to_simplify.png -------------------------------------------------------------------------------- /images/simplifying.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/images/simplifying.png -------------------------------------------------------------------------------- /lecture_notes/constant.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/lecture_notes/constant.png -------------------------------------------------------------------------------- /lecture_notes/exponential.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/lecture_notes/exponential.png -------------------------------------------------------------------------------- /lecture_notes/factorial.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/lecture_notes/factorial.png -------------------------------------------------------------------------------- /lecture_notes/linear.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/lecture_notes/linear.png -------------------------------------------------------------------------------- /lecture_notes/loglinear_graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/lecture_notes/loglinear_graph.png -------------------------------------------------------------------------------- /lecture_notes/logrithmic_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/lecture_notes/logrithmic_1.png -------------------------------------------------------------------------------- /lecture_notes/logrithmic_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/lecture_notes/logrithmic_2.png -------------------------------------------------------------------------------- /lecture_notes/polynomials.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/lecture_notes/polynomials.png -------------------------------------------------------------------------------- /lecture_notes/simplify_product.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/lecture_notes/simplify_product.png -------------------------------------------------------------------------------- /lecture_notes/simplify_sums.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romebell/algorithms_complexity/22f5be61e70750a90955eaa50f3c304011afe9f2/lecture_notes/simplify_sums.png -------------------------------------------------------------------------------- /linear.py: -------------------------------------------------------------------------------- 1 | # Linear - O(n) 2 | 3 | def linear_1(n): 4 | for i in range(0, n): 5 | print(i) 6 | 7 | # iterative linear function 8 | 9 | def linear_2(n): 10 | if (n == 1): 11 | return 12 | linear_2(n - 1) 13 | 14 | # pass in 10 15 | # 10 -> 9 -> 8 -> 7 -> 6 ... 16 | 17 | # JavaScript 18 | ```js 19 | function linear_1(n) { 20 | for (let i = 1; i <= n; i++) { 21 | something 22 | } 23 | } 24 | 25 | function linear_2(n) { 26 | if (n === 1) { 27 | return; 28 | } 29 | 30 | linear_2(n - 1); 31 | } 32 | ``` 33 | 34 | -------------------------------------------------------------------------------- /logarithmic.py: -------------------------------------------------------------------------------- 1 | # Logarithmic - O(log(n)) 2 | def logarithmic(n): 3 | if n <= 1: 4 | return 5 | logarithmic(n / 2) 6 | 7 | # If a pass in 8, my input keeps dividing 4 8 | # We not really solving problems, but rather than getting a framework 9 | # of how the problems will look like 10 | 11 | 12 | # Javascript 13 | ```js 14 | function logarithmic(n) { 15 | if (n <= 1) return 16 | logarithmic(n / 2) 17 | } 18 | ``` 19 | -------------------------------------------------------------------------------- /loglinear.py: -------------------------------------------------------------------------------- 1 | # Loglinear - O(n*log(n)) 2 | 3 | def loglinear(n): 4 | if n <= 1: 5 | return 6 | for i in range(1, n): 7 | # something 8 | pass 9 | loglinear(n / 2) 10 | loglinear(n / 2) 11 | 12 | # 8 as input 13 | # iterate 8 times 14 | 15 | # 8 - 8 16 | # | | 17 | # 4 4 - 8 18 | # | | | | 19 | # 2 2 2 2 - 8 20 | # | | | | | | | | 21 | # 1 1 1 1 1 1 1 1 - 8 22 | # log2(8) 23 | 24 | ```js 25 | function loglinear(n) { 26 | if (n <= 1) return; 27 | for (let i = 1; i <= n; i++) { 28 | // something 29 | } 30 | loglinear(n / 2); 31 | loglinear(n / 2); 32 | } 33 | ``` 34 | -------------------------------------------------------------------------------- /polynomial.py: -------------------------------------------------------------------------------- 1 | # Polynomial - O(n^2) 2 | 3 | def quadratic(n): 4 | for i in range(1, n): 5 | for j in range(1, n): 6 | pass 7 | 8 | # Polynomial - O(n^3) 9 | def cubic(n): 10 | for i in range(1, n): 11 | for j in range(1, n): 12 | for k in range(1, n): 13 | pass 14 | 15 | # we have to look for nested loops where both depend on the size of our input 16 | 17 | ```js 18 | function quadratic(n) { 19 | for (let i = 1; i <= n; i++) { 20 | for (let j = 1; j <= n; j++) { 21 | // something 22 | } 23 | } 24 | } 25 | ``` 26 | 27 | 28 | # Polynomial - O(n^3) 29 | ```js 30 | function cubic(n) { 31 | for (let i = 1; i <= n; i++) { 32 | for (let j = 1; j <= n; j++) { 33 | for (let k = 1; k <= n; k++) { 34 | // something 35 | } 36 | } 37 | } 38 | } 39 | ``` 40 | --------------------------------------------------------------------------------