1 / 12

11.2 Complexity Analysis

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 stopwatch to time our programs? No, different processors will cause the same program to run a different speeds.

nay
Download Presentation

11.2 Complexity Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. 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 1 operation here here & here = 3 operations 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);} Total Operations: 3n + 3

  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) = 4x2 + 15x + 90 • f(x) = 10x3 – 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) = 4x2 + 15x + 90 • f(x) = 10x3 – 30 • f(x) = log(x+30) • f(x) = 2(3x + 3) O(n) O(n2) O(n3) O(log n) O(2n)

  9. Loop runs a constant number of times, r. This causes 2r operations 2 assignments here n is to the first power: O(n) 1 cast & 1 division = 2 operations 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 2 loop assignments 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);} Operation Count = 2 Operation Count = 4 Operation Count = 6 Operation Count = 6 + 2r Operation Count = 2n + 6 + 2r Operation Count = 5n + 6 + 2r

  10. 1 Loop assignments. 3 operations for each of the n-1 iterations. Inner loop hasn-1 iterationsn-2 iterationsn-3 iterations…..1 iteration 2 loop ops1 comparison + 3 ops for swap6 operations On Each iteration O(n2) 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); } } Total = n(n-1)/2iterations 6[n(n-1)/2]=3n(n-1)=3n2-3n Operations from inner loop Operation Count = 1 Operation Count = 1 + 3(n-1) Operation Count = 3n – 2 Operation Count = 3n – 2 + 3n2 – 3n Operation Count = 3n2 – 2

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

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

More Related