Daa Unit-3
Daa Unit-3
Daa Unit-3
UNIT III
Dynamic Programming: The general method, multistage graphs, All pairs-shortest
paths, single-source shortest paths: general weights, optimal Binary search trees, 0/1
knapsack, reliability Design, The traveling salesperson problem, matrix chain
multiplication.
Dynamic Programming
Dynamic programming is a name, coined by Richard Bellman in 1955. Dynamic
programming, as greedy method, is a powerful algorithm design technique that can
be used when the solution to the problem may be viewed as the result of a sequence
of decisions. In the greedy method we make irrevocable decisions one at a time,
using a greedy criterion. However, in dynamic programming we examine the decision
sequence to see whether an optimal decision sequence contains optimal decision
subsequence.
Dynamic programming differs from the greedy method since the greedy method
produces only one feasible solution, which may or may not be optimal, while dynamic
programming produces all possible sub-problems at most once, one of which
guaranteed to be optimal. Optimal solutions to sub-problems are retained in a table,
thereby avoiding the work of recomputing the answer every time a sub-problem is
encountered
The divide and conquer principle solve a large problem, by breaking it up into smaller
problems which can be solved independently. In dynamic programming this principle
is carried to an extreme: when we don't know exactly which smaller problems to
solve, we simply solve them all, then store the answers away in a table to be used
later in solving larger problems. Care is to be taken to avoid recomputing previously
computed values, otherwise the recursive program will have prohibitive complexity.
CSE Page 3
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
In some cases, the solution can be improved and in other cases, the dynamic
programming technique is the best approach.
CSE Page 3
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
Let the vertex „s‟ is the source, and „t‟ the sink. Let c (i, j) be the cost of edge <i, j>.
The cost of a path from „s‟ to „t‟ is the sum of the costs of the edges on the path. The
multistage graph problem is to find a minimum cost path from „s‟ to „t‟. Each set Vi
defines a stage in the graph. Because of the constraints on E, every path from „s‟ to
„t‟ starts in stage 1, goes to stage 2, then to stage 3, then to stage 4, and so on, and
eventually terminates in stage k.
ALGORITHM:
CSE Page 3
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
The multistage graph problem can also be solved using the backward approach.
Let bp(i, j) be a minimum cost path from vertex s to j vertex in V i. Let Bcost(i, j) be
the cost of bp(i, j). From the backward approach we obtain:
Complexity Analysis:
The complexity analysis of the algorithm is fairly straightforward. Here, if G has E
edges, then the time for the first for loop is (V +E).
EXAMPLE 1:
Find the minimum cost path from s to t in the multistage graph of five stages shown
below. Do this first using forward approach and then using backward approach.
2 4 6
2 6 9
9 2 5 4
1
3 4
7 7 2
7 10 12 t
s 1 3 3
4 11
2 5
5
8 11
11 6
5 8
FORWARD APPROACH:
We use the following equation to find the minimum cost path from s to t:
<j, l> E
cost (1, 1) = min {c (1, 2) + cost (2, 2), c (1, 3) + cost (2, 3), c (1, 4) + cost (2, 4),
c (1, 5) + cost (2, 5)}
= min {9 + cost (2, 2), 7 + cost (2, 3), 3 + cost (2, 4), 2 + cost (2, 5)}
cost (2, 2) = min{c (2, 6) + cost (3, 6), c (2, 7) + cost (3, 7), c (2, 8) + cost (3, 8)}
= min {4 + cost (3, 6), 2 + cost (3, 7), 1 + cost (3, 8)}
cost (3, 6) = min {c (6, 9) + cost (4, 9), c (6, 10) + cost (4, 10)}
= min {6 + cost (4, 9), 5 + cost (4, 10)}
cost (3, 7) = min {c (7, 9) + cost (4, 9) , c (7, 10) + cost (4, 10)}
= min {4 + cost (4, 9), 3 + cost (4, 10)}
cost (3, 8) = min {c (8, 10) + cost (4, 10), c (8, 11) + cost (4, 11)}
= min {5 + cost (4, 10), 6 + cost (4 + 11)}
Therefore, cost (2, 3) = min {c (3, 6) + cost (3, 6), c (3, 7) + cost (3, 7)}
= min {2 + cost (3, 6), 7 + cost (3, 7)}
= min {2 + 7, 7 + 5} = min {9, 12} = 9
102
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
The path is 1 2 7 10 12
or
1 3 6 10 12
BACKWARD APPROACH:
We use the following equation to find the minimum cost path from t to s:
Bcost (5, 12) = min {Bcost (4, 9) + c (9, 12), Bcost (4, 10) + c (10, 12),
Bcost (4, 11) + c (11, 12)}
= min {Bcost (4, 9) + 4, Bcost (4, 10) + 2, Bcost (4, 11) + 5}
Bcost (4, 9) = min {Bcost (3, 6) + c (6, 9), Bcost (3, 7) + c (7, 9)}
= min {Bcost (3, 6) + 6, Bcost (3, 7) + 4}
Bcost (3, 6) = min {Bcost (2, 2) + c (2, 6), Bcost (2, 3) + c (3, 6)}
= min {Bcost (2, 2) + 4, Bcost (2, 3) + 2}
Bcost (3, 7) = min {Bcost (2, 2) + c (2, 7), Bcost (2, 3) + c (3, 7),
Bcost (2, 5) + c (5, 7)}
Bcost (4, 10) = min {Bcost (3, 6) + c (6, 10), Bcost (3, 7) + c (7, 10),
Bcost (3, 8) + c (8, 10)}
Bcost (3, 8) = min {Bcost (2, 2) + c (2, 8), Bcost (2, 4) + c (4, 8),
Bcost (2, 5) + c (5, 8)}
Bcost (2, 4) = min {Bcost (1, 1) + c (1, 4)} = 3
Bcost (4, 11) = min {Bcost (3, 8) + c (8, 11)} = min {Bcost (3, 8) + 6}
= min {10 + 6} = 16
103
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
Bcost (5, 12) = min {15 + 4, 14 + 2, 16 + 5} = min {19, 16, 21} = 16.
EXAMPLE 2:
Find the minimum cost path from s to t in the multistage graph of five stages shown
below. Do this first using forward approach and then using backward approach.
4 1
3
2 4 7
6 7
5
3 6
s 1 5 2 9 t
2 5
3
3 8
6
8 2
6
SOLUTION:
FORWARD APPROACH:
cost (1, 1) = min {c (1, 2) + cost (2, 2), c (1, 3) + cost (2, 3)}
= min {5 + cost (2, 2), 2 + cost (2, 3)}
cost (2, 2) = min {c (2, 4) + cost (3, 4), c (2, 6) + cost (3, 6)}
= min {3+ cost (3, 4), 3 + cost (3, 6)}
cost (3, 4) = min {c (4, 7) + cost (4, 7), c (4, 8) + cost (4, 8)}
= min {(1 + cost (4, 7), 4 + cost (4, 8)}
cost (3, 6) = min {c (6, 7) + cost (4, 7), c (6, 8) + cost (4, 8)}
= min {6 + cost (4, 7), 2 + cost (4, 8)} = min {6 + 7, 2 + 3} = 5
cost (2, 3) = min {c (3, 4) + cost (3, 4), c (3, 5) + cost (3, 5), c (3, 6) + cost
(3,6)}
cost (3, 5) = min {c (5, 7) + cost (4, 7), c (5, 8) + cost (4, 8)}= min {6 + 7, 2 + 3}
=5
104
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
BACKWARD APPROACH:
Bcost (5, 9) = min {Bcost (4, 7) + c (7, 9), Bcost (4, 8) + c (8, 9)}
= min {Bcost (4, 7) + 7, Bcost (4, 8) + 3}
Bcost (4, 7) = min {Bcost (3, 4) + c (4, 7), Bcost (3, 5) + c (5, 7),
Bcost (3, 6) + c (6, 7)}
= min {Bcost (3, 4) + 1, Bcost (3, 5) + 6, Bcost (3, 6) + 6}
Bcost (3, 4) = min {Bcost (2, 2) + c (2, 4), Bcost (2, 3) + c (3, 4)}
= min {Bcost (2, 2) + 3, Bcost (2, 3) + 6}
Bcost (3, 6) = min {Bcost (2, 2) + c (2, 6), Bcost (2, 3) + c (3, 6)}
= min {5 + 5, 2 + 8} = 10
Bcost (4, 8) = min {Bcost (3, 4) + c (4, 8), Bcost (3, 5) + c (5, 8),
Bcost (3, 6) + c (6, 8)}
= min {8 + 4, 7 + 2, 10 + 2} = 9
In the all pairs shortest path problem, we are to find a shortest path between every
pair of vertices in a directed graph G. That is, for every pair of vertices (i, j), we are
to find a shortest path from i to j as well as one from j to i. These two paths are the
same when G is undirected.
When no edge has a negative length, the all-pairs shortest path problem may be
solved by using Dijkstra‟s greedy single source algorithm n times, once with each of
the n vertices as the source vertex.
The all pairs shortest path problem is to determine a matrix A such that A (i, j) is the
length of a shortest path from i to j. The matrix A can be obtained by solving n
single-source problems using the algorithm shortest Paths. Since each application of
this procedure requires O (n2) time, the matrix A can be obtained in O (n 3) time.
105
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
Ak (i, j) = {min {min {Ak-1 (i, k) + Ak-1 (k, j)}, c (i, j)}
1<k<n
Example 1:
Given a weighted digraph G = (V, E) with weight. Determine the length of the
shortest path between all pairs of vertices in G. Here we assume that there are no
cycles with zero or negative cost.
6
1 2 0 4 11
4
0
Cost adjacency matrix (A ) = 6 0 2
3 1 1 2
3 0
3
General formula: min {Ak-1 (i, k) + Ak-1 (k, j)}, c (i, j)}
1<k<n
106
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
0 4 11
A (1)
= 2
6 0
3 7 0
A2 (1, 1) = min {(A1 (1, 2) + A1 (2, 1), c (1, 1)} = min {(4 + 6), 0} = 0
A2 (1, 2) = min {(A1 (1, 2) + A1 (2, 2), c (1, 2)} = min {(4 + 0), 4} = 4
A2 (1, 3) = min {(A1 (1, 2) + A1 (2, 3), c (1, 3)} = min {(4 + 2), 11} = 6
A2 (2, 1) = min {(A (2, 2) + A (2, 1), c (2, 1)} = min {(0 + 6), 6} = 6
A2 (2, 2) = min {(A (2, 2) + A (2, 2), c (2, 2)} = min {(0 + 0), 0} = 0
A2 (2, 3) = min {(A (2, 2) + A (2, 3), c (2, 3)} = min {(0 + 2), 2} = 2
A2 (3, 1) = min {(A (3, 2) + A (2, 1), c (3, 1)} = min {(7 + 6), 3} = 3
A2 (3, 2) = min {(A (3, 2) + A (2, 2), c (3, 2)} = min {(7 + 0), 7} = 7
A2 (3, 3) = min {(A (3, 2) + A (2, 3), c (3, 3)} = min {(7 + 2), 0} = 0
0 4 6
A (2)
= 6 0 2
3 7 0
A3 (1, 1) = min {A2 (1, 3) + A2 (3, 1), c (1, 1)} = min {(6 + 3), 0} = 0
A3 (1, 2) = min {A2 (1, 3) + A2 (3, 2), c (1, 2)} = min {(6 + 7), 4} = 4
A3 (1, 3) = min {A2 (1, 3) + A2 (3, 3), c (1, 3)} = min {(6 + 0), 6} = 6
A3 (2, 1) = min {A2 (2, 3) + A2 (3, 1), c (2, 1)} = min {(2 + 3), 6} = 5
A3 (2, 2) = min {A2 (2, 3) + A2 (3, 2), c (2, 2)} = min {(2 + 7), 0} = 0
A3 (2, 3) = min {A2 (2, 3) + A2 (3, 3), c (2, 3)} = min {(2 + 0), 2} = 2
A3 (3, 1) = min {A2 (3, 3) + A2 (3, 1), c (3, 1)} = min {(0 + 3), 3} = 3
A3 (3, 2) = min {A2 (3, 3) + A2 (3, 2), c (3, 2)} = min {(0 + 7), 7} = 7
107
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
A3 (3, 3) = min {A2 (3, 3) + A2 (3, 3), c (3, 3)} = min {(0 + 0), 0} = 0
0 4 6
A(3) = 5
0 2
3 7 0
Let G = (V, E) be a directed graph with edge costs Cij. The variable cij is defined such
that cij > 0 for all I and j and cij = if < i, j> E. Let |V| = n and assume n > 1. A
tour of G is a directed simple cycle that includes every vertex in V. The cost of a tour
is the sum of the cost of the edges on the tour. The traveling sales person problem is
to find a tour of minimum cost. The tour is to be a simple path that starts and ends
at vertex 1.
Let g (i, S) be the length of shortest path starting at vertex i, going through all
vertices in S, and terminating at vertex 1. The function g (1, V – {1}) is the length of
an optimal salesperson tour. From the principal of optimality it follows that:
The Equation can be solved for g (1, V – 1}) if we know g (k, V – {1, k}) for all
choices of k.
Complexity Analysis:
For each value of |S| there are n – 1 choices for i. The number of distinct sets S of
n 2
size k not including 1 and i is .
k
Hence, the total number of g (i, S)‟s to be computed before computing g (1, V – {1})
is:
n 1 n 2
n1 k
k 0
To calculate this sum, we use the binominal theorem:
(n 2 (n 2 (n 2 (n 2)
(n – 1)
0 1 2 (n 2)
Therefore,
108
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
n 1 n 2
n1 k (n 1) 2n 2
k 0
This is Φ (n 2n-2), so there are exponential number of calculate. Calculating one g (i,
S) require finding the minimum of at most n quantities. Therefore, the entire
algorithm is Φ (n2 2n-2). This is better than enumerating all n! different tours to find
the best one. So, we have traded on exponential growth for a much smaller
exponential growth. The most serious drawback of this dynamic programming
solution is the space needed, which is O (n 2 n). This is too large even for modest
values of n.
Example 1:
For the following graph find minimum cost tour for the traveling salesperson
problem:
1 2
0 10 15 20
The cost adjacency matrix =
5
0 9 10
6 13 0 12
8 9 0
3 4 8
Let us start the tour from vertex 1:
g (2, ) = C21 = 5
g (3, ) = C31 = 6
g (4, ) = C41 = 8
g (1, {2, 3, 4}) = min {c12 + g (2, {3, 4}, c13 + g (3, {2, 4}), c14 + g (4, {2, 3})}
g (2, {3, 4}) = min {c23 + g (3, {4}), c24 + g (4, {3})}
= min {9 + g (3, {4}), 10 + g (4, {3})}
109
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
Therefore, g (2, {3, 4}) = min {9 + 20, 10 + 15} = min {29, 25} = 25
g (3, {2, 4}) = min {(c32 + g (2, {4}), (c34 + g (4, {2})}
Therefore, g (3, {2, 4}) = min {13 + 18, 12 + 13} = min {41, 25} = 25
g (4, {2, 3}) = min {c42 + g (2, {3}), c43 + g (3, {2})}
Therefore, g (4, {2, 3}) = min {8 + 15, 9 + 18} = min {23, 27} = 23
g (1, {2, 3, 4}) = min {c12 + g (2, {3, 4}), c13 + g (3, {2, 4}), c14 + g (4, {2, 3})}
= min {10 + 25, 15 + 25, 20 + 23} = min {35, 40, 43} = 35
Let us assume that the given set of identifiers is {a1, . . . , an} with a1 < a2 < <
an. Let p (i) be the probability with which we search for ai. Let q (i) be the probability
that the identifier x being searched for is such that ai < x < ai+1, 0 < i < n (assume
a0 = - and an+1 = +). We have to arrange the identifiers in a binary search tree in
a way that minimizes the expected total access time.
In a binary search tree, the number of comparisons needed to access an element at
depth 'd' is d + 1, so if 'ai' is placed at depth 'di', then we want to minimize:
n
Pi (1 di ) .
i 1
Let P (i) be the probability with which we shall be searching for 'ai'. Let Q (i) be the
probability of an un-successful search. Every internal node represents a point where
a successful search may terminate. Every external node represents a point where an
unsuccessful search may terminate.
The expected cost contribution for the internal node for 'ai' is:
Unsuccessful search terminate with I = 0 (i.e at an external node). Hence the cost
contribution for this node is:
110
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
n n
Given a fixed set of identifiers, we wish to create a binary search tree organization.
We may expect different binary search trees for the same identifier set to have
different performance characteristics.
The computation of each of these c(i, j)‟s requires us to find the minimum of m
quantities. Hence, each such c(i, j) can be computed in time O(m). The total time for
all c(i, j)‟s with j – i = m is therefore O(nm – m2).
The total time to evaluate all the c(i, j)‟s and r(i, j)‟s is therefore:
nm m O n
1m n
2 3
Example 1: The possible binary search trees for the identifier set (a1, a2, a3) = (do,
if, stop) are as follows. Given the equal probabilities p (i) = Q (i) = 1/7 for all i, we
have:
st o p if
if do st o p
do
Tree 2
Tree 1
do do
if st o p
st o p if
Tree 3 T ree 4
1 1 1 1 1 1 1
Cost (tree # 1) = x1 x2 x3 x1 x2 x3 x3
7 7 7 7 7 7 7
1 2 3 1 2 3 3 6 9 15
=
7 7 7 7 1
1 1 1 1 1 1
Cost (tree # 2) = x 1 x 2 x2 x 2 x 2 x 2 x2
7 7
7 7 7 7 7
1 2 2 2 2 2 2 5 8 13
=
7 7 7 7
111
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
1 1 1 1 1 1 1
Cost (tree # 3) = x1 x2 x3 x1 x2 x3
7 7 7 7 7 x 3
7 7
1 23 1 2 33 6 9 15
=
7 7 7 7 1
1 1 1 1 1 1
Cost (tree # 4) = x1 x2 x3 x1 x2 x3 x3
7 7 7 7 7 7 7
1 23 1 2 33 6 9 15
=
7 7 7 7
Huffman coding tree solved by a greedy algorithm has a limitation of having the data
only at the leaves and it must not preserve the property that all nodes to the left of
the root have keys, which are less etc. Construction of an optimal binary search tree
is harder, because the data is not constrained to appear only at the leaves, and also
because the tree must satisfy the binary search tree property and it must preserve
the property that all nodes to the left of the root have keys, which are less.
A dynamic programming solution to the problem of obtaining an optimal binary
search tree can be viewed by constructing a tree as a result of sequence of decisions
by holding the principle of optimality. A possible approach to this is to make a
decision as which of the ai's be arraigned to the root node at 'T'. If we choose 'ak'
then is clear that the internal nodes for a1, a2, . . . . . ak-1 as well as the external
nodes for the classes Eo, E1, . . . . . . . Ek-1 will lie in the left sub tree, L, of the root.
The remaining nodes will be in the right subtree, R. The structure of an optimal
binary search tree is:
ak
L R
K K
Cost (L) = P(i)* level (a ) Q(i)* level (E ) 1
i i
i 1 i 0
n n
Cost (R) = P(i)* level (a ) Q(i)* level (E ) 1
i i
i K i K
C (i, J) = min {C (i, k-1) + C (k, J) + P (K) + w (i, K-1) + w (K, J)}
i<k<J
Equation (1) may be solved for C (0, n) by first computing all C (i, J) such that J - i =
1
Next, we can compute all C (i, J) such that J - i = 2, Then all C (i, J) with J - i = 3
and so on.
112
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
C (i, J) is the cost of the optimal binary search tree 'T ij' during computation we record
the root R (i, J) of each tree 'Tij'. Then an optimal binary search tree may be
constructed from these R (i, J). R (i, J) is the value of 'K' that minimizes equation (1).
We solve the problem by knowing W (i, i+1), C (i, i+1) and R (i, i+1), 0 ≤ i < 4;
Knowing W (i, i+2), C (i, i+2) and R (i, i+2), 0 ≤ i < 3 and repeating until W (0, n),
C (0, n) and R (0, n) are obtained.
Example 1:
Let n = 4, and (a1, a2, a3, a4) = (do, if, need, while) Let P (1: 4) = (3, 3, 1, 1) and Q
(0: 4) = (2, 3, 1, 1, 1)
Solution:
Column
0 1 2 3 4
Row
0 2, 0, 0 3, 0, 0 1, 0, 0 1, 0, 0, 1, 0, 0
1 8, 8, 1 7, 7, 2 3, 3, 3 3, 3, 4
4 16, 32, 2
This computation is carried out row-wise from row 0 to row 4. Initially, W (i, i) = Q
(i) and C (i, i) = 0 and R (i, i) = 0, 0 < i < 4.
113
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
114
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
From the table we see that C (0, 4) = 32 is the minimum cost of a binary search tree
for (a1, a2, a3, a4). The root of the tree 'T04' is 'a2'.
Hence the left sub tree is 'T01' and right sub tree is T24. The root of 'T01' is 'a1' and the
root of 'T24' is a3.
The left and right sub trees for 'T01' are 'T00' and 'T11' respectively. The root of T01 is
'a1'
The left and right sub trees for T24 are T22 and T34 respectively.
a1 a3
T 01 T24 do read
a4
T00 T11 T22 T34 while
Example 2:
115
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
R (2, 3) = 3
116
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
= 16 + min [0 + 18, 9 + 8, 18 + 3, 25 + 0] = 16 + 17 = 33
R (0, 4) = 2
Column
0 1 2 3 4
Row
0 2, 0, 0 1, 0, 0 1, 0, 0 1, 0, 0, 1, 0, 0
1 9, 9, 1 6, 6, 2 3, 3, 3 3, 3, 4
4 16, 33, 2
From the table we see that C (0, 4) = 33 is the minimum cost of a binary search tree
for (a1, a2, a3, a4)
Hence the left sub tree is 'T01' and right sub tree is T24. The root of 'T01' is 'a1' and the
root of 'T24' is a3.
The left and right sub trees for 'T01' are 'T00' and 'T11' respectively. The root of T01 is
'a1'
The left and right sub trees for T24 are T22 and T34 respectively.
a1 a3
T 01 T 24 a1 a3
a4
T 00 T 11 T 22 T 34 a4
Example 3:
WORD PROBABILITY
A 4
B 2
C 1
D 3
E 5
F 2
G 1
117
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
Solving c(0,n):
118
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
119
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
120
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
R(0, 5) = 2
0/1 – KNAPSACK
We are given n objects and a knapsack. Each object i has a positive weight w i and a
positive value Vi. The knapsack can carry a weight not exceeding W. Fill the knapsack
so that the value of objects in the knapsack is optimized.
121
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
decisions on the xi are made in the order xn, xn-1, . . . .x1. Following a decision on xn,
we may be in one of two possible states: the capacity remaining in m – wn and a
profit of pn has accrued. It is clear that the remaining decisions x n-1, . . . , x1 must be
optimal with respect to the problem state resulting from the decision on x n.
Otherwise, xn,. , x1 will not be optimal. Hence, the principal of optimality holds.
Equation-2 can be solved for fn (m) by beginning with the knowledge fo (y) = 0 for all
y and fi (y) = - , y < 0. Then f1, f2, . . . fn can be successively computed using
equation–2.
When the wi‟s are integer, we need to compute fi (y) for integer y, 0 < y < m. Since fi
(y) = - for y < 0, these function values need not be computed explicitly. Since
each fi can be computed from fi - 1 in Θ (m) time, it takes Θ (m n) time to compute fn.
When the wi‟s are real numbers, fi (y) is needed for real numbers y such that 0 < y
< m. So, fi cannot be explicitly computed for all y in this range. Even when the w i‟s
are integer, the explicit Θ (m n) computation of fn may not be the most efficient
computation. So, we explore an alternative method for both cases.
The fi (y) is an ascending step function; i.e., there are a finite number of y‟s, 0 = y1
< y2 < . . . . < yk, such that fi (y1) < fi (y2) < . . . . . < fi (yk); fi (y) = - , y < y1; fi
(y) = f (yk), y > yk; and fi (y) = fi (yj), yj < y < yj+1. So, we need to compute only fi
(yj), 1 < j < k. We use the ordered set Si = {(f (yj), yj) | 1 < j < k} to represent fi
(y). Each number of Si is a pair (P, W), where P = fi (yj) and W = yj. Notice that S0 =
{(0, 0)}. We can compute Si+1 from Si by first computing:
Now, Si+1 can be computed by merging the pairs in S i and Si1 together. Note that if
Si+1 contains two pairs (Pj, Wj) and (Pk, Wk) with the property that Pj < Pk and Wj >
Wk, then the pair (Pj, Wj) can be discarded because of equation-2. Discarding or
purging rules such as this one are also known as dominance rules. Dominated tuples
get purged. In the above, (Pk, Wk) dominates (Pj, Wj).
Example 1:
Consider the knapsack instance n = 3, (w1, w2, w3) = (2, 3, 4), (P1, P2, P3) = (1, 2,
5) and M = 6.
Solution:
122
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
Other Solution:
X - 2 = 0 => x = 2. y – 3 = 0 => y = 3
X - 2 = 1 => x = 3. y – 3 = 2 => y = 5
S3 = (S2 U S21 ) = {(0, 0), (1, 2), (2, 3), (3, 5), (5, 4), (6, 6), (7, 7), (8, 9)}
S3 = (S2 U S21 ) = {(0, 0), (1, 2), (2, 3), (5, 4), (6, 6)}
From (6, 6) we can infer that the maximum Profit pi xi = 6 and weight xi wi = 6
Reliability Design
123
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
If stage i contains mi copies of device Di. Then the probability that all mi have a
mi mi
malfunction is (1 - ri) . Hence the reliability of stage i becomes 1 – (1 - r)i .
Our problem is to use device duplication. This maximization is to be carried out under
a cost constraint. Let ci be the cost of each unit of device i and let c be the maximum
allowable cost of the system being designed.
We wish to solve:
Maximize
1 i n
i mi
Subject to C m C
1 i n
i i
Assume each Ci > 0, each mi must be in the range 1 < mi < ui, where
n
ui C Ci
CJ
Ci
1
C
1 j i
J mJ x and 1 < mj < uJ, 1 < j < i
The last decision made requires one to choose m n from {1, 2, 3, .......... un}
Once a value of mn has been chosen, the remaining decisions must be such as to use
the remaining funds C – Cn mn in an optimal way.
124
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
clearly, f0 (x) = 1 for all x, 0 < x < C and f (x) = - for all x < 0.
There is atmost one tuple for each different „x‟, that result from a sequence of
decisions on m1, m2, . . . . mn. The dominance rule (f1, x1) dominate (f2, x2) if f1 ≥ f2
and x1 ≤ x2. Hence, dominated tuples can be discarded from S i.
Example 1:
Design a three stage system with device types D1, D2 and D3. The costs are $30, $15
and $20 respectively. The Cost of the system is to be no more than $105. The
reliability of each device is 0.9, 0.8 and 0.5 respectively.
Solution:
We assume that if if stage I has mi devices of type i in parallel, then i (mi) =1 – (1-
ri)mi
Since, we can assume each ci > 0, each mi must be in the range 1 ≤ mi ≤ ui. Where:
n
ui C Ci
1
CJ
Ci
S1 = depends on u1 value, as u1 = 2, so
S1 S , S
1 1
1 2
S2 = depends on u2 value, as u2 = 3, so
125
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
S2 S , S 2 2
, S2
1 2 3
S3 = depends on u3 value, as u3 = 3, so
S3 S , S 3 3
, S3
1 2 3
Dominance Rule:
126
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
If Si contains two pairs (f1, x1) and (f2, x2) with the property that f1 ≥ f2 and x1 ≤ x2,
then (f1, x1) dominates (f2, x2), hence by dominance rule (f2, x2) can be discarded.
Discarding or pruning rules such as the one above is known as dominance rule.
Dominating tuples will be present in Si and Dominated tuples has to be discarded
from Si.
S13 0.5 (0.72), 45 20, 0.5 (0.864), 60 20, 0.5 (0.8928), 75 20
The best design has a reliability of 0.648 and a cost of 100. Tracing back for the
solution through Si „s we can determine that m3 = 2, m2 = 2 and m1 = 1.
Other Solution:
Since, we can assume each ci > 0, each mi must be in the range 1 ≤ mi ≤ ui.
Where:
127
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
n
ui C Ci C J
/ Ci
i
= max {3(1) f2(105 - 20), 3(2) f2(105 - 20x2), 3(3) f2(105 -20x3)}
128
DESIGN AND ANALYSIS OF ALGORITHMS (III - CSE) – II SEM
= max {2(1) f1(65 - 15), 2(2) f1(65 - 15x2), 2(3) f1(65 - 15x3)}
= max {2(1) f1(45 - 15), 2(2) f1(45 - 15x2), 2(3) f1(45 - 15x3)}
Cost = 30 x 1 + 15 x 2 + 20 x 2 = 100.
m3 = 2, m2 = 2 and m1 = 1.
129