CMSC 641 Design & Analysis of Algorithms, Spring 2018
For each of the following problems, use proof by induction to
show that the solution produced by the greedy algorithm is the best
solution. You must use proof by induction. Do not appeal to general
principles (e.g., "it is always best to ...").
In your proof by induction, you must
Here are the problems:
- Clearly state the induction hypothesis and what value you are
inducting on (usually some indication of the size of the problem).
- State the base case and briefly argue that it holds.
- In the inductive step, argue that if a solution X
for problem Π exists that is better than the greedy solution for
Π, then you can construct from X a solution X′ to a smaller
problem Π′ such that X′ is better than the greedy solution for
Π′. (This contradicts the induction hypothesis.) Make sure that
Π′ is clearly defined.
- Prove by induction that the greedy algorithm for the fractional
knapsack problem in Section 16.2 of the textbook packs the knapsack with
the greatest possible value.
- Briefly describe a greedy algorithm for Exercise 16.2-4 (page
and provide a proof by induction that your greedy algorithm
produces an itinerary with the fewest number of stops. It would be
helpful to invent some convenient notation for the locations
of the gas stations
where Professor Gecko can fill his water bottle.
- Briefly describe a greedy algorithm for Exercise 16.2-5 (page
and provide a proof by induction that your greedy algorithm
produces a covering with the fewest number of unit length intervals.
Here a unit length interval is just an interval
[a, b] on the real line such that b = a
The following questions ask you to provide a dynamic
programming algorithm. You must provide the following:
- Define a function OPT that can be used to solve
this dynamic programming problem. To do this, you must describe
the input parameters to OPT and the "return value" using English
sentences. (Note: you are specifying the input-output relation
of the function. You should not describe how to compute
- Give a mathematical formula that shows how OPT can
be computed recursively. Then, explain all the major parts of
the formula using English sentences. Remember to include the
- Describe how OPT can be computed bottom up using
a dynamic programming table. Be sure to include a description
of the dimensions of the table and the order that the entries
of the table are to be filled. Draw a diagram. Which entry has
the solution to the original problem?
- Analyze and justify the running time of your dynamic
- Planning a company party.
Problem 15-6, page 408.
Note: the dynamic programming "table" for this problem is
- Waldo's World.
You arrive at Waldo's World Amusement Park with T minutes
remaining until the park closes. The park has n rides and your
objective is to complete as many rides as possible before the
park closes. (For this problem, taking the same ride twice counts
as 2 rides.) You are given a table W such that
W(i, t) gives you the waiting time for ride
i at time t. For convenience, assume that t
is expressed as minutes before the park closes. Ride i
itself takes ri minutes and all times are
measured in integer minutes.
Describe a dynamic programming algorithm that produces a
schedule of rides with the maximum number of rides.
(In case you thought this is a totally made up problem,
check out RideMax.)
- Car ownership.
In this question, we consider a problem in car ownership. As a
car gets older, the maintenance costs (fuel costs, repair costs,
insurance costs, etc) for the car may increase to the extent that it
would be advantageous to sell the current car and buy a new car. The
difficulty in this problem is that the prices of new cars change from
year to year, the maintenance costs of cars purchased in different years
may be different and the resale value of a car can change from year to
year as well.
For this question, you are given the following information:
- p(i) = the price of a new car in year i ,
for 1 ≤ i ≤ n.
- v(i, k)
= The resale value of a car purchased in year i and sold
in year k, for 1 ≤ i ≤ k ≤ n.
- m( i, k)
= the maintenance cost during year k of a car purchased in
year i, for 1 ≤ i ≤ k ≤ n.
The problem is to determine the years
. . . ,
when you would purchase new cars such that the total cost of car ownership from years 1
through n is minimized. The total cost is the sum of the maintenance
costs for years 1 through n plus the price of each car purchased minus
the resale value of each car sold.
For example, if n = 10,
y1 = 1,
y2 = 5,
y3 = 7,
then this solution
states that you should purchase a new car in year 1, buy the second car
in year 5 and buy the third car in year 7. (You would also sell the
first car in year 5, the second car in year 7 and the third car at the
beginning of year 11.)
In addition, you should make the following assumptions:
- You buy and sell cars at the beginning of the year.
- You don't have a car before year 1. (Since you have to buy a new car
in year 1, y1 = 1 .)
- You only want to purchase new cars.
- At the end of n years, you sell your last car for
v(yr , n + 1) dollars.
- Exercise 17.2-3, page 459.
- Square Lists.
Read the description of the Square Lists data structure from
Project 1 of CMSC341 Data Structures (Fall 2013).
Use the accounting method to show that the
operations do indeed take
amortized running time as claimed in the write up.
Additional note: the main difficulty here is splitting long inner
lists. Since we have to find the middle of a long inner list when we perform a
split, the split operation on a list with m items takes
Θ(m) time. Furthermore, an inner list might become long
not because items are added to the that list, but because items were
removed from other parts of the data structure. That is, the list
becomes long because
got smaller. Thus, it is possible that a single consolidate process will be required
to split a non-constant number of long inner list.
For example, suppose we have a Square List with 20,000 items which has
10 inner lists that each have 201 items. Now, n = 20,000 means
≈ 283. So, the lists with 201 items are not long. Suppose that we
remove 9,900 items from the Square List (and none of the deleted items are in
the 10 inner lists with 201 items). We have:
2 √10,101 > 201
So, between the 9,899-th delete operation and the 9,900-th delete operation,
those 10 inner lists with 201 items became long. Note that 10 ≈
(10,100)0.25, so we have a non-constant number of splits
to do during a single consolidate operation.
2 √10,100 < 201
- Square Lists + addendum.
Scroll down to the addendum at the end of the project description for Square Lists.
Use the accounting method to show that if the additional strategies
( early emptylist deletion,
don't make long lists,
were implemented, then the amortized running time of
can be made O(1) without increasing the
amortized running time of the other operations from the first question
- Amortized weight-balanced trees.
Problem 17-3, parts a–e, pages 473–474
- Height of Fibonacci Trees.
Exercise 19.4-1, page 526.
- Off-line minimum.
Problem 21-1, parts a-c, pp. 582-583.
- Augmenting Paths Exercise 26.2-10, page 731.
- Updating Max Flow Problem 26-4, page 762.
- Spring 2010, Quiz 3.
Do Quiz 3 from Spring 2010:
- Max Flow by Scaling.
Problem 26-5, parts a–f, pages 762–763.
- Fall 2104, Test 4.
Do Test 4 from Fall 2014:
- 3-coloring a graph.
Suppose you are given a "black box" that can decide the 3-Coloring problem
in constant time. That is, when you give the black box an undirected graph
G, it will either say
"Yes, G can be colored with 3 colors"
"No, G cannot be colored with 3 colors."
Show how you can use this black box to find a 3-coloring of G
in polynomial time. (I.e., you must be able to assign red, green or blue
to each vertex of the graph such that no two adjacent vertices have the same
Finally, note that the graph G that you give to the black box
is just an undirected graph. That means you can only specify the set of
vertices and the set of edges. You cannot add any additional information
to the graph. For example, you cannot ask: "Can G be colored
with 3 colors assuming that vertex v1 is red?"
Addendum: yes, you should use the "black
box" many times.
- Hamiltonian Path.
In class, we discussed the Hamiltonian Cycle problem. An undirected graph G = (V, E) has a Hamiltonian
cycle if there is a cycle in G that visits every vertex of
G exactly once. (A cycle in a graph is a path that starts and
ends at the same vertex.) The HAM_CYCLE decision problem is the following:
- Input: an undirected graph G = (V, E).
- Decide: Does G have a Hamiltonian cycle?
In contrast, an undirected graph G = (V, E) has a Hamiltonian
path from vertex s to vertex t if the path includes
every vertex of G exactly once. (Here, the vertices s and
t must be specified, and the path cannot be a cycle because then
s = t would be visited twice.) The HAM_PATH decision problem is the following:
- Input: an undirected graph G = (V, E)
and two designated vertices s ∈ V
and t ∈ V,
where s ≠ t.
- Decide: Does G have a Hamiltonian path from s to t?
Construct a polynomial-time ≤m-reduction from
HAM_CYCLE to HAM_PATH. Make sure that you do the reduction in
the correct direction. Argue that your construction is correct by
proving separately both directions of the "if and only if".
- An edge in every cycle.
Consider the following decision problem X:
Prove that X is NP-complete.
- Input: a directed graph G = (V, E)
and a number k.
- Decide: Does there exist a subset of edges E' ⊆
E such that |E'| ≤ k
for every cycle in G, at least one edge in the cycle
is in E' ?
N.B.: a graph can have an exponential
number of cycles. So, when you show that X ∈ NP, you cannot simply
guess E' and check every cycle in G. That would take
- Fall 2014, Test 5.
Do Test 5 from Fall 2014:
- Bonnie and Clyde, Problem 34-2, parts a–d, page 1102.
Note: the answer to part b is "polynomial time". Describe a
greedy algorithm for dividing the bag of money equally and show that it
is correct. I.e., prove that when your algorithm divides the loot,
Bonnie and Clyde do indeed receive equal amounts of money, then prove separately
that if your algorithm does not find a way to divide
the loot equally, then there is no possible way to divide the loot
equally. You may find the following lemma useful for part b:
a0, a1, a2, ..., ak ∈ ℕ such that
a0 20 + a1 21 +
a2 22 + · · · + ak 2k ≥ 2k+1.
Then there exist
b0, b1, b2, ..., bk ∈ ℕ such that
each bi ≤ ai, and
b0 20 + b1 21 +
b2 22 + · · · + bk 2k = 2k+1.
The lemma says that if there is a way to combine lower denominational coins to exceed 2k+1 dollars, then there is a way to take a subset
of those coins to total exactly 2k+1 dollars. This lemma is easy to prove by induction. Just break into cases where
ak ≥ 2,
ak = 1, and
ak = 0.
You may use the lemma without proof.
- Longest Path.
Suppose that we are given a weighted undirected graph G = (V, E) with non-negative weights.
We can easily find the shortest path between two distinguished vertices s and t using
Show that it is NP-complete to determine whether the longest path (without cycles) in G
from s to t has total weight at least w.
Note: the restriction to paths without cycles is necessary since you can repeat a cycle arbitrarily many times.
Each repetition makes the path longer. If we allowed paths from s to t with cycles, then the answer would always be
"Yes, there is a path that has total weight greater than w."
- Time Slots for Classes.
At Backwards State College (BSC), class registration is ... backwards.
First, each department lists the classes that will be taught the next semester.
Then, each student signs up for the classes he/she will take.
Finally, the Registrar assigns each class to one of the standard time slots.
Suppose that the Fall 2018 semester at BSC has n classes, m students, t standard time slots and that
each student at BSC is allowed to sign up for at most k classes.
Show that it is NP-complete to determine if there is a way to assign classes to time slots
so that none of the students have a scheduling conflict — i.e., none of the students have two of the
classes they want to take assigned to the same time slot.
Argue that the problem remains NP-complete even when k = 2.
- Vertex Cover for Trees.
In graph theory, a tree is defined as an
acyclic connected undirected graph. (It does not have to have a root and
does not have to be binary.
You heard that there's a greedy algorithm that finds the minimum vertex cover for trees.
- Consider this greedy algorithm: Pick the vertex with
the highest degree. Then, remove that vertex and all edges incident
on that vertex. This leaves behind a collection of trees. Recursively
apply this greedy algorithm on the remaining trees.
Provide a simple counter-example that shows that this greedy
algorithm does not produce the smallest vertex cover.
Provide a "greedy" algorithm that does find the minimum vertex cover.
Prove by induction that your algorithm does indeed find the smallest
possible vertex cover for all trees.
- Bin Packing. Problem 35-1, parts b - e, page 1134.
- Approximating Clique. Problem 35-2, parts a & b, pages 1134-1135.
- Three-dimensional matching, revisited.
Recall that in the three-dimensional matching problem (3DM) we are given
three sets X, Y and Z such that the sizes of the
three sets are the same. That is, for some m,
| X | =
| Y | =
| Z |.
We are also given a set of triples T ⊆
X × Y × Z. The NP-complete problem
we discussed in class asks if there exists a perfect matching
— that is, a subset of triples T' ⊆ T such
that | T' | = m and
every x ∈ X appears in a triple in T' exactly once,
every y ∈ Y appears in a triple in T' exactly once
every z ∈ Z appears in a triple in T' exactly once.
A matching M ⊆ T (not necessarily a perfect one) just requires
every x ∈ X appears in a triple in M at most once,
every y ∈ Y appears in a triple in M at most once
every z ∈ Z appears in a triple in M at most once.
So, a matching enforces that triples in M do not share
components, but does not require that every x, y and
z appears in some triple.
The optimization version of 3DM asks us to find a matching that contains
the largest number of triples.
Describe a polynomial-time approximation algorithm that finds a matching
that has at least 1/3 as many triples as in the maximum matching.
Argue that the approximation factor you achieve is indeed 3. Briefly
state and justify the running time of your algorithm.
- Approximating 0-1 Knapsack. Problem 35-7, page 1137.
Note: The knapsack problem is considered NP-complete because the
value of the capacity of knapsack W can be exponential in the
number of items n. The dynamic programming algorithm we looked at runs
in O( nW ) time, which is considered exponential time.
- You toss n balls into n bins. For each toss
there is a 1/n probability that the ball you toss lands
in any one of the bins. After n tosses, what is the
expected number of empty bins?
- You keep tossing balls into bins until one of the bins
has a two balls. Again, for each toss there is a
1/n probability that the ball you toss lands
in any one of the bins. What is the expected number
- Mission Impossible?
You are a super-secret mercenary spy. You have just completed your
most recent job and you are entertaining offers for the next job.
Unlike the good guys on TV, you just want to pick the job that pays
you the most. (Hence "mercenary".) You have n offers from
various nefarious organizations. These offers arrived in your
mailbox (the physical one) in random order while you were away.
Unfortunately, the offers self-destruct in 5 seconds as soon as you
open the letter. Basically you have decide right away to accept
the mission or move on to the next offer. Once you accept
a mission, you are committed and cannot accept offers from
the letters you have not yet opened.
Devise an algorithm that gives you at least 1/4 probability
of accepting the offer that pays the most. The probability
is taken over the order that you open the letters which
we assume has been randomized by the Post Office.
Hint: think about the second highest offer.
Where can it be in the order? With what probability?
How does this help you accept the highest offer?
- 0-1 Integer Programming. Exercise 34.5-2, pages 1100-1101.
- Line fitting.
[ from Algorithms, Dasgupta, Papadimitriou & Vazirani ]
Consider these 7 points on the plane:
This points are not colinear (i.e., no line passes through all of the
points). We wish to find a line ax + by = c
such that the line minimizes the maximum absolute error, which
is defined by
max1 ≤ i ≤ 7 | axi
+ byi − c |
where (xi, yi) are the coordinates of the i-th point.
Formulate a linear program (you don't have to solve it), that finds
the values of a, b and c that determine the line
with the smallest maximum absolute error. Place your solution in
- Linear programs cannot use the absolute value function (since it's not linear).
- You may/should introduce new variables.
- Make sure you are not trying to simultaneous maximizing and
minimizing the objective function. (Also, standard form uses
- Optimality proof.
[ from Algorithms, Dasgupta, Papadimitriou & Vazirani ]
Consider the linear program:
max x1 − 2 x3
Prove that the solution
(x1 , x2, x3)
= (3/2, 1/2, 0) is an optimal solution.
x1 − x2 ≤ 1
2 x2 − x3 ≤ 1
x1 , x2, x3 ≥ 0
2 May 2018 10:32:47 EDT
to Spring 2018 CMSC 641 Homepage