DynamicProgrammingAlgorithms2021Part2
DynamicProgrammingAlgorithms2021Part2
25 mai 2021
Outline
Matrix-chain multiplication
Floyd-Warshall algorithm
Conclusion
Final exercises
Matrix-Chain Multiplication Problem (MCM)
Given a sequence of matrices A1 , A2 , . . . , An , where matrix Ai has size
a × b, find a full parenthesization of the product A1 A2 · · · An such that
the number of scalar multiplications required to compute A1 A2 · · · An
is minimized.
c b c
a a A x
C = b B
MCM Example
Two (of the 5) ways to compute this product are (A1 (A2 (A3 A4 ))) and
((A1 A2 )(A3 A4 )).
MCM Example
Computing the product as (A1 (A2 (A3 A4 ))) requires 208,500 scalar
multiplications, and computing it as ((A1 A2 )(A3 A4 )) requires 456,000
scalar multiplications.
(A1 ( A2 ( A3 A4 )) ) ( (A1 A2 ) ( A3 A4 ))
100x3 3x20 20x50 50x150 100x3 3x20 20x50 50x150
A1 A2 A3 A4 A1 A2 A3 A4
A1 A2 B A1 A2 B
The cost of the optimal solution M[1..n] is the cost of each of the two
subsolutions M[1..k] and M[k + 1..n] plus the cost of multiplying the
final two matrices :
Thus
int M(i, j)
if i == j then return (0) ;
else
return mini≤k<j (M(i, k) + M(k + 1, j) + di−1 dk dj ) ;
M[2,2] M[3,4] M[2,3] M[4,4] M[1,1] M[2,2] M[3,3] M[4,4] M[1,1] M[2,3] M[1,2] M[3,3]
M[1,4]
=Already Computed
M[2,2] M[3,4] M[2,3] M[4,4] M[1,1] M[2,2] M[3,3] M[4,4] M[1,1] M[2,3] M[1,2] M[3,3]
I Notice that many of the calls are repeated (all the shaded boxes).
I The divide-and-conquer algorithm has the following recurrence
n−1
X
T (n) = (T (k) + T (n − k)) + cn
k=1
If n > 0,
n−1
X
T (n) = 2 T (k) + cn
k=1
Therefore Pn−1
T (n) − T (n − 1) = (2 k=1 T (k) + cn)
Pn−2
− (2 k=1 T (k) + c(n − 1))
= 2T (n − 1) + c
That is
T (n) = 3T (n − 1) + c
Analysis of the recurrence for MCM
T (n) = 3T (n − 1) + c
= 3(3T (n − 2) + c) + c
= 9T (n − 2) + 3c + c
= 9(3T (n − 3) + c) + 3c + c
= 27T (n − 3) + 9c + 3c + c
= 3k T (n − k) + c k−1 3l
P
n Pn−1 l=0
l
= 3 T (0) + c l=0 3
n
= c3n + c 3 2−1
= (c + c2 )3n − c2
T (n) ∈ Θ(3n ).
This recurrence can be solved using the recursion tree method. There are n − 1
levels, each level i execute 3i c operations, yielding the following summation :
30 + 31 + 32 + · · · + 3n−1 , the dominant term in this expression is 3n−1 ∈ Θ(3n )
Optimal Substructure
int M(i, j)
if i == j then return (0) ;
else
return mini≤k<j (M(i, k) + M(k + 1, j) + di−1 dk dj ) ;
Given we first solve the base cases, i.e. sequences with only one matrix
(where i = j), we first fill the entries M[i, i] of the table :
1 2 3 4 5
0 1
0 2
0 3
0 4
0 5
A dynamic programming solution to MCM
int M(i, j)
if i == j then return (0) ;
else
return mini≤k<j (M(i, k) + M(k + 1, j) + di−1 dk dj ) ;
The next larger problem sizes to solve involve the product of two
matrices.
We multiple each 2 consecutive matrices, i.e. (1, 2), (2, 3), (n − 1, n),
and store the solution into M[i, i + 1]
Here k can only take one value, k = i. According to the recursive algo
M[i, i + 1] = M[i, i] + M[i + 1, i + 1] + di−1 dk dj = di−1 dk dj
1 2 3 4 5
0 d0 d1 d2 1
0 d1 d2 d3 2
0 d2 d3 d4 3
0 d3 d4 d5 4
0 5
A dynamic programming solution to MCM
int M(i, j)
if i == j then return (0) ;
else
return mini≤k<j (M(i, k) + M(k + 1, j) + di−1 dk dj ) ;
The next larger problem sizes to solve involve the product of three
consecutive matrices.
We multiple each 3 consecutive matrices, i.e. (1, 3), (2, 4), (n − 2, n),
and store the solution into M[i, i + 2]
Here k can only take two values, k = i and k = i + 1. According to the
k<j
recursive algo M[i, i + 2] = mink=i (M[i, k] + M[k + 1, j] + di−1 dk dj )
1 2 3 4 5
0 d0 d1 d2 1
0 d1 d2 d3 2
0 d2 d3 d4 3
0 d3 d4 d5 4
0 5
MCM dynamic programming Example
1 2 3 4 5 1 2 3 4 5
0 120 1 0 120 264 1
0 360 2 0 360 1320 2
⇒
0 720 3 0 720 1140 3
0 1680 4 0 1680 4
0 5 0 5
MCM Example (continued again)
1 2 3 4 5 1 2 3 4 5
0 120 264 1 0 120 264 1080 1
0 360 1320 2 0 360 1320 1350 2
⇒
0 720 1140 3 0 720 1140 3
0 1680 4 0 1680 4
0 5 0 5
MCM Example (continued again)
1 2 3 4 5 1 2 3 4 5
0 120 264 1080 1 0 120 264 1080 1344 1
0 360 1320 1350 2 0 360 1320 1350 2
⇒
0 720 1140 3 0 720 1140 3
0 1680 4 0 1680 4
0 5 0 5
MCM Example : Optimal Cost and Solution
Each time the optimal value for M[i, j] is found, store also the value of
k that was used.
1 2 3 4 5
0 120/1 264/2 1080/2 1344/2 1
0 360/2 1320/2 1350/2 2
0 720/3 1140/4 3
0 1680/4 4
0 5
The optimal solution for the second half comes from entry M[3, 5].
This
Pn algorithm has 3 nested loops. The summation is
Pn−1 Pi+s
s=1 i=1 k=i 1. The complexity of dynamic programming MCM is
3
Θ(n ).
Exercises 6 and 7 on matrix-chain multiplications
6. Given the sequence 2, 3, 5, 2, 4, 3, how many matrices do we
have and what is the dimension of each matrix. Using the previous
dynamic programming algorithm for matrix-chain multiplication,
compute the parenthetization of these matrices that minimize the
number of scalar multiplications.
Solution (see details in the next slides) :
1 2 3 4 5
0 30/1 42/1 58/3 78/3 1
0 30/2 54/3 72/3 2
0 40/3 54/3 3
0 24/4 4
0 5
(A1 )(A2 A3 )(A4 A5 )
7. Given the sequence 5, 4, 6, 2, 7, how many matrices do we have
and what is the dimension of each matrix. Using the previous
dynamic programming algorithm for matrix-chain multiplication,
compute the parenthetization of these matrices that minimize the
number of scalar multiplications.
Solution : 120, 48, 84, then 88, 104, then 158
Solving exercise 6
There are two ways one can split a sequence of 3 matrices in two
sub-sequences : (1) (2,3) or (1,2) (3). Given 5 matrices there are 3
sequences of 3 matrices. (2, 3, 5, 2, 4, 3)
M[1, 2] + M[3, 3] + d0 d2 d3 = 30 + 0 + 2 · 2 · 2 = 50
M[1, 3] = min
M[1, 1] + M[2, 3] + d0 d1 d3 = 0 + 30 + 2 · 3 · 2 = 42
M[2, 3] + M[4, 4] + d1 d3 d4 = 30 + 0 + 3 · 2 · 4 = 54
M[2, 4] = min
M[2, 2] + M[3, 4] + d1 d2 d4 = 0 + 40 + 3 · 5 · 4 = 100
M[3, 4] + M[5, 5] + d2 d4 d5 = 40 + 0 + 5 · 4 · 3 = 100
M[3, 5] = min
M[3, 3] + M[4, 5] + d2 d3 d5 = 0 + 24 + 5 · 2 · 3 = 54
1 2 3 4 5
0 30/1 42/1 1
0 30/2 54/3 2
0 40/3 54/3 3
0 24/4 4
0 5
Solving exercise 6
1 2 3 4 5
0 30/1 42/1 58/3 1
0 30/2 54/3 72/3 2
0 40/3 54/3 3
0 24/4 4
0 5
Solving exercise 6
1 2 3 4 5
0 30/1 42/1 58/3 78/3 1
0 30/2 54/3 72/3 2
0 40/3 54/3 3
0 24/4 4
0 5
Solving exercise 6
Let lcs(X [1..m], Y [1..n]) be the length of the LCS of X and Y . The
length of the LCS is computed recursively as follow :
Building a partial call tree for sequences ”AXYT” and ”AYZX”, it can
be verified that the recursive algorithm solves the same subproblems
several times. Soon you will observe that lcs(”AXY ”, ”AYZ ”) is being
solved twice.
Top Down DP algorithm for LCS
LCS-Length(X,Y,m,n)
let b[1..m, 1..n] and c[0..m, 0..n]
for i = 1 to m c[i, 0] = 0
for j = 0 to n c[0, j] = 0
for i = 1 to m
for j = 1 to n
if X [i] == Y [j]
c[i, j] = c[i − 1, j − 1] + 1
b[i, j] =”-”
else if c[i − 1, j] ≥ c[i, j − 1]
c[i, j] = c[i − 1, j]
b[i, j] =”↑”
else c[i, j] = c[i, j − 1]
b[i, j] =”←”
return c and b
LCS-Length(X,Y,m,n)
let b[1..m, 1..n] and c[0..m, 0..n]
for i = 1 to m c[i, 0] = 0
for j = 0 to n c[0, j] = 0
for i = 1 to m
for j = 1 to n
if X [i] == Y [j]
c[i, j] = c[i − 1, j − 1] + 1
b[i, j] =”-”
else if c[i − 1, j] ≥ c[i, j − 1]
c[i, j] = c[i − 1, j] X = [ABCBDAB]
b[i, j] =”↑”
Y = [BDCABA]
else c[i, j] = c[i, j − 1]
b[i, j] =”←” m = 7; n = 6
return c and b LCS is [BCBA]
Printing the LCS
Print-LCS(b,X,i,j)
if i == 0 or j == 0 return
if b[i, j] == ”-”
Print-LCS(b,X,i-1,j-1)
print xi
else if b[i, j] == ”↑”
Print-LCS(b,X,i-1,j)
else if b[i, j] == ”←”
Print-LCS(b,X,i,j-1)
Types of graphs :
I Undirected graph : Edge (u, v ) =
edge (v , u)
I Edges (0,4), (2,3), (1,2) is a subset
of edges of this graph
In this graph there are several paths between vertices 1,7 beside the one we found
above : (1,3,6,0,2,7), (1,3,6,4,7), (1,3,6,0,4,5,7), etc.
The All-Pairs Shortest-Path Problem
0 5 ∞ ∞
50 0 15 15
L=
30 ∞ 0 15
15 ∞ 5 0
Problem definition :
I G = {V , E } is a connected “directed” graph, V is the set of nodes
(|V | = n) and E is the set of edges.
I Each edge has an associated nonnegative length. A distance
matrix L[i, j] gives the length of each edge :
I L[i, i] = 0, L[i, j] ≥ 0 if i 6= j,
I L[i, j] = ∞ if the edge (i, j) does not exist.
I Find the shortest path distance between each pair of vertices in
the graph
Reasoning to find a D-&-C algorithm
i j
Let denote the shortest path problem between nodes 0 and 2 using all
the other nodes as intermediary nodes as Path(0, 2, n).
Denote shortest path problem between nodes 0 and 2 using only nodes
1 and 3 as intermediary nodes as Path(0, 2, n − 1).
A D-&-C algo for all-pairs shortest path
Let denote the function that returns the length of the shortest path
between nodes i, j using only intermediary nodes from the set
{1, 2, . . . , k} as Path(i, j, k)
Base case : the value returns by Path(i, j, 0), the length of the shortest
path between i, j with no intermediate node, is the cost of the direct
edge from i to j in the graph, i.e. Path(i, j, 0) = L[i, j]
Shortest path D&C
There are two possible ways one can compute the value of Path(i, j, k)
1. one may decide not to include node k in the computation of the shortest path
between i, j (only uses nodes in the set {1, 2, . . . , k − 1}, in which case
Path(i, j, k) = Path(i, j, k − 1)
2. one may decide to select node k, from i to k then from k to j using only
intermediate nodes {1, 2, . . . , k − 1}, then
Path(i, j, k) = Path(i, k, k − 1) + Path(k, j, k − 1)
The length of the path from i to k to j is the concatenation of the shortest path
from i to k and the shortest path from k to j, each one only using intermediate
vertices in {1, 2, . . . , k − 1}.
Recursive computation of Path(i, j, k)
There are two possible ways one can compute the value of Path(1, 7, k = 6)
1. one may decide not to include node k = 6 in the computation of the shortest
path between 1, 7 (only uses nodes in the set {0, 1, 2, . . . , 5}, in which case
Path(1, 7, 6) = Path(1, 7, 5)
2. one may decide to select node 6, from 1 to 6 then from 6 to 7 using only
intermediate nodes {0, 1, 2, . . . , 5}, then
Path(1, 7, 6) = Path(1, 6, 5) + Path(6, 7, 5)
Recursive computation of Path(i, j, k)
function Path(i, j, k)
if (k == 0) then return L[i, j] ; /* Base case */
else
return min(Path(i, j, k − 1),
Path(i, k, k − 1) + Path(k, j, k − 1))
The initial call is Path(i, j, n). This function is run for each pair of
node i and j
function Path(1, 6, 5)
return min(Path(1, 6, 4),
Path(1, 4, 4) + Path(4, 7, 4))
function Path(6, 7, 5)
return min(Path(6, 7, 4),
Path(6, 4, 4) + Path(4, 7, 4))
Floyd-Warshall Algorithm
It is a bottom up dynamic programming algorithm. It starts by
computing the shortest path for all pairs of nodes for k = 0. Then it
considers k = 1, k = 2, until k = n.
A matrix D gives the length of the shortest path between each pair of
nodes
At iteration k, the algo checks each pair of nodes (i, j) whether or not
there exists a path from i to j passing through node k that is better
than the present optimal path passing only through nodes in
{1, 2, . . . , k − 1}.
For k = 1, compute the shortest path between each pair of nodes (i, j)
when the path is allowed to pass through node 1.
Execution of Floyd’s algorithm
Algorithm Floyd(L[n, n])
D =L
for (k = 1; k ≤ n; k + +)
for (i = 1; i ≤ n; i + +)
for (j = 1; j ≤ n; j + +)
D[i, j] = min(D[i, j], D[i, k] + D[k, j])
return D
For k = 2, compute the shortest path between each pair of nodes (i, j)
when the path is allowed to pass through nodes 1 and 2.
Execution of Floyd’s algorithm
Algorithm Floyd(L[n, n])
D =L
for (k = 1; k ≤ n; k + +)
for (i = 1; i ≤ n; i + +)
for (j = 1; j ≤ n; j + +)
D[i, j] = min(D[i, j], D[i, k] + D[k, j])
return D
For k = 3, compute the shortest path between each pair of nodes (i, j)
when the path is allowed to pass through nodes {1, 2, 3}.
Execution of Floyd’s algorithm
Algorithm Floyd(L[n, n])
D =L
for (k = 1; k ≤ n; k + +)
for (i = 1; i ≤ n; i + +)
for (j = 1; j ≤ n; j + +)
D[i, j] = min(D[i, j], D[i, k] + D[k, j])
return D
0 0 0 0
0 0 0 0
0 1 0 0
0 1 0 0
Computing the shortest paths : k = 2
0 0 2 2
0 0 0 0
0 1 0 0
0 1 0 0
Computing the shortest paths : k = 3
0 0 2 2
3 0 0 0
0 1 0 0
0 1 0 0
Computing the shortest paths : k = 4
0 0 4 2
4 0 4 0
0 1 0 0
0 1 0 0
Computing the shortest paths
0 0 4 2
4 0 4 0
P=
0
1 0 0
0 1 0 0
10. Compute the all pairs of shortest paths for the following oriented
graph.
0 5 10 3
∞ 0 1 4
L=
∞
∞ 0 ∞
∞ ∞ 2 0
11. Compute the all pairs of shortest paths for the following oriented
graph.
0 3 8 ∞ 4
∞ 0 ∞ 1 7
∞
L= 4 0 ∞ ∞
2 ∞ 5 0 ∞
∞ ∞ ∞ 6 0
0
0 1 ∞ ∞ ∞
1
∞ 0 1 3 ∞
1 8
D0 = L = ∞ ∞ 0 1 6
2 1
6 1
8 ∞ ∞ 0 3 3
4 3
∞ ∞ ∞ ∞ 0 3
D1 [1, 0] = min D0 [1, 0], D0 [1, 0] + D0 [0, 0] = ∞; D1 [2, 0] = min D0 [2, 0], D0 [2, 0] + D0 [0, 0] = ∞ ;
D1 [1, 1] = 0
D1 [1, 2] = min D0 [1, 2], D0 [1, 0] + D0 [0, 2] =1 D1 [2, 1] = min D0 [2, 1], D0 [2, 0] + D0 [0, 1] = ∞
D1 [1, 3] = min D0 [1, 3], D0 [1, 0] + D0 [0, 3] =3 D1 [2, 2] = 0
D1 [1, 4] = min D0 [1, 4], D0 [1, 0] + D0 [0, 4] =∞ D1 [2, 3] = min D0 [2, 3], D0 [2, 0] + D0 [0, 3] = 1
D1 [2, 4] = min D0 [4, 4], D0 [2, 0] + D0 [0, 4] = 6
D1 [3, 0] = min D0 [3, 0], D0 [3, 0] + D0 [0, 0] = 8;
D1 [3, 1] = min D0 [3, 1], D0 [3, 0] + D0 [0, 1] = 9;
D1 [3, 2] = min D0 [3, 2], D0 [3, 0] + D0 [0, 2] =∞ D1 [4, 0] = min D0 [4, 0], D0 [4, 0] + D0 [0, 0] = ∞ ;
D1 [3, 3] = 0
D1 [3, 4] = min D0 [3, 4], D0 [3, 0] + D0 [0, 4] =3 D1 [4, 1] = min D0 [4, 1], D0 [4, 0] + D0 [0, 1] = ∞
D1 [4, 2] = min D0 [4, 3], D0 [4, 0] + D0 [0, 2] = ∞
D1 [4, 3] = min D0 [4, 3], D0 [4, 0] + D0 [0, 3] = ∞
D1 [4, 4] = 0
0
0 1 ∞ ∞ ∞
1
∞ 0 1 3 ∞
1 8
D1 = ∞ ∞ 0 1 6
2 1
6 1
8 9 ∞ 0 3 3
4 3
∞ ∞ ∞ ∞ 0 3
D1 [1, 0] = min D1 [1, 0], D1 [1, 1] + D1 [1, 0] = ∞; D1 [2, 0] = min D1 [2, 0], D1 [2, 1] + D1 [1, 0] = ∞ ;
D1 [1, 1] = 0
D1 [1, 2] = min D1 [1, 2], D1 [1, 1] + D1 [1, 2] =1 D1 [2, 1] = min D1 [2, 1], D1 [2, 1] + D1 [1, 1] = ∞
D1 [1, 3] = min D1 [1, 3], D1 [1, 1] + D1 [1, 3] =3 D1 [2, 2] = 0
D1 [1, 4] = min D1 [1, 4], D1 [1, 1] + D1 [1, 4] =∞ D1 [2, 3] = min D1 [2, 3], D1 [2, 1] + D1 [1, 3] = 1
D1 [2, 4] = min D1 [2, 4], D1 [2, 1] + D1 [1, 4] = 6
D1 [3, 0] = min D1 [3, 0], D1 [3, 1] + D1 [1, 0] = 8;
D1 [3, 1] = min D1 [3, 1], D1 [3, 1] + D1 [1, 1] = 9;
D1 [3, 2] = min D1 [3, 2], D1 [3, 1] + D1 [1, 2] = 10 D1 [4, 0] = min D1 [4, 0], D1 [4, 1] + D1 [1, 0] = ∞ ;
D1 [3, 3] = 0
D1 [3, 4] = min D1 [3, 4], D1 [3, 1] + D1 [1, 4] =3 D1 [4, 1] = min D1 [4, 1], D1 [4, 1] + D1 [1, 1] = ∞
D1 [4, 2] = min D1 [4, 3], D1 [4, 1] + D1 [1, 2] = ∞
D1 [4, 3] = min D1 [4, 3], D1 [4, 1] + D1 [1, 3] = ∞
D1 [4, 4] = 0
0
0 1 2 4 ∞
1
∞ 0 1 3 ∞
1 8
D2 = ∞ ∞ 0 1 6
2 1
8 6 1
9 10 0 3 3
4 3
∞ ∞ ∞ ∞ 0 3
D1 [1, 0] = min D2 [1, 0], D2 [1, 2] + D2 [2, 0] = ∞; D1 [2, 0] = min D2 [2, 0], D2 [2, 2] + D2 [2, 0] = ∞ ;
D1 [1, 1] = 0
D1 [1, 2] = min D2 [1, 2], D2 [1, 2] + D2 [2, 2] =1 D1 [2, 1] = min D2 [2, 1], D2 [2, 2] + D2 [2, 1] = ∞
D1 [1, 3] = min D2 [1, 3], D2 [1, 2] + D2 [2, 3] =2 D1 [2, 2] = 0
D1 [1, 4] = min D2 [1, 4], D2 [1, 2] + D2 [2, 4] =7 D1 [2, 3] = min D2 [2, 3], D2 [2, 2] + D2 [2, 3] = 1
D1 [2, 4] = min D2 [4, 4], D2 [2, 2] + D2 [2, 4] = 6
D1 [3, 0] = min D2 [3, 0], D2 [3, 2] + D2 [2, 0] = 8;
D1 [3, 1] = min D2 [3, 1], D2 [3, 2] + D2 [2, 1] = 9;
D1 [3, 2] = min D2 [3, 2], D2 [3, 2] + D2 [2, 2] = 10 D1 [4, 0] = min D2 [4, 0], D2 [4, 2] + D2 [2, 0] = ∞ ;
D1 [3, 3] = 0
D1 [3, 4] = min D2 [3, 4], D2 [3, 2] + D2 [2, 4] =3 D1 [4, 1] = min D2 [4, 1], D2 [4, 2] + D2 [2, 1] = ∞
D1 [4, 2] = min D2 [4, 3], D2 [4, 2] + D2 [2, 2] = ∞
D1 [4, 3] = min D2 [4, 3], D2 [4, 2] + D2 [2, 3] = ∞
D1 [4, 4] = 0
0
0 1 2 3 8
1
∞ 0 1 2 7
1 8
D3 = ∞ ∞ 0 1 6
2 1
8 6 1
9 10 0 3 3
4 3
∞ ∞ ∞ ∞ 0 3
D1 [1, 0] = min D3 [1, 0], D3 [1, 3] + D3 [3, 0] = 10 ; D1 [2, 0] = min D3 [2, 0], D3 [2, 3] + D3 [3, 0] = 9 ;
D1 [1, 1] = 0
D1 [1, 2] = min D3 [1, 2], D3 [1, 3] + D3 [3, 2] =1 D1 [2, 1] = min D3 [2, 1], D3 [2, 3] + D3 [3, 1] = 10
D1 [1, 3] = min D3 [1, 3], D3 [1, 3] + D3 [3, 3] =2 D1 [2, 2] = 0
D1 [1, 4] = min D3 [1, 4], D3 [1, 3] + D3 [3, 4] =5 D1 [2, 3] = min D3 [2, 3], D3 [2, 3] + D3 [3, 3] = 1
D1 [2, 4] = min D3 [4, 4], D3 [2, 3] + D3 [3, 4] = 4
D1 [3, 0] = min D3 [3, 0], D3 [3, 3] + D3 [3, 0] = 8;
D1 [3, 1] = min D3 [3, 1], D3 [3, 3] + D3 [3, 1] = 9;
D1 [3, 2] = min D3 [3, 2], D3 [3, 3] + D3 [3, 2] = 10 D1 [4, 0] = min D3 [4, 0], D3 [4, 3] + D3 [3, 0] = ∞ ;
D1 [3, 3] = 0
D1 [3, 4] = min D3 [3, 4], D3 [3, 3] + D3 [3, 4] =3 D1 [4, 1] = min D3 [4, 1], D3 [4, 3] + D3 [3, 1] = ∞
D1 [4, 2] = min D3 [4, 3], D3 [4, 3] + D3 [3, 2] = ∞
D1 [4, 3] = min D3 [4, 3], D3 [4, 3] + D3 [3, 3] = ∞
D1 [4, 4] = 0
Computing the P matrix
−1 −1 1 2 3
3 −1 −1 2 3
P=
3 3 0−1 −1 3
−1 0 1 −1 −1
−1 −1 −1 −1 −1
-1 means the shortest path between nodes x and y is the directed edge
in the graph. Shortest path between :
I (0, 2) = (0, 1)(1, 2) = (0, 1, 2)
I (0, 3) = (0, 2)(2, 3) where (0, 2) = (0, 1)(1, 2) = (0, 1, 2, 3)
I (0, 4) = (0, 3)(3, 4) = (0, 1, 2, 3, 4)
I (1, 0) = (1, 3)(3, 0) where (1, 3) = (1, 2)(2, 3) = (1, 2, 3) = (1, 2, 3, 0)
I (1, 3) = (1, 2)(2, 3) = (1, 2, 3)
I (1, 4) = (1, 3)(3, 4) = (1, 2, 3, 4)
I (2, 0) = (2, 3)(3, 0) = (2, 3, 0)
I (2, 1) = (2, 3)(3, 1) = (2, 3, 1)
I (2, 4) = (2, 3)(3, 4) = (2, 3, 4)
I (3, 1) = (3, 0)(0, 1) = (3, 0, 1)
I (3, 2) = (3, 1)(1, 2) where (3, 1) = (3, 0)(0, 1) = (3, 0, 1) = (3, 0, 1, 2)
Cost of dynamic programs
Calculate how many table or list entries you fill in (sometimes you
don’t use the entire table, just all entries under the diagonal or
something like that).
Then add the cost of retrieving the answer from the table.
Example : problem fails the optimal substructure test
I a time interval [si , fi ] where si is the start time when the activity should
begin and fi is the moment when the activity should finish
1 5
2 3
3 5
4 4
5 2
6 5
7 3
I If the optimal schedule achieving the benefit Bi does not include request
i, then Bi = Bi−1
HST (L, i)
if (i == 0) Bi = 0 ;
else
Bi = max{HST (L, i − 1), HST (L, pred(i)) + bi }