Presentation is loading. Please wait.

Presentation is loading. Please wait.

11.2 Complexity Analysis. Complexity Analysis As true computer scientists, we need a way to compare the efficiency of algorithms. Should we just use a.

Similar presentations


Presentation on theme: "11.2 Complexity Analysis. Complexity Analysis As true computer scientists, we need a way to compare the efficiency of algorithms. Should we just use a."— Presentation transcript:

1 11.2 Complexity Analysis

2 Complexity Analysis As true computer scientists, we need a way to compare the efficiency of algorithms. Should we just use a stopwatch to time our programs? No, different processors will cause the same program to run a different speeds. Instead, we will count the number of operations that must run.

3 Counting Operations public static double avg(int[] a, int n){ double sum=0; for(int j=0; j<n; j++) sum+=a[j]; return (sum/n); } n = number of elements There is one operation in the for-loop Loop runs n times 1 loop comparison 1 loop increment 1 operation 3n operations Total Operations: 3n + 3 1 operation here here & here = 3 operations

4 Grouping Complexities So the run-time of our average function depends on the size of the array. Here are some possibilities: Array size 1: runtime = 3(1) + 3 = 6 Array size 50: runtime = 3(50) + 3 = 153 Array size 1000: runtime = 3(1000) + 3 = 3003 Notice that increasing the size of the array by a constant multiple has approximately the same effect on the runtime.

5 Grouping Complexities In order to compare runtimes, it is convenient to have some grouping schema. Think of runtimes as a function of the number of inputs. How do mathematicians group functions?

6 Functions How might you “describe” these functions? What “group” do they belong to? f(x) = 3x + 5 f(x) = 4x 2 + 15x + 90 f(x) = 10x 3 – 30 f(x) = log(x+30) f(x) = 2 (3x + 3) Linear Quadratic Cubic Logarithmic Exponential

7 Big-O Notation Instead of using terms like linear, quadratic, and cubic, computer scientists use big-O notation to discuss runtimes. A function with linear runtime is said to be of order n. The shorthand looks like this: O(n)

8 Functions If the following are runtimes expressed as functions of the number of inputs (x), we would label them as follows: f(x) = 3x + 5 f(x) = 4x 2 + 15x + 90 f(x) = 10x 3 – 30 f(x) = log(x+30) f(x) = 2 (3x + 3) O(n) O(n 2 ) O(n 3 ) O(log n) O(2 n )

9 O(n) Method public static double avg(int[][] a){ int sum=0, count=0; for(int j=0; j<a.length; j++){ for(int k=0; k<a[j].length; k++){ sum+=a[j][k]; count++; } } return ((double)sum/count); } 2 assignments here Operation Count = 2Operation Count = 4 Loop runs a constant number of times, r. This causes 2r operations n is to the first power: O(n) Operation Count = 6Operation Count = 6 + 2rOperation Count = 2n + 6 + 2rOperation Count = 5n + 6 + 2r 1 cast & 1 division = 2 operations 2 loop assignments This loop runs k times for each outer loop iteration. These 3 ops run rk times. rk = n (# elements) 3n operations This causes 2rk operations. rk = n (# elements) 2n operations

10 O(n 2 ) Method public static void bubble(int[] a, int n){ for (i=0; i<n-1; i++) {//how many are sorted for (j=0; j<n-1-i; j++)//unsorted part if (a[j+1] < a[j]) //compare neighbors swap(a, j, j+1); } } Operation Count = 1 1 Loop assignments. Operation Count = 1 + 3(n-1)Operation Count = 3n – 2 Inner loop has n-1 iterations n-2 iterations n-3 iterations ….. 1 iteration Total = n(n-1)/2 iterations 2 loop ops 1 comparison + 3 ops for swap 6 operations On Each iteration 6[n(n-1)/2] =3n(n-1) =3n 2 -3n Operations from inner loop Operation Count = 3n – 2 + 3n 2 – 3nOperation Count = 3n 2 – 2 3 operations for each of the n-1 iterations.

11 The common Big-O values O(1) : constant O(log n): Logarithmic O(n) : Linear O(n logn) : n logn O(n 2 ): Quadratic O(n 3 ): Cubic O(2 n ): exponential

12 # of inputs # operations O(n 3 ) O(n 2 ) O(nlogn) O(n) O(logn) O(1) Algorithms are categorized by how their runtime increases in relation to the number of inputs.


Download ppt "11.2 Complexity Analysis. Complexity Analysis As true computer scientists, we need a way to compare the efficiency of algorithms. Should we just use a."

Similar presentations


Ads by Google