1.3. Algorithm Analysis¶
1.3.1. Algorithm Analysis¶
1.3.1.1. Algorithm Efficiency¶
There are often many approaches (algorithms) to solve a problem. How do we choose between them?
At the heart of computer program design are two (sometimes conflicting) goals.
To design an algorithm that is easy to understand, code, debug.
To design an algorithm that makes efficient use of the computer’s resources.
Goal (1) is the concern of Software Engineering
Goal (2) is the concern of data structures and algorithm analysis
1.3.1.2. How to Measure Efficiency?¶
Empirical comparison (run programs)
Asymptotic Algorithm Analysis
Critical resources:
Factors affecting running time:
For most algorithms, running time depends on “size” of the input.
Running time is expressed as T(n) for some function T on input size n.
1.3.1.3. Problems, Algorithms, Programs¶
1.3.1.4. Growth Rate Example (1)¶
Example 1: Find largest value
// Return position of largest value in integer array A static int largest(int[] A) { int currlarge = 0; // Position of largest element seen for (int i=1; i<A.length; i++) { // For each element if (A[currlarge] < A[i]) { // if A[i] is larger currlarge = i; // remember its position } } return currlarge; // Return largest position }
1.3.1.5. Growth Rate Example (2)¶
Example 2: Assignment statement
Example 3: Double loop
sum = 0; for (j=1; j<=n; j++) { // First for loop for (i=1; i<=j; i++) { // is a double loop sum++; } } for (k=0; k<n; k++) { // Second for loop A[k] = k; }
1.3.1.6. Growth Rate Graph¶
1.3.1.7. Best, Worst, Average Cases¶
Not all inputs of a given size take the same time to run.
Sequential search for K in an array of n integers:
Begin at first element in array and look at each element in turn until K is found
Best case:
Worst case:
Average case:
1.3.1.8. Which Analysis to Use?¶
While average time appears to be the fairest measure, it may be difficult to determine.
When is the worst case time important?
1.3.1.9. Faster Computer or Algorithm?¶
Suppose we buy a computer 10 times faster.
n: size of input that can be processed in one second on old computer (in 1000 computational units)
n’: size of input that can be processed in one second on new computer (in 10,000 computational units)
1.3.1.10. Faster Computer or Algorithm? 2¶
f(n)nn′Changen′/n10n100010,000n′=10n1020n5005000n′=10n105nlogn2501842√10n<n′<10n7.372n270223n′=√10n3.162n1316n′=n+3−−
1.3.1.11. Asymptotic Analysis: Big-oh¶
Definition: For T(n) a non-negatively valued function, T(n) is in the set O(f(n)) if there exist two positive constants c and n0 such that T(n)≤cf(n) for all n>n0.
Use: The algorithm is in O(n2) in [best, average, worst] case.
Meaning: For all data sets big enough (i.e., n>n0), the algorithm always executes in less than cf(n) steps in the [best, average, worst] case.
1.3.1.12. Big-oh Notation (cont)¶
Big-oh notation indicates an upper bound.
Example: If T(n)=3n2 then T(n) is in O(n2).
Look for the tightest upper bound:
While T(n)=3n2 is in O(n3), we prefer O(n2).
1.3.1.13. Big-Oh Examples¶
Example 1: Finding value X in an array (average cost).
Then T(n)=csn/2.
For all values of n>1,csn/2≤csn.
Therefore, the definition is satisfied for f(n)=n,n0=1, and c=cs. Hence, T(n) is in O(n).
1.3.1.14. Big-Oh Examples (2)¶
Example 2: Suppose T(n)=c1n2+c2n, where c1 and c2 are positive.
c1n2+c2n≤c1n2+c2n2≤(c1+c2)n2 for all n>1.
Then T(n)≤cn2 whenever n>n0, for c=c1+c2 and n0=1.
Therefore, T(n) is in O(n2) by definition.
Example 3: T(n)=c. Then T(n) is in O(1).
1.3.1.15. A Common Misunderstanding¶
“The best case for my algorithm is n=1 because that is the fastest.”
WRONG!
Big-oh refers to a growth rate as n grows to ∞
Best case is defined for the input of size n that is cheapest among all inputs of size n.
1.3.1.16. Big-Omega Ω¶
Definition: For T(n) a non-negatively valued function, T(n) is in the set Ω(g(n)) if there exist two positive constants c and n0 such that T(n)≥cg(n) for all n>n0.
Meaning: For all data sets big enough (i.e., n>n0), the algorithm always requires more than cg(n) steps.
Lower bound.
1.3.1.17. Big-Omega Example¶
T(n)=c1n2+c2n.
c1n2+c2n≥c1n2 for all n>1.
T(n)≥cn2 for c=c1 and n0=1.
Therefore, T(n) is in Ω(n2) by the definition.
We want the greatest lower bound.
1.3.1.18. Theta Notation Θ¶
When big-Oh and Ω coincide, we indicate this by using Θ (big-Theta) notation.
Definition: An algorithm is said to be in Θ(h(n)) if it is in O(h(n)) and it is in Ω(h(n)).
1.3.1.19. A Common Misunderstanding¶
Confusing worst case with upper bound.
Upper bound refers to a growth rate.
Worst case refers to the worst input from among the choices for possible inputs of a given size.
1.3.1.20. Simplifying Rules¶
If f(n) is in O(g(n)) and g(n) is in O(h(n)), then f(n) is in O(h(n)).
If f(n) is in O(kg(n)) for some constant k>0, then f(n) is in O(g(n)).
If f1(n) is in O(g1(n)) and f2(n) is in O(g2(n)), then (f1+f2)(n) is in O(max(g1(n),g2(n))).
If f1(n) is in O(g1(n)) and f2(n) is in O(g2(n)), then f1(n)f2(n) is in O(g1(n)g2(n)).
1.3.1.21. Summary¶
Costs for all inputs of an arbitrary (but fixed) size $n$ for three representative algorithms$I_n$cheapexpensive$2^n-1$_$*$Towers of Hanoi$I_n$cheapexpensive$n$_FindMax$I_n$cheapexpensive$n$_$1$_FindCosts, as $n$ grows, for some representative algorithms$n$cheapexpensive$2^n-1$_$T(n) = 2^n-1$Towers of Hanoi$n$cheapexpensive$n$_$1$_FindMax$n$cheapexpensive$1$_Find (Best)$n$cheapexpensive$n$_$1$_Find (Average)$n$cheapexpensive$n$_$1$_Find (Worst)
1.3.1.22. .¶
.
1.3.1.23. Time Complexity Examples (1)¶
Example: a = b;
This assignment takes constant time, so it is Θ(1).
Example:
sum = 0; for (i=1; i<=n; i++) { sum += n; }
1.3.1.24. Time Complexity Examples (2)¶
Example:
sum = 0; for (j=1; j<=n; j++) { // First for loop for (i=1; i<=j; i++) { // is a double loop sum++; } } for (k=0; k<n; k++) { // Second for loop A[k] = k; }
1.3.1.25. Time Complexity Examples (3)¶
Example: Compare these two code fragments:
sum1 = 0; for (i=1; i<=n; i++) { // First double loop for (j=1; j<=n; j++) { // do n times sum1++; } } sum2 = 0; for (i=1; i<=n; i++) { // Second double loop for (j=1; j<=i; j++) { // do i times sum2++; } }
1.3.1.26. Time Complexity Examples (4)¶
Not all double loops are Θ(n2).
sum1 = 0; for (k=1; k<=n; k*=2) { // Do log n times for (j=1; j<=n; j++) { // Do n times sum1++; } } sum2 = 0; for (k=1; k<=n; k*=2) { // Do log n times for (j=1; j<=k; j++) { // Do k times sum2++; } }
1.3.1.27. Binary Search¶
How many elements are examined in worst case?
// Return the position of an element in sorted array A with value K. // If K is not in A, return A.length. public static int binarySearch(int[] A, int K) { int low = 0; int high = A.length - 1; while(low <= high) { // Stop when low and high meet int mid = (low + high) / 2; // Check middle of subarray if( A[mid] < K) low = mid + 1; // In right half else if(A[mid] > K) high = mid - 1; // In left half else return mid; // Found it } return A.length; // Search value not in A }
1.3.1.28. Other Control Statements¶
while loop: Analyze like a for loop.
if statement: Take greater complexity of then/else clauses.
switch statement: Take complexity of most expensive case.
Subroutine call: Complexity of the subroutine.
1.3.1.29. Analyzing Problems¶
Upper bound: Upper bound of best known algorithm.
Lower bound: Lower bound for every possible algorithm.
1.3.1.30. Analyzing Problems: Example¶
May or may not be able to obtain matching upper and lower bounds.
Example of imperfect knowledge: Sorting
Cost of I/O: Ω(n).
Bubble or insertion sort: O(n2).
A better sort (Quicksort, Mergesort, Heapsort, etc.): O(nlogn).
We prove later that sorting is in Ω(nlogn).
1.3.1.31. Space/Time Tradeoff Principle¶
One can often reduce time if one is willing to sacrifice space, or vice versa.
- Encoding or packing information
Boolean flags
- Table lookup
Factorials
Disk-based Space/Time Tradeoff Principle: The smaller you make the disk storage requirements, the faster your program will run.
1.3.1.32. Multiple Parameters¶
Compute the rank ordering for all C pixel values in a picture of P pixels.
for (i=0; i<C; i++) { // Initialize count count[i] = 0; } for (i=0; i<P; i++) { // Look at all of the pixels count[value(i)]++; // Increment a pixel value count } sort(count); // Sort pixel value countsIf we use P as the measure, then time is (PlogP).
More accurate is Θ(P+ClogC).
1.3.1.33. Space Complexity¶
Space complexity can also be analyzed with asymptotic complexity analysis.
Time: Algorithm
Space: Data Structure