Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
analysis of algorithm
efficiency
Algorithms
The Analysis Framework
• There are two kinds of efficiency: time efficiency and space efficiency.
• E.g., the number of vertices and the number of edges for algorithms
on graphs represented by their adjacency lists.
The Analysis Framework
Units for Measuring Running Time
• We can simply use some standard unit of time measurement—a
second, or millisecond, and so on.
• We should select a metric that does not depend on these extra factors.
The Analysis Framework
Units for Measuring Running Time
• We identify the most important operation of the algorithm, called the
basic operation, the operation contributing the most to the total
running time, and compute the number of times the basic operation
is executed.
• On the other end of the spectrum are the exponential function and
the factorial function Both these functions grow so fast that their
values become astronomically large even for rather small values of .
The Analysis Framework
Orders of Growth
• There is a tremendous difference between the orders of growth of the
functions and , yet both are often referred to as “exponential-growth
functions” (or simply “exponential”).
The Analysis Framework
The Analysis Framework
• Common used orders of growth
The Analysis Framework
Worst-case
• The worst-case efficiency of an algorithm is its efficiency for the worst-
case input of size , which is an input (or inputs) of size for which the
algorithm runs the longest among all possible inputs of that size.
• The worst-case efficiency guarantees that for any instance of size , the
running time will not exceed it (worst-case).
• E.g., value not found in linear search
The Analysis Framework
Best-case
• The best-case efficiency of an algorithm is its efficiency for the best-
case input of size , which is an input (or inputs) of size for which the
algorithm runs the fastest among all possible inputs of that size.
Big-O Notation
• Big-O represents the upper bound running complexity of an
algorithm.
• It is mathematically defined as
• Let and be two functions of , where denotes the input size, then
• belongs to the class , so all the values greater (as highlighted in red
above) can be its upper bound.
• That would also be true, but we will choose the nearest value (or the
least upper bound) as the Big-O.
Big-Omega Notation
• Big-Omega represents the lower bound running complexity of an
algorithm.
• It is mathematically defined as
• Let and be two functions of , where denotes the input size, then
• We could also say that is lower bound of , but we will select the
closest one (or the greatest lower bound), which is in this case.
• So we will say =
Theta Notation
• Theta notation gives the tight bound running complexity of an
algorithm.
• Let and be two functions of , where denotes the input size, then
• For example, consider the case of Insertion Sort. It takes linear time in
best case and quadratic time in worst case.
• We can safely say that the time complexity of Insertion sort is O(n^2).
Big-O
• The general step wise procedure for Big-O runtime analysis is as
follows:
1. Figure out what the input is and what represents.
2. Express the maximum number of operations, the algorithm performs in
terms of .
3. Eliminate all terms except the highest order terms.
4. Remove all the constant factors.