Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

COMPSCI 311: Introduction to Algorithms Spring 2021

Homework 2
Released 2/23/2021 Due 3/8/2021 11:59pm in Gradescope

Instructions. You may work in groups, but you must write solutions yourself. List collaborators on your
submission. You are allowed to have at most 4 collaborators. Also list any sources of help (including online
sources) other than the textbook and course staff.
If you are asked to design an algorithm, please provide: (a) the pseudocode or precise description in words
of the algorithm, (b) an explanation of the intuition for the algorithm, (c) a proof of correctness, (d) the
running time of your algorithm and (e) justification for your running time analysis.
Submissions. Please submit a PDF file. You may submit a scanned handwritten document, but a typed
submission is preferred. Please assign pages to questions in Gradescope.

1. Magic words (20p) We have a set of n treasure chests. Each opens when its own magic word is said. Some
treasure chests contain notes with magic words for other chests. A record lists every chest and all chests it
contains notes for. Our goal is to open up all chests.
a) Design an algorithm that, given a chest c determines if it is possible to open up all chests if we know the
magic word for c.
b) Design an algorithm that determines the minimum number of magic words we need to know in order to
open up all chests.
Your algorithms should be linear in the number of chests and notes.

2. Scheduling with weights (20p) Assume now that in the interval scheduling problem, each job i ∈
{1, 2, . . . , n}, in addition to starting time si and finishing time fi , has a value vi , which is a non-negative
integer. Our goal is to find a schedule of non-overlapping jobs such that the total value of all the jobs in the
schedule is maximized.
For each of the following ideas, either prove that it produces an optimal schedule or give a counterexample
to show that it does not.
(a) (5p). Choose the job with earliest finishing time, remove overlapping jobs, and repeat.
(b) (5p). Choose the job with largest value, remove overlapping jobs, and repeat.
(c) (5p). Choose the job with largest ratio fi −si ,
vi
remove overlapping jobs, and repeat. (fi − si is the
length of the job i.)
(d) (5p). Choose the job that conflicts with a set of jobs of minimum total value, remove overlapping
jobs, and repeat.

3. Covering Array (20p) Given an array X that only contains −1, 0, 1. A negative subarray of X is a
contiguous subarray whose sum of all elements is a negative number. Find a minimum number of negative
subarrays that cover all negative elements of X (see Figure 1 for an example). Your algorithm should run
in polynomial time.

-1 -1 -2
X -1 1 0 0 -1 1 1 -1 0 1 -1 1 1 1 1 1 -1 -1 -1 1 0

Figure 1: An array X can be covered by 3 negative subarrays. The number on top of each interval is the
total sum of the elements of the corresponding subarray.

1
Homework 2 2

4. Round the Clock Scheduling (20p) Given a set of n jobs where each job is represented by an arc of a
circle; each arc is specified by two endpoints pi and qi (see Figure 2). A schedule is a set of jobs whose arcs
are pairwise non-overlapping. Design an algorithm that finds a schedule with a maximum number of jobs.
Your algorithm should run in polynomial time.

2
5
6
1

Figure 2: A set of arcs representing a set of jobs. A set of minimum non-overlapping jobs is {2, 4, 6}.

5. Shortest route (20p) Three friends want to drive from Boston to Los Angeles. They have a complete map
of cities, roads and distances in between. They set the following rules:
1) Once leaving a city, they only switch drivers at the next city.
2) They rotate driving, with Ava first, then Blue, then Cyd, etc. Ava also wants to drive the last stretch.
3) If they just drove from city X to Y , their next step cannot be driving back from Y to X.
Assume roads are two-way, and there aren’t two distinct roads between a pair of cities.
Give an algorithm that finds the shortest (not necessarily simple) path from Boston to Los Angeles that can
be driven with the rules above, otherwise report this is impossible.

You might also like