This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Introduction to Algorithms
Massachusetts Institute of Technology
Professors Piotr Indyk and Charles E. Leiserson December 1, 2004
6.046J/18.410J
Quiz 2 Solutions Quiz 2 Solutions
108 2630 0 0 2 3
Median = 74.5
Mean
= 76.4
Std. Dev. = 22.3 18
16
14
12
Count 22 10
8
6
4
2
0
3039 4049 5059 6069 7079 8089 Score 9099 100109 110119 120129 0 9099 6.046J/18.410J Quiz 2 Solutions 2 Problem 1. A Dynamic Set ¦
¦ E XTRACTO LDEST
: Remove and return the oldest item
the one inserted least recently.) ¡ ©
¨§
¡ .
§ that can perform any sequence of
¡
§
¡ ¦ Design a data structure for
¡ : Return (but do not remove) the item
¡ F IND M AX . The dynamic set . (The oldest item is for which key ¤
¥¡ ¢
I NSERT into ¡ : Insert item has an associated key key ¤¡
¥£¢ A dynamic set contains items, where each item
supports the following operations: is maximal. operations efﬁciently. Solution:
The goal of the problem is to come up with an efﬁcient implementation of a queue, augmented with
the additional F IND M AX operation. Thus, it makes sense take a queue implementation 1 (e.g., a
linkedlist) and extend it so that it supports F IND M AX as well.
For starters, we assume that all elements are distinct. This assumption is not crucial, but simpliﬁes the description and the analysis of the algorithms. Note that, in any case, we can break
ties by replacing each element by
, where is the time when was inserted, and using
lexicographic ordering.
¡ #
%! #!©
$"¨§
¡ ¡ We will see that there are, essentially, three basic ways of supporting F IND M AX :
1.Scan all elements in the list to ﬁnd the maximum. This gives
total worstcase running
time. This solution was worth up to 8 points.
A slightly better implementation maintains a pointer to the maximum element; when the element is deleted, the E XTRACTO LDEST procedure searches for the new maximum. Although
this implementation still has
worstcase time bound, it gives a much better time bound
2
in the average case . Thus, this solution was worth up to 10 points. Also, this was by far the
most popular solution.
)
('&
§ ) 0'&
§ 2.Augment the queue with a search tree (description below). In this way, all three operations
can be performed in
time. This gives
total worstcase running time. This
solution was worth up to 15 points.
87@9(21
6 43
§ 875021
6 43§ 3.Augment the queue with a queuestack, that is, a hybrid of a queue and a stack. This is the
best solution  it gives
total time in the worstcase (although individual operations could
take
time). Description below.
(21
§ 0BA
§ A remark: our “augmentation” is actually very noninvasive. That is, we do not modify the code or
structures of the queue; instead, we add a new data structure that operates in parallel to the queue.
1 One can also use a search tree with elements sorted by insertion time, but this is an overkill, since the elements
are not deleted in a random order.
2
E.g., when all insertions preceed all deletions, and the order of deletions and insertions is random 6.046J/18.410J Quiz 2 Solutions 3 This approach has an advantage of being simple and modular. E.g., there is no need to get into the
guts of the queue code. However, modifying the queue code works ﬁne as well.
The queue with a tree. Consider any implementation of a queue, with operations I NSERT
and E XTRACTO LDEST . In addition, we will also maintain a search data structure . The data
structure should support three operations in
time: I NSERT , D ELETE and F IND M AX .
E.g., one can use a 23tree, a RedBlack tree, or even a heap 3 .
C D 87@(21
6 43§ Each element that is present in is maintained in both and (although space savings can be
obtained by storing only once, and using pointers). Speciﬁcally, we implement insertions and
extraction to as follows.
D C ¡ ¡ Insert(Q,x):
Insert(L,x)
Insert(T,x)
End
ExtractOldest(Q):
x:=ExtractOldest(L)
Delete(T,x)
Return x
End
Therefore, we can ﬁnd the maximum as follows:
FindMaximum(Q):
Return FindMax(T)
End
8759021
6 43
§ time. This gives Clearly, all three operations run in total running time bound. 875021
6 43§ The queue with a queuestack. We will use a queue and a queuestack . The latter supports
I NSERT and E XTRACTO LDEST (as in the queue), as well as E XTRACTY OUNGEST (as in the
stack). For simplicity, we also assume functions O LDEST and YOUNGEST , which return the same
elements as E XTRACTO LDEST and E XTRACTY OUNGEST , but without removing them from .
All operations are performed in constant time.
E
FC E C C The queue contains all elements currently in . The queuestack contains only some of the
elements of , selected in the following way. Let
be the (current) elements of (and ),
the oldest element ﬁrst. Let
denote the elements of , the oldest element ﬁrst. The choice
of ’s is as follows. The element is the maximum element of . For each
, let be the
E
GC C C T HQQQ I
U"SSRPH a
hg e
fdb
c E C YVQQQ I
`"SSXWV I
WV a
`V 3
Although in this case one needs to argue how to delete an arbitrary element in the heap, since the data structure
described in CLRS, p. 127, does not enable arbitrary deletions, at least as stated. 6.046J/18.410J Quiz 2 Solutions iV
$t8rpH
s
qi , i.e., such that . For e
wvb
u in , a
`V a
`V index of 4
is deﬁned as the maximum element among . T H Q Q Q xq i
"SSyrhH In other words: as long as is in , it is the maximum element. However, when is deleted,
then becomes the new maximum. And so on. Notice that the sequence
is decreasing.
We call it the maximal sequence of
.
I
hV I
SV QQ
SSQ V©I
`pV V ) ) THQQQ I
SR¥H . E
8§
C Clearly, given , we can answer F IND M AX in constant time: simply return , i.e., O LDEST
To maintain and (that is, the sequences
and
), we do the following.
I
hV E
8C YV©QQQ©I
``SSS
V E TH©QQQ©I
`SSSH C C Insert(Q,x):
Insert(L,x)
While key[Youngest(L’)]<key[x] ExtractYoungest(L’)
Insert(L’,x)
End
ExtractOldest(Q):
x:=ExtractOldest(L)
If x=Oldest(L’) then ExtractOldest(Q)
Return x
End
The correctness of E XTRACTO LDEST is immediate from the deﬁnition of the maximal sequence.
The correctness of I NSERT follows from the observation that the maximum sequence for
contains only the elements of the maximum sequence for for
which are
.
¡©T HQ Q Q I
"0S
RH ¡
c THQQQ I
S
H To show that operations take
time, we ﬁrst observe that both F IND M AX and E XTRACTO LDEST
take
time in the worstcase. Moreover, the total cost of all I NSERT operations is bounded from
above by the total number of operations I NSERT into (which is at most ) plus the total number
of operations E XTRACTYOUNGEST from . But, the latter is at most the total number of insertions into , which again is at most . Thus, the total time per operations is
. Note that the
same analysis can be done using the accounting or potential method.
(1
§ e§
$1 E C E C E (1
§ C Problem 2. Great Minds Need Coffee
It is a beautiful autumn morning, and Professor Indyk has decided to walk from his apartment to
MIT where he will give a lecture for 6.046. To prepare for this trip, the professor has gone online
and downloaded a map of the roads and
intersections in the Boston area. For each road
connecting two intersections, the map lists the length
of the road in meters. (Recall that in
the Boston area, an arbitrarily large number of roads can meet at a single intersection, e.g., Davis
Square.)
¤ ¢
meters without sitting
Like many great theorists, Professor Indyk cannot walk more than
down and assuaging his caffeine addiction. (This is especially true before 9:30 A . M .) Fortunately,
the Moonbucks Coffee Company has recently opened a large number of coffee shops throughout
77We 6.046J/18.410J Quiz 2 Solutions
be the set of
g fYV©QQQ
p``
S
S© intersections hosting Moonbucks coffee V©IVd
`Spef
s
) the Boston area. Let
shops. 5 Thus, Professor Indyk is looking for a route from his apartment (located at one given intersection)
to the Stata Center (located at another given intersection) in which he never travels more than
meters without passing through an intersection with a coffee shop. Help the professor get to lecture
on time by designing an efﬁcient algorithm to ﬁnd the shortest acceptable route from his apartment
to the Stata Center.
7We Solution:
Summary: We model the city of Boston as a graph, , with the intersections as vertices, the
roads as edges and weights on the edges representing the length of the road. We call an intersection
“caffeinated” if Piotr is able to acquire coffee at the intersection. For the purposes of this problem,
caffeinated intersections (Note that coffee is available at each of the Moonbucks,
Boston has
as well as Piotr’s apartment and the Stata center). Our goal is to ﬁnd the shortest path from Piotr’s
apartment to the Stata center which never travels more than 1000 meters without passing through
a caffeinated intersection. To do this, we create a new graph, , which has a vertex for each of the
caffeinated. For each pair vertices
in , if the shortest path between the corresponding
vertices in , denoted
, is less than 1000 meters, then there is an edge in
between
whose weight is
. Since all street lengths in the city of Boston are nonnegative, we construct
the graph
by running
executions of Djikstra’s algorithm on (one execution starting at
each of the
caffeinated intersections). We then perform one more execution of Djikstra’s
algorithm on
to ﬁnd the shortest path between Piotr’s apartment and the Stata center in
(which we will show is the shortest acceptable path from Piotr’s apartment).
h g j
'g
i E
h E
lh g©b
%"0§ g©b
%"0§ j
kg
i E
h g©b§
%"0Rm h g©b§
%"0m h E j
ng
i h j
pog
i E
h E
Bh Algorithm: We assume that the input to our algorithm is a graph with
vertices and edges
represented as adjacency lists. We also assume that the ﬁrst vertices (numbered 1 through )
contain Moonbucks coffee shops and that vertex
is Piotr’s apartment and vertex
is
the Stata center. (If the vertices are not presented in this order, and we are merely given a list
of vertices with coffee shops, then we can easily relabel the vertices in
time). We
now run Djikstra’s algorithm
times where the th execution computes the singlesource
shortest paths from vertex to vertices
. We use a Fibonacci Heap to implement the
priority queue used by Djikstra’s algorithm, so the running time of each execution of Djikstra’s
algorithm is
. Therefore, the time for
executions of Djikstra’s algorithm
is
. Now that we know the length of the shortest Path between all pairs of
caffeinated vertices, we can construct the graph as described in the summary above. Finally, we
run Djikstra’s algorithm one last time to ﬁnd the shortest path from vertex
to vertex
in graph . Since graph
has that most
edges, this ﬁnal step takes that most
time.
Therefore, the total running time of our algorithm is
. Since is
,
this simpliﬁes to
.
g g j
pg
i e
qg
i
" g§ 6 43
¥7@9(21
§ b j
qrg
i j
ntsSSSe
i
g©QQQ© b j
vtg
i ni
§ 6 43
0¥7@u021
§
wpi
g E j
ntg
i e
xg
i ) w21
g§ g§
21
) g § 6 43
(75vw21
g§ h ) ) g
yBwyi
i
g g§
r1 E
Bh § 6 43
(75vw21
g§ E
Bh {zi
g § 6 43
(75vw21
g§ Correctness: We show correctness we ﬁrst argue that every path from Piotr’s house (vertex
)
) in
corresponds to an acceptable path in (with the same
to the Stata center (vertex
length). We then argue that any shortest acceptable path from vertex
to vertex
in
e
}g
i h j
~2g
i E
lh e
w2g
i j
~g
i 6.046J/18.410J Quiz 2 Solutions 6 corresponds to a path (of the same length) in . From this we conclude that by ﬁnding the
shortest path from vertex
to vertex
in
our algorithm (1) Finds an acceptable path in
the original graph and (2) Finds the shortest such path in .
E
h h E
h j
}9g
i e
9g
i h h Consider any path
in . Each edge in this path corresponds to a path of length at most 1000
meters between caffeinated vertices in . Therefore, by replacing each edge in
by the corresponding path in we can construct a path of the same length in which passes through a
caffeinated vertex at least once every 1000 meters.
E
Bh E
E
l h h h Now consider a shortest acceptable path in from vertex
to
. Let
be the caffeinated intersections that passes through (in the order that it passes through
them). Note that for each
, must travel on a shortest path from to
(because
every subpath of a shortest path must also be a shortest path). Additionally, for each
, the
subpath from to
must be at most 1000 meters because is acceptable. Therefore, for each
, the length of the subpath from to
is the same as the weight on the edge from
to
in . Thus if the path
consisting of the vertices
in
has
the same length as the original path .
s
8`
S
SsRr§
©QQQ©I
s
e
ig j
tRg
i e
Rg
i h I a
R` j
'g
i a
fe
b
le
b
a
I a
R` I a
Rys E
Bh a
a
vpte
b
j
`SSS`hpn§
i
g
s
©QQQ©I
s
e
i
g E
B E
Bh I a
Rs h Grading notes: Many students attempted to use a single execution of a greedy algorithm similar
to Djikstra’s algorithm ﬁnd the shortest acceptable path from Piotr’s apartment to the Stata center.
Such algorithms were very efﬁcient because they visited each vertex only once, but were generally
incorrect. These solutions generally received around 8 points provided that they demonstrated a
solid understanding of Djikstra’s algorithm and traditional shortest path problems.
The principal problem with visiting each vertex only once is as follows. Assume there is a short
Path from from the source to vertex that reaches vertex after having traveled 999 meters since
the last coffee shop. Additionally assume that there is a longer path from the source to vertex that
reaches vertex after having traveled only 100 meters since the last coffee shop. A Djikstralike
greedy algorithm will ﬁnd the former path but taking the latter path may be necessary to reach
the Stata center while satisfying the caffeine requirement. That is, you may want to take a longer
path to vertex in order to arrive there with more caffeine in your blood stream. (Some students
even observed that the optimal path might visit visit the same intersection twice. For instance, one
might ﬁrst walk to vertex , then walk to nearby vertex to buy coffee, then back to vertex and
onward to the Stata center. Clearly, any algorithm which visits each vertex only once will fail to
ﬁnd such a path).
b b b b b b g b Some students used FloydWarshall to construct
instead of using repeated executions of Djikstra’s algorithm. However, since all edge weights in this problem are nonnegative (Boston has
no negative length streets) it is more efﬁcient to use repeated applications of Djikstra’s algorithm.
Good solutions using FloydWarshall instead of Djikstra still generally received at least 20 points.
E
h Some creative students observed that Boston (having relatively few overpasses) is nearly planar
and hence graph is sparse. This allows for a tighter analysis of the above algorithm. If one additionally assumes that no two intersections are within 1 meter of each other then one can conclude
that the number of intersections within 1000 meters of a given point is a (somewhat large) constant.
This allows for a very (asymptotically) efﬁcient algorithm. Finally, two students observed that an
h 6.046J/18.410J Quiz 2 Solutions 7 asymptotically more efﬁcient solution can be found if one assumes that all street lengths are an
integer number of meters.
Problem 3. Radio 107.9 FM
The Eccentric Motors (EM) corporation is about to roll out their 2005 year car model when they
discover that many of their automobiles have faulty radio tuners that cannot access the highest
frequencies in the FM spectrum. In particular, these defective radios cannot receive radio stations
broadcast at 107.9 FM. Replacing the defective radios would delay the roll out of the 2005 model,
which would cost the company millions of dollars in lost sales. Fortunately, not every city has a
107.9 FM station. Therefore, EM has decided to send the defective cars only to those dealerships
that are not within range of any 107.9 FM station.
Eccentric Motors has given you a list of their dealerships and has asked you to locate the dealerships that are out of range of all 107.9 FM stations. The list is organized so that dealership is
miles east and
miles north of St. Louis, Missouri. (Negative values of
and
denote miles west and south, respectively.) You have contacted the FCC, which has given you the
location of the radio towers that broadcast at 107.9 FM. Tower is located
miles east and
miles north of St. Louis, Missouri, and it broadcasts at a signal strength that allows the signal
to be received within a radius of
miles from the tower. FCC regulations guarantee that no
point is able to receive signals from two different 107.9 FM broadcast towers.
b ¤b
£¢
t ¤ b¢
(U# ¤b
(¢ ¤
g {D
¢# g ¤ b¢
£{# g ¤
7g ¢ D ¤
g hXD
¢ Design an efﬁcient algorithm to locate the EM dealerships that are out of range of all 107.9 FM
broadcast towers.
Solution:
Since the problems involves points and disks (that is, dealerships and tower ranges), it is a computational geometry problem. The most natural geometric technique that can be used for this problem
is, the sweepline approach. This technique was introduced in lecture to ﬁnd segment intersection
using a linesweep approach. This algorithm runs in
time. In this method,
4
the data is swept from left to right (West to East) by a vertical line. The sweep line keeps a data
structure that maintains some information about objects (i.e., disks) that intersect the line. When a
sweepline hits a point, the data structure should be able to answer (quickly) if this point belongs to
one of the disks intersected by the sweep line, or not. This sufﬁces, since if a point
belongs
to a disk , then the sweep line positioned at must intersect .
"
tr§
i
g "1
i
g§§ ©
"0§
¡ ¡ The details are as follows. First, we need to construct a trajectory, or schedule, for the sweepline.
That is, the sequence of important points the sweepline encounters, sorted by the coordinate.
The points are:
¡ D©¤
"g hP7g {0§
¢D
¤
¢#D
¤
7g ¢ D©¤
"r7g h}{7g {0§
¢D
i¤
¢#D The rightmost points of the disks, that is, the points ¤
7g ¢ ¦
¦ 4 The leftmost points of the disks, that is, the points A few people used a horizontal line moving topdown The main effect of this approach was confusing the graders;
as such, this approach should be avoided as much as possible. 6.046J/18.410J Quiz 2 Solutions
t ¤b
£¢ © ¤ b¢
r(# § The dealership positions 8
. ¦ As in lecture and the book, we assume that all coordinates are different. In particular, this implies
that the radiae of the towers are nonzero (and therefore, the leftmost and rightmost points of each
disks do not coincide).
¡ The data structure associated with the sweepline is a search tree data structure . The disks
in the data structure are ordered by their coordinates (deﬁned below). Again, we assume (for
simplicity) that all such coordinates are distinct. The data structure supports insertions and deletion
of elements, as well as search for a predecessor and successor of a given . All three operations
can be performed in
time.
g
o(P7@(1
i
§ 6 43§ . The data structure holds
Denote the sweepline by . If is at position , we denote it by
one coordinate per disk intersecting . Speciﬁcally, it is the coordinate of the center of the disk.
This is done because of the following nice observation. Consider any two disjoint disks and ,
with centers
and
. Consider any “sweepline position” , such that intersects both
and . Then,
if and only if all points in
have coordinates lower than all points
in
.
¡§
0C ¡ C C C E C ¡ E ©E ¡
U¨P¨§ ©
0§
¡ E
C E encountered by the sweepline is handled as follows:
(with a key ) to
from ¦
¦
¦ If is a dealership: , remove If is the rightmost point of a disk , add
C If is the leftmost point of a disk ©
0q
¡§
s E
z2
Given the above, each new point
–Find , centered at
, which is the predecessor of in
–Find
, centered at
, which is the successor of in .
–Check if is contained in either
or
. If yes, report it as “covered”. If not, report it
as “not covered”.
E E ©E ¡
{5P0§ E U¨E P0§
E©E ¡ EE EE E Correctness. Clearly, any point reported as “covered” is indeed covered. We need to show the
converse. That is, if a point
is covered by
, then
must be either a predecessor or a sucessor of . We do it as follows. By symmetry, assume
. For contradiction,
assume there is another disk
with center
where
. Clearly,
does not
contain , since there can be only one such disk. Then, consider the sweep line
(at
position ). From the earlier observation (applied to
and a point ), it follows that all points in
preceed (w.r.t. coordinate). In the same way, we get that all points in
preceed all points in
. But, this implies that all points in
preceed  a
contradiction.
E EE
© E ¡§
0¡s E ©
0¡
¡§
s
nR¥
E
¢kE {¢k{
E
E E {E P¨§
E ©E ¡ ¡§
0£C
C
s EE E EE ¡§
¨
C E E
g
0¥7@(1
i
§ 6 43§
¡§
0
C time. Processing each event takes
events, we get a total time of
.
" ¡§
0
C g
l(753
i
§64 g
zo0P753
i
§64
g
l0"21
i
§§ g
z'0"21
i
§§ ¡ ¡§
0¤
C EE ¡
¨§ E E Running time. Sorting takes
time. Since there are
g
z'021
i
§ Variations. Quite a few people gave the brute force algorithm checking every tower with every
dealership with running time
. A few people observed that the problem becomes easier,
if the disks are replaced by (smallest) squares (or gons, for small ) containing the disks. In
gQ
¦¥021
§ ! ! 6.046J/18.410J Quiz 2 Solutions 9 particular, for the case of squares, the interval formed by intersecting
with a square is always
the same, i.e., does not depend on ; thus, one can use interval trees to maintain the intersections.
This introduces false “coverings”  a point can belong to the square but not to the disk. The
solutions to this problem can be classiﬁed into the following categories:
¡§
0C ¡ ¦ Claiming that a square (or a gon) is a “good enough” approximation of a disk. This might
be true in some cases, but is not true in general. 5 In any case, the problem statement assumed
exact solution.
¦ Whenever a square covers a point , explicitely checking if is covered by the disk
.
This gives a correct algorithm. However, its running time could be as big as
. This can
happen since the fact that two disks do not intersect, does NOT imply that their squares do
not intersect. See example in the ﬁgure ?? ! ¨
¡ § § )
©0BA
§ Other variations included using grids, buckets or hashing. These approaches have the same problems as before: approximation or potentially large running time, which depends on the spread of
the points  the ratio of largest to smallest distances. A few people gave an efﬁcient, and exact
divide and conquer algorithm.
Problem 4. Isolating Kryptonite
Professor Luthor has obtained a meteorite fragment that contains high levels of a useful Kryptonite
isotope. The fragment is a perfect cube centimeters on each side. The professor would like to
5 However, this approach sometimes leads to very efﬁcient approximation algorithms. E.g., see slide 6, Lecture 20
in http://theory.csail.mit.edu/ indyk/6.838/ .
ª 6.046J/18.410J Quiz 2 Solutions 10 distribute this fragment to secret laboratories for study in the hopes that the Kryptonite isotope
can be synthesized in large quantities. The Kryptonite is not evenly distributed throughout the
meteorite, however. The density of the Kryptonite at coordinate
is given by
, that
is,
gives the amount of isotope in a cubic centimeter with origin
.
g© g©b
`7%"0§ g© g©b
s7%(§ g© g©b
`7%"0§ g© g©b
s7%"0§ Professor Luthor has obtained access to a machine that will cut across the meteorite fragment at a
given position, thereby dividing it into exactly two pieces. The machine only cuts at right angles
(parallel to the , , or axis) and on centimeter boundaries. Thus, each cut results in two
rectangular parallelepipeds with integral dimensions.
« ¡ Design an efﬁcient algorithm for dividing the meteorite into pieces so as to maximize the minimum amount of Kryptonite contained in any piece. (Partial credit will be given for solving the
analogous twodimensional problem.)
Clariﬁcation: The machine can only be used to cut a single piece into two smaller pieces. That
is, after cutting the meteorite into two pieces, you must separate the pieces; you can then perform
different cuts on each piece. Also, you cannot glue pieces back together.
Solution:
This problem can be solved using dynamic programming in
time. The idea is to solve
subproblems of the following form: given a rectangular parallelepiped subsection (or “piece”)
of the meteorite, how can it be divided into smaller pieces so as to maximize the minimum
amount of Kryptonite in any piece? We demonstrate that the problem exhibits optimal substructure,
allowing each of
subproblems to be solved in
time. We also give special attention
to the base case (
), yielding an
algorithm for calculating the amount of Kryptonite in a
given piece as a function of the amounts in smaller pieces.
¬
) §
(1 g 021
§
021
§ e§
$1 e
®g
s Many students developed a greedy algorithm for this problem. A greedy approach does not result
in the optimal series of cuts; refer to the end of this discussion for counterexamples and grading
guidelines.
Terminology
A piece
is a rectangular parallelepiped subsection of the meteorite with
and
, where
,
opposite corners at coordinates
, and
. All coordinates are integers.
U¢
I
¯ ¡
q¯
I¡
«
© ) «
`© )
"© ) ¡
¨§ )
"© ¡© I «© I © I ¡§
"s`sUs0w2
s ) ) I«©I ©I
`{"0§
¡ ) «
knt
I«
) ) Optimal Substructure
Consider the problem of dividing a piece into smaller pieces. Because each cut of the machine
divides a piece into exactly two smaller pieces, the optimal solution must start by cutting the piece
into two pieces and (unless
, in which case the problem is trivial). Consider that the
optimal solution further divides into pieces and into
pieces. Let denote the problem
of dividing into pieces,
denote the subproblem of dividing into pieces, and
denote
the subproblem of dividing into
pieces.
g e
e g
s I
) g
'g
g I
) g I
I
F ) g
g
) g 6.046J/18.410J Quiz 2 Solutions 11 The optimal substructure is as follows: the value of the overall solution for can never be improved by substituting a nonoptimal solution for subproblem
or
(or both). The proof is
by contradiction. Suppose that the overall solution for did improve after changing the solution
of
from O PT
(the optimal solution) to N ON O PT
(a nonoptimal solution). In other
words, the minimum amount of Kryptonite over all pieces in has increased; since we did not
change the solution of , the minimum piece in N ON O PT
must be larger than the minimum
piece in O PT
. This contradicts the optimality of O PT
. The same argument applies to .
For the case in which both
and
are nonoptimal, one can change the solution to
and then
the solution to ; neither change improves the overall solution.
I
° ) I
G(§
I
§
I
I
F§
) I
G§
I
§
) I
° I
F ) ) Note that this argument is somewhat tricky because it is not the case that both subproblems must
be solved optimally in the overall optimal solution. In particular, the subproblem that does not
contain the piece with the minimum amount of Kryptonite can adopt a nonoptimal solution, so
long as the overall minimum is not affected. Nonetheless, the optimal substructure property shown
above is enough to guarantee the correctness of our algorithm, as it shows that optimal solutions
to subproblems will always result in an optimal overall solution (i.e., one of the solutions that
maximizes the minimum amount of Kryptonite in the pieces).
Dynamic Programming Formulation
The optimal substructure exposes the following subproblems: for every piece in the meteorite,
and for every
, what is the best way to divide into pieces? The solution is
comprised of optimal subsolutions once we determine two things: 1) the best position to cut
into two pieces, and 2) the best allocation of the desired pieces between the two sides of the cut.
We determine these values by exhaustive search: for a given subproblem, try every cut position
and every allocation of pieces to the two sides, keeping track of the conﬁguration that maximizes
the minimum value of Kryptonite.
g f
²SSS{s7U±g
©QQQ© j©ed
g Let
denote the maximum value of the minimumKryptonite piece across all ways of dividing into pieces. Expanding
, we observe that the overall
answer to the problem will be given by
. Each subproblem can be evaluated
¤g©
{s t³
¢ ¤g
{`© ) «
`© )
© ¡©I«©I ©I
"`{¡ tp{s t³
¢
³
s ¤g©
¢ ) ¤
2¥"¥µ´`´´³
©
©
©
© © © ¢ g 6.046J/18.410J Quiz 2 Solutions 12 as follows:
¶·
·
·
·
© ) ¡©I«©I ©I ¡
"`{¨§ ) S UM K RYPTONITE «
`© : e
®og
s ·
·
· if
·
·
·
· )
·
·
·
·
· : º
»
I«
± «
§
I
{± )
(§ I¡
¤ ) ¡§
¨~2g
u ·
· if
·
·
·
·
· )
·
·
·
·
·
·
· otherwise:
·
·
·
· ¶·
·
·
·
·
·
·
· ¶
¤g
7%© «
© ©©I«©I ©I
h`ss{¡ t³
¢ ) ) ¤g
7s©
g «
©
© ) ·
·
·
· ¸
¹ ·
ÉÈ
5o¼ ·
·
·
· ¿½
p¾¼ ·
·
·
·
·
· # À Å
Á ÄI ÃÆ ÂI
¨¥Ç# %R ¡ © I « © I © ¢
s`sUpt³ ) ·
·
·
·
·
¸ )
·
·
· ·
· s¤g
R{`©
· ·
·
· ·
·
·
·
·
«©
`h`©
) ¡ © I « © I © I ¡¢
`s{"£³
) ¤g
7s©
g «
`©
© ) ÉÈ
5o¼
ÄI
¨¥Ã Æ ) ¡ © I « © © I ¡¢
`p£³ ¿½
p¾¼
ÂI
%R ·
·
·
¸
·
· ¿½
p¾¼
Á
À Å · ·
·
· · · ·
·
·
· ÄI Ã Â Á À
¨¥Y %I `i ) ·
·
· ·
·
·
·
· ·
·
· ·
· · ·
·
·
·
·
·
·
·
· ¶
©¤ g©
Ëp`©
© ¡ © I « © I © I ¡¢
"``s{"£³ )
¤g
s©
g ) «
`©
)
"©
) ·
¹ ·
·
·
·
· ¸
·
ÉÈ
@¾¼ ·
·
·
· ¿½
h¼
Ä¨I¥Ã Æ Ê Â%IR Ê ÁÀ Å ¡ © © I © I ¡¢
"h`s{"£³
) ¡©I«©I ©I
"s``{¡ t³
¢
) · ¸
¹ ) ·
· ¶
©¤ g
7%©
© ·
· «
`©
) ·
·
·
·
·
·
·
·
·
·
·¹
·
·
·
·
·¹ The above equation has three cases. In the ﬁrst case,
and we sum the Kryptonite contained
in the piece; we use a special procedure to do the sum efﬁciently (see below). In the second case,
it is impossible to obtain the desired number of pieces because the volume of the parallelepiped is
too small. Thus, we return negative inﬁnity to ensure that this conﬁguration will never be selected
as the optimal (largest) value.
e
w g
s In the third case, we evaluate all possible cuts that could be performed on the parallelepiped. The
variable ranges over possible allocations of pieces between one side of the cut and the other. The
variable ranges over possible locations of the cut on the , , and axis. For each cut, we
evaluate the minimum amount of Kryptonite in the two resulting pieces. We select the value where
this minimum is maximized.
g « ¡ The S UM K RYPTONITE procedure calculates the total amount of Kryptonite contained in the parallelepiped. Instead of summing over all locations (which would require
time), it sums over
two smaller parallelepipeds (which requires only
time if the subproblems have already been
solved).
Ì
©021
§ e§
Ç21 6.046J/18.410J Quiz 2 Solutions 13
«
`©
"© )
¤e
S© «
`©
© )
¤e
S© «
`© )
© ) ) «
`©
) ¡
"© ) ) «
`©
) © I ¡¢
"£»{
©
³
i¤e
)
© ¡¢
£»{S©
³
i¤e
) ¡©I«
"``© ) ¤e
S© ¡©I«©I
``U©
) ©e
7
) )
"©
) e
¡
vs
Í
¡
I ) ¡ © I « © I © I ¡¢
"`{£³ e
vs
Í
9{
I )
©
) ¡©I«©I ©I ¡
"s`sU¨§ ) ¡ © I « © I © I ¡¢
"``{£³ ) «
`©
) ) ) «©e
`7
) « © I © I ¡¢
`s{"£»US7
³
i¤e©e S UM K RYPTONITE
1 if
then return
2 elseif
then return
3 elseif
then return
4 else return ¡ © I « © I © I ¡¢
"s``{£³
) e
) «
us
Í
9«
I ¤I «©I ©I ¡
Ç`{£¢ The S UM K RYPTONITE procedure ﬁrst tries to split the piece along the axis. However, if the
piece is only 1 cm wide, then it tries to split along the axis, and subsequently the axis. If the
piece is a single cubic centimeter, then it returns the density as given by .
¡ « Order of Evaluation
A bottomup dynamic programming algorithm solves subproblems in a speciﬁc order to guarantee
that solutions are ready before they are needed by other subproblems. In our case, each subproblem corresponds to an element of , and we solve it by evaluating the equation for given
previously. There are several legal orderings. We consider pieces in order of their size; for example,
pieces, then all
pieces, then all
pieces, and so
we ﬁrst evaluate all
on. Our toplevel algorithm is as follows:
³ ³ Ï
¤}e
Î
e
Î j
y}e
Î
e
Î e
±»e
Î
e
Î
F IND C UTS
1 Initialize all entries of to
2 for
to
3
do for
to
4
do for
to
5
do for
to
6
do for
to
7
do for
to
8
do for
to
9
do evaluate
©
¥0§
© º
d ³ e e e ¡
±
Ð
t} Ð
«
'} Ð
e
'tÐ
'Ð
«
rÐ
¡ ¡
'
«
og ¤g© «
Us´'`tB»t"w`R"£³
Ð
i
«©
Ð
i
©
¡
Ð
i
¡© «© ©
¡¢ This iteration order is correct because, in S UM K RYPTONITE and the equation for , a given piece
is evaluated only in terms of strictly smaller pieces, i.e., in terms of pieces which have lesser or
equal width, height, and depth. Because the loops on
,
, and
correspond to the width,
height, and depth, respectively, they impose a lexicographic ordering and guarantee that strictly
smaller pieces are evaluated before strictly larger pieces.
³ «
'Ð
tÐ ¡
Ð The iteration order, in combination with the optimal substructure presented previously, forms the
basis of the algorithm’s correctness. Every piece is evaluated for the best cut and the global optimum is selected.
Constructing the Cuts
. To reconstruct the actual seThe value of the optimal solution is given by
quence of cuts corresponding to this solution, one can maintain an auxiliary array
(with the
¤
2"¥µ¥w`´`´t³
©
©
©
© © © ¢ 6.046J/18.410J Quiz 2 Solutions 14 same dimensions as ) that tracks the values of and corresponding to the maximum for a given
subproblem. One can trace back through the matrix in the standard fashion to construct the
cuts.
g ³ Runtime Analysis
There are
distinct subproblems, which is evident from the dimensions of (as well as
the nested loops in F IND C UTS ). For each subproblem, we have to evaluate an element of . In
the base case, this evaluation is
, as S UM K RYPTONITE performs a constant amount of work.
³
021
§ ³ e§
Ç21 However, the general case requires more work. First, we vary the allocation of cuts between pieces
; this requires
iterations. On each iteration, we evaluate all possible cut
from to
positions on the , , and axis. Since there are three dimensions with at most positions on
each, we consider
cuts per allocation of pieces. Thus, the total amount of time to
evaluate a subproblem is
.
§
(21 e
tÑ
« 02qs
§
1 e ¡ 1
Ï§ (1
§ §
(1 to solve, so the total runtime is ¬
) (21
§ subproblems and each requires
X (1
§ There are A Faster Algorithm!
A few students achieved a runtime of
by improving the search for the best cut position. They observed that the maximum value is a “mountain function” with respect to the position
of the cut: if moving the cut in one direction worsens the score, then moving it further in that
direction is fruitless, as the score will only continue to go downhill.
@R )
63 §
(1 Thus, rather than evaluating
cut positions for a given subproblem, one can do a binary
search on intervals of cuts and reduce the runtime to
. Four points within each interval are
sampled; if all scores are distinct, then three of these points (including an endpoint) must form
a monotonic slope and the endpoint can be eliminated. The algorithm recurses on the interval
spanned by the remaining three points, maintaining the invariant that the optimal score (the “peak”
of the mountain) is always contained within the current interval. The runtime of the search is
; by the Master Theorem,
.
governed by the recurrence
(21
§ 85021
63§ 8@(us
63§
1 0D
§ e§
$2»i
1 ÓÒ
s
Ï§
D (D
§ However, it appears that this algorithm requires the scores to be distinct. For instance, if all scores
except for one are equal, then the algorithm must resort to exhaustive search. Nonetheless, there
are many cases in which this algorithm would perform better than the one detailed previously.
Greedy Approaches
More than onequarter of the class took a greedy approach to this problem. A common solution
was a topdown algorithm that ﬁnds the most Kryptoniteheavy piece and cuts it into two pieces
so as to minimize the difference in Kryptonite between sides of the cut. While this algorithm
is efﬁcient and might serve as a good heuristic, it does not guarantee an optimal division of the
meteorite. A counterexample is shown in Figure 1.
About 5 students suggested a bottomup greedy algorithm, in which each cubic centimeter of the
meteorite is initially considered as a distinct piece. At every step of the algorithm, the piece with
the minimum amount of Kryptonite is fused with its lowestKryptonite neighbor (assuming some . 6.046J/18.410J Quiz 2 Solutions m=3 greedy (topdown)
max of min = 1 2
1
1
2
(a) 2
1
1
2 2
1
1
2 15 optimal
max of min = 2 2
1
1
2 2
1
1
2 2
1
1
2 2
1
1
2 Figure 1: Counterexample for topdown greedy
strategy. Given the input in (a) with m=3, the
greedy algorithm produces the solution in (b)
while the optimal solution is shown in (c). 2
1
1
2
(b ) (a) (c) (b ) greedy (bottomup)
max of min = 2 m=2 2
1
1
2 optimal
max of min = 3
2
1
1
2
(c) Figure 2: Counterexample for bottomup
greedy strategy. Given the input in (a) with
m=2, the greedy algorithm produces the solution in (b) while the optimal solution is shown
in (c). geometrical constraints are satisﬁed). A counterexample illustrating the deﬁciency of this strategy
appears in Figure 2.
Other variants on greedy algorithms included 1) searching for a cut where the amount of Kryptonite
on a side can be expressed as
, where is an integer, 2) keeping the cut points constant but
varying which piece is recursively cut, and 3) trying to shave off
of the meteorite with each
cut. All of these strategies have similar failure cases. In all, a greedy strategy fails because it does
not consider all of the possibilities. A local choice might separate (resp. combine) two pieces that
need to be combined (resp. separated) for a cascade of cuts to offer further beneﬁts on the other
side of the meteorite.
g Ò
g Ò
phe Grading Notes
There were several bruteforce solutions that were correct but ran in exponential time. They received at most 7 points. A smaller number of solutions were on the right track algorithmically but
the analysis showed exponential runtime. These received at most 12 points.
Many solutions offered a greedy algorithm, which received at most 10 points. Extra points were
awarded for acknowledging that greedy is suboptimal, or for using simple dynamic programming
to keep track of partial sums. Solutions with incomplete or incorrect runtime analysis received
lower scores.
Dynamic programming algorithms that were buggy or incomplete received at most 18 points. More
than a quarter of the class designed the same algorithm as described here, and were generally
awarded 1925 points.
Several solutions neglected to factor in the cost of solving the subproblems; this resulted in a score
around 21 points. Also, many students used a bruteforce algorithm to sum the total Kryptonite
content in a given parallelepiped, thereby leading to an
runtime bottleneck that was usually
unaccounted for. Solutions missing an analysis of the base case received at most 23 points.
Ô
(21
§ 6.046J/18.410J Quiz 2 Solutions 16 Problem 5. Finding Coolmail Users
Macrohard Corporation has decided to start a free email service known as Coolmail. Coolmail users choose their favorite digit number as their user ID and get the email address
@coolmail.com. For instance, with
a user might chose 314159265 as her user ID and
be given the address 314159265@coolmail.com. Since many people ﬁnd it hard to remember digit numbers, Coolmail provides the following helpful service. Whenever an email is sent
to an address @coolmail.com, where is not a valid user ID, Coolmail ﬁnds a nearest valid
user ID , that is, an that minimizes
, and sends the reply, “Sorry, but @coolmail.com
is not a valid address. Did you mean to type @coolmail.com?”
¡ g Õ
tg
s ¡ g Ö
©d¡ Ö
¡ ¡ ¡ It has come to the attention of the opportunistic Professor Ralsky that many Coolmail users are
paying too much interest on their mortgages. He would therefore like to send every Coolmail user
a message informing him or her that “You can ref1nance your m0rtgage at the amazing rate of only
3.49%!!” Unfortunately, the professor does not know the email addresses of all Coolmail users.
Let
be the user ID’s of the Coolmail users, where is unknown to the
professor. He has hired you as an MIT student intern to design an efﬁcient algorithm to compute,
with the help of Coolmail’s automated reply service, the email addresses of all the Coolmail users.
Minimize the worstcase number of emails required by your algorithm.
Y e
Wp'¥»WWp°nt
Ø¡
×××
I¡
Solution:
We will assume that
; the helpful email service doesn’t make sense if there aren’t any users.
We also assume that helpful email responses from Coolmail arrive in a bounded amount of time;
otherwise we’ll never know if an email that we’ve sent was delivered successfully.
e
~
c In the interest of clarity, we are not going to worry about constant factors in the number of emails
sent. (We’re even going to actually send more than one email to the same address.) The idea of our
solution is to maintain a set of nonempty regions of identity space that are guaranteed to contain
at least one user. Given a nonempty region, we’ll query the region by sending an email to the
median(s) to ﬁnd a new user, and then update the set of regions on the basis of the query result.
This will result in an algorithm that computes a list of all valid addresses in
time, and sends
emails.
('&
§ ('&
§ Let NEAREST
be a function that returns a nearest valid address to , for a halfintegral value
(that is,
). If is an integer, then the function emails , waits long enough for a helpful
response to be returned, and then returns if the email was delivered successfully, and if the
response “did you mean @coolmail?” was received. (Note that if there is a tie—i.e., there are
two values and that are equidistant from —then NEAREST
will break the tie arbitrarily.) If
is nonintegral, then the function returns whichever of NEAREST
and NEAREST
is closer to , or one arbitrarily if there is a tie.
¡ ¡ ¡
¨§ ¡ ¡ ¡ ¡
0§ ÚQ
¦´ G0§
i
¡ Ù
upj
¡ ¡ ÚQ
Ûw¨§
¡ ¡ ¡ Let NONEMPTY ?
be a boolean function taking two values
that returns true if and only if
there is some valid address such that
. This function does the following: on input
,
NEAREST
. Return true if and only if
. Correctness of NONEMPTY ? is
let
immediate by inspection.
V
H
V©
`¥H V© H
`¥§ V
n²¾H
¡
¤ V © H¢
`¥}2
¡ j
7Ò V
±§
i
H§ sÜ
¦2 6.046J/18.410J Quiz 2 Solutions 17
be a function that, given two indices and and a stack
.
H §©V© H
sW`¥§ V© H
P(§ § ¤V© H
`¥¢ § V := empty list.
R := empty stack; push (0,10ˆk1) onto R. V Finally, let PUSH  IF  NON  EMPTY
, pushes
onto if NONEMPTY ? // V will hold all valid addresses.
// R holds nonempty regions. while (R is nonempty)
{
[a,b] := pop(R);
median := (a+b)/2;
valid := NEAREST(median);
add valid to V.
if valid < median
pushifnonempty(a,valid1,R)
pushifnonempty(2*medianvalid,b,R)
elseif valid == median
pushifnonempty(a,median1,R)
pushifnonempty(median+1,b,R)
else (valid > median)
pushifnonempty(a,2*medianvalid,R)
pushifnonempty(valid+1,b,R)
}
return V. Correctness. We ﬁrst note that, for a nonempty region
, the value of NEAREST
must be a valid email address in
. This ensures that every range
that we attempt to push
onto the stack has
.
j
7Ò V
9("§
i
H§ ¤V© H
`P¢ ¤V© H
`¥¢ ¤V© H
`¥¢ V
nH
We claim the following loop invariant: all valid addresses are contained either in or in a region
in , all regions in are disjoint from each other and from , and no element appears twice in
. This invariant is satisﬁed at initialization by assumption. We now show that the invariant is
maintained by an iteration of the loop. First note that in the case when
, the
invariant is trivially maintained. For the other cases, we prove maintenance for
;
the ﬁnal case is analogous. Observe that in this case the effect on
is (i) possibly to delete
regions that are empty (according to PUSH  IF  NON  EMPTY , which does not falsify the invariant
by the correctness of NONEMPTY ?), and (ii) to delete the range
.
But (ii) cannot falsify the invariant either: since
is the closest valid address to
, there
cannot be any valid addresses closer to
than
. Note that the resulting regions are
disjoint subregions of
, and are also disjoint from
. This fact implies that the invariant is
maintained.
Ý æ ßá âå
("äl(ypÞ
ã
s
s
âáà ß æ ßá âå
("¯¤(ypÞ
ã
âáà ß
ynÝ
ç e
GËb
m Hè
w ("s`7¤F(yp$§
æ ßá âå
ã×
j©e
i
âáà ß Þ æ ßá âå
("ã âáà ß
(ypÞ âáà ß
(ypÞ æ ßá âå
("ã âáà ß
(ypÞ ¤V© H
`¥¢ For termination, note that the number of addresses covered by is decreasing by at least one in
every iteration. Thus the loop eventually terminates. When it does, is empty, and by the above
loop invariant, we have that contains all valid email addresses. Furthermore, since we only add
Ý Ý 6.046J/18.410J Quiz 2 Solutions 18
an address returned by NEAREST , the resulting contains only valid email addresses. Since all
elements of are unique, the list contains exactly the valid addresses.
§ Running time. First, observe that NEAREST is the only place where our algorithm sends email. Now, note than an iteration of the loop makes one call to NEAREST and two to PUSH  IF  NON  EMPTY
(and thus two to NONEMPTY ?, and thus two more to NEAREST ). Thus each iteration of the loop
causes three calls to NEAREST , each of which may result in two emails being sent. Thus there are
at most six emails sent per loop iteration.
Since in each iteration, we ﬁnd exactly one a new address to add to , there are precisely
iterations. Thus the total number of emails sent is
. Furthermore, since everything in the loop
is a contanttime operation, the total running time is
as well.
('&
§ 0'&
§ Grading notes. The general breakdown of grading was that about half of the credit was given for
getting a correct algorithm that ran in
time, and half of the credit was given for correctness
proofs and runningtime analysis.
0'&
§ A lot of people came up with the correct
time algorithms (similar to the one above), but
people had signiﬁcantly more trouble with rigorous proofs of correctness and running time. For
example, in the divideandconquer recursive version of the above algorithm, you make recursive
calls on two subintervals of size at most half of the size of the original interval. Since
, people said that they has an algorithm linear in the number of users. This is
totally wrong: the true recurrence is in terms of the size of the interval, not in terms of the number
time running time.
of users. If done correctly, this analysis yields a
('&
§ 0D
§ (2udi
§
1
s
e Y jÒ
7(pj
§
D e§
W$1 Scores for the runningtime analysis (6.5 points) range from zero for no attempt whatsoever, to
about 2–3 points for a handwavy solution that asserted facts about why the algorithm was efﬁcient
but didn’t prove any of them, through 6 points for a rigorous, correct proof.
Scores for correctness (6.5 points) were similar. Almost all submitted solutions required a rigorous
inductive proof, and very few gave one. Signiﬁcant credit (about four points, depending on the
details provided) was given for people who argued for correctness without using induction. ...
View
Full
Document
 Spring '08
 ErikDemaine
 Algorithms

Click to edit the document details