CS 360: Data Structures and Algorithms
Spring 2013
Dynamic Programming (continued)
World Series problem:
•
•
•
•
•
Teams A and B compete in a series of games.
The winner is the first team to win n games.
The series ends as soon as the winner is decided.
At most 2n–1 games are played.
For 1 ≤ k ≤ 2n–1, let p[k] = probability that team A
wins the kth game. (These are the input values.)
• For 0 ≤ i ≤ n and 0 ≤ j ≤ n, let X(i, j) = probability that
the series reaches a situation where team A wins
exactly i games and team B wins exactly j games.
(These are the output values.)
Recursive formula:
X(0, 0) = 1
X(n, n) = 0
X(i, 0) = X(i–1, 0)*p[i], for i>0
X(0, j) = X(0, j–1)*(1–p[j]), for j>0
X(n, j) = X(n–1, j)*p[n+j], for j<n
X(i, n) = X(i, n–1)*(1–p[i+n]), for i<n
X(i, j) = X(i–1, j)*p[i+j] + X(i, j–1)*(1–p[i+j]), for 0<i<n and 0<j<n
Dynamic programming algorithm:
allocate array X[0…n][0…n];
for (i=0; i<=n; i++)
for (j=0; j<=n; j++)
if (i==0 && j==0)
X[i][j] = 1;
else if (i==n && j==n)
X[i][j] = 0;
else if ((i>0 && j==0) || (i==n && j<n))
X[i][j] = X[i–1][j]*p[i+j];
else if ((i==0 && j>0) || (i<n && j==n))
X[i][j] = X[i][j–1]*(1–p[i+j]);
else
X[i][j] = X[i–1][j]*p[i+j] + X[i][j–1]*(1–p[i+j]);
Running time = θ(n2)
Example: n=4, so 2n–1=7
1 2 3 4 5 6 7
p .5 .4 .6 .5 .7 .3 .5
X
i=0
i=1
i=2
i=3
i=4
j=0
1
.5
.2
.12
.06
j=1 j=2
j=3
j=4
.5
.3
.12
.06
.5
.38
.25 .075
.38 .38 .289 .2023
.25 .341 .3254 .1627
.175 .1023 .1627 0
To illustrate,
X[3][2] = X[3–1][2]*p[3+2] + X[3][2–1]*(1–p[3+2])
= X[2][2]*p[5] + X[3][1]*(1–p[5])
= (.38)*(.7) + (.25)*(.3)
= .341
0-1 Knapsack problem:
Objects {1…n}
Profits P[1…n]
Weights W[1…n]
Maximum weight capacity M
0-1 constraint: Must take none (0) or all (1) of each object.
Goal: Determine the amounts X[1…n] for each object so that
Σ1≤k≤n X[k]*W[k] ≤ M, and Σ1≤k≤n X[k]*P[k] is as large as possible.
Each X[j] must be either 0 or 1.
For comparison purposes, first recall the greedy algorithm for
the Fractional Knapsack problem:
P
W
Ratio
Fraction
M
8
6
3
0
1
12
2
6
1
Total
0
12
27
39
2
15
3
5
1
k
1
2
3
3
16
4
4
0.75
F[k]
1
1
3/4
4
18
6
3
0
Next we develop a dynamic programming algorithm for
the 0-1 Knapsack problem
Recursive function:
T (n, M) = max possible total profit such that we choose any
subset of objects {1…n} and maximum weight capacity is M
T (j, k) = max possible total profit such that we can only choose
from objects {1…j} and maximum weight capacity is k
(0 ≤ j ≤ n, 0 ≤ k ≤ M)
T (j, k) = 0
T (j–1, k)
if j==0 or k==0
if j>0 and 0 < k < W[j]
max {T (j–1, k), T (j–1, k–W[j]) + P[j]}
if j>0 and k ≥ W[j]
int T (int j, int k) {
if (j==0 || k==0) return 0;
if (k < W[j]) return T (j–1, k);
return max {T (j–1, k), T (j–1, k–W[j]) + P[j]};
}
Dynamic programming algorithm:
allocate array T[0…n][0…M];
for j = 0 to n
for k = 0 to M
if (j==0 || k==0) T[j][k] = 0;
else if (k < W[j]) T[j][k] = T[j–1][k];
else T[j][k] = max {T[j–1][k], T[j–1][k–W[j]] + P[j]};
Running time = θ(nM)
Example: n=4, M=8
1
12
2
P
W
T
j=0
j=1
j=2
j=3
j=4
k=0
0
0
0
0
0
k=1
0
0
0
0
0
2
15
3
k=2
0
12
12
12
12
k=3
0
12
15
15
15
3
16
4
k=4
0
12
15
16
16
k=5
0
12
27
27
27
4
18
6
k=6
0
12
27
28
28
k=7
0
12
27
31
31
k=8
0
12
27
31
31
So the max total profit is 31, but which objects to choose?
We still need to assign each X[j] = either 0 or 1.
k=M;
for (j=n; j>0; j –= 1)
if (T[j–1][k] == T[j][k])
X[j]=0;
else
{ X[j]=1; k –= W[j]; }
Takes θ(n) additional time
1
12
2
0
P
W
X
T
j=0
j=1
j=2
j=3
j=4
k=0
0
0
0
0
0
k=1
0
0
0
0
0
2
15
3
1
k=2
0
12
12
12
12
k=3
0
12
15
15
15
3
16
4
1
k=4
0
12
15
16
16
k=5
0
12
27
27
27
4
18
6
0
k=6
0
12
27
28
28
k=7
0
12
27
31
31
k=8
0
12
27
31
31
So the optimal solution places objects 2 and 3 in the knapsack.
© Copyright 2026 Paperzz