Algorithm Design and Analysis (CS60007) Assignment 1: 1 Interval Scheduling
Algorithm Design and Analysis (CS60007) Assignment 1: 1 Interval Scheduling
Assignment 1
Your name (Roll number)
1 Interval scheduling
Given a set of n closed time intervals, {[si , fi ] : i = 1, 2, . . . , n}, we have to find a
subset S of these intervals such that:
1. the intervals in S do not intersect each other at their interiors; i.e., for any
two i, j ∈ S, we have (si , fi ) ∩ (sj , fj ) = ∅;
2. the number of intervals in S is maximum.
1 1st selection
2 discarded after 1st selection
3 discarded after 1st selection
4 2nd selection
5 discarded after 2nd selection
6 discarded after 2nd selection
7 discarded after 2nd selection
8 3rd selection
discarded after
9
3rd selection
m M
1
Algorithm 1: Greedy algorithm for interval scheduling.
1 R ← {n intervals sorted in non-decreasing order of fi }
2 S←∅
3 while R 6= ∅ do
4 choose an interval i ∈ R that has the smallest fi
5 S ← S ∪ {i}
6 delete each interval j from R for which sj < fi
7 return S
i∈R S[j − 1]
i0 ∈ R OPT[j − 1]
S[j] lies here
i00 ∈ R OPT[j]
• Step: See Fig. 2. S[j] is either the same interval OPT[j] or some other interval
contained in [f (S[j − 1]), f (OPT[j])]. So, f (S[j]) ≤ f (OPT[j]).
2
2 Robot destroy
1 The residents of the city named PUBJEE have developed a device named AKUMAR
that can help fend off attacks by robots on their city. The robots arrive over a course
of n seconds; in the ith second, xi robots arrive. Based on remote sensing data, the
residents know this sequence x1 , x2 , ..., xn in advance.
AKUMAR’s power depends on how long it has been allowed to charge up. To
make this precise, there is a function f (·) so that if j seconds have passed since
AKUMAR was last used, then it is capable of destroying up to f (j) robots. So
specifically, if it is used in the kth second, and it has been j seconds since it was
previously used, then it will destroy min(xk , f (j)) robots. (After this use, it will be
completely drained). Also, assume that AKUMAR starts off completely drained.
You are given as inputs the data on robot arrivals x1 , x2 , . . . , xn and the recharg-
ing function f (·).
1. Derive a recursion for the maximum number of robots that can be destroyed
by a sequence of AKUMAR activations.
1
By Manish (TA), inspired from a problem in Kleinberg-Tardos book.
3
3 Auditorium booking
2 You are a member of placement team of IITKGP. A lot of companies are visiting
your campus and want to give pre-placement talk. Each company has given a start
time and end time of their talk. For each talk, you need to reserve an auditorium
for that time slot. A talk can be arranged in an auditorium if it has no conflict
with any talk in that auditorium. For example, the following is a valid allocation
for Auditorium 1:
Example:
n = 5 slots, and the slots of talks are (1, 3), (2, 4), (4, 7), (5, 8), (6, 9).
Minimum auditoriums required = 3.
1. You are given n time slots of n talks, each slot defined by a start time and an
end time. Design an algorithm to find minimum number of auditoriums that
need to be reserved.
2
By Arshdeep (TA), inspired from a problem in Kleinberg-Tardos book.
4
4 Building sequence
3 There are n buildings of heights h1 , h2 , . . . , hn , located in a straight line. The task
is to find consecutive buildings, say, {j, j + 1, . . . , k}, such that k − j is maximum
and hj ≤ hi ≤ hk ∀ i ∈ {j, j + 1, . . . , k}.
Example
Consider 12 buildings with heights 4, 2, 3, 2, 8, 3, 3, 5, 7, 5, 4, 6. The optimal value for
this is 4. (There are two optimal solutions: 2, 3, 2, 8 and 3, 3, 5, 7; printing any one
will be okay.)
3
By Aditya (TA), inspired from a popular problem.
5
5 Painting exhibition
4 You are fond of painting since your childhood days. In this lockdown period,
you have thought of preparing some beautiful paintings. You have also talked to a
convener to put them in an exhibition when things become normal. The convener
has posed some conditions on the paintings. It says, the canvas size of each painting
should be exactly m × m sq. units and you can use at most n colors c1 , . . . , cn in it.
Each color ci can acquire a maximum area of ai < m2 sq. units of the canvas. You
get a profit pi for utilizing the area ai . However, you need
Pnot necessarily utilize ai
n
completely. Assume that m and ai are all integers and i=1 ai ≥ m2 .
2
Your objective is to maximize the profit you can earn from each painting.
Example
Assume that m = 8. The total area of the canvas in that case is 64 sq. units.
Suppose you can use 4 colors. The specifications are given below.
color ci c1 c2 c3 c4
ai (in sq. units) 16 32 48 64
pi (in Rs.) 96 80 96 64
1. Propose a greedy algorithm to maximize the profit you can earn from each
painting. Your algorithm should print the maximum profit, and the colors
with painted areas.
4
By Preetam (TA), inspired from a well-known problem.