0% found this document useful (0 votes)
7 views23 pages

Mathematical Analysis of Algorithms

The document discusses the mathematical analysis of algorithms, focusing on calculating Big-O by counting basic operations. It provides examples such as finding the largest element in an array and checking for element uniqueness, outlining a general plan for analyzing time efficiency. Additionally, it covers the analysis of recursive algorithms, including the use of recurrence relations to determine the number of operations required.

Uploaded by

Adolf Hitler
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views23 pages

Mathematical Analysis of Algorithms

The document discusses the mathematical analysis of algorithms, focusing on calculating Big-O by counting basic operations. It provides examples such as finding the largest element in an array and checking for element uniqueness, outlining a general plan for analyzing time efficiency. Additionally, it covers the analysis of recursive algorithms, including the use of recurrence relations to determine the number of operations required.

Uploaded by

Adolf Hitler
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Mathematical analysis

of algorithms
Algorithms
Calculating Big-O
• We count the basic operations in an algorithm and add them to
calculate the Big-O.

• E.g. Linear search, bubble sort


Mathematical analysis of algorithms
• Example: Consider the problem of finding the value of the largest
element in an array of numbers
Mathematical analysis of algorithms
• Example: Consider the problem of finding the value of the largest
element in an array of numbers
Mathematical analysis of algorithms
• Example: Consider the problem of finding the value of the largest
element in an array of numbers

• The obvious measure of an input’s size here is the number of


elements in the array, i.e., .

• 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

• There are two operations in the loop’s body: the comparison


• A[i] > maxval and the assignment maxval ← A[i]

• Since the comparison is executed on each repetition of the loop, we


will count it
Mathematical analysis of algorithms
• Example: Consider the problem of finding the value of the largest
element in an array of numbers

• Let us denote the number of times this comparison is executed and


try to find a formula expressing it as a function of size .

• The algorithm makes one comparison on each execution of the loop,


which is repeated for each value of the loop’s variable within the
bounds and , inclusive. Therefore, we get the following sum for :
Mathematical analysis of algorithms
• Example: Consider the problem of finding the value of the largest
element in an array of numbers

• This is an easy sum to compute because it is nothing other than 1


repeated n − 1 times. Thus,
General Plan for Analyzing the Time
Efficiency of Nonrecursive
Algorithms
1. Decide on a parameter (or parameters) indicating an input’s size.
2. Identify the algorithm’s basic operation. (As a rule, it is located in the
innermost loop.)
3. Check whether the number of times the basic operation is executed
depends only on the size of an input. If it also depends on some additional
property, the worst-case, average-case, and, if necessary, best-case
efficiencies have to be investigated separately.
4. Set up a sum expressing the number of times the algorithm’s basic operation
is executed.
5. Using standard formulas and rules of sum manipulation, either find a closed-
form formula for the count or, at the very least, establish its order of growth.
General Plan for Analyzing the Time
Efficiency of Nonrecursive
Algorithms
• Two basic rules of sum manipulation

• Two summation formulas


Mathematical analysis of algorithms
• EXAMPLE 2: Consider the element uniqueness problem: check
whether all the elements in a given array of n elements are distinct.
Mathematical analysis of algorithms
• EXAMPLE 2: Consider the element uniqueness problem: check
whether all the elements in a given array of n elements are distinct.
Mathematical analysis of algorithms
• EXAMPLE 2: Consider the element uniqueness problem: check
whether all the elements in a given array of n elements are distinct.
• The natural measure of the input’s size here is again , the number of
elements in the array.
• The innermost loop contains a single operation (the comparison of
two elements). We will count it.
• The number of element comparisons depends not only on but also
on whether there are equal elements in the array and, if there are,
which array positions they occupy.
• We will limit our investigation to the worst case only.
Mathematical analysis of algorithms
• There are two kinds of worst-case inputs.

• 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 compute with the following recursive algorithm


Analysis of Recursive algorithms
Analysis of Recursive algorithms
• The basic operation of the algorithm is multiplication, whose number
of executions are denoted by .

• Since the function is computed according to the formula

• The number of multiplications must satisfy the equality


Analysis of Recursive algorithms
• The last equation defines not explicitly, i.e., as a function of , but
implicitly as a function of its value at another point, .

• Such equations are called recurrence relations or, recurrences.

• Our goal now is to solve the recurrence relation

• i.e., to find an explicit formula for in terms of only.


Analysis of Recursive algorithms
• To determine a solution uniquely, we need an initial condition that
tells us the value with which the sequence starts.

• 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,

You might also like