Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 261 - Winter 2010 Big-Oh Review. A Machine-independent Way to Describe Execution Time The purpose of a big-Oh characterization is to describe the change.

Similar presentations


Presentation on theme: "CS 261 - Winter 2010 Big-Oh Review. A Machine-independent Way to Describe Execution Time The purpose of a big-Oh characterization is to describe the change."— Presentation transcript:

1 CS 261 - Winter 2010 Big-Oh Review

2 A Machine-independent Way to Describe Execution Time The purpose of a big-Oh characterization is to describe the change in execution time relative to the change in input size in a way that is independent of issues such as machine times or compilers

3 A Simple Example Suppose we have an algorithm that is O(n) (e.g., summing elements of array) Suppose to sum 10,000 elements takes 32 ms. How long to sum 20,000 elements? If the size doubles, the execution time doubles

4 Non-linear times Suppose the algorithm is O(n 2 ) (e.g., sum elements of a two-D array) Suppose size doubles, what happens to execution time? It goes up by 4. Why 4? Need to figure out how to do this…

5 The Calculation The ratio of the big-Oh sizes should equal the ratio of the execution times N 2 T ______ = _______ (2N) 2 X (then solve for X)

6 A more complex problem Acme Widgets uses a mergesort algorithm to sort their inventory of Widgets. If the algorithm takes 66 milliseconds to sort a list of 4096 widgets, then approximately how long will it take to sort a list of 1,048,576 widgets. (Btw, 4096 is 2 12, 1,048,576 is 2 20,. Merge sort is O(n log n)

7 Setting up the formula 2 12 log 2 12 66 __________ = ___________ 2 20 log 2 20 X Solve for X. remember log 2 y is just y

8 Determining Big Oh For simple loops, just ask yourself how many times loop executed as a function of input size double sum(double * data, int n) { int i; double s = 0.0; for (i = 0; i < n; i++) s += data[i]; return s; }

9 Recursion For recursion, ask yourself (a) how many times will function be executed, and (b) how much time does it spend on each call - multiply these together double exp(double a, int n) { if (n < 0) return 1/exp(a, -n); else if (n = 0) return 1; return a * exp(a, n-1); }

10 Calling Functions When one function calls another, big-Oh of called function is considered in determining big-Oh void removeAllOccurences(double e, struct dyArray*v) { int i; for (i = dyArraySize(v)-1; i>0; i--) { if (EQ(e, dyArrayGet(v, i)))) dyArrayRemove(v, i); }

11 Logarithms I’m thinking of a number between 0 and 1024. How many guesses do you need to find my number? Ans: approx log 1024 = log 2 10 = 10 To a computer scientist, the log of n is the number of times you can split n in half. (binary search, etc).

12 Best of times, worst of times void insertionSort (double * v, int n) { int i, j; double element; for (i = 1; i < n; i++) { element = v[i]; j = i - 1; while (j >= 0 && element < v[j]) { v[j+1] = v[j]; j = j - 1; } v[j+1] = element; } } // normally we use worst case time

13 Dominating Functions When we have two big-Oh factors, one aways dominates the other void makeIdentity (int m[N][N]) { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) m[i][j] = 0; for (int i = 0; i < N; i++) m[i][i] = 1; }

14 Table of dominating functions

15 Your turn Worksheet time Before we start worksheets 9 and 10, lets do a couple of examples together. Fill in the answers in worksheet 10

16 What is the O( ?? ) int countOccurrences (double [ ] data, double testValue) { int i, count; count = 0; for (i = 0; i < data.length; i++) { if (data[i] == testValue) count++; } return count; }

17 O( ?? ) in terms of n (worst case) int isPrime (int n) { int i; for (i = 2; i * i <= n; i++) { if (0 == n % i) return 0; } return 1; /* 1 is true */ }

18 Worst case O( ?? ) void printPrimes (int n) { int i; for (i = 2; i < n; i++) { if (isPrime(i)) printf(“Value %d is prime\n”, i); }

19 Nested Loops - O( ?? ) void matMult (int [][] a, int [][] b, int [][] c) { int n = n; // assume all same size for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { c[i][j] = 0; for (k = 0; k < n; k++) c[i][j] += a[i][k] * b[k][j]; }

20 Less obvious O( ?? ) void selectionSort (double * storage, int n) { int p, indexLargest, I; for (p = n – 1; p > 0; p--) { indexLargest = 0; for (i = 1; i <= p; i++) { if (storage[i] > storage[indexLargest]) indexLargest = i; } if (indexlargest != p) swap(storage, indexLargest, p); }

21 Now do worksheets Worksheets on dominating values, determining big-Oh


Download ppt "CS 261 - Winter 2010 Big-Oh Review. A Machine-independent Way to Describe Execution Time The purpose of a big-Oh characterization is to describe the change."

Similar presentations


Ads by Google