Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Issues:
• Correctness
• Time efficiency
• Space efficiency
• Optimality
Approaches:
• Theoretical analysis
• Empirical analysis
Theoretical analysis of time efficiency
Time efficiency is analyzed by determining the number of
repetitions of the basic operation as a function of input size
T(n) ≈ copC(n)
running time execution time Number of times
for basic operation basic operation is
executed
Input size and basic operation examples
Floating point
Compute an n
multiplication
Visiting a vertex or
Graph problem #vertices and/or edges
traversing an edge
Empirical analysis of time efficiency
Select a specific (typical) sample of inputs
OR
Best case
Average case
Types of formulas for basic operation count
Exact formula
e.g., C(n) = n(n-1)/2
Example:
• How much faster will algorithm run on computer that is twice as
fast?
• How much longer does it take to solve problem of double input size?
Examples:
• 10n vs. 2n2
• n(n+1)/2 vs. n2
Then
5n+20 is O(10n)
Basic Asymptotic Efficiency classes
1 constant
log n logarithmic
n linear
n log n n log n
n2 quadratic
n3 cubic
2n exponential
n! factorial
Time efficiency of nonrecursive algorithms
Selection sort
Insertion sort
Mystery Algorithm
Matrix multipliacation
Selection sort
Insertion sort
Mystery algorithm
for i := 1 to n - 1 do
max := i ;
for j := i + 1 to n do
if |A[ j, i ]| > |A[ max, i ]| then max := j ;
for k := i to n + 1 do
swap A[ i, k ] with A[ max, k ];
for j := i + 1 to n do
for k := n + 1 downto i do
A[ j, k ] := A[ j, k ] - A[ i, k ] * A[ j, i ] / A[ i, i ] ;
Example Recursive evaluation of n !
Definition: n ! = 1*2*…*(n-1)*n
Algorithm:
if n=0 then F(n) := 1
else F(n) := F(n-1) * n
return F(n)
Fibonacci recurrence:
F(n) = F(n-1) + F(n-2)
2nd order linear homogeneous
F(0) = 0
recurrence relation
F(1) = 1
with constant coefficients
Another example:
A(n) = 3A(n-1) - 2(n-2) A(0) = 1 A(1) = 3
Solving linear homogeneous recurrence
relations with constant coefficients
Easy first: 1st order LHRRCCs:
C(n) = a C(n -1) C(0) = t … Solution: C(n) = t an
Extrapolate to 2nd order
L(n) = a L(n-1) + b L(n-2) … A solution?: L(n) = r n