Introduction to Algorithms
Solution Set 3
CS 482, Spring 2008
(1)
(a)
Here’s a counterexample in which
n
= 3.
Week 1
Week 2
Week 3
‘
i
1
1
1
h
i
0
3
10
The optimal solution picks the lowstress job in week 1 and the highstress job in week 3. The
greedy algorithm given in the book will instead take the highstress job in week 2 and the
lowstress job in week 3.
(b)
The algorithm uses fills in a dynamic programming table with two rows and
n
columns.
Define
Opt
(
L, i
) (respectively,
Opt
(
H, i
)) to be the value of an optimal plan that picks the
lowstress job (respectively, highstress job) on day
i
and picks “none” every day after that. We
have
Opt
(
L, i
) =
Opt
(
H, i
) = 0 for
i
≤
0, and for
i >
0 we have
Opt
(
L, i
) =
‘
i
+ max
{
Opt
(
L, i

1)
,
Opt
(
H, i

1)
}
(1)
Opt
(
H, i
) =
h
i
+ max
{
Opt
(
L, i

2)
,
Opt
(
H, i

2)
}
.
(2)
The algorithm runs a loop from
i
= 1 to
i
=
n
, filling the values of
Opt
(
L, i
)
,
Opt
(
H, i
) according
to the formulas (1) and (2). Finally it outputs the maximum of
Opt
(
L, n
) and
Opt
(
H, n
).
The running time is
O
(
n
) because there are
n
loop iterations, with
O
(1) work per iteration.
The correctness of the algorithm follows by induction on
i
.
The induction hypothesis is that
Opt
(
L, i
) and
Opt
(
H, i
) are computed correctly, i.e. those entries in the dynamic programming
table satisfy the definitions of
Opt
(
L, i
)
,
Opt
(
H, i
) given above.
In the induction step, one
assumes that the induction hypothesis is correct for
i

1
, i

2 and derives that it is correct for
i
because the recurrence in (1),(2) is valid.
(2)
For each request, define the finishing time to be
f
i
=
t
i
+
D
(
x
i
, y
i
). This is the time at which
the cab will arrive at the passenger’s destination after satisfying request
i
. Assume the requests
are numbered in order of increasing
f
i
. (In other words, sort them so they are in this order.) For
i
= 1
,
2
, . . . , n
, let
Opt
(
i
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 KLEINBERG
 Algorithms, Dynamic Programming, Recursion, 1 week, OPT, 2 1 1 0 3 Week

Click to edit the document details