Mathematical Analysis of Algorithms
Mathematical Analysis of Algorithms
of algorithms
Algorithms
Calculating Big-O
• We count the basic operations in an algorithm and add them to
calculate the Big-O.
• The operations that are going to be executed most often are in the
algorithm’s for loop
Mathematical analysis of algorithms
• Example: Consider the problem of finding the value of the largest
element in an array of numbers
• Inputs for which the algorithm does not exit the loop prematurely:
arrays with no equal elements and
• Arrays in which the last two elements are the only pair of equal
elements.
Mathematical analysis of algorithms
• For such inputs, one comparison is made for each repetition of the
innermost loop, i.e., for each value of the loop variable between its
limits and .
• This is repeated for each value of the outer loop, i.e., for each value of
the loop variable between its limits 0 and .
Mathematical analysis of algorithms
Analysis of Recursive algorithms
• Example: Compute the factorial function for an arbitrary nonnegative
integer . Since
• and by definition.
• We can obtain this value by inspecting the condition that makes the
algorithm stop its recursive calls:
Analysis of Recursive algorithms
• Therefore, the initial condition is
• Thus, the recurrence relation and initial condition for the algorithm’s
number of multiplications would be:
Analysis of Recursive algorithms
• We use the method of backward substitutions to solve this
recurrence.
• After inspecting the first three lines, we find a general formula for the
pattern
• So,