CMSC 250 040 x Fall 2018 Lecture 27

  • Slides: 19
Download presentation
CMSC 250 040 x Fall 2018 Lecture 27: “Big-Oh” notation O(2) Roger Eastman Clyde

CMSC 250 040 x Fall 2018 Lecture 27: “Big-Oh” notation O(2) Roger Eastman Clyde Kruskal (slide credits Jason Flippou)

Analyze code: Read and sort file • File in. File = open( System. in.

Analyze code: Read and sort file • File in. File = open( System. in. read() ); while (in. File. has. Next()) a[i++] = parse(in. File. next()); for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { if (a[j] > a[j + 1]) { swap(a[j], a[j + 1]); }}} • Operation count? • Open file? • Read file? • Sort file? // Open // Read // Sort

Analyze code: Read and sort file • File in. File = open( System. in.

Analyze code: Read and sort file • File in. File = open( System. in. read() ); while (in. File. has. Next()) a[i++] = parse(in. File. next()); for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { if (a[j] > a[j + 1]) { swap(a[j], a[j + 1]); }}} • Operation count? • Open file? • Read file? • Sort file? O(1) O(n 2) // Open // Read // Sort Detailed time complexity: T(n) = k 1 + k 2*n + k 3*n 2 Dominant term: n 2 So O(n 2)

Observations on T(n) = k 1 + k 2*n + k 3*n 2 •

Observations on T(n) = k 1 + k 2*n + k 3*n 2 • What are we counting? • How do they relate to actual time? • Do the relative values of k 1, k 2 and k 3 matter?

Observations on T(n) = k 1 + k 2*n + k 3*n 2 •

Observations on T(n) = k 1 + k 2*n + k 3*n 2 • What are we counting? • Depends – are we counting all operations or just major ones? • How do they relate to actual time? • If k 2 is a file access, how does that relate to k 3, with array accesses? • Do the relative values of k 1, k 2 and k 3 matter? • Yes. If file access is 10000 x longer than array access, then for short lists the linear term could dominate – but, under big Oh, that is approximated away • Moral: don’t ignore the constants

What are the big oh of these algorithms? • Linear search • Perfect hashing

What are the big oh of these algorithms? • Linear search • Perfect hashing search • Binary search • Sorting – Selection • Sorting – Quicksort • Printing all permutations • Naïve knapsack algorithm • Choose k of n items that sum as close to m as possible

What are the big oh of these algorithms? • Linear search • Perfect hashing

What are the big oh of these algorithms? • Linear search • Perfect hashing search • Binary search • Sorting – Selection • Sorting – Quicksort • Printing all permutations • Naïve knapsack algorithm O(n) O(1) O(log n) O(n 2) O(n log n) O(n!) O(2 n) • Choose k of n items that sums as close to m as possible • Why? Choosing best among all subsets of n items

Does the log base matter? •

Does the log base matter? •

What are the big oh of these algorithms? •

What are the big oh of these algorithms? •

Big oh of naïve primality? • Is the natural number n prime? for (int

Big oh of naïve primality? • Is the natural number n prime? for (int d = 2; d < n; d++) if (n mod d == 0) return false; return true;

Big oh of naïve primality? • Is the natural number n prime? for (int

Big oh of naïve primality? • Is the natural number n prime? for (int d = 2; d < n; d++) if (n mod d == 0) return false; return true; Can we do better?

Big oh of naïve primality? • Is the natural number n prime? for (int

Big oh of naïve primality? • Is the natural number n prime? for (int d = 2; d < sqrt(n) ; d++) if (n mod d == 0) return false; return true; Can we do better?

How do these functions rank? •

How do these functions rank? •

How do these functions rank? •

How do these functions rank? •

How do these functions rank? •

How do these functions rank? •

Linear combinations. What’s the Big oh of? •

Linear combinations. What’s the Big oh of? •

Linear combinations. What’s the Big oh of? •

Linear combinations. What’s the Big oh of? •