Fundamentals of Analysis of Algorithms Efficiency
Fundamentals of Analysis of Algorithms Efficiency
Fundamentals of Analysis of
Algorithms Efficiency
Topics
Analysis Framework
Definitions
Asymptotic Notations and Basic Efficiency Classes
Mathematical Analysis of Nonrecursive Algorithms.
Examples
Mathematical Analysis of Recursive Algorithms.
Examples
Assignment.
Empirical Analysis of algorithms
Algorithm Visualization.
There are two kinds of Algorithm efficiency:
Time efficiency: It indicates how fast an algorithm in question runs.
Space efficiency: It deals with the extra space an algorithm requires.
Input’s size:
The algorithm’s efficiency is measured as a function if some
parameter n indicating the algorithm’s input size.
OR
An algorithm’s time efficiency is principally measured as a function of
its input size by counting the number of time its basic operation is
executed.
Eg : Some algorithm require more than one parameter to indicate the
size if their inputs, like the number of vertices and the number of
edges for algorithms on graphs represented by the adjacency linked
lists.
Units of measuring Running Time
Basic Operation:
It is the operation that contributes most towards running
time, and computer the number of times the basic
operation is executed.
Running Time T(n):
T(n) ≈ copC(n)
Where cop = time of execution of an algorithm’s basic operation.
C(n) = the number of times this operation needs to be
executed for this algorithm
Input size and basic operation
examples
Problem Input size measure Basic operation
as fast as we have?
Worst case
Best case
Average case
Asymptotic order of growth
A way of comparing functions that ignores constant factors and
small input sizes
Examples:
10n O(n2),
n O(n2),
5n+20 O(n)
- notation:
Definition: A function t(n) is said to be in (g(n)) if order of growth of
t(n) ≥ order of growth of g(n) (within constant multiple), i.e., there
exist positive constant c and non-negative integer n0 such that
t(n) ≥ c g(n) for every n ≥ n0
Example:
n2 (n2)
Θ- notation:
Definition: A function t(n) is said to be in Θ(g(n)) if order of growth of
t(n) is in between order of growth of g(n) (within constant multiple),
i.e., there exist positive constant C1 and C2and non-negative
integer n0 such that
C2 g(n) ≤t(n)≤ C1 g(n) for every n ≥ n0
Useful properties involving the Asymptotic
Notations
Examples:
• 10n vs. n2
• n(n+1)/2 vs. n2
L’Hôpital’s rule and Stirling’s
formula
L’Hôpital’s rule: If limn f(n) = limn g(n) = and
the derivatives f´, g´ exist, then
Stirling’s formula: n! (2n)1/2 (n/e)n
lim f(n) lim f ´(n)
=
n g(n) n g ´(n)
Example: log n vs. n
Example: 2n vs. n!
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
All polynomials of the same degree k belong to the same class: aknk + ak-
k-1 + … + a (nk)
1n 0
order log n < order n (>0) < order an < order n! < order nn
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
Mathematical Analysis of
Nonrecursive Algorithms
General Plan for Analyzing efficiency of nonrecursive algorithms
Set up a sum for the number of times the basic operation is executed
Simplify the sum using standard formulas and rules (see Appendix
A)
Example 1: Maximum element
In the above example:
Input size= n (number of elements in the array)
Basic Operation = A[i] > maxval;
In this example there are no cases, as the algorithm has
to continue for all the elements of the array.
Let C(n) be the number of times the comparison is done.
The algorithm makes one comparison on each execution
of the loop, which is repeated for each value of i within
the bounds betweenn-1 1 and n-1.
i=1
Size: n
Basic operation: multiplication
Recurrence relation: ???
Solving the recurrence for M(n)
1 3
n-1 n-1
1 1 1 1 1 1 1 1
Example 3: Counting #bits
Fibonacci numbers
The Fibonacci numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, …
Characteristic equation: