2-CST3011 - Algorithms and Data Structures-Algorithm Analysis
2-CST3011 - Algorithms and Data Structures-Algorithm Analysis
Structures
Lecture II: Algorithm Analysis
Objectives
At the end of this lesson, students are
expected to be able to
Use the mathematical framework that more
rigorously describe the running time of an
algorithm
Limitations
The necessity to implement the algorithm
Results may not be indicative of the running time on other
inputs not included in the experiment
To compare two algorithms, the same hardware must be used
Theoretical Analysis
Use a high level description of the algorithm
instead of implementation
P := M*M; 2
Q:= N*N; 2
Return P + Q; 2
End.
6 operations
Primitive operations
Basic computation performed by an algorithm
Identifiable in pseudocode
Largely independent from the programming
language
Exact definition not important
Examples:
Evaluating an expression
Assigning a value to a variable
Indexing into an array
*Calling a method
Return from a method
Rules for counting operators
Rule 1 – For loops:
The running time of the for loop is at most the running time of the
statements inside the for loop (including tests) times the number of
iterations. E.g:
For i 1 to n – 1 Do 1+ n – 1 = n
s s + A[i] 3(n – 1) = 3n – 3,
Next n-1 total 5n – 4 ops
Rule 2 – Nested loops:
Analyse these inside out. The total running time of a statement
inside a group of nested loops is the running time of the
statement multiplied by the product of the sizes of all the
loops: E.g.
For i 0 to n – 1 1+n
For j 0 to m – 1 (1 + m)(n)
A[i][j] i * j 4 (m)(n)
Next (m)(n)
Next (n)
Rules for counting operators cont’d
Rule 3: Consecutive statements
These just add
Define:
T(N) worst case time function dependant on N the
input
A, Time taken by the fastest operation
B ,Time taken by the slowest operation
We have
A(6N – 3) T(N)B(6N – 3)
Therefore, T(N) is bound by two linear
functions
Growth Rate of Running Time
Changing the hardware/software environment
Affects T(N) by a constant factor
Does not alter the growth rate of T(N)
Logarithmic 4000
Linear f(n) = n
f(n) = log(n)
3000
Quadratic f(n) = n log(n)
f(n) = n^2
Cubic 2000 f(n) = n^3
f(n) = 2^n
1000
Others include
0
LogLogN 1 3 5 7 9 11 13 15 17 19
Nlog(N)
Exponential
Asymptotic Notation
The analysis required to estimate resource
use is a theoretical issue
2N + 10 = (N)
Since:
2N + 10 cN
(c – 2)N 10
N 10/(c – 2) for c = 3 and n0 = 10, it is true
Exercise:
Show that g(N) = (f(N)) f(N) = (g(N))
Big-Oh Rules and Growth Rates
The Big-Oh notation gives a upper bound on
the growth rate of the function
Rule II
If f(n) is a polynomial of order k, then f(n) is (nk) i.e.
Rule III
Logk(n) = O(n) for any constant k
Soln:
??QUESTIONS??