(a) We want to minimize the average waiting time (the average of the amount of time each customer waitsbefore they are seen, not counting the time they spend with the doctor). What order should we use?Hint: sort the customers by.
2.)
Solution:
First observe that swapping
x
i
and
x
j
does not affect the waiting time customers
x
1
,
x
2
,...,
x
i
or cus
tomers
x
j
+
1
,
x
j
+
1
,...,
x
n
(i.e., for customers
x
k
where
k
i
or
k
>
j
). Therefore we only have to deal
with customers
x
i
+
1
,...,
x
j
, i.e., for customer
k
, where
i
<
k
j
. For customer
x
k
, the waiting time
before the swap is
T
k
=
Â
1
l
<
k
t
(
x
l
)
,
and the waiting time after the swap is
T
0
k
=
Â
1
l
<
i
t
(
x
l
)+
t
(
x
j
)+
Â
i
<
l
<
k
t
(
x
l
) =
T
k

t
(
x
i
)+
t
(
x
j
)
.
Since
t
(
x
i
)
≥
t
(
x
j
)
,
T
0
k
T
k
, so the waiting time is never increased for customers
x
i
+
1
,...,
x
j
, hence the
average waiting time for all the customers will not increase after the swap.
(c) Let
u
be the ordering of customers you selected in part (a), and
x
be any other ordering. Prove that the
average waiting time of
u
is no larger than the average waiting time of
x
—and therefore your answer
in part (a) is optimal.
Hint: Let
i
be the smallest index such that
u
i
6
=
x
i
. Use what you learned in part (b). Then, use proof
by induction (maybe backwards, in the order
i
=
n
,
n

1
,
n

2
,...,
1, or in some other way).
Solution:
Let
u
be the ordering in part (a), and
x
be any other ordering. Let
i
be the smallest index such that
u
i
6
=
x
i
. Let
j
be the index of
x
i
in
u
, i.e,
x
i
=
u
j
and
k
be the index of
u
i
in
x
. It’s easy to see that
j
>
i
.
By the construction of
x
, we have
T
(
x
i
) =
T
(
u
j
)
≥
T
(
u
i
) =
T
(
x
k
)
, therefore by swapping
x
i
and
x
k
, we
will not increase the average waiting time. If we keep doing this, eventually we will transform
x
into
u
. Since we never increase the average waiting time throughout the process,
u
is the optimal ordering.
3. (15 pts.)
Job Scheduling
You are given a set of
n
jobs. Each takes one unit of time to complete. Job
i
has an integervalued deadline
time
d
i
≥
0 and a realvalued penalty
p
i
≥
0. Jobs may be scheduled to start at any nonnegative integer
CS 170, Fall 2014, Soln 7
3
time (0, 1, 2, etc), and only one job may run at a time. If job
i
completes at or before time
d
i
, then it incurs
no penalty; otherwise, it is late and incurs penalty
p
i
. The goal is to schedule all jobs so as to minimize the
total penalty incurred.