11.2 Complexity Analysis. Complexity Analysis As true computer scientists, we need a way to compare...
-
Upload
harry-williams -
Category
Documents
-
view
213 -
download
1
Transcript of 11.2 Complexity Analysis. Complexity Analysis As true computer scientists, we need a way to compare...
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.
Instead, we will count the number of operations that must run.
Counting Operationspublic 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 operationsTotal Operations:
3n + 3
1 operation here here & here = 3 operations
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.
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?
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
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)
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)
O(n) Methodpublic 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
O(n2) Methodpublic 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 hasn-1 iterationsn-2 iterationsn-3 iterations
…..1 iteration
Total = n(n-1)/2iterations
2 loop ops1 comparison + 3 ops for
swap6 operations
On Each iteration
6[n(n-1)/2]=3n(n-1)=3n2-3n
Operations from inner loop
Operation Count = 3n – 2 + 3n2 – 3nOperation Count = 3n2 – 2
3 operations for each of the n-1 iterations.
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
# of inputs
# operations
O(n3)
O(n2)O(nlogn
) O(n)
O(logn)
O(1)
Algorithms are categorized by how their runtime increases in relation to the number of inputs.