Algorithm analysis: Problem
Definition
What is the task to be accomplished?
Calculate the average grade for a given student
Find the nth fibonacci number
What are the time / space / speed /
performance requirements ?
Algorithm Analysis
Algorithm: Finite set of instructions that, if followed,
accomplishes a particular task.
Algorithm Analysis:
Space complexity
How much space is required
Time complexity
How much time does it take to run the algorithm
Often, we deal with estimates!
Space Complexity
Space complexity = The amount of memory
required by an algorithm to run to completion
Core dumps = the most often encountered cause is
memory leaks – the amount of memory
required is larger than the memory available on a
given system
Space Complexity (cont’d)
1. Fixed part: The size required to store certain
data/variables, that is independent of the size of the
problem:
- e.g. name of the data collection
- same size for classifying 2GB or 1MB of texts
1. Variable part: Space needed by variables, whose size
is dependent on the size of the problem:
- e.g. actual text
- load 2GB of text VS. load 1MB of text
- Or amount of info being held while we run through
recursive cycle (e.g., fibonacci sequence)
Space Complexity (cont’d)
S(P) = c + S(instance characteristics)
c = constant
Example:
void float sum (float* a, int n)
{
float s = 0;
for(int i = 0; i n0.
No!
50n3 + 20n + 4 is O(n3)
Would be correct to say is O(n3+n)
• Not useful, as n3 exceeds by far n, for large values
Would be correct to say is O(n5)
• OK, but g(n) should be as closed as possible to f(n)
3log n = O( ? )
Algorithm Analysis
We simplify the analysis by getting rid of
unneeded information
“rounding” 39999≈40000
We drop constants when expressing big Oh.
E.g., if we have a program that runs in 3n +2 time, we’d say
that the function runs in O(n).
We drop lower order terms when expressing big
Oh
E.g., if we have a function that runs in polynomial time (4n4
+ 300n3 +7n + 2), we can say that it runs in O(n4).
Why? Because after a certain point the lower order is
subsumed by the higher order.
• e.g., if n is 500 in the above example, then 4n4 could be
changed to 5n4 and it would definitely be greater than 300 n3.
So we know that 5 g(n) is greater than 4n4 + 300n3 if g(n) is n4.
• Hence we get O(n4) for this polynomial.
General Rules
For loops: running time is at most the
running time of the statements inside the for
loop times the number of iterations
Example: for (i = 0; i o)
for (i = o; i 1
Some Numbers
log n n n log n n2 n3 2n
0 1 0 1 1 2
1 2 2 4 8 4
2 4 8 16 64 16
3 8 24 64 512 256
4 16 64 256 4096 65536
5 32 160 1024 32768 4294967296
Example
Remember the algorithms for computing prefix averages?
- compute an array avg starting with an array orig
- avg[i] is the average of all elements orig[j] with j & a)
{
int maxSum = 0;
for (int i = 0; i maxSum
maxSum = thisSum;
}
return maxSum;
}
Algorithm 2:
int maxSubSum2(const vector & a)
{
int maxSum = 0;
for (int i = 0; i maxSum
maxSum = thisSum;
}
}
return maxSum;
}
Search
Given an integer X and integers A0, A1, … An-1,
which are presorted and already in memory,
find i such that Ai = X, or return i = -1 if X is
not in the input.
Write the algorithm that does this. What is
the big Oh of this algorithm?
Another Example:
Raising an integer to a power: xn
Could write:
int num = 1;
for (int i=0;i0 there
is n0 such that f(n) n0.