dynamic programming
dynamic programming
1
Q2:A county chairwoman of a certain political party is making plans for an
upcoming presidential election. She has received the services of 10 volunteer
workers for precinct work and wants to assign them to five precincts in such a
way as to maximize their effectiveness. She feels that it would be inefficient
to assign a worker to more than one precinct, but she is willing to assign
no workers to any one of the precincts if they can accomplish more in other
precincts.
The following table gives the estimated increase in the number of votes for
the party’s candidate in each precinct if it were allocated the various number
of workers.
Number Precinct
of Workers 1 2 3 4 5
0 0 0 0 0 0
1 4 7 5 6 4
2 10 11 10 11 12
3 15 16 15 14 15
4 18 18 18 16 17
5 22 20 21 17 20
6 24 21 22 18 22
7 26 25 24 23 22
8 28 27 27 25 24
9 32 25 30 28 26
10 33 28 34 30 29
where ai,r is the increased number of votes gained from i workers in precinct r.
f0 (w) = 0.
2
w f1 x1 f2 x2 f3 x3 f4 x4 f5 x5
0 0 0 0 0 0 0 0 0
1 4 1 7 1 7 0 7 0
2 10 2 11 1,2 13 1 13 1
3 15 3 17 1 17 0,2 18 1,2
4 18 4 22 1 22 0,1,3 28 1,2
5 22 5 26 2 27 1,2 28 1,2
6 24 6 31 3 32 2,3 33 1,2
7 26 7 34 3 37 3 38 1,2
8 28 8 38 3 41 2,3 43 1,2
9 32 9 40 3,4 46 3 48 2
10 33 10 42 3,4,5 49 3,4 52 1,2 55 2
Solutions: Maximum = 55.
x1 = 3 x 2 = 1 x 3 = 3 x 4 = 1 x 5 = 2
x1 = 3 x 2 = 1 x 3 = 2 x 4 = 2 x 5 = 2
x1 = 2 x 2 = 1 x 3 = 3 x 4 = 2 x 5 = 2
Q3: The people of a certain area live at the side of a long straight road of
length L. The population is clustered into several villages at points a1 , a2 , . . . ,
ak along the road. There is a proposal to build ℓ fire stations on the road.
The problem is build them so that the maximum distance of a village to
its nearest fire station is minimised. Formulate the problem of finding the
optimum placement of fire stations as a dynamic program. (Assume that fire
stations are to be placed at integer points only on the line.)
Solution: Let
c(i, j : a) = max |ak − a|
k:i≤ak ≤j
Let f (x, i) be the maximum distance from a village in [0, x] to its nearest fire
station in [0, x] if i fire stations are optimally placed to service the villages
in [0, x]. Then f (i, i) = 0 for i = 0, 1, 2, . . . , ℓ and
3
Here y is the tentative place to put the ith firestation.
Q4: Let Σ be a k letter alphabet and let v = v1 v2 · · · vm , w = w1 w2 · · · wn be
two sequences over Σ. The distance d(v, w) between v and w is defined as
follows: A padding π of v is the insertion of a number of ⋆ symbols into the
sequence. For example A ⋆ CGC ⋆ T ⋆ G is a padding of ACGCT G. Now
let x = x1 x2 · · · xp , y = y1 y2 · · · yp be two sequence of the same length over
A ∪ {⋆}. Let
p
X
δ(x, y) = θ(xi , yi )
i=1
where
0 xi = yi 6= ⋆
1 xi = yi = ⋆
θ(xi , yi ) = .
a xi 6= yi , {xi , yi } ⊆ Σ
b xi ∈ Σ and yi = ⋆ or xi = ⋆ and yi ∈ Σ
Then
and
f (1, 0) = f (0, 1) = b and f (1, 1) = a × 1v1 6=w1
Q5: Consider an electronic system consisting of four components each of
which must work for the system to function. The reliability of the system
can be improved by installing several parallel units in one or more of the
components. The following table gives the probability that the respective
components will function if they consist of one, two or three parallel units.
4
No. of units Component 1 Component 2 Component 3 Component 4
1 0.4 0.6 0.7 0.5
2 0.6 0.7 0.8 0.7
3 0.8 0.8 0.9 0.9
The probability that the system will function is the product of the probabil-
ities that the respective components will function. The cost (in hundred’s of
dollars) of installing one, two or three parallel units in the respective com-
poonents is given by the following table.
φ4 (w) = p4 (w)
φr = max(pr (x)φr+1 (w − cr (x)))
x
5
w φ1 x1 φ2 x2 φ3 x3 φ4 x4
0 0 0 0
1 0 0 0
2 0 .35 1 .5 1
3 0 .35 1 .7 2
4 0 .49 1 .9 3
5 .210 1 .63 1 .9 3
6 .294 1 .63 1 .9 3
7 .378 1 .72 2 .9 3
8 .378 1 .81 3 .9 3
9 .441 2 .81 3 .9 3
10 .3024 3 .504 3 .81 3 .9 3
1. If today’s price is $8 and you must purchase a beanie baby for your
niece’s birthday party tomorrow evening, should you wait until tomor-
row?
3. Suppose that the Beanie outlet advertises an offer: Under this offer,
you can buy a beanie baby at a fixed price of $6 any time during the
next five days. What is the maximum you would be the maximum you
would be prepared to pay for this option.
It is assumed that you wish to minimise the expected price that you pay for
the beanie baby.
Solution: Let fn denote the minimum expected cost of buying a beanie
baby if there are n days to go. Then f1 = (5 + 10)/2 = 15/2 and
10
1X
fn = min{p, fn−1 }. (1)
6 p=5
6
2.
15
f1 = .
2
1 15 15 15 27
f2 = 5+6+7+ + + =
6 2 2 2 4
1 27 27 27 27 19
f3 = 5+6+ + + + =
6 4 4 4 4 3
1 19 19 19 19 109
f4 = 5+6+ + + + =
6 3 3 3 3 18
1 109 109 109 109 317
f5 = 5+6+ + + + =
6 18 18 18 18 54
Strategy: Buy at 5 or 6 for first 3 days and then at 5 or 6 or 7 for next
day. Must buy on last day.
3. replace (1) by
10
1X
fn = min{p, 6, fn−1 }.
6 p=5
Then
f1 = 6.
1 35
f2 = (5 + 6 + 6 + 6 + 6 + 6) =
6 6
1 35 35 35 35 35 205
f3 = 5+ + + + + =
6 6 6 6 6 6 36
1 205 205 205 205 205 1205
f4 = 5+ + + + + =
6 36 36 36 36 36 216
1 1205 1205 1205 1205 1205 7105
f5 = 5+ + + + + =
6 216 216 216 216 216 1296
317 7105
So the offer is worth 54
− 1296
.
Q7 A mathematics student has been asked to compute the product A1 ×
A2 × · · · × An where each Ai is an mi × mi+1 matrix. It will cost the student
$pqr to multiply a p × q matrix by a q × r matrix. Describe a dynamic
programming algorithm for finding the cheapest way of multiplying the n
matrices together.
7
To see that there is a problem: let m = (10, 5, 8, 12). There are two ways
to compute A1 A2 A3 : compute A1 A2 first and then multiply this by A3 –
total cost 400+960=1360. The other way is to compute A2 A3 first and then
multiply this by A1 – total cost 480+600=1080.
Solution: Let Mi,j denote the minimum number of multiplications needed
to compute Ai × Ai+1 × · · · × Aj for 1 ≤ i < j ≤ n. Then, by considering the
last matrix multiplication needed,
We can use this recurrence to compute Mi,n , computing the terms Mi,j in
order of the sum i + j.
Q8 A system can be in 3 states 1,2,3 and the cost of moving from state i
to state j in one period is c(i, j), where the c(i, j) are given in the matrix
below. The one period discount factor α is 1/2.
The aim is to find a policy which simultaneously minimises the discounted
cost of operating from any starting state. Start with the policy
8
Check y1 :
y1
7 + =9
2
y2
2 + =4 ⋆
2
y3 13
3 + =
2 2
Check y2 :
y1
5 + =7
2
y2
2 + =4 ⋆
2
y3 17
7 + =
2 2
Check y3 :
y1
1 + =3 ⋆
2
y2
6 + =8
2
y3 7
2 + =
2 2
So our initial guess is optimal.