Algorithm Design And Analysis
Asst. Prof. Ali Kadhum Idrees
1.1. The Scheduling problem:
Suppose a hair stylist has several customers waiting for different treatments
(e.g., simple cut, cut with shampoo, permanent, hair coloring). The treatments don't
all take the same amount of time, but the stylist knows how long each takes. A
reasonable goal would be to schedule the customers in such a way as to minimize
the total time they spend both waiting and being served (getting treated). Such a
schedule is considered optimal. The time spent both waiting and being served is
called the time in the system. The problem of minimizing the total time in the
system has many applications. For example, we may want to schedule users' access
to a disk drive to minimize the total time they spend waiting and being served.
Another scheduling problem occurs when each job (customer) takes the same
amount of time to complete but has a deadline by which it must start to yield a profit
associated with the job. The goal is to schedule the jobs to maximize the total profit.
We will consider scheduling with deadlines after discussing the simpler scheduling
problem of minimizing the total time in the system.
2.5.1 Minimizing Total Time in the System
A simple solution to minimizing the total time in the system is to consider all possible
schedules and take the minimum. This is illustrated in the following example.
Department of Computer Science
1
College of Science for Women
Algorithm Design And Analysis
Asst. Prof. Ali Kadhum Idrees
Clearly, an algorithm that considers all possible schedules is factorial-time. Notice in the
previous example that an optimal schedule occurs when the job with the smallest service time (job 3,
with a service time of 4) is scheduled first, followed by the job with the second-smallest service time
(job 1, with a service time of 5), followed finally by the job with the largest service time (job 2, with
a service time of 10). Intuitively, it seems that such a schedule would be optimal because it gets the
shortest jobs out of the way first. A high-level greedy algorithm for this approach is as follows:
Algorithm simpler scheduling
sort the jobs by service time in nondecreasing order;
while ( the instance is not solved) do
begin
schedule the next job;
// selection procedure and
// feasibility check
if ( there are no more jobs) then
// solution check
the instance is solved;
end;
End of Algorithm
We wrote this algorithm in the general form of the greedy approach to show that it is indeed a
greedy algorithm. However, clearly all the algorithm does is sort the jobs according to service time.
Its time complexity is therefore given by
𝑊(𝑛) = 𝜃(𝑛𝑙𝑜𝑔� 𝑛)
Department of Computer Science
2
College of Science for Women
Algorithm Design And Analysis
Asst. Prof. Ali Kadhum Idrees
Although intuitively it seems that the schedule created by this algorithm is optimal, this supposition
needs to be proved.
2.5.2 Scheduling with Deadlines:
In this scheduling problem, each job takes one unit of time to finish and has a
deadline and a profit. If the job starts before or at its deadline, the profit is obtained.
The goal is to schedule the jobs so as to maximize the total profit. Not all jobs have to
be scheduled. We need not consider any schedule that has a job scheduled after its
deadline because that schedule has the same profit as one that doesn't schedule the
job at all. We call such a schedule impossible. The following example illustrates this
problem.
Example 2: Suppose we have the following jobs, deadlines, and profits:
Job
1
2
3
4
Deadline
2
1
2
1
Profit
30
35
25
40
When we say that job 1 has a deadline of 2, we mean that job 1 can start at
time 1 or time 2. There is no time 0. Because job 2 has a deadline of 1, that job can
start only at time 1. The possible schedules and total profits are as follows:
Schedule
[1, 3]
[2, 1]
[2, 3]
[3, 1]
[4, 1]
[4, 3]
Total Profit
30+25 = 55
35+30 = 65
35+25 = 60
25+30 = 55
40+30 = 70
40+25 = 65
Impossible schedules have not been listed. For example, schedule [1,2] is not
possible, and is therefore not listed, because job 1 would start first at time 1 and
Department of Computer Science
3
College of Science for Women
Algorithm Design And Analysis
Asst. Prof. Ali Kadhum Idrees
take one unit of time to finish, causing job 2 to start at time 2. However, the deadline
for job 2 is time 1. Schedule [1,3], for example, is possible because job 1 is started
before its deadline, and job 3 is started at its deadline. We see that schedule [4,1] is
optimal with a total profit of 70.
Notice in the example that the job with the greatest profit (job 4) is included
in the optimal schedule, but the job with the second-greatest profit (job 2) is not.
Because both jobs have deadlines equal to 1, both cannot be scheduled. Of course,
the one with the greatest profit is the one scheduled. The other job scheduled is job
1, because its profit is greater than that of job 3. This suggests that a reasonable
greedy approach to solving the problem would be to first sort the jobs in
nonincreasing order by profit, and next inspect each job in sequence and add
it to the schedule if it is possible. Before we can create even a high-level algorithm
for this approach, we need some definitions.
A sequence is called a feasible sequence if all the jobs in the sequence start by
their deadlines. For example, [4,1] is a feasible sequence in Example 2, but [1,4] is
not a feasible sequence. A set of jobs is called a feasible set if there exists at least one
feasible sequence for the jobs in the set. In Example 2, {1,4} is a feasible set because
the scheduling sequence [4,1] is feasible, whereas {2,4} is not a feasible set because
no scheduling sequence allows both jobs to start by their deadlines. Our goal is to
find a feasible sequence with maximum total profit. We call such a sequence an
optimal sequence and the set of jobs in the sequence an optimal set of jobs. We can
now present high-level greedy algorithm for the Scheduling with Deadlines problem.
sort the jobs in nonincreasing order by profit;
S=Ø
while (the instance is not solved) do
begin
select next job;
// selection procedure
Department of Computer Science
4
College of Science for Women
Algorithm Design And Analysis
Asst. Prof. Ali Kadhum Idrees
if (S is feasible with this job added) then // feasibility check
add this job to S;
if (there are no more jobs) then
// solution check
the instance is solved;
end;
The following example illustrates this algorithm
Example 3: Suppose we have the following jobs, deadlines, and profits:
Job
1
2
3
4
5
6
7
Deadline
3
1
1
3
1
3
2
Profit
40
35
30
25
20
15
10
We have already sorted the jobs before labeling them. The previous greedy
algorithm does the following:
1. S is set to Ø.
2. S is set to {1} because the sequence [1] is feasible.
3. S is set to {1,2} because the sequence [2,1] is feasible.
4. {1,2, 3} is rejected because there is no feasible sequence for this set.
5. S is set to {1,2,4} because the sequence [2,1,4] is feasible.
6. {1,2,4,5}is rejected because there is no feasible sequence for this set.
7. {1,2,4,6}is rejected because there is no feasible sequence for this set.
8. {1,2,4,7}is rejected because there is no feasible sequence for this set.
The final value of S is {1,2,4}, and a feasible sequence for this set is [2,1,4]. Because
jobs 1 and 4 both have deadlines of 3, we could use the feasible sequence [2,4,1]
instead.
Department of Computer Science
5
College of Science for Women
Algorithm Design And Analysis
Asst. Prof. Ali Kadhum Idrees
The algorithm follows. It is assumed that the jobs have already been sorted by
profit in nonincreasing order, before being passed to the algorithm. Because the profits
are needed only to sort the jobs, they are not listed as parameters of the algorithm.
procedure schedule_with_Deadlines (n: integer; deadline:ElemList;var J:
sequence_of_integer)
var
i : integer;
K : sequence_of_integer;
begin
J = [1];
for (i = 2; i <= n; i++)
begin
K = J with i added according to nondecreasing values of deadline [i];
if (K is feasible)
J = K;
end;
end;
Before analyzing this algorithm, let's apply it.
Example 4: Suppose we have the jobs in Example 3. Recall that they had the following
deadlines:
Job
1
2
3
4
5
6
7
Deadline
3
1
1
3
1
3
2
The above Algorithm does the following:
1. J is set to [1].
2. K is set to [2, 1] and is determined to be feasible.
J is set to [2, 1] because K is feasible.
3. K is set to [2, 3, 1] and is rejected because it is not feasible.
4. K is set to [2, 1, 4] and is determined to be feasible.
J is set to [2, 1, 4] because K is feasible.
5. K is set to [2, 5, 1, 4] and is rejected because it is not feasible.
6. K is set to [2, 1, 6, 4] and is rejected because it is not feasible.
Department of Computer Science
6
College of Science for Women
Algorithm Design And Analysis
Asst. Prof. Ali Kadhum Idrees
7. K is set to [2, 7, 1, 4] and is rejected because it is not feasible.
The final value of J is [2, 1, 4].
Analysis of Algorithm: Worst-Case Time Complexity (Scheduling with Deadlines)
Basic operation: We need to do comparisons to sort the jobs, we need to do more
comparisons when we set K equal to J with job i added, and we need to do
comparisons to check if K is feasible. Therefore, a comparison instruction is the basic
operation.
Input size: n, the number of jobs.
It takes a time of Θ (n lg n) to sort the jobs before passing them to the procedure. In
each iteration of the for-i loop, we need to do at most i - 1 comparisons to add the ith
job to K, and at most i comparisons to check if K is feasible. Therefore, the worst case
is
∑ni=�[(i − 1) + i] = n� − 1 = θ(n� )
Homework:
1- Consider the following jobs and service times. Use the algorithm of simpler scheduling
to minimize the total amount of time spent in the system.
Job
1
2
3
4
Service time
7
3
10
5
2- Consider the following jobs, deadlines, and profits. Use the Scheduling with Deadlines
algorithm to maximize the total profit.
Job
1
2
3
4
5
6
7
Service time
2
4
3
2
3
1
1
Department of Computer Science
7
profit
40
15
60
20
10
45
55
College of Science for Women
© Copyright 2026 Paperzz