Documente Academic
Documente Profesional
Documente Cultură
We use especially frequently two basic rules that are useful of sum
manipulation.
u u
ca c a
i 1
i
i 1
i
u u u
(a b ) a b
i 1
i i
i 1
i
i 1
i
1 u l 1
i 1
where ℓ < u are some lower and upper limits
n n
i i 1 2 ... n n(n 1) / 2 1 / 2 2 (
i 0 i 1
n n2 )
( n 1) 2 (n 2)(n 1) / 2 (n 1)n / 2 1 / 2n 2 ( n 2)
n 2
i0
(n 1 i ) (n 1) ( n 2) ... 1 (n 1)n / 2
Note that this result was perfectly predictable: in the worst case, the algorithm needs to compare all n(n-1)/2 distinct
pairs of its n elements
General plan for Analyzing Efficiency of Nonrecursive Algorithms
row i * = C[i,j]
col.j
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
General plan for Analyzing Efficiency of Nonrecursive Algorithms:
k 0
Comment
And the total number
of multiplication M(n)
is expresses by the n 1 n 1 n 1
Further computation
gives the following
result
n 1 n 1 n 1 n 1 n 1 n 1
M (n) 1 n n 2 n3
i 0 j 0 k 0 i 0 j 0 i 0
Comment
If you want to estimate the running time of the algorithm
on a particular machine, we do it by the product
T(n)=cmM(n)=cmn3
Where cm is the time of multiplication on the machine in
question. We would get a more accurate estimate if we
took into the account the time spent on the addition too
T(n)=cmM(n)+caA(n)=cmn3+can3=(cm+ca)n3
Where ca is the time of one addition.
Note that the estimates differ only by their multiplicative
constants and not by their order of growth
General plan for Analyzing Efficiency of Nonrecursive Algorithms:
ALGORITHM: Binary(n)
//Input: A positive decimal integer n
//Output:The number of binary digits in n’s binary representation
count ←1
while n > 1 do
count ← count + 1
n ←[n/2]
return count
Comment
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.
A more significant feature of this example is the fact that the loop’s
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 log2 n.
The exact formula for the number of times the comparison n>1 will
be executed is actually |log 2 n| + 1 the number of bits in the binary
representation of n