For more Computer Science, Linux, C++ content visit ProgrammerCave
Follow us on Facebook | Instagram | Telegram

Contribute on Paypal

Dynamic Programming Notes - Introduction to Algorithms CSLR

1. Dynamic programming, like the divide-and-conquer method, solves problems by  combining the solutions to subproblems. (“Programming” in this context refers to a  tabular method, not to writing computer code.)

2. Dynamic programming is applicable when the subproblems are not independent, that  is, when subproblems share subsubproblems.

3. A dynamic-programming algorithm solves every subsubproblem just once and then saves its answer in a table, thereby avoiding the work of recomputing the answer every time the subsubproblem is encountered.

4. Dynamic programming is typically applied to optimization problems. In such
problems there can be many possible solutions.

5. The development of a dynamic-programming algorithm can be broken into a
sequence of four steps.

  1. Characterize the structure of an optimal solution.
  2. Recursively define the value of an optimal solution.
  3. Compute the value of an optimal solution in a bottom-up fashion.
  4. Construct an optimal solution from computed information.

15.1 Assembly-line scheduling

1. A manufacturing problem to find the fastest way through a factory. There are two assembly lines, each with n stations; the jth station on line i is denoted Si, j and the assembly time at that station is ai, j .

2. An automobile chassis enters the factory, and goes onto line i (where i = 1 or 2), taking ei time.

3. After going through the jth station on a line, the chassis goes on to the ( j+1)st
station on either line. There is no transfer cost if it stays on the same line, but it takes time ti, j to transfer to the other line after station Si, j .

4. After exiting the nth station on a line, it takes xi time for the completed auto to exit the factory.

5. The problem is to determine which stations to choose from line 1 and which to
choose from line 2 in order to minimize the total time through the factory for one auto.

6. In the example of Figure 15.2(a), the fastest total time comes from choosing stations 1, 3, and 6 from line 1 and stations 2, 4, and 5 from line 2.

7. Let us consider the fastest possible way for a chassis to get from the starting point through station S1, j. If j = 1, there is only one way that the chassis could have gone, and so it is easy to determine how long it takes to get through station S1, j.

8. For j = 2, 3, . . . , n, however, there are two choices: the chassis could have come from station S1, j−1 and then directly to station S1, j, the time for going from station j − 1 to station j on the same line being negligible.

9. Alternatively, the chassis could have come from station S2, j−1 and then been
transferred to station S1, j, the transfer time being t2, j−1.

10. First, let us suppose that the fastest way through station S1, j is through station S1, j−1.

11. Similarly, let us now suppose that the fastest way through station S1, j is through station S2, j−1.

12. Now we observe that the chassis must have taken a fastest way from the starting point through station S2, j−1.

13. We can say that for assembly-line scheduling, an optimal solution to a problem (finding the fastest way through station Si, j ) contains within it an optimal solution to subproblems (finding the fastest way through either S1, j−1 or S2, j−1).

14. We refer to this property as optimal substructure, and it is one of the hallmarks of the applicability of dynamic programming.

15. Let fi[ j] denote the fastest possible time to get a chassis from the starting point through station Si, j .

16. Our ultimate goal is to determine the fastest time to get a chassis all the way through the factory, which we denote by f ∗.. f ∗. = min( f1[n] + x1, f2[n] + x2)

17. To get through station 1 on eitherline, a chassis just goes directly to that station.
Thus,
   f1[1] = e1 + a1,1 ,
   f2[1] = e2 + a2,1 .

18. f1[ j] = min( f1[ j − 1] + a1, j, f2[ j − 1] + t2, j−1 + a1, j)

19. for j = 2, 3, . . . , n. Symmetrically, we have
    f2[ j] = min( f2[ j − 1] + a2, j, f1[ j − 1] + t1, j−1 + a2, j)

20. We obtain the recursive equations


21. Let us define li[ j] to be the line number, 1 or 2, whose station j − 1 is used in a fastest way through station Si, j .

22. We also define l∗. to be the line whose station n is used in a fastest way through the entire factory.

23. The li[ j] values help us trace a fastest way.

24. FASTEST-WAY(a, t, e, x, n)
1. f1[1] ← e1 + a1,1
2. f2[1] ← e2 + a2,1
3. for j ← 2 to n
4. do if f1[ j − 1] + a1, j ≤ f2[ j − 1] + t2, j−1 + a1, j
5. then f1[ j] ← f1[ j − 1] + a1, j
6. l1[ j] ← 1
7. else f1[ j] ← f2[ j − 1] + t2, j−1 + a1, j
8. l1[ j] ← 2
9. if f2[ j − 1] + a2, j ≤ f1[ j − 1] + t1, j−1 + a2, j
10. then f2[ j] ← f2[ j − 1] + a2, j
11. l2[ j] ← 2
12. else f2[ j] ← f1[ j − 1] + t1, j−1 + a2, j
13. l2[ j] ← 1
14. if f1[n] + x1 ≤ f2[n] + x2
15. then f ∗. = f1[n] + x1
16. l∗. = 1
17. else f ∗. = f2[n] + x2
18. l∗. = 2

25. PRINT-STATIONS(l, n)
1. i ← l∗.
2. print “line ” i “, station ” n
3. for j ← n downto 2
4. do i ← li[ j]
5. print “line ” i “, station ” j − 1

15.2 Matrix-chain multiplication

1. Matrix multiplication is associative, and so all parenthesizations yield the same product.

2. The way we parenthesize a chain of matrices can have a dramatic impact on the cost of evaluating the product.

3. MATRIX-MULTIPLY(A, B)
1. if columns[A] ≠ rows[B]
2. then error “incompatible dimensions”
3. else for i ← 1 to rows[A]
4. do for j ← 1 to columns[B]
5. do C[i, j] ← 0
6. for k ← 1 to columns[A]
7. do C[i, j] ← C[i, j] + A[i, k] · B[k, j]
8. return C

4. The time to compute C is dominated by the number of scalar multiplications in line 7.

5. The matrix-chain multiplication problem can be stated as follows: given a chain (A1, A2,. . . , An) of n matrices, where for i = 1, 2, . . . , n, matrix Ai has dimension pi−1 × pi, fully parenthesize the product A1 A2 ··· An in a way that minimizes the number of scalar multiplications.

6. Denote the number of alternative parenthesizations of a sequence of n matrices by P(n).

7. When n = 1, there is just one matrix and therefore only one way to fully parenthesize the matrix product.

8. When n ≥ 2, a fully parenthesized matrix product is the product of two fully
parenthesized matrix subproducts, and the split between the two subproducts may occur between the kth and (k + 1)st matrices for any k = 1, 2, . . . , n − 1.

9. Let us adopt the notation Ai.. j, where i ≤ j, for the matrix that results from evaluating the product Ai Ai+1 · · · A j .

10. The optimal substructure of this problem is as follows. Suppose that an optimal parenthesization of Ai Ai+1 · · · A j splits the product between Ak and Ak+1.

11. Then the parenthesization of the “prefix” subchain Ai Ai+1 · · · Ak within this optimal parenthesization of Ai Ai+1 · · · A j must be an optimal parenthesization of Ai Ai+1 · · · Ak.

12. Let m[i, j] be the minimum number of scalar multiplications needed to compute the matrix Ai.. j ; for the full problem, the cost of a cheapest way to compute A1..n would thus be m[1, n].

13. Let us assume that the optimal parenthesization splits the product Ai Ai+1 · · · A j between Ak and Ak+1, where i ≤ k < j. Then, m[i, j] is equal to the minimum cost for computing the subproducts Ai..k and Ak+1.. j , plus the cost of multiplying these two matrices together.

14. Recalling that each matrix Ai is pi−1 × pi , we see that computing the matrix product Ai..k Ak+1.. j takes pi−1 pk p j scalar multiplications.
m[i, j] = m[i, k] + m[k + 1, j] + pi−1 pk p j

15. This recursive equation assumes that we know the value of k, which we do not. There are only j −i possible values for k, however, namely k = i, i +1, . . . , j −1.

16. To help us keep track of how to construct an optimal solution, let us define s[i, j] to be a value of k at which we can split the product Ai Ai+1 · · · A j to obtain an optimal parenthesization.

17. That is, s[i, j] equals a value k such that m[i, j] = m[i, k] + m[k + 1, j] + pi−1 pk p j

18. The following pseudocode assumes that matrix Ai has dimensions pi−1 × pi for i = 1, 2, . . . , n.

19. The input is a sequence p = (p0, p1, . . . , pn), where length[p] = n + 1.

20. The procedure uses an auxiliary table m[1 . . n, 1 . . n] for storing the m[i, j] costs and an auxiliary table s[1 . . n, 1 . . n] that records which index of k achieved the optimal cost in computing m[i, j].

21. The algorithm should fill in the table m in a manner that corresponds to solving the parenthesization problem on matrix chains of increasing length.

22. MATRIX-CHAIN-ORDER(p)
1. n ← length[p] − 1
2. for i ← 1 to n
3. do m[i, i] ← 0
4. for l ← 2 to n //l is the chain length.
5. do for i ← 1 to n − l + 1
6. do j ← i + l − 1
7. m[i, j] ← ∞
8. for k ← i to j − 1
9. do q ← m[i, k] + m[k + 1, j] + pi−1 pk p j
10. if q < m[i, j]
11. then m[i, j] ← q
12. s[i, j] ← k
13. return m and s

23. Figure 15.3 illustrates this procedure on a chain of n = 6 matrices. Since we have defined m[i, j] only for i ≤ j, only the portion of the table m strictly above the main diagonal is used.

24. The figure shows the table rotated to make the main diagonal run horizontally.

25. Each entry s[i, j] records the value of k such that the optimal parenthesization of Ai Ai+1 · · · A j splits the product between Ak and Ak+1.

26. PRINT-OPTIMAL-PARENS(s, i, j)
1. if i = j
2. then print “A”i
3. else print “(”
4. PRINT-OPTIMAL-PARENS(s, i, s[i, j])
5. PRINT-OPTIMAL-PARENS(s, s[i, j] + 1, j)
6. print “)

15.3 Elements of dynamic programming

1. Recall that a problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems.

2. Whenever a problem exhibits optimal substructure, it is a good clue that dynamic programming might apply.

3. To characterize the space of subproblems, a good rule of thumb is to try to keep the space as simple as possible, and then to expand it as necessary.

4. Dynamic programming uses optimal substructure in a bottom-up fashion. That is, we first find optimal solutions to subproblems and, having solved the subproblems, we find an optimal solution to the problem.

5. There is a variation of dynamic programming that often offers the efficiency of the usual dynamic-programming approach while maintaining a top-down strategy. The idea is to memoize the natural, but inefficient, recursive algorithm.

6. A memoized recursive algorithm maintains an entry in a table for the solution to each subproblem.

7. Each table entry initially contains a special value to indicate that the entry has yet to be filled in. When the subproblem is first encountered during the execution of the recursive algorithm, its solution is computed and then stored in the table.

8. Each subsequent time that the subproblem is encountered, the value stored in the table is simply looked up and returned.

9. MEMOIZED-MATRIX-CHAIN(p)
1. n ← length[p] − 1
2. for i ← 1 to n
3. do for j ← i to n
4. do m[i, j] ← ∞
5. return LOOKUP-CHAIN(p, 1, n)
10. LOOKUP-CHAIN(p, i, j)
1. if m[i, j] < ∞
2. then return m[i, j]
3. if i = j
4. then m[i, j] ← 0
5. else for k ← i to j − 1
6. do q ← LOOKUP-CHAIN(p, i, k)
+ LOOKUP-CHAIN(p, k + 1, j) + pi−1 pk p j
7. if q < m[i, j]
8. then m[i, j] ← q
9. return m[i, j]

11. In general practice, if all subproblems must be solved at least once, a bottom  dynamic-programming algorithm usually outperforms a top-down memoized algorithm by a constant factor, because there is no overhead for recursion and less overhead for maintaining the table.

12. Moreover, there are some problems for which the regular pattern of table accesses in the dynamic-programming algorithm can be exploited to reduce time or space requirements even further.

15.4 Longest common subsequence

1. Formally, given a sequence X = (x1, x2, . . . , xm), another sequence Z = (z1, z2, . . . , zk) is a subsequence of X if there exists a strictly increasing sequence (i1,i2,. . . ,ik) of indices of X such that for all j = 1, 2, . . . , k, we have xi j = z j.

2. For example, Z = (B, C, D, B) is a subsequence of X = (A, B, C, B, D, A, B) with
corresponding index sequence (2, 3, 5, 7).

3. Given two sequences X and Y , we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y .

4. In the longest-common-subsequence problem, we are given two sequences X = (x1, x2, . . . , xm) and Y = (y1, y2, . . . , yn) and wish to find a maximum-length common subsequence of X and Y.

5. Given a sequence X = (x1, x2, . . . , xm), we define the ith prefix of X, for i = 0, 1, . . . , m, as Xi = (x1, x2, . . . , xi).

6. For example, if X = (A, B, C, B, D, A, B), then X4 = (A, B, C, B) and X0 is the empty sequence.

7. Let X = (x1, x2, . . . , xm) and Y = (y1, y2, . . . , yn) be sequences, and let Z = (z1, z2, . . . ,zk) be any LCS of X and Y .

1. If xm = yn, then zk = xm = yn and Zk−1 is an LCS of Xm−1 and Yn−1.
2. If xm ≠ yn, then zk ≠ xm implies that Z is an LCS of Xm−1 and Y .
3. If xm ≠ yn, then zk ≠ yn implies that Z is an LCS of X and Yn−1.

8. The optimal substructure of the LCS problem gives the recursive formula.

9. Procedure LCS-LENGTH takes two sequences X = (x1, x2, . . . , xm) and Y = (y1, y2, . . . , yn) as inputs.

10. It stores the c[i, j] values in a table c[0 . . m, 0 . . n] whose entries are computed in row-major order.

11. It also maintains the table b[1 . . m, 1 . . n] to simplify construction of an optimal solution. Intuitively, b[i, j] points to the table entry corresponding to the optimal subproblem solution chosen when computing c[i, j].

12. LCS-LENGTH(X, Y )
1. m ← length[X]
2. n ← length[Y ]
3. for i ← 1 to m
4. do c[i, 0] ← 0
5. for j ← 0 to n
6. do c[0, j] ← 0
7. for i ← 1 to m
8. do for j ← 1 to n
9. do if xi = y j
10. then c[i, j] ← c[i − 1, j − 1] + 1
11. b[i, j] ← “”
12. else if c[i − 1, j] ≥ c[i, j − 1]
13. then c[i, j] ← c[i − 1, j]
14. b[i, j] ← “↑”
15. else c[i, j] ← c[i, j − 1]
16. b[i, j] ← “←”
17. return c and b

13. Here “&” equals titled arrow in figure below.

14. PRINT-LCS(b, X, i, j)
1. if i = 0 or j = 0
2. then return
3. if b[i, j] = “&”
4. then PRINT-LCS(b, X, i − 1, j − 1)
5. print xi
6. elseif b[i, j] = “↑”
7. then PRINT-LCS(b, X, i − 1, j)
8. else PRINT-LCS(b, X, i, j − 1)

15.5 Optimal binary search trees

1. The number of nodes visited when searching for a key in a binary search tree is one plus the depth of the node containing the key.

2. We are given a sequence K = (k1, k2, . . . , kn) of n distinct keys in sorted order (so that k1 < k2 < · · · < kn), and we wish to build a binary search tree from these keys.

3. For each key ki, we have a probability pi that a search will be for ki.

4. Some searches may be for values not in K , and so we also have n + 1 “dummy keys d0, d1, d2, . . . , dn representing values not in K.

5. In particular, d0 represents all values less than k1, dn represents all values greater than kn and for i = 1, 2, . . . , n−1, the dummy key di represents all values between ki and ki+1.

6. For each dummy key di, we have a probability qi that a search will correspond to di.

7. Figure 15.7 shows two binary search trees for a set of n = 5 keys. Each key ki is an internal node, and each dummy key di is a leaf. Every search is either successful (finding some key ki) or unsuccessful (finding some dummy key di) and so we have

8. For a given set of probabilities, our goal is to construct a binary search tree whose expected search cost is smallest. We call such a tree an optimal binary search tree.

9. We need to use the optimal substructure to show that we can construct an optimal solution to the problem from optimal solutions to subproblems.

10. Given keys ki, . . . , k j , one of these keys, say kr (i ≤ r ≤ j), will be the root of an optimal subtree containing these keys.

11. The left subtree of the root kr will contain the keys ki, . . . , kr−1 (and dummy keys di−1 ,. . . , dr−1), and the right subtree will contain the keys kr+1, . . . , k j (and dummy keys dr, . . . , d j )

12. Let us define e[i, j] as the expected cost of searching an optimal binary search tree containing the keys ki, . . . , k j. Ultimately, we wish to compute e[1, n].

13. The easy case occurs when j = i − 1. Then we have just the dummy key di−1. The expected search cost is e[i, i − 1] = qi−1.

14. For a subtree with keys ki, . . . , k j , let us denote this sum of probabilities as

15. Thus, if kr is the root of an optimal subtree containing keys ki, . . . , k j, we have e[i, j] = pr + (e[i,r − 1] + w(i,r − 1)) + (e[r + 1, j] + w(r + 1, j))

16. Noting that
w(i, j) = w(i,r − 1) + pr + w(r + 1, j)

17. we rewrite e[i, j] as
e[i, j] = e[i,r − 1] + e[r + 1, j] + w(i, j)

18. OPTIMAL-BST(p, q, n)
1. for i ← 1 to n + 1
2. do e[i, i − 1] ← qi−1
3. w[i, i − 1] ← qi−1
4. for l ← 1 to n
5. do for i ← 1 to n − l + 1
6. do j ← i + l − 1
7. e[i, j] ← ∞
8. w[i, j] ← w[i, j − 1] + p j + q j
9. for r ← i to j
10. do t ← e[i,r − 1] + e[r + 1, j] + w[i, j]
11. if t < e[i, j]
12. then e[i, j] ← t
13. root[i, j] ← r
14. return e and root


Get this post in pdf - Dynamic Programming Notes from Introduction to Algorithms

Source - https://programmercave0.github.io/downloads/


Comments

For more Computer Science, Linux, C++ content visit ProgrammerCave
Follow us on Facebook | Instagram | Telegram

Contribute on Paypal

Popular posts from this blog

Notes : Hacking the Art of Exploitation

C++: Basic Data Types