Task Scheduling Problem
Task Scheduling Problem
16.4-5
Show how to transform the weight function of a weighted matroid problem, where
the desired optimal solution is a minimum-weight maximal independent subset, to
make it an standard weighted-matroid problem. Argue carefully that your transfor-
mation is correct.
An interesting problem that can be solved using matroids is the problem of op-
timally scheduling unit-time tasks on a single processor, where each task has a
deadline, along with a penalty that must be paid if the deadline is missed. The
problem looks complicated, but it can be solved in a surprisingly simple manner
using a greedy algorithm.
A unit-time task is a job, such as a program to be run on a computer, that requires
exactly one unit of time to complete. Given a finite set S of unit-time tasks, a
schedule for S is a permutation of S specifying the order in which these tasks are
to be performed. The first task in the schedule begins at time 0 and finishes at
time 1, the second task begins at time 1 and finishes at time 2, and so on.
The problem of scheduling unit-time tasks with deadlines and penalties for a
single processor has the following inputs:
• a set S = {a1 , a2 , . . . , an } of n unit-time tasks;
• a set of n integer deadlines d1 , d2 , . . . , dn , such that each di satisfies 1 ≤ di ≤ n
and task ai is supposed to finish by time di ; and
• a set of n nonnegative weights or penalties w1 , w2 , . . . , wn , such that we incur
a penalty of wi if task ai is not finished by time di and we incur no penalty if a
task finishes by its deadline.
We are asked to find a schedule for S that minimizes the total penalty incurred for
missed deadlines.
Consider a given schedule. We say that a task is late in this schedule if it fin-
ishes after its deadline. Otherwise, the task is early in the schedule. An arbitrary
schedule can always be put into early-first form, in which the early tasks precede
the late tasks. To see this, note that if some early task ai follows some late task a j ,
then we can switch the positions of ai and a j , and ai will still be early and a j will
still be late.
We similarly claim that an arbitrary schedule can always be put into canonical
form, in which the early tasks precede the late tasks and the early tasks are sched-
uled in order of monotonically increasing deadlines. To do so, we put the schedule
into early-first form. Then, as long as there are two early tasks ai and a j finishing
400 Chapter 16 Greedy Algorithms
at respective times k and k + 1 in the schedule such that d j < di , we swap the
positions of ai and a j . Since a j is early before the swap, k + 1 ≤ d j . Therefore,
k + 1 < di , and so ai is still early after the swap. Task a j is moved earlier in the
schedule, so it also still early after the swap.
The search for an optimal schedule thus reduces to finding a set A of tasks that
are to be early in the optimal schedule. Once A is determined, we can create the
actual schedule by listing the elements of A in order of monotonically increasing
deadline, then listing the late tasks (i.e., S − A) in any order, producing a canonical
ordering of the optimal schedule.
We say that a set A of tasks is independent if there exists a schedule for these
tasks such that no tasks are late. Clearly, the set of early tasks for a schedule forms
an independent set of tasks. Let I denote the set of all independent sets of tasks.
Consider the problem of determining whether a given set A of tasks is indepen-
dent. For t = 0, 1, 2, . . . , n, let Nt ( A) denote the number of tasks in A whose
deadline is t or earlier. Note that N0 ( A) = 0 for any set A.
Lemma 16.12
For any set of tasks A, the following statements are equivalent.
1. The set A is independent.
2. For t = 0, 1, 2, . . . , n, we have Nt ( A) ≤ t.
3. If the tasks in A are scheduled in order of monotonically increasing deadlines,
then no task is late.
Proof Clearly, if Nt ( A) > t for some t, then there is no way to make a schedule
with no late tasks for set A, because there are more than t tasks to finish before
time t. Therefore, (1) implies (2). If (2) holds, then (3) must follow: there is no
way to “get stuck” when scheduling the tasks in order of monotonically increasing
deadlines, since (2) implies that the ith largest deadline is at most i. Finally, (3)
trivially implies (1).
Using property 2 of Lemma 16.12, we can easily compute whether or not a given
set of tasks is independent (see Exercise 16.5-2).
The problem of minimizing the sum of the penalties of the late tasks is the same
as the problem of maximizing the sum of the penalties of the early tasks. The
following theorem thus ensures that we can use the greedy algorithm to find an
independent set A of tasks with the maximum total penalty.
Theorem 16.13
If S is a set of unit-time tasks with deadlines, and I is the set of all independent
sets of tasks, then the corresponding system (S, I) is a matroid.
16.5 A task-scheduling problem 401
Task
ai 1 2 3 4 5 6 7
di 4 2 4 3 1 4 6
wi 70 60 50 40 30 20 10
Figure 16.7 An instance of the problem of scheduling unit-time tasks with deadlines and penalties
for a single processor.
Exercises
16.5-1
Solve the instance of the scheduling problem given in Figure 16.7, but with each
penalty wi replaced by 80 − wi .
402 Chapter 16 Greedy Algorithms
16.5-2
Show how to use property 2 of Lemma 16.12 to determine in time O(|A|) whether
or not a given set A of tasks is independent.
Problems
b. Suppose that the available coins are in the denominations that are powers of c,
i.e., the denominations are c0 , c1 , . . . , ck for some integers c > 1 and k ≥ 1.
Show that the greedy algorithm always yields an optimal solution.
c. Give a set of coin denominations for which the greedy algorithm does not yield
an optimal solution. Your set should include a penny so that there is a solution
for every value of n.
d. Give an O(nk)-time algorithm that makes change for any set of k different coin
denominations, assuming that one of the coins is a penny.
a. Give an algorithm that schedules the tasks so as to minimize the average com-
pletion time. Each task must run non-preemptively, that is, once task ai is
started, it must run continuously for pi units of time. Prove that your algorithm
minimizes the average completion time, and state the running time of your al-
gorithm.