Mathematical Analysis of Nonrecursive Algorithms
Mathematical Analysis of Nonrecursive Algorithms
Algorithms
simple example that demonstrates all the principal steps typically taken in analysing
Time complexity of an algorithms.
EXAMPLE : Consider the problem of finding the value of the largest element in a
list of n numbers. For simplicity, we assume that the list is implemented as an array
ALGORITHM MaxElement(A[0..n − 1])
input’s size =the number of elements in the array, i.e., n.
//Determines the value of the largest element in a given array
//Input: An array A[0..n − 1] of real numbers The operations that are going to be executed most often are
//Output: The value of the largest element in A in the algorithm’s for loop. (two operations)
Since the loop runs n-1 times, and there's one comparison in each iteration, the total
number of comparisons, denoted by C(n), is equal to n-1. Thus,
The function C(n) = n - 1 is in the class Θ(n), which denotes the time complexity.
This means the number of comparisons grows linearly with the size of the input, n.
General Plan for Analyzing the Time Efficiency of Non-recursive 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.4
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.
Two basic rules of sum manipulation,
where C[i, j ]= A[i, 0]B[0, j]+ . . . + A[i, k]B[k, j]+ . . . + A[i, n − 1]B[n − 1, j] for every
pair of indices 0 ≤ i, j ≤ n − 1.
ALGORITHM MatrixMultiplication(A[0..n − 1, 0..n − 1], B[0..n − 1, 0..n − 1])
//Multiplies two square matrices of order n by the definition-based algorithm
//Input: Two n × n matrices A and B input’s size = matrix order n.