CSE225 Lecture04 AnalysisAlgorithms
CSE225 Lecture04 AnalysisAlgorithms
Analysis of Algorithms
• An algorithm is a finite set of precise
instructions for performing a computation
or for solving a problem.
– It must produce the correct result
– It must finish in some finite time
– You can represent an algorithm using
pseudocode, flowchart, or even actual code
Analysis of Algorithms
Analysis of Algorithms
• What does it mean to analyze an algorithm?
– To have an estimate about how much time an
algorithm may take to finish, or in other words, to
analyze its running time
– Sometimes, instead of running time, we are interested
in how much memory the algorithm may consume
while it runs
– It enables us to compare between two algorithms
• What do we mean by running time analysis?
– Also referred to as time-complexity analysis
– To determine how running time increases as the size of
the problem increases.
Analysis of Algorithms
What is the goal?
• Analyze time requirements - predict how
running time increases as the size of the
problem increases:
time = f(size)
Why is it useful?
• To compare different algorithms.
Defining “problem size”
• Typically, it is straightforward to identify the size of
a problem, e.g.:
– size of array
– size of stack, queue, list etc.
– vertices and edges in a graph
Algorithm 1 Algorithm 2
Cost Cost
arr[0] = 0; c1 for(i=0; i<N; i++) c2
arr[1] = 0; c1 arr[i] = 0; c1
arr[2] = 0; c1
...
arr[N-1] = 0; c1
----------- -------------
c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 =
(c2 + c1) x N + c2
How do we find f(n)? (cont.)
Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N x N
Comparing algorithms
• Given two algorithms having running times
f(n) and g(n), how do we decide which one
is faster?
Approximation:
Cost ~ cost_of_elephants
Understanding Rate of Growth
(cont’d)
n4 + 100n2 + 10n + 50
Approximation:
n4
Value of function
fA(n)=30n+8
right, a faster
growing
function
fB(n)=n2+1
eventually
becomes
larger... Increasing n
Rate of Growth ≡Asymptotic Analysis
c is a constant
c is a constant
c is a constant
fA(n)=30n+8 is O(n)
fB(n)=n2+1 is O(n2)
n3 - n2 is O(n3)
1273 is O(1)
Common orders of magnitude
Algorithm speed vs function growth
• An O(n ) algorithm will be slower than an O(n)
2
fA(n)=30n+8
fB(n)=n2+1
Increasing n
Estimating running time
Algorithm 1 Algorithm 2
Cost Cost
arr[0] = 0; c1 for(i=0; i<N; i++) c2
arr[1] = 0; c1 arr[i] = 0; c1
arr[2] = 0; c1
...
arr[N-1] = 0; c1
----------- -------------
c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 =
(c2 + c1) x N + c2
O(N)
Estimate running time (cont.)
Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N2 x (N+1) + c3 x N x N
O(N )
Running time of various statements
while-loop for-loop
Examples
i = 0;
while (i<N) {
X=X+Y; // O(1)
result = mystery(X); // O(N), just an example...
i++; // O(1)
}
• The body of the while loop: O(N)
• Loop is executed: N times
N x O(N) = O(N2)
Examples (cont.’d)
if (i<j)
for ( i=0; i<N; i++ ) O(N)
X = X+i;
else
O(1)
X=0;