Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 16: Big-Oh Notation

Similar presentations


Presentation on theme: "Lecture 16: Big-Oh Notation"— Presentation transcript:

1 Lecture 16: Big-Oh Notation
CSC 212 – Data Structures Lecture 16: Big-Oh Notation

2 Question of the Day What is smallest number of matchsticks moved to make this equation valid (using roman numerals) + =

3 Algorithm Analysis Approximate time to run a program with n inputs on 1GHz machine: n = 10 n = 50 n = 100 n = 1000 n = 106 O(n log n) 35 ns 200 ns 700 ns 10000 ns 20 ms O(n2) 100 ns 2500 ns 1 ms 17 min O(n5) 0.1 ms 0.3 s 10.8 s 11.6 days 3x1013 years O(2n) 1000 ns 13 days 4 x 1014 years Too long! O(n!) 4 ms

4 Big-Oh Notation Only consider very, very large data sets
Nobody cares when entire program takes 2 minutes Leading multipliers are ignored O(5n) = O(2n) = O(n) Often an implementation issue, anyway Only keep the largest terms O(n5 + n2) = O(n5) What is extra 17 min. after 3x1013 years?

5 Big-Oh Notation Number of primitive operations executed
Assignments, method calls, arithmetic operations, comparisons, indexing into arrays, following a reference, … Good, rough approximation of time Useful to compare algorithms Really good at eliminating algorithms

6 Big-Oh Rules of Thumb Primitive statements take O(1) time
Loop from 1 - n Runs O(n) times, when adding constant amount Runs O(log n) times when multiplying by constant Multiply complexities of nested loops Add complexities of consecutive loops

7 Big-Oh Measures Worst-Case
int power(int a, int b > 0) if a == 0 && b == 0 return -1; exp = 1; loop from 1 to b exp *= a; return exp; power takes O(n) in most cases Is O(1) when a & b are both 0, however Would still call this algorithm O(n)

8 Not All Nesting Is The Same
public static int sum(int[][] a) { int total = 0; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { total += a[i][j]; } } return total; } Despite nested loops, this runs in O(n) time n here would be entries in entire array

9 Handling Method Calls Method call is O(1) operation, …
… but method may take more time Must consider the complexity of the entire process Including complexity of all the methods called

10 Methods Calling Methods
public static int sumOdds(int m) { int total = 0; for (int i = 1; i <= m; i+=2) { total += i; } return total; } public static void oddSeries(int n) { for (int i = 1; i < n; i++) { System.out.println(i + “ ” + sumOdds(n)); } oddSeries calls sumOdds n times Each call does O(n) work, takes O(n2) total time!

11 Justifying an Answer Important to explain your answer
Saying method is O(n) does not make it O(n) Especially true for tricky & recursive methods Process can derive difficult answer May simplify the big-Oh computation Discover that big-Oh is less than thought Does not need to be formal proof But must explain your answer

12 Big-Oh Notation Ignoring recursive call, runs in O(1) time
public static int factorial(int n) { if (n <= 2) { return 1; } else { return n * factorial(n - 1); } } Ignoring recursive call, runs in O(1) time There can at most n - 1 calls to factorial Therefore total time is n - 1 * O(1) = O(n)

13 Big-Oh Notation This function takes O(2n) time
int fibonacci(int k) if k < 1 return k else return fibonacci(k-1) + fibonacci(k - 2) This function takes O(2n) time k is 2, makes 2 calls k is 3, makes 4 calls - 3: fibonacci(2) & 1: fibonacci(1) k is 4, makes 8 calls - 5: fibonacci(3) & 3: fibonacci(2) k is n, makes 2n-1 = 2-1 * 2n = O(2n) calls

14 Your Turn Get back into groups and do activity

15 Midterm Overview Midterm results were very mixed
Max score = 99 Mean score = 79 Standard deviation = 14.03 May apply curve at end of term Will not curve any single item Some of you may need to work harder, come and ask me for help as we go on Everyone still has passing grade, however

16 Before Next Lecture… Finish week #6 assignment Finish lab #5
Programming assignment #2 next week


Download ppt "Lecture 16: Big-Oh Notation"

Similar presentations


Ads by Google