You are on page 1of 33

CMP3501

Analysis of Algorithms
Lecture Notes 3
- Mathematical Analysis of Non-Recursive and
Recursive Algorithms
- Empirical Analysis of Algorithms

1
Mathematical Analysis
of NonRecursive Algorithms
• Let us start with a very simple example that
demonstrates all the principal steps typically
taken in analyzing such algorithms.
• EXAMPLE 1
Consider the problem of finding the value of
the largest element in a list of n numbers.

2
Mathematical Analysis
of NonRecursive Algorithms

3
Largest Element in an Array
• Input’s size: Number of elements in the array,
i.e., n.
• The operations that are going to be executed
most often are in the algorithm’s for loop.

4
Largest Element in an Array
• Basic Operation
• Two operations in the loop’s body:
The comparison: A[i]> maxval
The assignment: maxval←A[i].
Which of these two operations should we consider
basic?
Since the comparison is executed on each
repetition of the loop and the assignment is not,
we should consider the comparison to be the
algorithm’s basic operation.
5
Largest Element in an Array
• Do the worst case, best case and average case
differ for this problem?

6
Largest Element in an Array
• Let us denote C(n) the number of times this
comparison is executed
• The algorithm makes one comparison on each
execution of the loop, which is repeated for
each value of the loop’s variable i within the
bounds 1 and n − 1, inclusive.
• So, we get the following sum:

7
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.

8
Another Nonrecursive Algorithm:
Element uniqueness
• EXAMPLE 2
Consider the element uniqueness problem:
check whether all the elements in a given
array of n elements are distinct.

9
Another Nonrecursive Algorithm:
Element uniqueness

10
Another Nonrecursive Algorithm:
Element uniqueness
• Input’s size: n, the number of elements in the
array.
• Algorithm’s basic operation: The comparison of
two elements
– Important Note: The number of element comparisons
depends not only on n 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.

11
Another Nonrecursive Algorithm:
Element uniqueness
Worst Case of this problem:
• The worst case input is an array for which the
number of element comparisons Cworst(n) is
the largest among all arrays of size n.
• Two kinds of worst-case inputs:
– the algorithm does not exit the loop prematurely:
arrays with no equal elements
– the last two elements are the only pair of equal
elements.

12
Another Nonrecursive Algorithm:
Element uniqueness

13
Another Nonrecursive Algorithm:
Matrix Multiplication
• Given two n × n matrices A and B, find the
time efficiency of the definition-based
algorithm for computing their product C = AB.

14
Another Nonrecursive Algorithm:
Matrix Multiplication

15
Another Nonrecursive Algorithm:
Matrix Multiplication

16
Practise

17
Practise

18
Mathematical Analysis of
Recursive Algorithms

19
Mathematical Analysis of Recursive
Algorithms: Factorial
• EXAMPLE 1
Compute the factorial function F(n) = n! for an
arbitrary nonnegative integer n.
• Since, n!= 1 . . . . . (n − 1) . n = (n − 1)! . n
for n ≥ 1 and 0!= 1 by definition, we can
compute F(n) = F(n − 1) x n with the following
recursive algorithm.

20
Mathematical Analysis of Recursive
Algorithms: Factorial

21
Mathematical Analysis of Recursive
Algorithms: Factorial
• Input size: n
• The basic operation of the algorithm is
multiplication: number of executions we
denote M(n)

22
Mathematical Analysis of Recursive
Algorithms: Factorial

Recurrence relation
or Recurrences

Goal: To solve the recurrence relation we find an


explicit formula for M(n) in terms of n only

23
Mathematical Analysis of Recursive
Algorithms: Factorial

Recurrence relation
or Recurrences

An initial condition is needed:

24
Mathematical Analysis of Recursive
Algorithms: Factorial

Pattern:

25
Mathematical Analysis of Recursive
Algorithms

26
Mathematical Analysis of Recursive
Algorithms: Towers of Hanoi
• Have n disks of different sizes that can slide onto any of three
pegs
• Initially, all the disks are on the first peg in order of size, the
largest on the bottom and the smallest on top
• The goal is to move all the disks to the third peg, using the
second one as an auxiliary, if necessary
• We can move only one disk at a time, and it is forbidden to
place a larger disk on top of a smaller one

27
Source: Stackoverflow.com
Mathematical Analysis of Recursive
Algorithms: Towers of Hanoi

28
Mathematical Analysis of Recursive
Algorithms: Towers of Hanoi
• We first move recursively n − 1 disks from peg
1 to peg 2 (with peg 3 as auxiliary)
• Then move the largest disk directly from peg 1
to peg 3
• Finally, move recursively n − 1 disks from peg 2
to peg 3 (using peg 1 as auxiliary).
• if n = 1, move the single disk directly from the
source peg to the destination peg.

29
Mathematical Analysis of Recursive
Algorithms: Towers of Hanoi

30
Mathematical Analysis of Recursive
Algorithms: Towers of Hanoi

31
Mathematical Analysis of Recursive
Algorithms: Sum of First n Cubes
• Consider the following recursive algorithm for
computing the sum of the first
n cubes: S(n) = 13 + 23 + . . . + n3.

32
Mathematical Analysis of Recursive
Algorithms: Sum of First n Cubes
a. Set up and solve a recurrence relation for
the number of times the algorithm’s basic
operation is executed.
b. Implement a nonrecursive algorithm for the
same task
c. How does this algorithm compare with the
straightforward nonrecursive algorithm for
computing this sum?

33

You might also like