Lecture Chapter 2 Part 2
Lecture Chapter 2 Part 2
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.
Some summation examples
• Exercise
• (a)
• (b)
• EXAMPLE 2 Consider the element uniqueness
problem: check whether all the elements in a
given array of n elements are distinct.
• This problem can be solved by the following
straightforward algorithm.
– (Next slide)
Algorithm UniqueElements
• The natural measure of the input’s size here is again
n, the number of elements in the array.
• Since the innermost loop contains a single operation
(the comparison of two elements), we should
consider it as the algorithm’s basic operation.
• Note, however, that 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.
Analysis Method 1
• When i = 0, there are n – 1 comparisons in the inner loop,
• When i = 1, there are n – 2 comparisons in the inner loop,
• When i = 2, there are n – 3 comparisons in the inner loop,
• …
• When i = m, we can see that there are (n – 1) - m comparison in the
inner loop,
• Hence,
• When i = n - 2, there are (n – 1) – (n – 2) = 1 comparison in the inner
loop,
• This gives Cworst (n) = (n-1) + (n- 2) + (n – 3) + … + 3 + 2 + 1
= (n – 1)(n)/2 (Arithmetic Progression)
= O(n2)
Analysis Method 2
• Recall that 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.
• In this case 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.
– For such input, one comparison is made for each repetition of
the innermost loop, i.e., for each value of the loop variable j
between its limits i + 1 and n − 1;
– this is repeated for each value of the outer loop, i.e., for each
value of the loop variable i between its limits 0 and n − 2.
EXAMPLE 3
• This simplifies to
`
• This example is simple enough so that we could get
this result without all the summation machinations.
• How?
• The algorithm computes n2 elements of the product
matrix. Each of the product’s elements is computed
as the scalar (dot) product of an n-element row of
the first matrix and an n-element column of the
second matrix, which takes n multiplications.
• So the total number of multiplications is n . n2 = n3.
• You should not have the erroneous impression that
the plan outlined (for analyzing algorithms) always
succeeds in analyzing a nonrecursive algorithm.
– An irregular change in a loop variable
– a sum too complicated to analyze, and
– the difficulties intrinsic to the average case analysis are
just some of the obstacles that can prove to be
insurmountable.
• These caveats notwithstanding, the plan does work
for many simple nonrecursive algorithms
EXAMPLE 4 The following algorithm finds the number
of binary digits in the binary representation of a
positive decimal integer.
Analysis
• First, notice that the most frequently executed
operation here is not inside the while loop but
rather the comparison n > 1 that determines
whether the loop’s body will be executed.
• Since the number of times the comparison will
be executed is larger than the number of
repetitions of the loop’s body by exactly 1, the
choice is not that important.
• A more significant feature of this example is that the loop
variable takes on only a few values between its lower and
upper limits;
• Therefore, we have to use an alternative way of computing
the number of times the loop is executed.
• Since the value of n is about halved on each repetition of the
loop, the answer should be about log 2 n .
• The exact formula for the number of times the comparison
n>1 will be executed is actually log2 n + 1 = the number of bits
in the binary representation of n