01 - Fundamentals of The Analysis of Algorithm Efficiency
01 - Fundamentals of The Analysis of Algorithm Efficiency
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-1
What is an algorithm?
An algorithm is a sequence of unambiguous instructions
for solving a problem, i.e., for obtaining a required
output for any legitimate input in a finite amount of
time.
problem
algorithm
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-2
1-2
Algorithm
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-4
1-4
Euclid’s Algorithm
Problem: Find gcd(m,n), the greatest common divisor of two
nonnegative, not both zero integers m and n
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-5
1-5
Two descriptions of Euclid’s algorithm
while n ≠ 0 do
r ← m mod n
m← n
n←r
return m
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-6
1-6
Analysis of algorithms
Issues:
• correctness
• time efficiency
• space efficiency
• optimality
Approaches:
• theoretical analysis
• empirical analysis
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-7
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
or cost executed
Visiting a vertex or
Typical graph problem #vertices and/or edges
traversing an edge
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-9
Empirical analysis of time efficiency
Select a specific (typical) sample of inputs
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-10
Best-case, average-case, worst-case
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-11
Example: Sequential search
Exact formula
e.g., C(n) = n(n-1)/2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-13
Order of growth
Most important: Order of growth within a constant multiple
as n→∞
Example:
• How much faster will algorithm run on computer that is
twice as fast?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-14
Values of some important functions as n
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-15
Asymptotic order of growth
A way of comparing functions that ignores constant factors and
small input sizes (because?)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-16
Big-oh
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-17
Big-omega
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-18
Big-theta
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-19
Establishing order of growth using the definition
Examples:
10n is in O(n2)
5n+20 is in O(n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-20
-notation
Formal definition
• A function t(n) is said to be in (g(n)), denoted t(n)
(g(n)), if t(n) is bounded below by some constant
multiple of g(n) for all large n, i.e., if there exist some
positive constant c and some nonnegative integer n0
such that
t(n) cg(n) for all n n0
Formal definition
• A function t(n) is said to be in (g(n)), denoted t(n)
(g(n)), if t(n) is bounded both above and below by
some positive constant multiples of g(n) for all large
n, i.e., if there exist some positive constant c1 and c2
and some nonnegative integer n0 such that
c2 g(n) t(n) c1 g(n) for all n n0
=
(g(n)), functions that grow at the same rate as g(n)
g(n)
<=
O(g(n)), functions that grow no faster than g(n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-23
Theorem
If t1(n) O(g1(n)) and t2(n) O(g2(n)), then
t1(n) + t2(n) O(max{g1(n), g2(n)}).
• The analogous assertions are true for the -notation
and -notation.
f(n) O(f(n))
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-25
Establishing order of growth using limits
Examples:
• 10n vs. n2
• n(n+1)/2 vs. n2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-26
L’Hôpital’s rule and Stirling’s formula
Example: 2n vs. n!
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-27
Orders of growth of some important functions
All logarithmic functions loga n belong to the same class
(log n) no matter what the logarithm’s base a > 1 is
because log a n log b n / log b a
All polynomials of the same degree k belong to the same class:
order log n < order n (>0) < order an < order n! < order nn
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-28
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
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-29
Basic asymptotic efficiency classes
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-30
Time efficiency of nonrecursive algorithms
General Plan for Analysis
Decide on parameter n indicating input size
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-31
Useful summation formulas and rules
lin1 = 1+1+…+1 = n - l + 1
In particular, lin1 = n - 1 + 1 = n (n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-32
Example 1: Maximum element
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-33
Example 2: Element uniqueness problem
Size: n
Basic operation: multiplication
Recurrence relation: M(n) = M(n-1) + 1
M(0) = 0
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-38
Solving the recurrence for M(n)
M(n) = M(n-1) + 1
= (M(n-2) + 1) + 1 = M(n-2) + 2
= (M(n-3) + 1) + 2 = M(n-3) + 3
…
= M(n-i) + i
= M(0) + n
=n
The method is called backward substitution.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-39
Example 2: The Tower of Hanoi Puzzle
1 3
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-41
Example 3: Counting #bits
A(n) = A( n / 2 ) + 1, A(1) = 0
A(2 k ) = A( 2 k 1) + 1, A( 2 0) = 1 (using the Smoothness Rule)
= (A( 2 k 2) + 1) + 1 = A( 2 k 2) + 2
= A(2 k i ) + i
= A( 2 k k) + k = k + 0
= log 2 n
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-42
Smoothness Rule
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 2-43