Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1.

Similar presentations


Presentation on theme: "CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1."— Presentation transcript:

1 CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

2 Running time CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick Content: Dr. Hasker 2 f(n)f(50)f(100)f(100)/f(50) 1111 log 2 n5.646.641.18 n501002 n2n2 250010,0004 n3n3 12,500100,0008 2n2n 1e151e301e15 n!3e649e1573e93

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 3

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

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

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

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 7

8 Big-O based on analysis of Code public void f() { if(isG()) { h(); x++; } else { for(int i=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 8

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 9

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 10

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 11

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

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 13

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 14

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 15

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

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

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

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 19

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 20

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 21

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 22

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 23

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 24

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


Download ppt "CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1."

Similar presentations


Ads by Google