140 likes | 156 Views
This tutorial explains the upper and lower bounds of algorithm complexity using Big O, Big Omega, and Big Theta notation. It also discusses the importance of asymptotic analysis and provides examples of different time complexities. Additionally, it explores the design of a queue with a minimum value function.
E N D
Tutorial 2 COEN 352 TawfiqJawhar
Big O • Describes an upper bound on the unit of complexity (usually time or space). • An algorithm that prints all the values in an array • For element in array: • Print element • Can be described as O(N) where N is the size of the array • It can also be described as O(N2), O(N3), O(2N) • Or any other big O times that is at least as fast as O(N)
Big Omega Ω • Similar to big O but for lower bound. • The same algorithm before can be described as Ω(N) • Can also be described as Ω(log N) and Ω(1) • As it wont be faster that those times
Big Theta Ɵ • Both O and Ω • Printing the values in an array is both O(N) and Ω(N) so it is Ɵ(N) • Ɵ is a tight bound
Drop the constant • When using big O, theta, omega we drop the constant of the complexity • O(2N) is O(N) • O(10000000000000) is O(1) • O(N2+10000000000000000000*N) is O(N2) • You can notice that complexity like big O is not the only thing you should look at when deciding on an algorithm. Asymptotic analysis describes the scalability of an algorithm as N increases. • An algorithm with time complexity of O(N) might be worse than one with complexity of O(N2) if there’s a huge overhead cost and the input size we are using are relatively small. • In such case, an empirical test can help us decide.
What is the big O time complexity? intmin = Integer.MAX_VALUE; intmax = Integer.MIN_VALUE; for(intx: array) { if(x < min) min = x; } for(intx: array) { if(x > max) max = x; } intmin = Integer.MAX_VALUE; intmax = Integer.MIN_VALUE; for(intx: array) { if(x < min) min = x; if(x > max) max = x; }
What is big O time and space complexity? intf(int n) { if(n <= 1) return1; returnf(n-1) + f(n-1); }
Let’s walk through the code 20+ 21+ 22+ 23+… 2N= 2N+1-1 Review series and logarithm properties
Time and Space complexities? • The time big O complexity is O(2N) • The space complexity? • Hint: think of the biggest memory the program can be using at the same time. It is not O(2N)
Queue • How would you design a queue that in addition to pop and push has a function min which returns the minimum value in the stack • All three pop, push and min will have O(1) time complexity
2 stacks in one array Beginning of stack 1 Beginning of stack 2
3 stacks in one array? Possible? • Divide the array into 3 sections and use each section as an array stack • Is this design space efficient? Why? • Say you are not worried about time and you care more about space. Can you design something better?
Reference • Cracking the Coding Interview