250 likes | 435 Views
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
E N D
CS-2852Data Structures • Week 10, Class 1 • Lab 8 notes • Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick
Running time CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick Content: Dr. Hasker
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick