Block-3 MS-031 Unit-4
Block-3 MS-031 Unit-4
1.0 INTRODUCTION
In the earlier units of the course, we have discussed some well-known techniques,
including the divide-and-conquer technique, for developing algorithms for
algorithmically solvable problems. The divide-and-conquer technique, though quite
useful in solving problems from a number of problem domains, yet in some cases, as
shown below, may give quite inefficient algorithms to solve problems.
n
Example 1.0.1: Consider the problem of computing binomial coefficient (or in
k
linear notation c (n, k)) where n and k are given non-negative integers with n≥k. One
way of defining and calculating the binomial coefficient is by using the following
recursive formula
1 if k = n or k = 0
n n − 1 n − 1
= + if 0 < k < n (1.0.1)
k k − 1 k
0 otherwise
The following recursive algorithm named Bin (n, k), implements the above formula
for computing the binomial coefficient.
If k = n or k = 0 then return 1
else return Bin (n−1, k−1) + Bin (n−1, k)
For computing Bin (n, k) for some given values of n and k, a number of terms Bin
(i, j), 1≤ i ≤ and 1 ≤ j ≤ k, particularly for smaller values of i and j, are repeatedly
calculated. For example, to calculate Bin (7, 5), we compute Bin (6, 5) and Bin (6, 4).
Now, for computing Bin (6, 5), we compute Bin (5, 4) and Bin (5, 5). But for
calculating Bin (6, 4) we have to calculate Bin (5, 4) again. If the above argument is
further carried out for still smaller values, the number of repetitions for Bin (i, j)
increases as values of i and j decrease.
For given values of n and k, in order to compute Bin (n, k), we need to call Bin (i, j)
for 1 ≤ i ≤ n ─ 1 and 1 ≤ j ≤ k─1 and as the values of i and j decrease, the number of
times Bin (i, j) is required to be called and executed generally increases.
The above example follows the Divide-and-Conquer technique in the sense that the
task of calculating C(n, k) is replaced by the two relatively simpler tasks, viz.,
calculating C(n−1, k) and C (n−1, k−1). But this technique, in this particular case,
5
Design Techniques-II makes large number of avoidable repetitions of computations. This is not an isolated
instance where the Divide-and-Conquer technique leads to inefficient solutions. In
such cases, an alternative technique, viz., Dynamic Programming, may prove quite
useful. This unit is devoted to developing algorithms using Dynamic Programming
technique. But before, we discuss the technique in more details, let us briefly discuss
underlying idea of the technique and the fundamental difference between Dynamic
Programming and Divide-and-Conquer technique.
Essential idea of Dynamic Programming, being quite simple, is that we should avoid
calculating the same quantity more than once, usually by keeping a table of known
results for simpler instances. These results, in stead of being calculated repeatedly,
can be retrieved from the table, as and when required, after first computation.
The (i, j) th entry of the table contains the value C(i, j). We know,
C (i, 0) = 1 for all i = 0, 1, 2,…., n and
C (0, j) = 0 for j = 1, 2, …., k
0 1 2 3 …….. k
0 1 0 0 0 …….. 0
1 1
2 1
3 1
. .
. .
. .
. .
n 1
C (i, j) = C (i – 1, j – 1) + C (i – 1, j).
6
After filling up the entries of the first row, the table takes the following form: Dynamic Programming
0 1 2 3 …….. k
0 1 0 0 0 0
1 1 1 0 0 0
2 1
. 1
. .
. .
. .
n 1
From the already calculated values of a given row i, adding successive pairs of
consecutive values, we get the values for (i + 1)th row. After completing the entries
for row with index 4, the table may appear as follows, where the blank entries to the
right of the main diagonal are all zeros.
0 1 2 3 4 ….. k
0 1
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1
. .
. .
. .
. .
n 1
We Summarize below the process followed above, for calculating C(i, j):
First of all, the simplest values C(i, 0) = 1 for i = 1, 2, …., n and C(0, j) = 0 for
j ≥ 1, are obtained directly from the given formula. Next, more complex values are
calculated from the already available less complex values. Obviously, the above
mentioned process is a bottom-up one.
Though the purpose in the above discussion was to introduce and explain the Dynamic
Programming technique, yet we may also consider the complexity of calculating
C (n, k) using the tabular method.
(i) The value in column 0 is always 1 and hence need not be stored.
(ii) Initially 0th row is given by C(0,0) = 1 and C (0, j) = 0 for j = 1, 2, …., k. Once
any value of row 1, say C(1, j) is calculated the values C(0, j−1) and C(0, j) are
no more required and hence C (1, j) may be written in the space currently
occupied by C(0, j) and hence no extra space is required to write C (1, j).
In general, when the value C(i, j) of the ith row is calculated the value C (I – 1, j) is no
more required and hence the cell currently occupied by C (i –1, j) can be used to store
7
Design Techniques-II the value C(i, j). Thus, at any time, one row worth of space is enough to calculate
C(n, k). Therefore, space requirement is θ (k).
In the next sections, we shall discuss solution of some well-known problems, using
Dynamic Programming technique.
1.1 OBJECTIVES
After going through this unit, you should be able to:
We, in India, have currency notes or coins of denominations of Rupees 1, 2, 5, 10, 20,
50, 100, 500 and 1000. Suppose a person has to pay an amount of Rs. 5896 after
having decided to purchase an article. Then, the problem is about how to pay the
amount using minimum number of coins/notes.
8
of making payments. Further, let A, a positive integer, be the amount to be paid using Dynamic Programming
the above-mentioned coins. The problem is to use the minimum number of coins, for
the purpose.
The problem with above mentioned algorithm based on greedy technique, is that in
some cases, it may either fail or may yield suboptimal solutions. In order to establish
inadequacy of greedy technique based algorithms, we consider the following two
examples.
Example 1.2.2: Next, we consider another example, in which greedy algorithm may
yield a solution, but the solution may not be optimal, but only suboptimal. For this
purpose, we consider a hypothetical situation, in which currency notes of
denominations 1, 4 and 6 are available. And, we have to collect an amount of
8 = 6+1+1. But this solution uses three currency notes/coins, whereas another
solution using only two currency notes/coins, viz., 8 = 4+4, is available.
Next, we discuss how the Coin Problem is solved using Dynamic Programming
technique.
Each of the denomination di, 1 ≤ i ≤ k, is made a row label and each of the value j for
1 ≤ j ≤ A is made a column label of the proposed table as shown below, where A is the
amount to be paid:
Amount → 1 2 3 4 ….. j …. A
denomination
1= d1
d2
.
.
.
di C[i, j]
.
.
.
dk
9
Design Techniques-II In the table given on page no. 9, 0 < d1 < d2 < … < dk and C[i, j] denotes the
minimum number of coins of denominations d1, d2, …., di (only) that is used to make
an amount j, where more than one coin of the same denomination may be used. The
value C[i, j] is entered in the table with row label di and column label j.
Next, in respect of entries in the table, we make the following two observations:
(i) In order to collect an amount 0, we require zero number of coins, which is true
whether we are allowed to choose from, say, one of the successively larger sets
of denominations viz., {d1}, {d1, d2}, {d1, d2, d3}, …, {d1, d2, …, dk}. Thus,
entries in the table in the column with 0 as column label, are all 0’s.
(ii) If d1 ≠ 1, then there may be some amounts j (including j = 1) for which, even
with dynamic programming technique, no solution may exist, i.e., there
may not be any number of coins of denominations d1, d2, … dk for which
the sum is j. Therefore, we assume d1 = 1. The case d1 ≠ 1 may be handled
similarly.
As d1 = 1, therefore, the first row of the table in the jth column, contains j, the
number of coins of denomination only d1 = 1 to form value j.
0 1 2 3 4 ….. j …. A
d1 0 1 2 3 4 … j … A
d2 0
di 0
. .
. .
. .
dk 0
Next for i ≥ 2 and j ≥ 1, the value C[i , j], the minimum number of coins of
denominations upto di (only) to sum up to j, can be obtained recursively through
either of the following two ways:
10
By definition, C[i, j] = min { 1 + C[ i, j – di], C [ i─1, j]} and can be calculated, as Dynamic Programming
the two involved values viz., C [ i, j – di] and C [ i─1, j] are already known.
Comment 1.2.3
If j < di in case (1.1) then the Equation (1.1) is impossible. Mathematically we can
say C [ i, j – di] = ∞ if j < di, because, then the case is automatically excluded from
consideration for calculating C[i, j].
Similarly we take
C[i−1, j] = ∞ if i<1
Following the above procedure, C{k, A] gives the desired number.
In order to explain the above method, let us consider the earlier example for which
greedy algorithm gave only suboptimal solution.
Example 1.2.4: Using Dynamic Programming technique, find out minimum number
of coins required to collect Rupees 8 out of coins of denominations 1, 4, 6.
From the earlier discussion we already know the following portion of the table to be
developed using Dynamic Programming technique.
0 1 2 3 4 5 6 7 8
d1 = 1 0 1 2 3 4 5 6 7 8
d2 = 4 0
di = 6 0
Next we consider
Next, interesting case is C[2, 4], i.e., to find out minimum number of coins to make an
amount 4 out of coins of denominations 1, 4, 6. By definition,
11
Design Techniques-II C[2, 4] = min {1 + C (2, 4 ─ 4), C(1, 4)}
But C[2, 0] = 0 and C [1, 4] = 4, therefore,
= min {1 + 0, 4} = 1 C[2, 4]
By following the method explained through the above example steps, finally, we get
the table as
0 1 2 3 4 5 6 7 8
d1 = 1 0 1 2 3 4 5 6 7 8
d2 = 4 0 1 2 3 1 2 3 4 2
di= 6 0 1 2 3 1 2 1 2 2
Let us formalize the method explained above of computing C[k, A], in the general
case, in the form of the following algorithm:
array C [1… k, 0… A]
For i = 1 to k
Read (d [i ] )
{reads the various denominations available with each denomination coin in sufficient
numbers}.
{assuming d1 = 1, initialize the table C { } as follows}
For i = 1 to k
C[ i, 0] = 0
For j = 1to A
C [ 1, j] = j
Comments 1.2.5
Comment 1: The above algorithm explicitly gives only the number of coins, which
are minimum to make a pre-assigned amount A, yet it can also be used to determine
the set of coins of various denominations that add upto A.
By definition, C [i, j] = either 1 + C [i, j − di] or C [i −1, j], which means either we
choose a coin of denomination di or we do not choose a coin of denomination di,
12
depending upon whether 1 + C [i, j − di] ≤ C [ i ─ 1, j] or not. Applying the above rule Dynamic Programming
recursively for decreasing values of i and j, we know which coins are chosen for
making an amount j out of the available coins.
While using the Dynamic Programming technique in solving the coin problem we
tacitly assumed the above mentioned principle in defining C [i, j], as minimum of
1 + C [i, j − di] and C [ i – 1, j]. The principle is used so frequently and without our
being aware of having used it.
However, we must be aware that there may be situations in which the principle may
not be applicable. The principle of optimality may not be true, specially, in situations
where resources used over the (sub) components may be more than total resources
used over the whole, and where total resources are limited and fixed. A simple
example may explain the point. For example, suppose a 400-meter race champion
takes 42 seconds to complete the race. However, covering 100 meters in 10.5 seconds
may not be the best/optimal solution for 100 meters. The champion may take less
than 10 seconds to cover 100 meters. The reason being total resources (here the
concentration and energy of the athlete) are limited and fixed whether distributed
over 100 meters or 400 meters.
Similarly, for a vehicle with best performance over 100 miles, can not be thought of
in terms of 10 times best performance over 10 miles. First of all, fuel usage after
some lower threshold, increases with speed. Therefore, as the distance to be covered
increases (e.g., from 10 to 100 miles) then fuel has to be used more cautiously,
restraining the speed, as compared to when distance to be covered is less (e.g., 10
miles). Even if refuelling is allowed, refuelling also takes time. The drivers
concentration and energy are other fixed and limited resources; which in the case of
shorter distance can be used more liberally as compared to over longer distances, and
in the process produce better speed over short distances as compared to over long
distances. The above discussion is for the purpose of driving attention to the fact that
principle of optimality is not universal, specially when the resources are limited and
fixed. Further, it is to draw attention that Dynamic Programming technique assumes
validity of Principle of Optimality for the problem domain. Hence, while applying
Dynamic Programming technique for solving optimisation problems, in order for the
validity of the solution based on the technique, we need to ensure that the Optimality
Principle is valid for the problem domain.
Ex. 1) Using Dynamic Programming, solve the following problem (well known as
Knapsack Problem). Also write the algorithm that solves the problem.
13
Design Techniques-II into pieces∗. In other words, either a whole object is to be included or it has to be
excluded.
(iv) Though, for three or more matrices, matrix multiplication is associative, yet
the number of scalar multiplications may very significantly depending upon
how we pair the matrices and their product matrices to get the final product.
Summering: The product of matrices A (14 × 6), B(6 × 90) and C(90 × 4) takes
12600 scalar operators when first the product of A and B is computed and then
product AB is multiplied with C. On the other hand, if the product BC is calculated
first and then product of A with matrix BC is taken then only 2496 scalar
multiplications are required. The later number is around 20% of the former. In case
when large number of matrices are to be multiplied for which the product is defined,
proper parenthesizing through pairing of matrices, may cause dramatic saving in
number of scalar operations.
∗
Another version allows any fraction xi with 0≤xi≤1. However, in this problem, we
assume either xi = 1 or xi = 0.
14
This raises the question of how to parenthesize the pairs of matrices within the Dynamic Programming
expression A1A2 … An, a product of n matrices which is defined; so as to optimize
the computation of the product A1A2 … An. The product is known as Chained
Matrix Multiplication.
Brute-Force Method: One way for finding the optimal method (i.e., the method
which uses minimum number of scalar (numerical) operations) is to parenthesize the
expression A1A2 … An in all possible ways and calculate number of scalar
multiplications required for each way. Then choose the way which requires minimum
number of scalar multiplications.
However, if T(n) denotes the number of ways of putting parentheses for pairing the
expression A1A2 … An, T(n) is an exponentially increasing function. The rate at
which values of T(n) increase may be seen from the following table of values of T(n)
for n = 1, 2, …….
n : 1 2 3 4 5 … 10 … 15
T(n): 1 1 2 5 14 … 4862 … 2674440
Hence, it is almost impossible to use the method for determining how to optimize the
computation of the product A1A2 … An.
Thus, the Dynamic Programming technique can be applied to the problem, and is
discussed below:
Let us first define the problem. Let Ai, 1 ≤ i ≤ n, be a di-1 × di matrix. Let the vector
d [ 0.. n] stores the dimensions of the matrices, where the dimension of
Ai is di-1 × di for i = 1, 2, …, n. By definition, any subsequence Aj…Ak of
A1A2 … An for 1 ≤ j ≤ k ≤ n is a well-defined product of matrices. Let us consider a
table m [1.. n, 1 .. n] in which the entries mij for 1 ≤ i ≤ j ≤ n, represent optimal (i.e.,
minimum) number of operations required to compute the product matrix (Ai … Aj).
We fill up the table diagonal-wise, i.e., in one iteration we fill-up the table one
diagonal mi, i+s, at a time, for some constant s ≥ 0. Initially we consider the biggest
diagonal mii for which s = 0. Then next the diagonal mi, i+s for s = 1 and so on.
Now mii stands for the minimum scalar multiplications required to compute the
product of single matrix Ai. But number of scalar multiplications required are zero.
Hence,
mii = 0 for i = 1, 2, … n.
15
Design Techniques-II Filling up entries for mi(i +1) for i = 1, 2, …, (n – 1).
mi(i +1) denotes the minimum number of scalar multiplication required to find the
product Ai Ai+1. As Ai is di-1 × di matrix and Ai+1 is di × di+1 matrix. Hence,
there is a unique number for scalar multiplication for computing Ai Ai+1 giving
Assuming optimal number of scalar multiplication viz., mi,j and mi+1, j are already
known, we can say that
for i = 1, 2, …, n – s.
When the term di-1 dj di+s represents the number of scalar multiplications required to
multiply the resultant matrices (Ai … Aj) and (Aj+1 … Ai+s)
Summing up the discussion, we come the definition mi,i+s for i=1,2, …(n −1) as
Let us illustrate the algorithm to compute mj+1,i+s discussed above through an example
A1 of order 14 × 6
A2 of order 6 × 90
A3 of order 90 × 4
A4 of order 4 × 35
16
m12 = d0 d1 d2 = 14 × 6 × 90 = 7560 Dynamic Programming
m23 = d1 d2 d3 = 6 × 90 × 4 = 3240
m34 = d2 d3 d4 = 9 × 4 × 35 = 1260
1 2 3 4
1 0 7560
2 0 3240
3 0 1260
4 0
Finally, for s = 3
1.6 SUMMARY
17
Design Techniques-II (5) The Knapsack Problem: We are given n objects and a knapsack. For i = 1,
2, …, n, object i has a positive weight wi and a positive value vi. The
knapsack can carry a weight not exceeding W. The problem requires that
the knapsack is filled in a way that maximizes the value of the objects
included in the knapsack.
1.7 SOLUTIONS/ANSWERS
Ex. 1)
First of all, it can be easily verified that Principle of Optimality is valid in the
sense for an optimal solution of the overall problem each subsolution is also
optimal. Because in this case, a non-optimal solution of a subproblem, when
replaced by a better solution of the subproblem, would lead to a better than
optimal solution of the overall problem. A contradiction.
In order to label the rows we first of all, order the given objects according to
increasing relative values R = v/w.
Thus first object O1 is the one with minimum relative value R1. The object O2 is
the one with next least relative value R2 and so on. The last object, in the
sequence, is On, with maximum relative weight Rn.
The ith row of the Table Corresponds to object Oi having ith relative value,
when values are arranged in increasing order. The jth column corresponds to
weight j for 0≤j ≤W. The entry Knap [i, j] denotes the maximum value that can
be packed in knapsack when objects O1, O2, …,Oi only are used and the
included objects have weight at most j.
Next, in order to fill up the entries Knap[i, j], 1≤i ≤ n and 0≤j ≤W, of the table,
we can check, as was done in the coin problem that,
∗
Another version allows any fraction xi with 0≤xi≤1. However, in this problem, we assume either xi = 1
or xi = 0.
18
0 1 2 3 4 … j … W Dynamic Programming
1 0 V V V V V V
2 0
. .
. .
. .
j 0
. .
. .
. .
n 0
Further, when calculating Knap [i, j], there are two possibilities: either
(i) The ith object Oi is taken into consideration, then
Knap [i, j] = Knap [i−1, j − wi] + vi or
(ii) The ith object Oi is not taken into consideration then
Knap [i, j] = V[i−1, j]
Thus we define
Knap [i, j] = max {Knap [i−1, j], Knap [i−1, j - wi] + vi}
The above equation is valid for i = 2 and j ≥ wi. In order that the above equation may
be applicable otherwise also, without violating the intended meaning we take,
We explain the Dynamic Programming based solution suggested above, through the
following example.
We are given six objects, whose weights are respectively 1, 2, 5, 6, 7, 10 units and
whose values respectively are 1, 6, 18, 22, 28, 43. Thus relative values in increasing
order are respectively 1.00, 3.00, 3.60, 3.67, 4.00 and 4.30. If we can carry a
maximum weight of 12, then the table below shows that we can compose a load
whose value is 49.
Weights 0 1 2 3 4 5 6 7 8 9 10 11 12
Relative Values
w1= 1, v1 = 1, R1 = 1.00 0 1 1 1 1 1 1 1 1 1 1 1 1
w2 = 2, v2 = 6, R2 = 3.00 0 1 6 7 7 7 7 7 7 7 7 7 7
w3 = 5, v3 = 18, R3 = 3.60 0 1 6 7 7 18 19 24 25 25 25 25 25
W4 = 6, v4 = 22, R4 =3.67 0 1 6 7 7 18 22 24 28 29 29 40 41
W5 = 7, v5 = 28 R5 = 4.00 0 1 6 7 7 18 22 28 29 34 35 40 46
19
Design Techniques-II Algorithm for the solution of the solution explained above of the Knapsack
Problem.
begin
read (Weight [i ] )
read (value [i ] )
R [ i ] / weight [ i ]
end
For j = 1 to n do
Begin
k=j
For t = j + 1 to n do
If R [ t ] < R [ k ] then
k=t
Exchange (R [ j ], R [ k ]);
Exchange (Weight [ j ], Weight [ k ]);
Exchange (Value [ j ], value [ k ] );
end
{At this stage R[ 1.. n] is a sorted array in increasing order and Weight [ j] and value
[ j ] are respectively weight and value for jth least relative value}
{Next, we complete the table knap for the problem}
For i = 1 to n do
Knap [ i , 0] = 0 for i = 1, …, n
Knap [ 1, j] = value [ 1 ] for j = 1, …, W
If i ≤ 0 and j ≥ 0 then
Knap [ i , j ] = 0
Else if j < 0 ─ then
Knap [ i, j ] = ─ ∞
For i = 2 to n do
For j = 1 to W do
Knap [ i, j] = maximum {Knap [i –1, j], Knap [ i − 1, j – Weight [i] ]
+ value [ i ]
Retu rn Knap [ n, W]
20
Dynamic Programming
1.8 FURTHER READINGS
1. Foundations of Algorithms, R. Neapolitan & K. Naimipour:
(D.C. Health & Company, 1996).
2. Algoritmics: The Spirit of Computing, D. Harel, (Addison-Wesley
Publishing Company, 1987).
3. Fundamental Algorithms (Second Edition), D.E. Knuth, (Narosa Publishing
House).
4. Fundamentals of Algorithmics, G. Brassard & P. Brately, (Prentice-Hall
International, 1996).
5. Fundamentals of Computer Algorithms, E. Horowitz & S. Sahni, (Galgotia
Publications).
6. The Design and Analysis of Algorithms, Anany Levitin, (Pearson Education,
2003).
7. Programming Languages (Second Edition) ─ Concepts and Constructs, Ravi
Sethi, (Pearson Education, Asia, 1996).
21