1 / 25

CS-2852 Data Structures

CS-2852 Data Structures. Week 10, Class 1 Lab 8 notes Big-O revisited. Running time. Big-O Motivation. Want to ignore minor details Focus on what really makes the difference as n grows Each function on the previous slide is in a class of its own Want to find that class

brasen
Télécharger la présentation

CS-2852 Data Structures

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. CS-2852Data Structures • Week 10, Class 1 • Lab 8 notes • Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  2. Running time CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick Content: Dr. Hasker

  3. Big-O Motivation • Want to ignore minor details • Focus on what really makes the difference as n grows • Each function on the previous slide is in a class of its own • Want to find that class • Multiplication by scalar doesn’t matter • Addition of lower-order operations doesn’t matter CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  4. Big-O Definition T(n) = O(f(n) if and only if There exist n0 and c such that T(n) ≤ cf(n) for all n > n0 CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  5. Simplifying Big-O expressions Addition • O(1) < O(log n) < O(nk) < O(kn) < O(n!) • e.g. O(nk+n!) = O(n!) • e.g. O(log n + n2) = O(n2) • e.g. O(n log n + n2) = O(n2) • e.g. O(n log n + n) = O(n log n CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  6. Simplifying Big-O expresionsMultiplication by scalar • O(kf(n)) = O(f(n)) for any fixed k • e.g. O(5) = O(1) • e.g. O(2 log2 n + 5) = O(log n) CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  7. Strategies for determining Big-O • Analysis of Code • Intuition based on • Structure of data • Analysis of Code CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  8. Big-O based on analysis of Code public void f() { if(isG()) { h(); x++; } else { for(inti=0;i<j();i++) { k(); for(A a: list) { m(); } } } // end of f max – sequence of simple expressions max – different if clauses prod – number of iterations over loop * contents of loop subs – method calls CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  9. Big-O based analysis of a Recursive algorithm • Each recursive call should be O(1) except for method calls • No loops • So order is number of recursive calls needed • Number of recursive calls can be exponential • e.g. simple implementation of Fibbonaci • Often, number of recursive calls is O(n) or even O(log n) CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  10. Example • Suppose binary search of an array is implemented recursively. What is the Big-O running time? CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  11. Example • Suppose binary search of a Red-Black tree is implement recursively. What is the Big-O running time? CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  12. Example • Suppose binary search of a simpleBinarySearchTree is implemented recursively. What is the Big-O running time? CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  13. Example • Suppose binary search of a simple BinarySearchTree is implemented iteratively (with a loop). What is the Big-O running time? CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  14. Example • Suppose we insert n items into an ArrayList using add(E). What is the Big-O running time? CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  15. Example • Suppose we insert n items into an ArrayList using add(0, E). What is the Big-O running time? CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  16. Example • Suppose we insert n items into a LinkedListusing add(0, E). What is the Big-O running time? CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  17. Example • Suppose we insert n items into a LinkedListusing add(E). What is the Big-O running time? CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  18. Example • Suppose we insert just 1 iteminto a LinkedListusing add(n/2, e). What is the Big-O running time? CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  19. Example • Suppose we insert one item into an empty hash-table. • What is the Big(O) running time? CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  20. Example • Suppose we insert n items into an empty hash-table, and then remove them • What is the Big(O) running time? • Assume: No collisions occur CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  21. Example • Suppose we insert n items into an empty hash-table, and then remove them • What is the Big(O) running time? • Assume: Collisions always occur CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  22. Example • Suppose we insert an item into a properly-implemented stack. • What is the Big-O running time? CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  23. Example • Suppose we remove an item from a properly-implemented circular queue (the wrap-around array implementation) • What is the Big-O running time? CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  24. Example • What is the Big-O running time of the word search lab? • (in terms of the size of the grid) CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

  25. CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick

More Related