CS350-CH03 - Algorithm Analysis
CS350-CH03 - Algorithm Analysis
CS350-CH03 - Algorithm Analysis
Chapter 03 Algorithm
Analysis
Chaminade University of
Honolulu
Department of Computer
Science
1
Goal
Critical Resources
Two approaches
Estimation
Example 3.1
// Return position of largest value in array
int largest(int array[], int n) {
int currlarge = 0; // Largest value seen
for (int i=1; i<n; i++) // For each val
if (array[currlarge] < array[i])
currlarge = i;
// Remember pos
return currlarge;
// Return largest
}
Example 3.1
Example 3.2
int a =
array[0];
Example 3.3
sum = 0;
for (i=1; i<=n; i++)
for (j=1; j<n; j++)
sum++;
}
What is the running time for this code?
The basic operation is sum++
The costs of the sum operation can be
bundled into time c2
The running time is T(n) = C n2
2
10
11
12
Not all inputs of a given size take the same time to run.
13
It is too optimistic
Not a fair characterization of the
algorithms running time
Useful in some rare cases, where the best
case has high probability of occurring.
14
16
A Faster Computer?
18
Linear Algorithms
T(n)
10n
n
1,000
n
Change
10,000
n =
10n
n/n
10
19
Quadratic Algorithms
T(n)
2n2
n
70
n
223
Change
n = 10n
n/n
3.16
20
Exponential Algorithms
T(n)
2n
n
13
n
16
Change
n = n +3
n/n
---
21
n
n
Change
1,00 10,00 n = 10n
0
0
500 5,000 n = 10n
250 1,842 10 n < n <
10n
70
223 n = 10n
n/n
10
10
7.37
3.16
22
Conclusions
24
Sequential search
Best case, worst case and average
case
When are we interested in:
Asymptotic Analysis
Big-Oh
Big Omega
Big Theta
26
Big-Oh Examples
Example 1: Finding value X in an array.
T(n) = csn/2.
For all values of n > 1, csn/2 <= csn.
Therefore, by the definition, T(n) is in O(n)
for n0 = 1 and c = cs.
29
Big-Oh Examples
Example 2: T(n) = c1n2 + c2n in average case.
c1i2 + c2n <= c1n2 + c2n2 <= (c1 + c2)n2 for all n > 1.
T(n) <= cn2 for c = c1 + c2 and n0 = 1.
Therefore, T(n) is in O(n2) by the definition.
Example 3: T(n) = c. We say this is in O(1).
30
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 as which input of size n
is cheapest among all inputs of size n.
31
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 executes in more than
cg(n) steps.
Lower bound.
32
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.
33
Theta Notation
When big-Oh and meet, we indicate this
by using (big-Theta) notation.
Definition: An algorithm is said to be
(h(n)) if it is in O(h(n)) and it is in (h(n)).
34
A Common Misunderstanding
Confusing worst case with upper bound.
35
Simplifying Rules
1.
2.
3.
4.
38
39
40
Binary Search
Binary Search
// Return position of element in sorted
// array of size n with value K.
int binary(int array[], int n, int K) {
int l = -1;
int r = n; // l, r are beyond array bounds
while (l+1 != r) { // Stop when l, r meet
int i = (l+r)/2; // Check middle
if (K < array[i]) r = i;
// Left half
if (K == array[i]) return i; // Found it
if (K > array[i]) l = i;
// Right half
}
return n; // Search value not in array
}
42
43
Analyzing Problems
Upper bound: Upper bound of best known
algorithm.
Lower bound: Lower bound for every
possible algorithm.
44
45
2.
3.
4.
Multiple Parameters
Compute the rank ordering for all C pixel values in
a picture of P pixels.
for (i=0; i<C; i++)
count[i] = 0;
for (i=0; i<P; i++)
count[value(i)]++;
sort(count);
// Initialize count
// Look at all pixels
// Increment count
// Sort pixel counts
Space Bounds
Space bounds can also be analyzed with
asymptotic complexity analysis.
Time: Algorithm
Space: Data Structure
48
49
Packing Information
50
32
integers
32
chars
32 bit
fields
32 x 4 =
128
32 x 1 =
32
resources
Space
required
(bytes)
Time
required
T
T
5T
to set a
value approximation machine dependent 51
me T is a relative
Calculating Functions
Calculate 12 x 11 x 10 x 1 (using no
space)
Pre-store 479001600 (using 4 bytes)
Sine, cosine
52
53
Practical Considerations
Example:
(10,000) = 10,000
1
2(10,000) =
=10,000 log1010,000 = 40,000
54
Practical Considerations
1(10,000) = 100,000,000
2(10,000) = 10,000 log10 10,000
=
= 40,000
55
Practical Considerations
Sorting
Searching
56
Code tuning
Practical Considerations
Code tuning
Remarks
Code tuning
Remarks
Remarks
60
Remarks
61
Appendix A - Notes
Algorithm Analysis
62
Computational complexity
theory
63
Computational complexity
theory
Computational complexity
theory
Big Oh
Big Omega
67
68
Remarks
Theta Notation
C2g(n)
f(n)
C1g(n)
n0
f(n) = (g(n))
70
Appendix B - Exercises
Algorithm Analysis
71
2.
3.
5.
6.
74
Exercise
CS350-Data Structures
Created by David Schneider
Modified by Prof. Martins
Appendix C
Calculating the Running Time
of a Program
Chaminade University of
Honolulu
Department of Computer
Science
77
78
Number of executions
Outer
loop
j
Inner
loop
i
1,2
1,2,3
1,2,..
n
#
runs
1 + 2 + 3 + 4 + 5 + N
79
Number of executions
n
# runs = 1 + 2 + 3 + 4 ..+ n = j
j=1
n
j = n(n+1)/2
j=1
Which is (n2)
80
81
## of executions outer
loop
k
1,2,..
n
#
runs
1,2,.. 1,2..n
n
2
1,2,. n
Log n
N x log N
82
Number of executions
Log n
n = n log n
Log n
j=1
j=1
Which is (n log n)
83
84
Remarks
85
Running Time
sum2 = 0;
for (k=1; k<=n; k*=2)
for (j=1; j<=k; j++)
sum2++;
## of executions outer
loop
k
1,2
#
runs
Log n
1 + 2 + 4 + 8 + 16 + n
87
Number of executions
= 1 + 21 + 22 + 23 + 24 + + 2logn
log n
2 =
i
2n - 1
i=1