You are on page 1of 6

Algorithm Design and Analysis (CS60007)

Assignment 1
Your name (Roll number)

September 16, 2020

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.1 Greedy algorithm


The greedy strategy is simple: select the interval that finishes first, then select the
one that starts no sooner and finishes first, and so on, ensuring that the new selection
does not intersect the interior of the previous selection and maximizes the leftover
span. The steps are shown in Algorithm 1, and a demonstration is shown in Fig. 1.
Notice that as an interval i is selected, the intervals with start points less than fi
are discarded to ensure non-intersection.

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

Figure 1: Demo of interval scheduling on a set of 9 intervals.

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]

f (S[j − 1]) f (OPT[j − 1]) f (OPT[j])

Figure 2: Inductive step of the proof.

1.2 Proof of correctness


Let S[j] denote the jth interval in S, whereas OPT[j] denote the jth interval in OPT
(optimal solution). Note that S[j] and OPT[j] may be same or two different intervals
in R. Let f (S[j]) denote the finishing time of the jth interval in S. For OPT[j], we
denote it by f (OPT[j]). We prove that f (S[j]) ≤ f (OPT[j]) by induction on S[j].

• Basis: Holds true because f (S[1]) = min {fi } ≤ f (OPT[1]).


1≤i≤n

• Hypothesis: Assume f (S[j − 1]) ≤ f (OPT[j − 1]).

• 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]).

So, if |S| = k, then f (S[k]) ≤ f (OPT[k]). Hence, |OPT| = k because existence


of OPT[k + 1] implies OPT[k + 1] can be included in S (as its (k + 1)th interval),
thereby contradicting |S| = k.

1.3 Time and space complexities


Sorting the intervals by fi takes O(n log n) time.
In the while loop, the next smallest interval is selected by discarding each and every
interval j having sj < fi and fj ≥ fi , where i is the currently selected interval. This
is done by traversing on R exactly once. Hence, it takes O(n) time.
So, the total time complexity is O(n log n).
The space complexity is O(n) owing to R and S.

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.

2. Based on the above recurrence, give an efficient dynamic programming algo-


rithm that returns the maximum number of robots that can be destroyed by
a sequence of AKUMAR activations.

3. Demonstrate with a suitable example how your algorithm works.

4. Derive its time and space complexities. [25 × 4 = 100]

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:

Microsoft Google JPMC Nutanix


09:00–10:30 11:30–13:00 13:00–15:00 15:15–17:00

As auditorium is an important resource and there are other activities going on


in campus, you need to reserve minimum number of auditoriums such that all talks
can be scheduled without any conflict.

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. Demonstrate with a suitable example how your algorithm works.

3. Write its proof of correctness.

4. Derive its time and space complexities. [25 × 4 = 100]

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.)

1. Design a divide-and-conquer algorithm for the given problem. Assume that


the array H[1..n] contains the building heights.
Your algorithm should also print the buildings, as mentioned in Example.

2. Demonstrate with a suitable example how your algorithm works.

3. Write its proof of correctness.

4. Derive its time and space complexities. [25 × 4 = 100]

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

Maximum profit = 208.


(Use the complete areas of the first two colors and one-third area of the third color.
Total area A = 16 + 32 + 13 × 48 = 64 sq. units.
So, maximum profit = 96 + 80 + 13 × 96 = 208.)

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.

2. Demonstrate with a suitable example how your algorithm works.

3. Write its proof of correctness.

4. Derive its time and space complexities. [25 × 4 = 100]

4
By Preetam (TA), inspired from a well-known problem.

You might also like