You are on page 1of 20

Fundamentals of the

Analysis
of Algorithm Efficiency
The Analysis Framework

• In this section, we outline a general framework for


analyzing the efficiency of algorithms
Properties of an algorithm

1. Algorithms must contain finite number of steps


2. Algorithms must be precise and unambiguous.
The analysis Framework

• There are two kinds of efficiency:


• time efficiency and space efficiency.
• Time efficiency, also called time complexity, indicates
how fast an algorithm in question runs.
• Space efficiency, also called space complexity, refers to
the amount of memory units required by the algorithm in
addition
• to the space needed for its input and output.
The Analysis Framework
• Space Complexity
Space Complexity is the space (memory) needed for an
algorithm to solve the problem. An efficient algorithm take
space as small as possible.
• Time Complexity
Time complexity is the time required for an algorithm to
complete its process.It allows comparing the algorithm to
check which one is the efficient one.
The Analysis Framework
• Measuring an Input’s Size
• Units for Measuring Running Time
• Orders of Growth
• Worst-Case, Best-Case, and Average-Case Efficiencies
Best-case, average-case, worst-case
For some algorithms efficiency depends on form of input:

• Worst case: Cworst(n) – maximum over inputs of size n

• Best case: Cbest(n) – minimum over inputs of size n

• Average case: Cavg(n) – “average” over inputs of size n


• Number of times the basic operation will be executed on typical input
• NOT the average of worst and best case
• Expected number of basic operations considered as a random variable under some assumption
about the probability distribution of all possible inputs
Input size and basic operation examples

Problem Input size measure Basic operation

Searching for key in a Number of list’s items,


Key comparison
list of n items i.e. n

Multiplication of two Matrix dimensions or Multiplication of two


matrices total number of elements numbers

Checking primality of n’size = number of digits


Division
a given integer n (in binary representation)

Visiting a vertex or
Typical graph problem #vertices and/or edges
traversing an edge
Recapitulation of the Analysis
Framework
• Both time and space efficiencies are measured as functions of
the algorithm’s input size.
• Time efficiency is measured by counting the number of times
the algorithm’s basic operation is executed. Space efficiency is
measured by counting the number of extra memory units
consumed by the algorithm.
• The efficiencies of some algorithms may differ significantly
for inputs of the same size. For such algorithms, we need to
distinguish between the worst-case, average-case, and best-
case efficiencies.
• The framework’s primary interest lies in the order of growth of
the algorithm’s running time (extra memory units consumed)
as its input size goes to infinity.
Asymptotic Notations
Asymptotic notation is used to compute the complexity of
an algorithm in terms of time and space. It is normally
mentioned as the following terms.
• Big O notation Example: O(n2)
• Big Omega notation Example: Ω(n)
• Big Theta Example: Θ(n log n)
Big-oh
Big-oh
Big-theta
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-1nk-1 + … + a0
(nk)

• Exponential functions an have different orders of growth for different a’s

• 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
Time efficiency of nonrecursive algorithms
General Plan for Analysis

• Decide on parameter n indicating input size

• Identify algorithm’s basic operation

• Determine worst, average, and best cases for input of size n

• 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)
Plan for Analysis of Recursive Algorithms
• Decide on a parameter indicating an input’s size.
• Identify the algorithm’s basic operation.
• Check whether the number of times the basic op. is executed may vary on different
inputs of the same size. (If it may, the worst, average, and best cases must be
investigated separately.)
• Set up a recurrence relation with an appropriate initial condition expressing the number
of times the basic op. is executed.
• Solve the recurrence (or, at the very least, establish its solution’s order of growth) by
backward substitutions or another method.

You might also like