COMP3121/3821/9101/9801 - School of Computer Science and

NEW SOUTH WALES
Algorithms:
COMP3121/3821/9101/9801
Aleks Ignjatovi´c
School of Computer Science and Engineering
University of New South Wales
LECTURE 5: THE GREEDY METHOD
COMP3121/3821/9101/9801
1 / 7
The Greedy Method
Activity selection problem.
Instance: A list of activities ai , (1 ≤ i ≤ n) with starting times si and
finishing times fi . No two activities can take place simultaneously.
Task: Find a maximum size subset of compatible activities.
Activity selection problem II.
Instance: A list of activities ai , (1 ≤ i ≤ n) with starting times si and
finishing times fi = si + d; thus, all activities are of the same duration. No two
activities can take place simultaneously.
Task: Find a subset of compatible activities of maximal total duration.
Question: What happens if the activities are not all of the same
duration?
COMP3121/3821/9101/9801
2 / 7
The Greedy Method
Minimising job lateness.
Instance: A start time T0 and a list of jobs ai , (1 ≤ i ≤ n), with duration
times ti and deadlines di . Only one job can be performed at any time; all jobs
have to be completed. If a job ai is completed at a finishing time fi > di then
we say that it has incurred lateness li = fi − di .
Task: Schedule all the jobs so that the lateness of the job with the largest
lateness is minimised.
COMP3121/3821/9101/9801
3 / 7
The Greedy Method
k-clustering of maximum spacing
Instance: A complete graph G with weighted edges representing distances
between the two vertices.
Task: Partition the vertices of G into k disjoint subset so that the minimal
distance between two points belonging to different sets of the partition is as
large as possible.
COMP3121/3821/9101/9801
4 / 7
The Greedy Method
Optimal caching
Instance: You are writing a very long paper and you will need 26 books
A, B, C, . . . , X, Y, Z, some of them at several times. However, the university
library allows you to keep at most 7 books at each time. You carefully compile
the list of books in the order you will need them: ABACBABAD..... Each
time you go to the library, you return one book and check out another book.
Task: Decide which book to return on each library trip so that you minimise
the total number of trips to the library.
COMP3121/3821/9101/9801
5 / 7
The Greedy Method
Tape storrage.
Instance: A list of n files fi of lengths li which have to be stored on a tape.
Each file is equally likely to be needed. To retrieve a file, one must start from
the beginning of the tape and scan it until the tape is fund and read.
Task: Order the files on the tape so that the expected retrieval time is
minimised.
Tape storrage II.
Instance:
A list of n files fi of lengths li and probabilities to be needed pi ,
Pn
i=1 pi = 1, which have to be stored on a tape. To retrieve a file, one must
start from the beginning of the tape and scan it until the tape is fund and read.
Task: Order the files on the tape so that the expected retrieval time is
minimised.
COMP3121/3821/9101/9801
6 / 7
The Greedy Method
0-1 knapsack problem
Instance: A list of weights wi and values vi for discrete items ai , 1 ≤ i ≤ n,
and a maximal weight limit W of your knapsack.
Task: Find a subset S of all items available such that its weight does not
exceed W and its value is maximal.
Special case: Here are just three items with weights and values: (10kg, $60),
(20kg, $100), (30kg, $120) and a knapsack of capacity W = 50kg.
So when does the Greedy Strategy work??
Unfortunately no easy rule...
COMP3121/3821/9101/9801
7 / 7