1 / 14

Tutorial 2

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.

curtisk
Download Presentation

Tutorial 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tutorial 2 COEN 352 TawfiqJawhar

  2. 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)

  3. 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

  4. 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

  5. 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.

  6. 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; }

  7. What is big O time and space complexity? intf(int n) { if(n <= 1) return1; returnf(n-1) + f(n-1); }

  8. Let’s walk through the code

  9. Let’s walk through the code 20+ 21+ 22+ 23+… 2N= 2N+1-1 Review series and logarithm properties

  10. 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)

  11. 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

  12. 2 stacks in one array Beginning of stack 1 Beginning of stack 2

  13. 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?

  14. Reference • Cracking the Coding Interview

More Related