0% found this document useful (0 votes)
8 views

dynamic programming

Uploaded by

Abqary's Daddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

dynamic programming

Uploaded by

Abqary's Daddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Department of Mathematical Sciences

CARNEGIE MELLON UNIVERSITY


OPERATIONS RESEARCH II 21-393
Answers to homework 1.

Q1 Solve the following knapsack problem:


maximise 2x1 + 6x2 + 8x3
subject to
2x1 + 4x2 + 5x3 ≤ 15
x1 , x2 , x3 ≥ 0 and integer.
Solution:
w f1 δ1 f2 δ2 f3 δ3
0 0 0 0 0 0 0
1 0 0 0 0 0 0
2 2 1 2 0 2 0
3 2 1 2 0 2 0
4 4 1 6 1 6 0
5 4 1 6 1 8 1
6 6 1 8 1 8 0/1
7 6 1 8 1 10 1
8 8 1 12 1 12 0
9 8 1 12 1 14 1
10 10 1 14 1 16 1
11 10 1 14 1 16 1
12 12 1 18 1 18 1
13 12 1 18 1 20 1
14 14 1 20 1 22 1
15 14 1 20 1 24 1
Solution: x1 = 0, x2 = 0, x3 = 3. Maximum = 24.
Start with x1 = x2 = x3 = 0. δ3 (21) = 1 and so we add one to x3 . We have
used up 5 units of the knapsack. There are 10 units left. δ3 (10) = 1 and
so we add one to x3 . We use up another 5 units and so we are left with 5.
δ3 (5) = 1. We add one more to x3 . There are now 0 units in the knapsack.
δ3 (0) = 0 and so we move to column 2. δ2 (0) = 0 and so we move to column
1. δ(0) = 0 and we are done.

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

Use dynamic programming to find all solutions to the problem of maximising


votes.
Solution: Let fr (w) be the maximum increase in votes in precincts 1, 2, . . . , r
assuming that w workers are assigned to them.

fr (w) = max {ai,r + fr−1 (w − i)}, r ≥ 1,


0≤i≤w

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

be the maximum distance of a village in [i, j] to a firestation serving [i, j] if


it is placed at a. Then let

c(i, j) = min{c(i, j : a) : i ≤ a ≤ 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

f (x, i) = min {max{f (y, i − 1), c(y, x)}}.


i≤y≤x

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

d(v, w) = min{δ(x, y) : x, y are paddings of v, w respectively}.

Show how to use dynamic programming to compute d(v, w).


(When the alphabet Σ = {A, C, G, T }, this is an important problem in Com-
putational Biology).
Solution: Let
f (i, j) = d(v1 v2 · · · vi , w1 w2 · · · wj ).
Then 
f (i − 1, j) + b

f (i, j) = min f (i, j − 1) + b

f (i − 1, j − 1) + a × 1vi 6=wj

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.

No. of units Component 1 Component 2 Component 3 Component 4


1 1 2 1 1
2 2 4 3 3
3 3 5 4 4

Because of budget limitations, a maximum of $1000 can be spent. use Dy-


namic Programming to determine how many parallel units should be installed
in each of the four components in order to maximise the probability that the
system will function.

Solution: Let pi (x) be the probability that component i functions if there


are x parallel units placed in it and let cr (x) be the cost of putting x parallel
units into component i. Let φr (w) be the maximum reliability of a system
of components r, r + 1, . . . , 4 if one has enough money to put in w parallel
components altogether. We wish to compute φ1 (10). In general

φ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

Solution: x1 = 3, x2 = 1, x3 = 1, x4 = 3. Maximum = .3024.


Q6 Suppose that the price of a beanie baby changes from day to day; on a
given day it is equally to $x where x ∈ {5, 6, 7, 8, 9, 10}. Assume also that
the price on any day is independent of the price on any other day.

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?

2. What is the best strategy with 5 days to go?

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

1. f1 < 8 and so you should not buy today.

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,

Mi,j = min {Mi,k + Mk+1,j + mi mk+1 mj+1 }.


i≤k<j

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

π(1) = 1, π(2) = 1, π(3) = 2.

Find an optimal policy.


The matrix of costs is
 
7 2 3
 5 2 7 
1 6 2

Solution: Start with an initial solution of π(1) = 2, π(2) = 2, π(3) = 1 i.e.


chose the minimum cost in each row (a reasonable starting solution).
Then we solve
y2
y1 = 2 +
2
y2
y2 = 2 +
2
y1
y3 = 1 +
2
Solving these equations gives y1 = 4, y2 = 4, y3 = 3.
We now check for optimality:

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.

You might also like