Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic...
Transcript of Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic...
![Page 1: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/1.jpg)
Dynamic Programming (DP)
Have we being doing too much work in our recursions?
CS/SE2C03
![Page 2: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/2.jpg)
Dynamic Programming (DP)
Have we being doing too much work in our recursions?
CS/SE2C03
![Page 3: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/3.jpg)
Dynamic Programming (DP)
Solving optimization (maximization or minimization)problems
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.
4 Construct an optimal solution from computed information.
Step 4 is not needed if want only the value of the optimalsolution.
CS/SE2C03
![Page 4: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/4.jpg)
Dynamic Programming (DP)
Solving optimization (maximization or minimization)problems
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.
4 Construct an optimal solution from computed information.
Step 4 is not needed if want only the value of the optimalsolution.
CS/SE2C03
![Page 5: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/5.jpg)
Dynamic Programming (DP)
Solving optimization (maximization or minimization)problems
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.
4 Construct an optimal solution from computed information.
Step 4 is not needed if want only the value of the optimalsolution.
CS/SE2C03
![Page 6: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/6.jpg)
Dynamic Programming (DP)
Solving optimization (maximization or minimization)problems
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.
4 Construct an optimal solution from computed information.
Step 4 is not needed if want only the value of the optimalsolution.
CS/SE2C03
![Page 7: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/7.jpg)
Dynamic Programming (DP)
Solving optimization (maximization or minimization)problems
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.
4 Construct an optimal solution from computed information.
Step 4 is not needed if want only the value of the optimalsolution.
CS/SE2C03
![Page 8: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/8.jpg)
Dynamic Programming (DP)
Solving optimization (maximization or minimization)problems
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.
4 Construct an optimal solution from computed information.
Step 4 is not needed if want only the value of the optimalsolution.
CS/SE2C03
![Page 9: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/9.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.
Divide-and-conquer algorithms:
1 No choice to define subproblems (e.g. split in halves).
2 Optimal solution of (the many) subproblems.
3 A theorem that combines (2) ⇒ Optimal solution.
CS/SE2C03
![Page 10: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/10.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.Divide-and-conquer algorithms:
1 No choice to define subproblems (e.g. split in halves).
2 Optimal solution of (the many) subproblems.
3 A theorem that combines (2) ⇒ Optimal solution.
CS/SE2C03
![Page 11: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/11.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.Greedy algorithms:
1 Greedy choice (out of many) defines subproblem.
2 Optimal solution of (the one) subproblem.
3 A theorem that combines (1) + (2) ⇒ Optimal solution.
CS/SE2C03
![Page 12: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/12.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.Dynamic Programming:
1 Best choice (out of many) defines subproblems.
2 Optimal solution of (the many) subproblems.
3 A theorem that combines (1) + (2) ⇒ Optimal solution.
CS/SE2C03
![Page 13: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/13.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.
Recursively define the value of an optimal solution.
Divide-and conquer:
e.g . OPT (P) = OPT (P/2) ] OPT (P/2)
CS/SE2C03
![Page 14: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/14.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.
Recursively define the value of an optimal solution.
Divide-and conquer:
e.g . OPT (P) = OPT (P/2) ] OPT (P/2)
CS/SE2C03
![Page 15: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/15.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.
Recursively define the value of an optimal solution.
Greedy:
OPT (P) = cost(g) + OPT (SP(g)), for greedy choice g .
CS/SE2C03
![Page 16: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/16.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.
Recursively define the value of an optimal solution.
DP:
OPT (P) = cost(b)+OPT (SP1(b))+. . .+OPT (SPk(b)), for best b.
Q: Wait a minute...which choice is the best choice b???A: I don’t know! Try them all and pick the one that gives you thebest solution !
CS/SE2C03
![Page 17: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/17.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.
Recursively define the value of an optimal solution.
DP:
OPT (P) = cost(b)+OPT (SP1(b))+. . .+OPT (SPk(b)), for best b.
Q: Wait a minute...which choice is the best choice b???
A: I don’t know! Try them all and pick the one that gives you thebest solution !
CS/SE2C03
![Page 18: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/18.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.
Recursively define the value of an optimal solution.
DP:
OPT (P) = cost(b)+OPT (SP1(b))+. . .+OPT (SPk(b)), for best b.
Q: Wait a minute...which choice is the best choice b???A: I don’t know!
Try them all and pick the one that gives you thebest solution !
CS/SE2C03
![Page 19: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/19.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.
Recursively define the value of an optimal solution.
DP:
OPT (P) = cost(b)+OPT (SP1(b))+. . .+OPT (SPk(b)), for best b.
Q: Wait a minute...which choice is the best choice b???A: I don’t know! Try them all and pick the one that gives you thebest solution !
CS/SE2C03
![Page 20: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/20.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.
Recursively define the value of an optimal solution.
DP:
OPT (P) = minb{cost(b) + OPT (SP1(b)) + . . .+ OPT (SPk(b))}
CS/SE2C03
![Page 21: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/21.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.
Recursively define the value of an optimal solution.
Compute the value of an optimal solution.
Solution 1: We have the recursion, implement recursive (oriterative) algorithm.Solution 2 (only for DP): Implement recursive algorithm but alsouse a table with optimal values of subproblems we have alreadysolved.
CS/SE2C03
![Page 22: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/22.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.
Recursively define the value of an optimal solution.
Compute the value of an optimal solution.
Solution 1: We have the recursion, implement recursive (oriterative) algorithm.Solution 2 (only for DP): Implement recursive algorithm but alsouse a table with optimal values of subproblems we have alreadysolved.
CS/SE2C03
![Page 23: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/23.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.
Recursively define the value of an optimal solution.
Compute the value of an optimal solution.
Solution 1: We have the recursion, implement recursive (oriterative) algorithm.
Solution 2 (only for DP): Implement recursive algorithm but alsouse a table with optimal values of subproblems we have alreadysolved.
CS/SE2C03
![Page 24: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/24.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.
Recursively define the value of an optimal solution.
Compute the value of an optimal solution.
Solution 1: We have the recursion, implement recursive (oriterative) algorithm.Solution 2 (only for DP): Implement recursive algorithm but alsouse a table with optimal values of subproblems we have alreadysolved.
CS/SE2C03
![Page 25: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/25.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.
Recursively define the value of an optimal solution.
Compute the value of an optimal solution.
Construct an optimal solution from computed information.
Solution: Keep track of the best choice b in the recursion everytime you find it!
CS/SE2C03
![Page 26: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/26.jpg)
Dynamic Programming (DP)
Characterize the structure of an optimal solution.
Recursively define the value of an optimal solution.
Compute the value of an optimal solution.
Construct an optimal solution from computed information.
Solution: Keep track of the best choice b in the recursion everytime you find it!
CS/SE2C03
![Page 27: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/27.jpg)
Dynamic Programming (DP)
Example: Rod-Cutting
Note: There are 2n−1 ways to cut a rod of length n.
CS/SE2C03
![Page 28: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/28.jpg)
Dynamic Programming (DP)
Example: Rod-Cutting
Note: There are 2n−1 ways to cut a rod of length n.
CS/SE2C03
![Page 29: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/29.jpg)
Dynamic Programming (DP)
Step 1: Characterize the structure of an optimal solution.
Optimal break: i1 + i2 + . . .+ ik = nOptimal revenue: pi1 + pi2 + . . .+ pik = rn
⇒ Best first cut of length i + optimal cutting of rest n − i
Step 2: Recursively define the value of an optimal solution.
r0 = 0, rn = max1≤i≤n
{pi + rn−i}
CS/SE2C03
![Page 30: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/30.jpg)
Dynamic Programming (DP)
Step 1: Characterize the structure of an optimal solution.
Optimal break: i1 + i2 + . . .+ ik = nOptimal revenue: pi1 + pi2 + . . .+ pik = rn
⇒ Best first cut of length i + optimal cutting of rest n − i
Step 2: Recursively define the value of an optimal solution.
r0 = 0, rn = max1≤i≤n
{pi + rn−i}
CS/SE2C03
![Page 31: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/31.jpg)
Dynamic Programming (DP)
Step 1: Characterize the structure of an optimal solution.
Optimal break: i1 + i2 + . . .+ ik = nOptimal revenue: pi1 + pi2 + . . .+ pik = rn
⇒ Best first cut of length i + optimal cutting of rest n − i
Step 2: Recursively define the value of an optimal solution.
r0 = 0, rn = max1≤i≤n
{pi + rn−i}
CS/SE2C03
![Page 32: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/32.jpg)
Dynamic Programming (DP)
Step 3: Compute the value of an optimal solution.
CS/SE2C03
![Page 33: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/33.jpg)
Dynamic Programming (DP)
Step 3: Compute the value of an optimal solution.
CS/SE2C03
![Page 34: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/34.jpg)
Dynamic Programming (DP)
Step 3: Compute the value of an optimal solution.
T (n) = 1 +n−1∑j=0
T (j)
⇒ T (n) = 2n
CS/SE2C03
![Page 35: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/35.jpg)
Dynamic Programming (DP)
Step 3: Compute the value of an optimal solution.
T (n) = 1 +n−1∑j=0
T (j)⇒ T (n) = 2n
CS/SE2C03
![Page 36: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/36.jpg)
Dynamic Programming (DP)
CS/SE2C03
![Page 37: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/37.jpg)
Dynamic Programming (DP)
CS/SE2C03
![Page 38: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/38.jpg)
Dynamic Programming (DP)
CS/SE2C03
![Page 39: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/39.jpg)
Dynamic Programming (DP)
CS/SE2C03
![Page 40: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/40.jpg)
Dynamic Programming (DP)
CS/SE2C03
![Page 41: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/41.jpg)
Dynamic Programming (DP)
CS/SE2C03
![Page 42: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/42.jpg)
Dynamic Programming (DP)
Step 4: Construct an optimal solution from computedinformation.
i 0 1 2 3 4 5 6 7 8 9 10
r [i ] 0 1 5 8 10 13 17 18 22 25 30s[i ] 0 1 2 3 2 2 6 1 2 3 10
CS/SE2C03
![Page 43: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/43.jpg)
Dynamic Programming (DP)
Step 4: Construct an optimal solution from computedinformation.
i 0 1 2 3 4 5 6 7 8 9 10
r [i ] 0 1 5 8 10 13 17 18 22 25 30s[i ] 0 1 2 3 2 2 6 1 2 3 10
CS/SE2C03
![Page 44: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/44.jpg)
Dynamic Programming (DP)
Step 4: Construct an optimal solution from computedinformation.
i 0 1 2 3 4 5 6 7 8 9 10
r [i ] 0 1 5 8 10 13 17 18 22 25 30s[i ] 0 1 2 3 2 2 6 1 2 3 10
CS/SE2C03
![Page 45: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/45.jpg)
DP application: All-Pairs Shortest Paths
Assumption
No negative cycles
Input: Directed G = (V ,E ), n× n weights matrix W = [wij ]
Output: n × n distance matrix D = [di ,j ], and predecessormatrix Π = [πij ]
CS/SE2C03
![Page 46: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/46.jpg)
DP application: All-Pairs Shortest Paths
Assumption
No negative cycles
Input: Directed G = (V ,E ), n× n weights matrix W = [wij ]
Output: n × n distance matrix D = [di ,j ], and predecessormatrix Π = [πij ]
CS/SE2C03
![Page 47: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/47.jpg)
DP application: All-Pairs Shortest Paths
Step 1: Structure of shortest paths.
Step 2: Recursively define the value of an optimal solution.
d(0)ij = wij
d(k)ij = min{d (k−1)
ij , d(k−1)ik + d
(k−1)kj }, k ≥ 1
CS/SE2C03
![Page 48: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/48.jpg)
DP application: All-Pairs Shortest Paths
Step 1: Structure of shortest paths.
Step 2: Recursively define the value of an optimal solution.
d(0)ij = wij
d(k)ij = min{d (k−1)
ij , d(k−1)ik + d
(k−1)kj }, k ≥ 1
CS/SE2C03
![Page 49: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/49.jpg)
DP application: All-Pairs Shortest Paths
Step 1: Structure of shortest paths.
Step 2: Recursively define the value of an optimal solution.
d(0)ij = wij
d(k)ij = min{d (k−1)
ij , d(k−1)ik + d
(k−1)kj }, k ≥ 1
CS/SE2C03
![Page 50: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/50.jpg)
DP application: All-Pairs Shortest Paths
Step 3: Compute the value of an optimal solution.
Step 4: Construct an optimal solution from computedinformation.
π(0)ij =
{NIL, (i = j) ∨ (wij =∞)i , (i 6= j) ∧ (wij <∞)
π(k)ij =
{π(k−1)ij , min{d (k−1)
ij , d(k−1)ik + d
(k−1)kj } = d
(k−1)ij
π(k−1)kj , min{d (k−1)
ij , d(k−1)ik + d
(k−1)kj } = d
(k−1)ik + d
(k−1)kj
CS/SE2C03
![Page 51: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/51.jpg)
DP application: All-Pairs Shortest Paths
Step 3: Compute the value of an optimal solution.
Step 4: Construct an optimal solution from computedinformation.
π(0)ij =
{NIL, (i = j) ∨ (wij =∞)i , (i 6= j) ∧ (wij <∞)
π(k)ij =
{π(k−1)ij , min{d (k−1)
ij , d(k−1)ik + d
(k−1)kj } = d
(k−1)ij
π(k−1)kj , min{d (k−1)
ij , d(k−1)ik + d
(k−1)kj } = d
(k−1)ik + d
(k−1)kj
CS/SE2C03
![Page 52: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/52.jpg)
DP application: Transitive closure
Input: Directed G = (V ,E )
Output: Directed G = (V ,E ∗) where
E ∗ = {(i , j) : there is a path from i to j in G}
Can apply Floyd-Warshall indirectly, or directly:
t(0)ij =
{1, (i = j) ∨ ((i , j) ∈ E )0, (i 6= j) ∧ ((i , j) /∈ E )
t(k)ij = t
(k−1)ij ∨ (t
(k−1)ik ∧ t
(k−1)kj ), k ≥ 1
(compare to d(k)ij = min{d (k−1)
ij , d(k−1)ik + d
(k−1)kj })
CS/SE2C03
![Page 53: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/53.jpg)
DP application: Transitive closure
Input: Directed G = (V ,E )
Output: Directed G = (V ,E ∗) where
E ∗ = {(i , j) : there is a path from i to j in G}
Can apply Floyd-Warshall indirectly, or directly:
t(0)ij =
{1, (i = j) ∨ ((i , j) ∈ E )0, (i 6= j) ∧ ((i , j) /∈ E )
t(k)ij = t
(k−1)ij ∨ (t
(k−1)ik ∧ t
(k−1)kj ), k ≥ 1
(compare to d(k)ij = min{d (k−1)
ij , d(k−1)ik + d
(k−1)kj })
CS/SE2C03
![Page 54: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/54.jpg)
DP application: Transitive closure
Input: Directed G = (V ,E )
Output: Directed G = (V ,E ∗) where
E ∗ = {(i , j) : there is a path from i to j in G}
Can apply Floyd-Warshall indirectly, or directly:
t(0)ij =
{1, (i = j) ∨ ((i , j) ∈ E )0, (i 6= j) ∧ ((i , j) /∈ E )
t(k)ij = t
(k−1)ij ∨ (t
(k−1)ik ∧ t
(k−1)kj ), k ≥ 1
(compare to d(k)ij = min{d (k−1)
ij , d(k−1)ik + d
(k−1)kj })
CS/SE2C03
![Page 55: Dynamic Programming (DP) - McMaster Universitygk/courses/dynamic programming.pdf · Dynamic Programming (DP) Solving optimization (maximization or minimization) problems 1 Characterize](https://reader034.fdocuments.us/reader034/viewer/2022042807/5f7dbeab22eda44b17562992/html5/thumbnails/55.jpg)
DP application: Transitive closure
CS/SE2C03