4P03Week4Slides
4P03Week4Slides
Find:
A schedule (ordering of elements of S) that minimizes the total penalty.
Canonical form:
All early tasks precede all late tasks (“early-first”), and
Early tasks are scheduled in order of non-decreasing deadlines.
We can always put a schedule into canonical form:
Early-first: if an early task x follows a late task y, then switch x and y (this won’t
affect x being early or y being late)
Swap any 2 early tasks ai (finishing at time t) and aj (finishing at time t+1) with
deadlines dj<di.
Since aj was early before the swap, this means that t+1≤dj. Therefore since dj<di we have
t+1<di and so ai is still early after the swap.
Task aj is moved earlier in the schedule, so it’s still early.
COSC 4P03 WEEK 4 2
TASK SCHEDULING – CANONICAL FORM (2)
A = ;
Sort S into non-increasing order by weight w;
For each element x in S (in non-increasing order)
{
if A U {x} is in I
then A = A U {x};
}
return A;
A = ;
n = 0;
sort S into non-increasing order by penalty wi;
for each x in S (taken in non-increasing order)
{
if Nt(A U {x}) ≤ t for all 1≤t≤n+1
{
A = A U {x}; ai 1 2 3 4 5 6 7
n = n+1;
} di 4 2 4 3 1 4 6
}
wi 70 60 50 40 30 20 10
return A;
COSC 4P03 WEEK 4 8
GREEDY ALGORITHMS FOR OTHER PROBLEMS
Some other problems for which greedy algorithms are guaranteed to give an optimal solution:
MST algorithms by Kruskal and Prim
Single-source shortest path by Dijkstra
Some problems for which greedy algorithms “come close”:
Travelling salesman problem, in which we put restrictions on weights such that they
represent distances on a plane – guaranteed to be at most 2 times worse than optimal
0-1 Knapsack problem – guaranteed to be at most 1+1/k worse, where k = # of items
considered at a time
Some problems for which greedy algorithms cannot be guaranteed to come within a constant
factor of being optimal:
Travelling salesman problem (general)
Graph colouring
People have shown that if there were a “guarantee” then P=NP.
COSC 4P03 WEEK 4 9
APPROXIMATION ALGORITHMS
(CHAPTER 35, CLRS)
The (7,4,3) Hamming Code can correct up to 1 error in a string of 7 bits. How
it works:
All data must be stored as codewords, i.e. we can’t use just any one of the 27
possible strings of 7 bits, but only the chosen codewords.
When the data is read:
If it’s the same as one of the codewords, then we assume there was no error and
accept it as is.
Otherwise, we pick the closest codeword, guessing that was what the string was
meant to be (because this will be as a result of the smallest number of errors).
For this code (not all codes), there will always be one codeword at distance ≤ 1
from any string of 7 bits.
Example: if we read 1111000 then we guess it should be 1101000 since it’s
the only codeword that differs in just 1 bit from 1111000.
COSC 4P03 WEEK 4 26