Anda di halaman 1dari 2

6.

Consider the following version of an important algorithm that we will study later in the
book.
Algorithm GE(A[0..n 1, 0..n])
//Input: An n-by-n +1 matrix A[0..n 1, 0..n] of real numbers
for i 0 to n 2 do
for j i +1 to n 1 do
for k i to n do
A[j, k] A[j, k] A[i, k] * A[j, i] / A[i, i]
a. Find the time efficiency class of this algorithm.
The number of multiplications M(n) and the number of divisions D(n) made by the algorithm are
given by the same sum:
n1

n2 n2

j=i +1 k=i

(ni+1)=

i=0 j=i+1

n 2

M ( n )=D ( n )=
i=0

n2

n2

i=0

i=0

( ni+ 1 ) ( n1 (i+1 ) +1 )= ( ni+1 ) (ni1)


( n+1 ) ( n1 ) +n ( n2 ) ++31
n1

n1

n1

j=1

j=1

j=1

( j+2 ) j= j 2 +

( n1 ) n(2 n1) 2 ( n1 ) n
+
6
2

n ( n1 ) (2 n+5) 1 3
n (n3 )
6
3

b. What glaring inefficiency does this pseudocode contain and how can it be eliminated
to speed the algorithm up?
The inefficiency is the repeated evaluation of the ratio A[j, i] / A[i, i] in the algorithms
innermost loop, which, in fact, does not change with the loop variable k.
Compute loop variant:

temp A[j, i] / A[i, i]; the innermost loop is then changed to


A[j, k] A[j, k] A[i, k] * temp.
This change eliminates the most expensive operation of the algorithm, the division, from its
innermost loop. The running time gain obtained by this change can be estimated as follows:
Told (n)

Told (n)

cM

1 3
1
n +cD n 3
3
3
cM + cD cD
=
=
+1,
1 3
cM
cM
cM n
3

Where
cD =Time for one division
cM=One multiplication.

Anda mungkin juga menyukai