Diagnostic Quiz 0: Introduction To Algorithms
Diagnostic Quiz 0: Introduction To Algorithms
Diagnostic Quiz 0
Read these instructions carefully.
The purpose of this diagnostic quiz is twofold: first, to measure the extent to which the
prerequisite courses have covered material relevant to the course; second, in conjunction
with a similar quiz planned for the end of the semester, to measure how well this course
meets its objectives as specified in Handout 5. The purpose is NOT to evaluate you, and
your term grade will not be affected.
You must complete this diagnostic and turn it in at your first recitation. If you do not do so,
you will be considered UNREGISTERED in the course.
Please read each question and make sure you understand it. After that, answer off the top
of your head. Don’t think hard so as to solve a problem cleverly, or chase references, etc.
If you are unsure of the answer for any question, simply answer, “Don’t know,” rather than
guessing. Write all your answers in the spaces provided.
Please do not spend more than ONE HOUR completing this diagnostic. It is not intended to
measure your intelligence or ability, and it will lose its value as a diagnostic if you spend too
much time on it. Just do the best you can in an hour.
Name:
2 Handout 6: Diagnostic Quiz 0
Problem 1
Consider the following pseudocode:
ROUTINE
1 if
2 then return
3 else return ROUTINE
(a) Give a one-sentence description of what ROUTINE does. (Remember, don’t guess.)
Problem 2
Give a short (1–2-sentence) description of each of the following data structures:
Problem 3
Using -notation, describe the worst-case running time of the best algorithm that you know for
each of the following:
Problem 4
Describe an algorithm that locates the first occurrence of the largest element in a finite list of
integers, where the integers are not necessarily distinct. What is the worst-case running time of
your algorithm?
Problem 5
How does the height of a balanced binary search tree relate to the number of nodes in the tree?
Problem 6
Does an undirected graph with vertices, each of degree , exist? If so, draw such a graph. If not,
explain why no such graph exists.
Handout 6: Diagnostic Quiz 0 5
Problem 7
It is known that if a solution to Problem A exists, then a solution to Problem B exists also.
(a) Professor Goldbach has just produced a 1,000-page proof that Problem A is unsolvable. If
his proof turns out to be valid, can we conclude that Problem B is also unsolvable? Answer yes or
no (or don’t know).
(b) Professor Wiles has just produced a 10,000-page proof that Problem B is unsolvable. If the
proof turns out to be valid, can we conclude that problem A is unsolvable as well? Answer yes or
no (or don’t know).
Problem 8
Consider the following statement:
If points are placed anywhere on or inside a unit square, then there must exist two
that are no more than units apart.
Proof (a): Place of the points on the vertices of the square; that way they are maximally sepa-
rated from one another. The th point must then lie within units of one of the other
points, since the furthest from the corners it can be is the center, which is exactly units
from each of the four corners.
Proof (b): Partition the square into squares, each with a side of unit. If any two points are
on or inside one of these smaller squares, the distance between these two points will be at
most units. Since there are points and only squares, at least two points must fall on
or inside one of the smaller squares, giving a set of points that are no more than apart.
Which of the proofs are correct: (a), (b), both, or neither (or don’t know)?
6 Handout 6: Diagnostic Quiz 0
Problem 9
Give an inductive proof of the following statement:
For every natural number , we have ! .
Problem 10
$#
We want to line up " out of children. Which of the following expresses the number of possible
line-ups? (Circle the right answer.)
$#%
(a) "
$#%
(b) &
(c) ')(+-
* ,
(d) ' (. * ,0/ "
Problem 11
A deck of cards is shuffled thoroughly. What is the probability that the aces are all next to
each other? (Circle the right answer.)
(a) 21
(b)
(c)
(d) 23
Problem 12
The weather forecaster says that the probability of rain on Saturday is 4 and that the probability
of rain on Sunday is 4 . Consider the following statement:
#
The probability of rain during the weekend is 4 .
(a) If the two events (rain on Sat/rain on Sun) are independent, then we can add up the two
probabilities, and the statement is true. Without independence, we can’t tell.
Problem 13
A player throws darts at a target. On each trial, independently of the other trials, he hits the bull’s-
eye with probability & . How many times should he throw so that his probability is 54 of hitting
the bull’s-eye at least once?
(a) 3
(b) 4
(c) 5
Problem 14
Let 6 be an indicator random variable. Which of the following statements are true? (Circle all
that apply.)
<#%=> ?=>
(a) 798;:$6 798;:$6
=>A@CB
(b) 798;:$6 6ED
@CB F@EB
(c) 6ED 6HGID
@CB J@CB
(d) 6ED 6ED G
Introduction to Algorithms
Massachusetts Institute of Technology 6.046J/18.410J
Singapore-MIT Alliance SMA5503
Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Handout 10
Problem 1
Consider the following pseudocode:
ROUTINE(n)
1 if n = 1
2 then return 1
3 else return n + ROUTINE(n − 1)
(a) Give a one-sentence description of what ROUTINE(n) does. (Remember, don’t guess.)
Solution: The value n must be greater than 0; otherwise, the routine loops forever.
Problem 2
Give a short (1–2-sentence) description of each of the following data structures:
Solution: A dynamic set where the element removed is always the one that has been in the set
for the longest time.
Solution: A dynamic set where each element has an associated priority value. The element
removed is the element with the highest (or lowest) priority.
2 Handout 10: Diagnostic Test Solutions
Solution: A dynamic set where the location of an element is computed using a function of the
element’s key.
Problem 3
Using Θ-notation, describe the worst-case running time of the best algorithm that you know for
each of the following:
Solution: Θ (log n)
Solution: Θ (n)
Solution: Θ (n)
Solution: Θ (1)
Problem 4
Describe an algorithm that locates the first occurrence of the largest element in a finite list of
integers, where the integers are not necessarily distinct. What is the worst-case running time of
your algorithm?
Solution: Idea is as follows: go through list, keeping track of the largest element found so far
and its index. Update whenever necessary. Running time is Θ (n).
Problem 5
How does the height h of a balanced binary search tree relate to the number of nodes n in the tree?
Solution: h = O(lg n)
Handout 10: Diagnostic Test Solutions 3
Problem 6
Does an undirected graph with 5 vertices, each of degree 3, exist? If so, draw such a graph. If not,
explain why no such graph exists.
Solution: No such graph exists by the Handshaking Lemma. Every edge adds 2 to the sum of
the degrees. Consequently, the sum of the degrees must be even.
Problem 7
It is known that if a solution to Problem A exists, then a solution to Problem B exists also.
(a) Professor Goldbach has just produced a 1,000-page proof that Problem A is unsolvable. If
his proof turns out to be valid, can we conclude that Problem B is also unsolvable? Answer yes or
no (or don’t know).
Solution: No
(b) Professor Wiles has just produced a 10,000-page proof that Problem B is unsolvable. If the
proof turns out to be valid, can we conclude that problem A is unsolvable as well? Answer yes or
no (or don’t know).
Solution: Yes
Problem 8
Consider the following statement:
If 5 points are placed √
anywhere on or inside a unit square, then there must exist two
that are no more than 2/2 units apart.
Here are two attempts to prove this statement.
Proof (a): Place 4 of the points on the vertices of the square; that√way they are maximally sepa-
rated from one another. The 5th point must then lie within 2/2 units of one of √ the other
points, since the furthest from the corners it can be is the center, which is exactly 2/2 units
from each of the four corners.
Proof (b): Partition the square into 4 squares, each with a side of 1/2 unit. If any two points are
on or √inside one of these smaller squares, the distance between these two points will be at
most 2/2 units. Since there are 5 points and only 4 squares, at least two points √must fall on
or inside one of the smaller squares, giving a set of points that are no more than 2/2 apart.
Which of the proofs are correct: (a), (b), both, or neither (or don’t know)?
4 Handout 10: Diagnostic Test Solutions
Problem 9
Give an inductive proof of the following statement:
Problem 10
We want to line up 6 out of 10 children. Which of the following expresses the number of possible
line-ups? (Circle the right answer.)
(a) 10!/6!
(b) 10!/4!
10
(c) 6
10
(d) 4
· 6!
(e) None of the above
(f) Don’t know
Problem 11
A deck of 52 cards is shuffled thoroughly. What is the probability that the 4 aces are all next to
each other? (Circle the right answer.)
(a) 4!49!/52!
(b) 1/52!
(c) 4!/52!
(d) 4!48!/52!
(e) None of the above
(f) Don’t know
Handout 10: Diagnostic Test Solutions 5
Solution: (a)
Problem 12
The weather forecaster says that the probability of rain on Saturday is 25% and that the probability
of rain on Sunday is 25%. Consider the following statement:
(a) If the two events (rain on Sat/rain on Sun) are independent, then we can add up the two
probabilities, and the statement is true. Without independence, we can’t tell.
(c) If the events are independent, the statement is false, because the the probability of no rain
during the weekend is 9/16. If they are not independent, we can’t tell.
Solution: (c)
Problem 13
A player throws darts at a target. On each trial, independently of the other trials, he hits the bull’s-
eye with probability 1/4. How many times should he throw so that his probability is 75% of hitting
the bull’s-eye at least once?
(a) 3
(b) 4
(c) 5
Solution: (c), assuming that we want the probability to be ≥ 0.75, not necessarily exactly 0.75.
6 Handout 10: Diagnostic Test Solutions
Problem 14
Let X be an indicator random variable. Which of the following statements are true? (Circle all
that apply.)
(a) Pr {X = 0} = Pr {X = 1} = 1/2
(b) Pr {X = 1} = E [X]
(c) E [X] = E [X 2 ]
True-false questions
(1) T F The best case running time for I NSERTION S ORT to sort an n element array is O(n).
(2) T F By the master theorem, the solution to the recurrence T (n) = 3T (n/3) + log n is
T (n) = Θ(n log n).
(3) T F Given any binary tree, we can print its elements in sorted order in O(n) time by per-
forming an inorder tree walk.
(4) T F Computing the median of n elements takes Ω(n log n) time for any algorithm working
in the comparison-based model.
(5) T F Every binary search tree on n nodes has height O(log n).
(6) T F Given a graph G = (V, E) with cost on edges and a set S ⊆ V , let (u, v) be an edge
such that (u, v) is the minimum cost edge between any vertex in S and any vertex in V − S.
Then, the minimum spanning tree of G must include the edge (u, v). (You may assume the
costs on all edges are distinct, if needed.)
(8) T F There exists a data structure to maintain a dynamic set with operations Insert(x,S),
Delete(x,S), and Member?(x,S) that has an expected running time of O(1) per operation.
(9) T F The total amortized cost of a sequence of n operations (i.e., the sum over all operations,
of the amortized cost per operation) gives a lower bound on the total actual cost of the
sequence.
(10) T F The figure below describes a flow assignment in a flow network. The notation a/b
describes a units of flow in an edge of capacity b.
True or False: The following flow is a maximal flow.
6.046J/18.410J/SMA5503 Final Exam Review 2
5/5
a - b
3
Q
Q 4/6
Q
2/4
6 Q
Q
Q
s
Q
0/2 Q
Q
s 3/7 1/4 t
3
Q
Q
Q
6/6QQ
? 4/4
/
Q
Q
Qs
Q -
c d
3/3
(11) T F Let G = (V, E) be a weighted graph and let M be a minimum spanning tree of G.
The path in M between any pair of vertices v1 and v2 must be a shortest path in G.
(12) T F n lg n = O(n2 )
(13) T F Let P be a shortest path from some vertex s to some other vertex t in a graph. If the
weight of each edge in the graph is increased by one, P remains a shortest path from s to t.
(14) T F Suppose we are given n intervals (li , ui ) P for i = 1, · · · , n and we would like to find a
set S of non-overlapping intervals maximizing i∈S wi , where wi represents the weight of
interval (li , ui ). Consider the following greedy algorithm. Select (in the set S) the interval,
say (li , ui ) of maximum weight wi , remove all intervals that overlap with (li , ui ) and repeat.
This algorithm always provides an optimum solution to this interval selection problem.
(15) T F Given a set of n elements, one can output in sorted order the k elements following
the median in sorted order in time O(n + k log k).
(19) T F A heap can be constructed from an unordered array of numbers in linear worst-case
time.
(20) T F No adversary can elicit the Θ(n2 ) worst-case running time of randomized quicksort.
(21) T F Radix sort requires an “in place” auxiliary sort in order to work properly.
(23) T F The Bellman-Ford algorithm is not suitable if the input graph has negative-weight
edges.
(24) T F Memoization is the basis for a top-down alternative to the usual bottom-up version
of dynamic programming.
(25) T F Given a weighted, directed graph G = (V, E) with no negative-weight cycles, the
shortest path between every pair of vertices u, v ∈ V can be determined in O(V 3 ) worst-
case time.
(26) T F For hashing an item into a hash table in which collisions are resolved by chaining,
the worst-case time is proportional to the load factor of the table.
(27) T F A red-black tree on 128 keys must have at least 1 red node.
(28) T F The move-to-front heuristic for self-organizing lists runs no more than a constant
factor slower than any other reorganization strategy.
(31) T F Fibonacci heaps can be used to make Dijkstra’s algorithm run in O(E + V lg V ) time
on a graph G = (V, E).
(32) T F The Floyd-Warshall algorithm solves the all-pairs shortest-paths problem using dy-
namic programming.
(33) T F A maximum matching in a bipartite graph can be found using a maximum-flow al-
gorithm.
(34) T F For any directed acyclic graph, there is only one topological ordering of the vertices.
(35) T F If some of the edge weights in a graph are negative, the shortest path from s to t can
be obtained using Dijkstra’s algorithm by first adding a large constant C to each edge weight,
where C is chosen large enough that every resulting edge weight will be nonnegative.
(36) T F If all edge capacities in a graph are integer multiples of 5 then the maximum flow
value is a multiple of 5.
(37) T F For any graph G with edge capacities and vertices s and t, there always exists an
edge such that increasing the capacity on that edge will increase the maximum flow from s
to t in G. (Assume that there is at least one path in the graph from s to t.)
(38) T F Heapsort, quicksort, and mergesort are all asymmptotically optimal, stable comparison-
based sort algorithms.
6.046J/18.410J/SMA5503 Final Exam Review 4
(39) T F If each operation on a data structure runs in O(1) amortized time, then n consecutive
operations run in O(n) time in the worst case.
(40) T F A graph algorithm with Θ(E log V ) running time is asymptotically better than an
algorithm with a Θ(E log E) running time for a connected, undirected graph G(V, E).
(42) T F n integers each of value less than n100 can be sorted in linear time.
(43) T F For any network and any maximal flow on this network there always exists an edge
such that increasing the capacity on that edge will increase the network’s maximal flow.
(44) T F If the depth-first search of a graph G yields no back edges, then the graph G is acyclic.
(45) T F Insertion in a binary search tree is “commutative”. That is, inserting x and then y
into a binary search tree leaves the same tree as inserting y and then x.
(46) T F A heap with n elements can be converted into a binary search tree in O(n) time.
Introduction to Algorithms
Massachusetts Institute of Technology 6.046J/18.410J
Singapore-MIT Alliance SMA5503
Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Final Exam Review
True-false questions
(1) T F The best case running time for I NSERTION S ORT to sort an element array is
.
Answer: True
(2) T F By the master theorem, the solution to the recurrence is
.
Answer: False
(3) T F Given any binary tree, we can print its elements in sorted order in
time by per-
forming an inorder tree walk.
Answer: True. For each node in the tree we will be calling I NORDER - TREE - WALK recur-
sively exactly twice (once for the left subtree and once for the right subtree).
(4) T F Computing the median of elements takes time for any algorithm working
in the comparison-based model.
Answer: False
Answer: False
(6)
(')!#*
T F Given a graph !#"
with cost on edges and a set $&% , let (')!#* be an,edge
+ $.
such that is the minimum cost edge between any vertex in $-'.!/* and
. (You may assume the
any vertex in
Then, the minimum spanning tree of must include the edge
costs on all edges are distinct, if needed.)
Answer: True
(8) T F There exists a data structure to maintain a dynamic set with operations Insert(x,S),
Delete(x,S), and Member?(x,S) that has an expected running time of per operation.
354
(9) T F The total amortized cost of a sequence of operations (i.e., the sum over all operations,
of the amortized cost per operation) gives a lower bound on the total actual cost of the
sequence.
Answer: False. This only gives an upper bound on the actual cost.
(10) T F The figure below describes a flow assignment in a flow network. The notation 02
0
describes units of flow in an edge of capacity . 2
True or False: The following flow is a maximal flow.
?A@ TT 9;:
<>0 = D 8 6 7
EE GE F H BB 2 I I I RS N
QMER E BB I I
E E I I
E Q B B I GJ 8 9L:
98: 4 MR
6V7 I
8 O PB B EGF 687
I I B E E
I
NN I I BC B E E
I I GJ E E E RSMR
98: ?>K @
6W7
8 D <>U =
M
Answer: True. The flow pushes 8 units and the cut vs. X V ! 0 ! WZY X[2 ! U ! L Y has capacity 8. The
cut must be maximum by the Max-Flow Min-cut theorem.
(11) T F Let !\" be a weighted graph*S^ and let*[_ ] be a minimum spanning tree of .
The path in ] between any pair of vertices and must be a shortest path in .
! ! ! !` ! !` !
Answer: False. Consider the graph in which X[0 2 W`Y and the edges are 0 2 2 Wa W 0 .
! !` !
The weight of the edges are 3,3 and 4 respectively. The MST is clearly 0 2 2 Wa . However
the shortest path between 0 and W is of cost 4 not 6 as seen from the MST.
(12) T F b
_
e V L
(13) T F Let be a shortest path from some vertex to some other vertex in a graph. If the
e
weight of each edge in the graph is increased by one, remains a shortest path from to . V L
!#" ! ! WZY " ! !` ! Wa !` ! WfgY
Answer: False Consider the graph where
h 0 ! 2 i 4! h 2 ! fW i 4! h [X 0 0 2 ! aW >Qkj T X 0 2 2 0 .
and
0 W 0 2 W 0 W
Let the weight of the edges be . The shortest path
from to is – – . However if the weights are increased by 1, the new shortest path is – .
lmn!/'om for p 4!rqZqZqs! and we would like to find a
(14) T F Suppose we are given intervals
set $ of (non-overlapping intervals maximizing t
murv h m , where h m represents the weight of
l G
m #
! w
' m . Consider the following greedy algorithm. Select (in the set $ ) the interval,
l
interval
say
x
m #
! w
' m of maximum weight h m , remove all intervals that overlap with lmx!#'wm and repeat.
This algorithm always provides an optimum solution to this interval selection problem.
X 54!r4 P Y
Answer: False. Consider the following 3 intervals (1,10), (2,5) and (6,9) with weights 10, 6
X Q ! T !` N \! y \ Y
and 6. The above greedy strategy would choose the intervals which has total weight
10 while the optimal set of intervals is which has total weight 12.
Answer: True. Find the median and the partition about it to obtain all the elements greater
z
than it. Now find the th largest element in this set and partition about it and obtain all the
costs
{ z z
elements less than it. Output the sorted list of the final set of elements. Clearly, this operation
time.
(16) T F Consider a graph x fu !\ " with a weight h}|~ P definedur for_ every edge " . If
a spanning tree minimizes t | }h | then it also minimizes t | h | , and vice versa.
_
Answer: False. BFS uses a queue.
3 3
_
Answer: True. Merge sort runs in time which is .
(19) T F A heap can be constructed from an unordered array of numbers in linear worst-case
time.
6.046J/18.410J/SMA5503 Final Exam Review 4
_
procedure.
(20) T F No adversary can elicit the worst-case running time of randomized quicksort.
Answer: True. No adversary has control over which random numbers the algorithm will
(21) T F Radix sort requires an “in place” auxiliary sort in order to work properly.
Answer: True
(23) T F The Bellman-Ford algorithm is not suitable if the input graph has negative-weight
edges.
Answer: False. Bellman Ford is used when there are negative weight edges, it’s Dijkstra’s
algorithm that cannot be used.
(24) T F Memoization is the basis for a top-down alternative to the usual bottom-up version
of dynamic programming.
Answer: True
(26) T F For hashing an item into a hash table in which collisions are resolved by chaining,
the worst-case time is proportional to the load factor of the table.
_^
Answer: False. Even with a low load factor (say ) in the worst case you can get all
elements to hash to the same slot
6.046J/18.410J/SMA5503 Final Exam Review 5
Answer: True
(28) T F The move-to-front heuristic for self-organizing lists runs no more than a constant
factor slower than any other reorganization strategy.
R
Answer: True. In recitation we’ve seen that it’s 4-competitive, which means that the best
algorithm that can exist (“God’s algorithm”) could only do better by a factor of .
3("
x!\"
(31) T F Fibonacci heaps can be used to make Dijkstra’s algorithm run in time
on a graph .
Answer: True
(32) T F The Floyd-Warshall algorithm solves the all-pairs shortest-paths problem using dy-
namic programming.
Answer: True
(33) T F A maximum matching in a bipartite graph can be found using a maximum-flow al-
gorithm.
(34) T F For any directed acyclic graph, there is only one topological ordering of the vertices.
Answer: False. Consider a graph with two vertices and no edges. Either order is a topologi-
cal ordering.
(35) T F If some of the edge weights in a graph are negative, the shortest path from to can V L
be obtained using Dijkstra’s algorithm by first adding a large constant to each edge weight,
where is chosen large enough that every resulting edge weight will be nonnegative.
6.046J/18.410J/SMA5503 Final Exam Review 6
V L *
Answer: False. Consider the following figure. The shortest path from to goes through .
But if we add 2 to each edge, so that we make the costs nonnegative, the edge from to V L
becomes a shorter path.
v
-2 -1
s t
-2
(36) T F If all edge capacities in a graph are integer multiples of 5 then the maximum flow
value is a multiple of 5.
Answer: True. Consider the minimum cut. It is made up of edges with capacities that are
multiples of 5, so the capacity of the cut (sum of capacities of edges in the cut) must be a
multiple of 5. By the maxflow-mincut theorem, the maximum flow has the same value.
V L
(37) T F For any graph with edge capacities and vertices and , there always exists an
V
L V L
edge such that increasing the capacity on that edge will increase the maximum flow from
to in . (Assume that there is at least one path in the graph from to .)
Answer: False. There may be more than one minimum cut. For example, consider the
following graph. There is no single edge you can increase to increase the flow.
1 1
s t
(38) T F Heapsort, quicksort, and mergesort are all asymmptotically optimal, stable comparison-
based sort algorithms.
Answer: False
(39)
operations run in
T F If each operation on a data structure runs in O(1) amortized time, then
time in the worst case.
consecutive
Answer: True. Recall that amortized analysis does not make any assumptions about the pos-
sible distribution of inputs (that is, it doesn’t take into account what happens “on average”)
( "
(40)
algorithm with a (" "
T F A graph algorithm with running time is asymptotically better than an
running time for a connected, undirected graph . !\"
" " _
Q
Compare
c
pair of vertices), which means
Answer: False. and
" cQ.We
.know
That factor of Q won’t do us any good
that (one edge for each
asymptotically
6.046J/18.410J/SMA5503 Final Exam Review 7
(41) T F In
3x " time a matching in a bipartite graph x!\" can be tested to
determine if it is maximum.
Answer: True: use DFS to determine there is an augmenting path in the corresponding flow
graph, which can be done in linear time
^(n
(42) T F integers each of value less than can be sorted in linear time.
(43) T F For any network and any maximal flow on this network there always exists an edge
such that increasing the capacity on that edge will increase the network’s maximal flow.
Answer: False. There may be two min-cuts, with the edge in question increasing the capacity
of only one of them. The other one will remain as it is, preventing the max-flow from
increasing any further
(44) T F If the depth-first search of a graph yields no back edges, then the graph is acyclic.
Answer: True
(45) T F Insertion in a binary search tree is “commutative”. That is, inserting and then
into a binary search tree leaves the same tree as inserting and then .
Answer: False. Consider inserting , in an empty binary search tree to see why.
(46) T F A heap with elements can be converted into a binary search tree in
time.
3
3
Answer: False. Building a heap takes time. If we could convert that heap into a binary
search tree in
time, then we could perform an inorder tree walk (which we know takes
time) and thus sort elements in a total of time. This contradicts the fact that
any comparison-based sorting algorithm takes time to sort elements
Introduction to Algorithms
Massachusetts Institute of Technology 6.046J/18.410J
Singapore-MIT Alliance SMA5503
Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Handout 30
Practice Quiz 2
fixed-size slots of primary memory. The virtual memory can be viewed as a set
of virtual-memory pages. At any time a subset of having size at most is resident in the slots
of primary memory. The job of the data structure is to maintain a dynamic mapping so that the
system can identify whether a user’s reference to a page resides in a slot of primary memory, and
if not, drop the least-recently used page and replace it with the referenced page.
The LRU data structure must therefore support the following operations:
I NSERT : Insert virtual page into slot of primary memory. The slot must
U : Return the slot containing virtual page ! , or else if does not reside
be empty.
SE NIL
D : Remove the least-recently used page "# from its slot , and return the
in primary memory.
ROP
now-empty slot .
Describe briefly why these operations suffice to implement the LRU page replacement policy. Give
an efficient implementation of this dynamic set.
&
$ % '
(*) contains a probability distribution over the set ,+-
./(0
; that is, we have
Problem Practice-2. Random-number generation
$&%21 )4365 798:<;>=?$&%21 )' . We wish to generate a random integer @ in the range BA @ A(
The array
CED
F )G
and
@ 1 $&%21
such that
Your algorithm may include an initialization phase to preprocess the array $ . After the prepro-
which should return a random integer according to the distribution $ . The highest priority in your
cessing phase, the user can make any number of calls to your random-number generator, each of
design is to make your random-number generator run as fast as possible, but your preprocessing
phase should be efficient as well. Analyze both the time for preprocessing and the time for actual
random-number generation.
Handout 30: Practice Quiz 2 2
J KML = L>NM
.L 8
has recently programmed his computer to listen to a stream of music from a recorder and convert
it into a set of (possibly overlapping) time intervals, where each interval
L : O J
corresponds to the duration of a note transcribed by the system. Moreover, the professor has
P :
1 P : 1
developed a heuristic that gives for each interval , a metric indicating how likely it is
that note was played by the recorder. The larger , the greater the confidence that note was
played by the recorder.
(a) The professor would like to determine which notes are played by the recorder. Since
the recorder can produce only one note at a time (when properly played), if two in-
QSRJ
tervals have a common intersection, one of the notes must be spurious (produced by
background noise). Give an efficient algorithm to determine a set of nonover-
T
lapping intervals (ostensibly corresponding to the notes played by the recorder) that
WU VYXMZ P :
maximizes
(b) The professor now wishes to extend his algorithm to recorder quartets, which consist
L : [J P :]\
of soprano, alto, tenor, and bass instruments. He upgrades his heuristic to give for
1
each interval
^ ^
, a metric
_
,
?
+ a
b
`
c
indicating how likely it is that note was played by
conditions are satisifed. We assume that a student is honor-bound to attend if his condition is
satisfied, and that he doesn’t register for conflicting parties.
The SIPB party service wishes to process the database of conditions so that as many people go
to a given party as possible. Thus, in the Alice and Bob example, both should be sent a positive
response. Model the problem formally, and give an efficient algorithm to select as many party-
goers as possible subject to the students’ conditions. For bonus points, devise a more general set
of conditions that can be efficiently processed by a similar algorithm.
xznBx{y niy |
distribution from a given source is a set of directed paths from to each of the other
vertices in . The cost of a distribution is the sum of the weights of its constituent
paths. (Thus, some edges may be counted more than once in the cost of the distribution.)
[n n
(a) Give an efficient algorithm to determine the cheapest distribution from a given source
. You may assume all nodes in are reachable from .
(b) One of the edges in the communication network may fail, but we don’t know which
one. Give an efficient algorithm to determine the maximum amount by which the cost
o
of the cheapest distribution from might increase if an adversary removes an edge
from . (The cost is infinite if the adversary can make a vertex unreachable from .)
Introduction to Algorithms
Massachusetts Institute of Technology 6.046J/18.410J
Singapore-MIT Alliance SMA5503
Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Handout 31
Managing the primary memory of a virtual-memory system with LRU page replacement can be
viewed as a dynamic data structure problem. The data structure manages a set
of
fixed-size slots of primary memory. The virtual memory can be viewed as a set
of virtual-memory pages. At any time a subset of having size at most is resident in the slots
of primary memory. The job of the data structure is to maintain a dynamic mapping so that the
system can identify whether a user’s reference to a page resides in a slot of primary memory, and
if not, drop the least-recently used page and replace it with the referenced page.
The LRU data structure must therefore support the following operations:
I NSERT : Insert virtual page into slot of primary memory. The slot must
U : Return the slot containing virtual page ! , or else if does not reside
be empty.
SE NIL
D : Remove the least-recently used page "# from its slot , and return the
in primary memory.
ROP
now-empty slot .
Describe briefly why these operations suffice to implement the LRU page replacement policy. Give
an efficient implementation of this dynamic set.
$ %
fying assumptions. It should be assumed that for a paging system on any real computer, anything
less than a time for any of those operations would simply be unacceptable.
We implement LRU by the use of
A doubly linked list, call it &
A hash table, call it ' .
.
The two data structures contain a structure holding the virtual page number ()and the corre-
sponding physical page slot it occupies for all pages that are currently occupying physical
* +
memory slots. The hash table entries also hold a pointer to the linked list node corresponding to
the virtual page . The space requirement for these is therefore .
In order to implement &-,/.
, we need a complete ordering in terms of last access for every page
currently occupying a physical memory slot. As was pointed out in lecture, a doubly-linked list
coupled with a move to front heuristic provides exactly this. Doubly-linked lists are convenient as
Handout 31: Practice Quiz 2 Solutions 2
$ 2 134
dictionary problem, which we know how to efficiently solve using a hash table. We use a chained
3 65 7
hash table as described in chapter 12 of CLR. The average access time in the table is ,
8
$9;: :
where is the load factor defined as where is the number of things to store and is the
number of slots. We therefore decide to use a table of size to ensure that the load factor
remains small. As for a hash function, we can simply use a random hash function drawn from a
collection of universal hash functions, or any of the other hash functions presented in CLR.
Assuming that we have implemented such a list and hash table, the D ROP operation can be imple-
mented as follows:
=<>+?A@B<>;
1 DC
FEHGJIFK &ML N
2 if
PORQ &
ORQ &
3 return
FEGSIFK &TLUC FEHGSIK &TL V<XW
YGJ>%ZU
4
5
FEHGSIK &TL V<XW
YGJ>%ZUDORQ &
else
The U SE is then simply (assuming that the hash function H ASH returns the hash key of its argu-
ment):
C PORQ ' H
U SE
&
1 ref CHAINED-HASH-SEARCH ASH
2 if ref
3
7 >+[W 7 W\V] V<XW
YGJ>%Z^ C 7 >+[_W V<XW
YGJ>%ZU
error “Page not in primary memory”
4 else ref
7 >+[W V<XW
YGJ>%Z^H 7 W\V C 7 +> [_W 7 W\V
ref
5 ref
7 >+[W V<XW
YGJ>%Z^ C ORQ &
ref
>+[W W\V W0E[aK
W` 0E7 [aK &ML^C 7 C ` &TL
6 ref
7 ref
8
9 return ref
ref
b
7 >X>X[[WW C W\V & W0GJE[^FOcK >+[_W%
I NSERT
7 C Oc>+[& W%
2
e
E
7C >+[_W C 7 ' >X[W `
3
4 ref N EW
5 ref N C
6 ref
7 ref
fC
The three functions that we asked you to implement, would be provided as a front end to your
system, therefore, it is conceivable that D ROP could be called an arbitrary number of times, without
being immediately followed by an I NSERT . With this in mind, the above functions do not suffice
for a correct implementation of LRU. We need some way to first look if there is an empty slot of
phyical memory and only if one does not exist should we drop the tail of the list. Modifying the
: :
D ROP and I NSERT procedures to do this is easy, by using a direct mapping table and doubly linked
list of free physical memory slots, exactly as we do above for the LRU. Since we only have
physical memory slots, we do not need to use a hashtable. Then, the D ROP and I NSERT procedures
$ ] : %
operate on all four data structures.
*g
Unfortunately, a number of people assumed that it was possible to keep an array of size
with an entry for each virtual page and store all information needed, therefore obtaining an
hi i
time. This solution however bears little physical realism. Most modern computer systems have a
ljj knoqmp r+t j
s
bit virtual address space, and KB pages. Therefore, a direct mapping lookup table for each
virtual page would require entries. If only one integer of information was kept in each
table entry, one would need 16384TB...
A number of other people used some form of balanced tree to do lookups, leading to logarithmic
running times for some or all of the procedures. However, again, such a solution is not desirable,
since a VM system is one of the most heavilly loaded components of the operating system.
Some people claimed that the move to front heuristic combined with the locality that program
memory references exhibit, allows us to use the LRU linked list to implement the U SE procedure by
a linear scan. However, an operating system runs a number of programs at the same time, therefore
the system as a whole exhibits less locality. Furthermore, (also for the people who claimed U SE
might not be used too much) this proceudre is probably the most used of the system for obvious
(!) reasons. All these solutions might work for Windowz, but not on a usable OS.
Other sources for loss of points were forgetting to update the LRU information when U SE was
called. LRU does indeed stand for least recently used.
u vK w
a7 L contains a probability distribution over the set ]r_
x 7
; that is, we have
Problem Practice-2. Random-number generation
The array
Handout 31: Practice Quiz 2 Solutions 4
u yK G {L z}| ~ 5^ u yK G L . We wish to generate a random integer in the range ( 7
M G
f KyG
and
u L
such that
Your algorithm may include an initialization phase to preprocess the array u . After the prepro-
which should return a random integer according to the distribution u . The highest priority in your
cessing phase, the user can make any number of calls to your random-number generator, each of
design is to make your random-number generator run as fast as possible, but your preprocessing
phase should be efficient as well. Analyze both the time for preprocessing and the time for actual
random-number generation.
Solution:
$ 7
First, we present an algorithm based on generating random variates from a given probability distri-
$9M7
bution by inverting the cumulative distribution function. This algorithm uses preprocessing
u
time and then takes time for actual generation of a random number according to the distri-
bution .
u
RAND-
1.
Kv LaC u Kv L
BINARY
GPr to 7
Z KyG LUC KyG L 1 u yK G L
2. for
3.
4. C U
5. return B
NIFORM
S
INARY EARCH(
7 Z )
6. end
Our next algorithm is based on expressing a given probability distribution as a weighted sum of
some other probability mass functions that are easy to generate from (using the inversion technique
mentioned above), and then selecting one of these probability functions according to the weights
$ 7 j u $ %
followed by generating (easily) from it. We give an algorithm that performs the preprocessing
7
in time , but generates random numbers from the distribution in time . The idea is
u
mass functions , e.g. , with the special property that each assigns positive
probabilities to at most two components, then in order to sample from , all we need to do is to
Handout 31: Practice Quiz 2 Solutions 5
2
Z 7 %FZ 7 2% FZV
generate a uniform random variate to select a distribution , and then to generate another uniform
random variable to select the correct positive component of . In fact we will use the fact that for
Z
u
a uniformly generated random variate , is independent of and thus we
only need to generate one uniform random variate to sample from distribution .
One way to implement this idea is to first create 7
¡
]
e
r
x
7 and the total mass according to the distribution u in each
buckets such that each bucket contains
exactly two elements from
bucket is .
5+
P REPROCESS K u %] K % x rx K r x
] ] K K %
1. u£¢C
w
¤ u w
¤ L
w
¤ u w
¤ L
M
¤
7 u w
¤
7 L L , where u ¤w L
K
¥0¥0¥ u ¤w7 L and ¤ is a permutation of
;
e
r
x
7 .
2. for C
G to 7
¦
3. § KyG L K L IW Ee8 % W 7 C
y
K G
K ¨
0
W FIRST ]u ¢ xE_ ¨W0%I W 8 W 7
4. §0IE?AL © L 8 C FIRST u£¢x 8 Ee%
5.
y
K G ¨
K r C ¨W0IW 5X W u£¢ 8 x ¨W0IW W
FIRST
6. § KyG L K¨r L Ee8 % 7 C %IE?A© u£¢ 8 7 LAST
§ E_+L L E_8 C
u ¢x 8 Ee%ª«%IE?A©
7.
8. 8 & C LAST
9. REMOVE FIRST u ¢ u ¢
10. REMOVE LAST u£¢ u2¢ KyG K¨r ¨W0IW W ] E_% E_
11. I O u£¢ § L L 8 7 8 &
W0IW 8 W 7 ] 8 E_%% from the elements of the array u , where
ADD N RDER
list such that the new list is still sorted on mass. This also takes time $97 . Hence, in all, the
ADD N RDER I O
inserts the given element in the given
4. 7
G [W\ C Z9°¯²±A³ ´µ
5. if §
K Za?©eW L Kv L 8 5XE_ %·¶¦G 7 [W\ then
6. return §
K Za?©®W L Kv L ¨W0IW 8 W 7
7. else return §
K Za?©®W L Kbr L ¨W0IW 8 W 7
Handout 31: Practice Quiz 2 Solutions 6
The preprocessing time in the above algorithm can be improved to $ 7DqM7 using a min-heap and
a max-heap, though a slight modification is needed to the data structures.
Correctness follows by observing
Mthat
the sum of masses
M with element
G ]re
A 7
KyG
over all the buckets is u L and
X¸ Z Q 6 ]
~ %Z Q 6
, if no two distinct intervals
associated
Q Q¹ overlap.
5
Comments on solutions: Almost every student used an approach similar to the first one described
or a balanced binary search tree and devised $ qM7 algorithms for the actual generation. However
above, and computed the cumulative distribution function. Most got the idea of using binary search
full credit was not given to these solutions. Some students linearly scanned the cumulative distribu-
tion array and thus lost some points. Some folks lost credit by making an assumption about a small
number of significant figures, though essentially they needed an arbitrarily large amount of stor-
age space to implement their algorithm to work for the problem as specified. A very few students
got near optimal performance. Some needlessly used red-black trees and got worse performance
bounds than efficiently implemented simpler data structures would have.
º »0¼ ¼ j
x¼ 5
has recently programmed his computer to listen to a stream of music from a recorder and convert
it into a set of (possibly overlapping) time intervals, where each interval
¼ ½ º 8
corresponds to the duration of a note transcribed by the system. Moreover, the professor has
G
developed a heuristic that gives for each interval
8
, a metric indicating how likely it is
that note was played by the recorder. The larger , the greater the confidence that note was
G
played by the recorder.
(a) The professor would like to determine which notes are played by the recorder. Since
the recorder can produce only one note at a time (when properly played), if two in-
¾P¿º
tervals have a common intersection, one of the notes must be spurious (produced by
background noise). Give an efficient algorithm to determine a set of nonover-
À
lapping intervals (ostensibly corresponding to the notes played by the recorder) that
AÁ ÂnÃ0Ä 8
maximizes
FEH<+ ¼ W 7 [ ¼
Solution: We will solve this using dynamic programming. Let ¼ and
*7DT7
denote the start and end time, respectively, of interval . First, sort the intervals in
increasing order of start time. This takes time. Of course, we can avoid this
step if the intervals are already sorted in this order. This seems reasonable since the
recorder encounters the time intervals in increasing order of their start times.
0¼ ¼ Å
¼
Our subproblems have the following form: find a non-overlapping set of intervals ¿
5
such that the quantity (henceforth called the objective function)
Handout 31: Practice Quiz 2 Solutions 7
À
AÁ ÂnÃ0Ä 8
is maximized. Thus, there are 7 subproblems, one for each
Æ}ÇÈ 7 . Let 7= be
the smallest value of ¢ such that
Ç FEH<% ¼ FÉ ·¶W 7 [ ¼ . That is, if the instrument is
¼
played during interval , then the earliest interval that the instrument can be played
¼ . Let Ëe be the maximum value of the objective function for the subproblem
0¼ ¼ Å 5;
Ê
¼
. The answer to the original problem is Ë .
next is
5
In any optimal subset of non-overlapping intervals for the subproblem Å
0¼ ¼
A¼
,
¼
there are two possible cases for time interval . It is easy to observe the optimal sub-
5
structure in both these cases.
Case 1: The instrument is not played during interval . Then, the value of the ob-
¼
jective function for the optimal subset is equal to ËeÅ .
¼
interval . Then, the earliest interval during
Case 2: The instrument is played during
which it can be played next is1
¼
for the optimal subset is 8
5 Ê
Ë 5Ê .
. In this case, the value of the objective function
¾ ¾ j ¾ÑÐ ¾Ò¿°º
the recorder for . Give an efficient algorithm to determine four disjoint
sets , where each contains nonoverlapping intervals (ostensibly
corresponding to the notes played by the four respective recorders), that maximizes
ÒÀ À
^ ÁA²Ã0ÄAÓ 8
Solution:
¼
Observe that if we allow two or more instruments to play during the same time interval
, then the optimal solution is obtained by solving separately for each instrument,
using the algorithm for part (a).
Handout 31: Practice Quiz 2 Solutions 8
The problem is more complicated when two instruments cannot play during the same
time interval. However, note that this allows two instruments to play during the same
time instant, e.g., when two instruments play during two overlapping time intervals.
We will solve this by generalizing the dynamic program from part (a).
0¼ Ëe¼ oÅ Ô pFÔ
SÕ
Ô m Ô ¼
Let be the maximum value of the objective function for the subproblem
© 5 Ç RÖ©) i
under the restriction that the earliest interval during which instru-
Ç Ç ×Ö©) i
Ë Ô Ô Ô Ô Ç
ment can play is for . Note that is at least for each
and the answer to the original problem is
r Ò # h . We will divide the number of table
entries into
Ç f©( i
categories depending on whether is equal to or strictly greater
Ç ÖÇ sÇ Ç Ð lÇ Ò ¶#Ç
than for . We give the dynamic programming equations for the case when
and
j . You are encouraged to work out the equations for the
other cases.
Ç ÏÇ ÏÇ Ç Ð JÇ Ò ¶PÇ
In the case when
¼ and
j
, there are three possible cases for time
interval . It is easy to observe the optimal substructure in each of these three cases.
Case 1: No instrument is played during interval . Then, the value of the objective
¼
function in this case is equal to Ëe Å Å Õ Å .
Case 2: Instrument 1 is played during interval . Then, the earliest interval during
o Ô p Ô Ô¼ m Ô
¼
is equal to Ë
which it can be played next is
p Å Ô Õ Ô m Ô Å . ;
5 Ê o . In this case, the value of the objective function
5 Ê o Ô
Case 3: Instrument 2 is played during interval . Then, the earliest interval during
¼
¼ . In this case, the value of the objective function
is equal to Ëe Å
which it can be played next is
S
Õ
Å 5;Ê p
o Ô 5;Ê p Ô Ô m Ô .
As before, Ëe SÕ will be the maximum of the objective function values in each of
oJÔ pFÔ TheÔ m Ô boundary condition is given by Ë 5 5 5 5 5 ØlÌgÙ Í%Ù Î Ò 8 5 .
the above cases.
i 1Ú2 cases for each table Ô Ô Ô entry
Ô (the largest number
of cases occurs when
Note that there can be at most
Ç Ç ½Ç Ð Ç Ò ½Ç ). Thus, computation of each table entry
%
takes * time. The number of table entries is at most 7 , so that the table filling
j t
time is *7
t
bottom-up approach !). Note that this dominates the *7DqM7 time for sorting of the
(try to figure out the order in which you need to fill up the table in a
intervals and precomputation of the 7® ’s. Thus, the overall running time is *g7 .
t
Remark: The coordinate in the above dynamic program can be removed without
Ç
running time becomes *g7 .
changing the essential structure of the dynamic programming equations. Then, the
Ò
To encourage more social behavior, the Student Invitational Party Board (SIPB) is developing a
web service to help organize party going. For a given party, each student registers if he definitely
wishes to attend or if he conditionally wishes to attend depending on whether a sufficient quorum
zs| &
of friends also attends. Specifically, the student indicates his condition on a SIPB web form by
giving a threshold
& &
and a list of or more other students such that he agrees to attend if
at least of the students on also attend. Some of the students on may not register, in which
case we assume that they will not attend. At some designated time before the party, the SIPB
service emails a message to each registered student whether the student should attend. The service
guarantees that if all students who are emailed positive responses attend, then all the attendees’
conditions are satisifed. We assume that a student is honor-bound to attend if his condition is
satisfied, and that he doesn’t register for conflicting parties.
The SIPB party service wishes to process the database of conditions so that as many people go
to a given party as possible. Thus, in the Alice and Bob example, both should be sent a positive
response. Model the problem formally, and give an efficient algorithm to select as many party-
goers as possible subject to the students’ conditions. For bonus points, devise a more general set
of conditions that can be efficiently processed by a similar algorithm.
Solution:
1 Model
Let & and be the list and threshold submitted by student G;.ÝÞ
accepts as input a directed graph, ÛBSÜ . Û is represented as an array of
Algorithm P ARTY
y
K G G
adjacency lists. That is ßà®á L lists neighbors of . In addition to a name, each vertex has attributes
âeãä=å%æ and ßHçç®èXé®à . There is an edge W ( Ý (from Y to Y ) if and only if student G appears in
list &ÑÚ . That is student Ç ’s attendance
y
K G
G âeãHä®å%æ KNG L =
:y& : . We initialize, ßHçç=èXéeà L ç_êëVè . We use one key additional data structure. Queue, ,
may depend on student ’s attendance. We define
lists indices “recently” discovered not to be attending the party. Initially is empty.
2 PARTY (G)
Y )Ü âeãä®å%æ KyY Lw| , then assign ßHç_ç®èXéeà KyY L ¡ì ä®ã_í è and enqueue Y on
1.For each vertex , if
.
2.While is not empty do the following. Dequeue the head index . For each student listed
G Ç
KNG
in ß_àeá L , decrement
âeãä®å%æ K Ç L . If ßHç_ç®èXéeà K Ç L ç_êëVè and âeãä®å+æ K Ç L"| enqueue Ç on .
Y
3.For each vertex #Ü , if ßHç_ç®èXéeà L
KyY çeêë=è , add Y to the solution set of students to be
informed to attend.
Handout 31: Practice Quiz 2 Solutions 10
3 Analysis
The loop in step 1 does constant work for each vertex in Ü $ ] :NGÜ(: ]
. Step 1 running time:
In step 2, each vertex is added to at most once. To see this, note that an index is added to
only if ßHçç®èXé®ìà äeKyG ã_L í ç_êë=è , ßHçç®èéeà KNG L is assigned ì ä®ã_í è before enqueing, and ßHçç®èéeà KNG L never
è to ç_êë=è . Thus, each adjacency Ý list is traversed at mostÝ once.
] Therefore, the
while loop does constant work for each edge in . Step 2 running time: $ ]: :
transitions from
time: $ ]:NÜ(:
]
Tallying the solution set takes takes time proportional to the number of students. Step 3 running
4 Correctness
G
To show correctness, we show that every solution is feasible and optimal.
At any point a student is viable if there exist members, , of such that
Ç & .A ßHçç®èéeà K Ç L ç_êë=è
î
solution is feasible if every student in the solution set is viable. To show feasibility it is sufficient
to prove the folowing loop invariant .
G
A solution is optimal if the solution set is as large as any other feasible solution set. To show
optimality, it is sufficient to show that a node is enqueued only if is not viable. Again we show
G
G G
this by induction on the iterations of step 2. Step 1 sets up the base case by enqueuing every
non-viable . Similarly, step 2 only enqueues if viability is violated.
$9;:NÜ(: 1 : Ý : ]
Now, we have a graph with edges from students to their friends. To get our input graph, transpose
this graph. Transposing a graph takes time
6 Comments
Many students submitted and$ ]:yÜ×: j $9;:NÜ(: Ð
solutions. Some students forgot to check for the
knock-on effects of removing a node. Some students tried to calculate students that should go
rather than students that should not. Those attempts did not succesfully handle cycles.
The standard (slow) solution was not to transpose the graph and then just iterate until no change
$ ]:yÜ×:
happened. Each iteration checks that a quorum of friends is still in the graph. If not, the node is
$ ]:yÜ×:
removed. Checking the lists of friends for each node takes time on this graph representa-
$ ]:NÜ×: $9;:NÜ(: Ð
tion. There are nodes to be checked on each iteration. Since only one node is removed per
iteration, there are iterations. Giving the result. With work this time can get even
worse if some measure of the slack is not memoized.
Many students knocked off a factor of :yÜ×:
either by using a queue or otherwise ensuring a student
$ ]:yÜ×: j
is only removed once. Some students transposed the graph but used an adjacency matrix rather
than adjacency lists and so were still stuck with a result.
\ G
A few creative students represented the problem as a system of linear constraint equations. Let
ø 5^ \
constraints of the form
øT Ã
ùXÂ \
be 1 if student attends. 0 otherwise. Now the problem is to maximize
.
subject to the
ÆÜ Ü
(a) Give an efficient algorithm to determine the cheapest distribution from a given source
. You may assume all nodes in are reachable from .
(b) One of the edges in the communication network may fail, but we don’t know which
one. Give an efficient algorithm to determine the maximum amount by which the cost
Ý
of the cheapest distribution from might increase if an adversary removes an edge
from . (The cost is infinite if the adversary can make a vertex unreachable from .)
Solution:
Handout 31: Practice Quiz 2 Solutions 12
1.This problem can be modelled as a single-source shortest paths problem. A distribution is a
tree of paths from to every other vertex in the graph. Since the cost of a distribution is the
sum of the lengths of its paths, a minimum cost distribution is a set of shortest paths. Since
* ;: :þqÿ:NÜ(:
Dijkstra’s algorithm can be improved from to using a binary
heap. Since we assumed that the graph is connected, the running time is
use a Fibonacci heap, the running time is .
1 Ý
*]:yÜ(:þqD:NÜ×: : : . If we
KNY L
In addition, we need to return the minimum cost distribution. Dijkstra’s algorithm as given in
CLR computes backpointers
Y
to represent the shortest paths. We can use these to represent
Y
the distribution; when asked for the shortest path from to , we trace the backpointers from
to and return the traversed edges (in reverse order).
Notes:
Some folks stated that the min-cost distribution problem is equivalent to the single-source
shortest path problem but failed to explain why. Others used Dijkstra without explaining
that this is correct because all weights are non-negative. Still others did not present the most
efficient version possible (using Fibonacci heaps). Then there were those that used Bellman-
:NÜ(:
Ford or all-pairs shortest paths, or invented their own algorithms.
When returning the distribution, some folks returned a set of shortest paths, one for
each vertex. This is less efficient than the shortest paths tree, but they were not penalized
harshly for this.
2.If an edge is removed from the graph, it is possible that the cost of the minimum cost dis-
@
tribution on the resulting graph may be more than the cost of original chepeast distribution.
g @(
W W%
Ý"+BW
That is, let be the minimum cost distribution found in part (a), and let be its cost. If
we remove edge from the graph, let denote the cost of the minimum cost distribution
for the new graph (with edge set ). We need to compute
Ìgà Í+ Î B W%4 B @(]
´
First recall that cost of a distribution is ~ Ã [aKyY L , where the [^KyY L are the distance values
B W%
returned by Dijkstra.
W
The straightforward brute-force approach to solve this problem is compute by deleting
W @
B W%2 B @( W
from the graph and rerunning the algorithm from (a). However, note that if the deleted edge
then , since the removal of does not affect the distribution . So we
@
only need to find
ÌgÃÍ+ Î B W%4 B @(]
@ ´ ! edges.
Since the edges in are a set of shortest paths, they form a tree, and a tree has :NÜ×:
W%
To compute g for an edge
W @ , we can delete W and then rerun Dijkstra’s algorithm
W
W
on the resulting graph. It is important to note that removing may make some vertices
[ ¢ KyY L distances are
. If so, then B W%
and we should halt the algorithm and return
unreachable from . To check this, we remove , rerun Dijkstra, and then check if any of the
1 Ý :NÜ(: P
*g]:NÜ(: j qf:yÜd: N: Üd:q: :
The running time of this solution is the cost of calls to Dijkstra’s algorithm. Using
Fibonacci heaps, this is .
:Ý : :yÜ×: }
Notes:
Many folks got the idea of running Dijkstras, and quite a few optimized to
Dijkstras.
Some forgot to check the case when the graph becomes disconnected by the removal of an
edge. Even if they assumed that operations with
are well-defined (a poor assumption but
accepted if clearly stated), they still should have explained why their code worked correctly
for this case.
Z Y
Z Y
Some tried to fix up the original distribution in the following way: if the edge from to was
Y Z
deleted, they found the second best path from to and added it to the distribution. This is
not optimal, because the new shortest path to may no longer go through .
Others misinterpreted the question, thinking that the edge deleted by the adversary was input
to the problem.
Introduction to Algorithms
Massachusetts Institute of Technology 6.046J/18.410J
Singapore-MIT Alliance SMA5503
Professors Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Handout 15
Practice Quiz 1
Do not open this quiz booklet until you are directed to do so.
When the quiz begins, write your name on every page of this quiz booklet in the space
provided.
Write your solutions in the space provided. If you need more space, write on the back of the
sheet containing the problem. Do not put part of the answer to one problem on the back of
the sheet for another problem, since the pages will be separated for grading.
Do not spend too much time on any problem. Read them all through first and attack them in
the order that allows you to make the most progress.
Show your work, as partial credit will be given. You will be graded not only on the correct-
ness of your answer, but also on the clarity with which you express it. Be neat.
Good luck!
Handout 15: Practice Quiz 1 2
M ULT
1
2 while
3 do if "!
4 then # %$&
5 '(*)!,+
6
- !.
7 return
Let 0/2143 ,
/5143 , and 6/2143 be the values of ,
, and , respectively, immediately before the loop
executes, and for 798 , let /2:;3 ,
/5:3 , and /2:3 be the values of these variables immediately after
the 7 th iteration of the loop. Give a loop invariant that can be used to prove the correctness of the
algorithm. You need not actually prove correctness.
Handout 15: Practice Quiz 1 3
T F Radix sort works correctly even if insertion sort is used as its auxiliary sort instead of
counting sort.
Handout 15: Practice Quiz 1 4
T F If bucket sort is implemented by using heapsort to sort the individual buckets, instead of
by using insertion sort as in the normal algorithm, then the worst-case running time of
bucket sort is reduced to I =LBEDO=> .
T F Sorting R elements with a comparison sort requires at least comparisons in the worst
case.
Handout 15: Practice Quiz 1 6
T F The sum of the smallest S = elements in an unsorted array of = distinct numbers can be
found in TUK=Q time.
T F The collection V WYX X [X Z]\ of hash functions is universal, where the three hash
functions map the universe W
^_`;a \ of keys into the range W b c! \ according to the
following table: d d d d
X X X[Z
!
!
_
a
Handout 15: Practice Quiz 1 7
@u u
T F Suppose that a r -input sorting network correctly sorts the sequences st!rb , sKrb ! , and
u
s c!v;r . Then, it also correctly sorts all sequences of r numbers. (Hint: Apply threshold
functions to the sequence elements.)
Handout 15: Practice Quiz 1 8
e +
(a) Argue that the stage- A DDER requires I BEDNp output wires.
Handout 15: Practice Quiz 1 10
(b) For an input word with = q bits, the total number of wires in the professor’s pop-
count circuit is K=> wires. Explain briefly why the recurrence
=> = l >$ I KBMDO=>
accurately describes the total number of wires in an = -input circuit. Give a good
asymptotic lower (big- P ) bound for => , and briefly justify your answer.
Handout 15: Practice Quiz 1 11
u
A divide-and-conquer pop-count circuit operating on an = -bit word sw w yxzxzx w{Y| can be con-
1
structed by using an A DDER component to combine the recursively computed pop-count of the
first =>).! bits with the recursively computed pop-count of the last =>)! bits.
(c) Draw a picture of such a divide-and-conquer pop-count circuit on q inputs. Label the
number of wires comprising each cable, as was done for Professor Blaise’s circuit.
How many wires does the divide-and-conquer circuit require for = q ?
Handout 15: Practice Quiz 1 12
(d) Give a recurrence that describes the total number K=Q of wires required by the
divide-and-conquer pop-count circuit for an = -bit word. Give a good asymptotic upper
(big- T ) bound on => , and briefly justify your answer.
When the quiz begins, write your name on every page of this quiz booklet in the space
provided.
Write your solutions in the space provided. If you need more space, write on the back of the
sheet containing the problem. Do not put part of the answer to one problem on the back of
the sheet for another problem, since the pages will be separated for grading.
Do not spend too much time on any problem. Read them all through first and attack them in
the order that allows you to make the most progress.
Show your work, as partial credit will be given. You will be graded not only on the correct-
ness of your answer, but also on the clarity with which you express it. Be neat.
Good luck!
Handout 16: Practice Quiz 1 Solutions 2
M ULT
1
2 while
3 do if "!
4 then # %$&
5 '(*)!,+
6
- !.
7 return
Let 0/2143 ,
/5143 , and 6/2143 be the values of ,
, and , respectively, immediately before the loop
executes, and for 798 , let /2:;3 ,
/5:3 , and /2:3 be the values of these variables immediately after
the 7 th iteration of the loop. Give a loop invariant that can be used to prove the correctness of the
algorithm. You need not actually prove correctness.
Answer:
5/ 143
/2143 5/ :3
2/ :;3 &
$ /2:;34<
Notes: Almost everyone had an idea of what an invariant should look like. About one-third of
the students got the invariant right. Many folks gave a recursive invariant that did not involve the
product that the algorithm is trying to compute. This invariant can not be used directly together
with the negation of loop-test to establish the post-condition.
Some people believed that the invariant should be true at every point inside the loop. They did not
understand that the invariant must be true at the beginning of every iteration of the loop. That is,
assuming it was true at the beginning of iteration = , the body of the loop re-establishes the invariant,
so that it is true at the beginning of iteration =>$ .
Handout 16: Practice Quiz 1 Solutions 3
T F Radix sort works correctly even if insertion sort is used as its auxiliary sort instead of
counting sort.
True. The correctness of radix sort requires the auxiliary sort to be stable. Insertion sort
as presented in this course is stable.
Notes: Most students identified correctly that we needed a stable sort, however a sig-
nificant portion visualized an unstable version of insertion sort and thus gave the wrong
answer.
Handout 16: Practice Quiz 1 Solutions 4
T F If bucket sort is implemented by using heapsort to sort the individual buckets, instead of
by using insertion sort as in the normal algorithm, then the worst-case running time of
bucket sort is reduced to L @OEHGg@A .
True. Bucket sort was presented in Recitation 4. Given an input of @ numbers from a
specified range, bucket sort divides the range into @ intervals and associates one bucket
with each interval. It distributes the items into the buckets, which takes L @A time. It then
sorts the items in each bucket using an auxiliary sort. It finally concatenates the sorted
bucket lists together in L N@T time.
Let h;i be the number of items that fall into bucket = , for = <<< ;@ . Let jk@A be the
running time of the auxiliary sort used to sort the items in each bucket. The total running
?
time N@T of bucket sort is then
? n
N@T L N@TA$ LRl9m jkNhi(4q <
ipo
We argued in recitation that the worst case is when all of the items fall into one bucket.
?
In this case @A L N@Tk$ L rjk@A] . If insertion sort is used as the auxiliary sort, then
? ?
@A ML @ . If heap sort is used, then @A ML @OEHGQ@A .
?
Notes: Many people did not give the full running time formula for @A or did not argue
?
that N@A is dominated by the time to sort the items in the buckets.
Some folks argued that bucket sort runs in expected L @A time (assuming a uniform dis-
tribution over the inputs), which is true and irrelevant.
False. R ANDOMIZED -S ELECT was the first selection algorithm presented in Lecture 7. It
is a randomized algorithm. The running time of a randomized algorithm is a random vari-
able whose value for a particular run of the algorithm is determined by the random num-
bers the algorithm uses for that run. The running time of R ANDOMIZED -S ELECT does not
does not depend its input (if all numbers are distinct). No adversary has control over which
random numbers the algorithm will use, and no adversary can determine which random
numbers the algorithm will use. Therefore, although it is true that R ANDOMIZED -S ELECT
runs in L N@ time in the worst case, no adversary can force this behavior.
Notes: Some people thought that R ANDOMIZED -S ELECT was a randomized version of the
deterministic S ELECT algorithm (the second algorithm presented in Lecture 7). We guess
that this was unfortunate inference from the pattern seemingly set by R ANDOMIZED -PARTITION
and R ANDOMIZED -Q UICKSORT .
Handout 16: Practice Quiz 1 Solutions 5
False. The procedure BUILD -H EAP runs in L @A time. BUILD -H EAP was presented in
Lecture 5, is given in CLR, and was part of Problem 3 of Problem Set 2.
An acceptable justification is that building a heap of @ elements does not sort them, so the
sorting lower bound does not apply.
Notes: Some people felt compelled to empty the heap after building it. That is, they argued
that H EAPSORT requires s0@OEHGQ@A comparisons, which is true and irrelevant.
Others argued that the information-theoretic lower bound does not apply to the worst case
running time of a sorting algorithm, which is false and irrelevant.
Still others pointed out that the information-theoretic lower bound says that asymptoti-
cally at least @OEPGg@ comparisons are required; it does not say how many comparisons are
actually used by a particular algorithm on a worst case input. This is quite right and quite
irrelevant. We can only guess that these people misread s0N@OEPGS@T as U N@OEPGg@A .
T F Sorting t elements with a comparison sort requires at least comparisons in the worst
case.
True. As shown in Lecture 6, the number of leaves of a decision tree which sorts t
elements is tuJ and the height of the tree is at least EHGvwtuJ5 . Since tuJ x !. and !zy M{ ! and
! 1 C!z| , we have B}&EHG>wt~J2} . Thus at least 10 comparisons are needed.
Notes: This was identical to the problem given on the practice quiz except that the partic-
ular numbers were changed.
Some folks had difficulty computing powers of 2 correctly. Starting today make sure you
have the first 11 powers of 2 memorized: k [!!Kw|Sk Q|g tu { C!~
tkt.| x ! [! { tuQBk { !~ k C!z| <
Some people calculated a lower bound on the height of the decision tree using the function
@OEHGQ@ . This is not a good strategy since this function is only asymptotically equivalent to
EHGI@KJ5 . Most discovered that tQEPGQt is at least 12, some going as far as 18, or as stated by
more than one person, “6 2.5ish = 18ish” (in fact EHGQt
#! < { ). Of these, many concluded
that indeed at least 10 comparisons are needed because at least 12 comparisons are needed.
However, the rest of the folks asserted that it is not true that at least 10 comparisons,
precisely because at least 12 are needed. These folks were gently reminded that } ! .
Handout 16: Practice Quiz 1 Solutions 6
T F The sum of the smallest @ elements in an unsorted array of @ distinct numbers can be
found in U N@T time.
True.
1. Find the @ th smallest element, , using the S ELECT algorithm.
2. Partition the array around .
3. Sum the first @ elements of the array.
Step 1 takes L N@T time, Step 2 takes L @A time, and Step 3 takes L @A time. Hence this
gives a L @A algorithm for summing up the smallest @ elements in the given array.
Notes: People either nailed this question or didn’t. Some used R ADIX -S ORT to sort the
elements in L N@T time, which can’t be done since we don’t know the range of the numbers.
Others tried to prove that it couldn’t be done by giving an algorithm that didn’t work.
This only proves that they couldn’t do it. A favorite was to find each of the @ smallest
elements in L @A time (via S ELECT or a linear scan through the array), for a total running
time of L @D4 .
T F The collection z of hash functions is universal, where the three hash
functions map the universe
; of keys into the range u ! according to the
following table:
!
!
True. A hash family that maps a universe of keys into slots is universal if for each
pair of distinct keys ;" , the number of hash functions
for which M
is exactly ), . In this problem, and . Therefore, for any pair of the four
distinct keys, exactly hash function should make them collide. By consulting the table
above, we have:
N * w
only for
N* M mapping into slot !
N only for mapping into slot
O M N only for mapping into slot
N
w only for mapping into slot
w
6 e w only for mapping into slot
w M w only for
mapping into slot
Notes: Many folks misinterpreted the definition of a universal family of hash functions.
They thought that for every , the probability that
Nu for and chosen
uniformly at random is _)z .
Handout 16: Practice Quiz 1 Solutions 7
¤ ¢ £ ¦
¥ ª¦ ¨4© © §
¤ ¤© ).! )! )z|
which is false because ª¦ are not independent, or
and
¢& ¦ ] ® ¢¤£ ¦ ¤ ¢ £ ¢¤£ ¦¯
¤© ¤© ¤© ).! )z| z) |
which is false because and are not independent.
¢
£
Other folks did not remember rules of expectation, saying such things as, “ ¥ §¦ ¨4©
¢¤£ ¢¤£
¤©v° ª¦ ¤© by linearity of expectation.” Many did not do sanity checks on their an-
swers, which is particularly easy for indicator random variables, which only take on two
values. Those who did noticed that ¥ ª¦ ¨ for both and .
Handout 16: Practice Quiz 1 Solutions 8
C² ²
T F Suppose that a -input sorting network correctly sorts the sequences ±!u , ±Nu ! , and
²
± !~; . Then, it also correctly sorts all sequences of numbers. (Hint: Apply threshold
functions to the sequence elements.)
True. If we apply a threshold function which flips from to at $0³M and
respectively, we will get four ¦e sequences for each of the correctly sorted sequences.
Then, if all the possible ¦M length sequences are accounted for, we can say that the
sorting network will correctly sort any sequence of numbers, by the ¦´ principle. The
following table shows the sequences obtained by applying the threshold function at each
of the above-mentioned points to each of the three sequences:
original sequence ! ! !
threshold at $c³
threshold at
threshold at
threshold at !
All possible sequences of u appear, therefore we conclude that the sorting network cor-
rectly sorts any three numbers.
Note that any comparator network correctly sorts the sequence composed of all zeros and
that composed of all ones. Therefore, we could have avoided the first and last thresholds.
Notes: People did much better on this question than expected. Those who knew the 0-1
Principle generally got it right, even though the problem required some nontrivial thinking.
Those who got it wrong did not seem to know the 0-1 Principle. Only a few seemed to
know the 0-1 Principle yet were unable to figure out the solution.
Handout 16: Practice Quiz 1 Solutions 9
Problem 3. Pop Count [35 points] (5 parts) Some computers provide a population-count,
or pop-count, instruction P OP C that determines the total number of bits in a word that are “ .”
Specifically, if an @ -bit word is µ ±rµ µ °¶°¶°]µ ² , then
1 m\
m n\
P OP C wµ µ i <
ipo 1
For example, P OP C ]±r ² .
This problem explores how to implement the P OP C instruction as a circuit. The basic building
block we shall use is an A DDER component that takes in two binary values and · and produces
their sum ¸ ¹$&· :
+ ¸
Each stage = of this circuit adds the bit µi into a running sum, which it forwards to stage =,$ . Each
cable of wires in the figure is labeled with the number of its constituent wires.
Handout 16: Practice Quiz 1 Solutions 10
(a) Argue that the stage-= A DDER requires L EHGQ=[ output wires.
Answer: The output of the stage-= adder must carry the addition of =D$ bits, which
²
is a value of at most =D$ since all the bits in the sequence ±rµ µ °¶°¶°¾µi may be .
1
As was stated in the problem, a cable of º wires can carry numbers up to !,» ¦% . We
therefore have the following equality and need to solve for º :
=>$ !» ¯
¦
>= $c! !»
EHGd=>$c! º
Since EPG¿=>$c! describes the number of wires needed to carry at most the value =~$ ,
we must take the ceiling of it. Therefore,
(b) For an input word with @ | bits, the total number of wires in the professor’s pop-
count circuit is ÃN@A wires. Explain briefly why the recurrence
ÃÄ@A Ã@ ¦¯ A$ L NEPGg@A
accurately describes the total number of wires in an @ -input circuit. Give a good
asymptotic lower (big- s ) bound for Ã@A , and briefly justify your answer.
²
Answer: Assume that, in order to add the first @ ¦R bits ( ±rµ µ °¶°¶°Åµ ), the
1 m \ in the
circuit uses Ã@ ¦½ wires total in its @ ¦ ! stages. Then, in order to add
last bit, we add one adder to the circuit, whose one input is the output of the last
adder of the @ ¦¯ circuit. The other input to the new stage-@ ¦¯ adder is the last bit
µ \ . From part (a) we know that this new adder will require L NEHGÆ@ ¦% ] eL EHGQ@A
m
output wires. The recurrence that describes the total number of wires then becomes
²
A divide-and-conquer pop-count circuit operating on an @ -bit word ±rµ µ °¶°¶°;µ
1 m \ can be con-
structed by using an A DDER component to combine the recursively computed pop-count of the
first @A).! bits with the recursively computed pop-count of the last @A)! bits.
(c) Draw a picture of such a divide-and-conquer pop-count circuit on | inputs. Label the
number of wires comprising each cable, as was done for Professor Blaise’s circuit.
How many wires does the divide-and-conquer circuit require for @ | ?
1 2 3
µ 1 + +
1
µ
µ
1 2
+
1
µ
(d) Give a recurrence that describes the total number ÃN@T of wires required by the
divide-and-conquer pop-count circuit for an @ -bit word. Give a good asymptotic upper
(big- U ) bound on ÃÄ@A , and briefly justify your answer.
Answer: The last stage of the divide-and-conquer pop-count circuit adds the output of
two sized @T)! divide-and-conquer pop-count adders. That sum requires L EHGQ@A wires
to carry it (see part (a)). Therefore, the recurrence for the number of wires is
Answer: It’s not clear where Blaise works, but it is clear that he doesn’t know the
material from 6.046. His iterative adder uses asymptotically more wires than the
divide-and-conquer adder. In addition, his adder requires @ stages to add while the
divide-and-conquer adder has depth EHGQ@ . Clearly, one of his graduate students saw
the professor’s initial design, suggested the improvement, and submitted the problem
for use on the 6.046 quiz. [Answers along these lines were awarded one extra credit
point.]
Introduction to Algorithms
Massachusetts Institute of Technology 6.046J/18.410J
Singapore-MIT Alliance SMA5503
Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Handout 39
(c) Which of the following can be performed in worst-case linear time in the input size?
1. building a binary heap from an unsorted list of numbers
2. determining if a graph is bipartite
3. walking a red-black tree inorder
4. solving the single-source shortest paths problem on an acyclic directed graph
Handout 39: Practice Final Exam 2
T (n) = 2T (n/2) + n
•I NSERT(x)—Inserts picture x into the data structure. Picture x has an integer field time[x]
and a boolean field scandalous[x].
•D ELETE(x)—Deletes picture x from the data structure.
•N EXT-P ICTURE(x)—Returns the picture that was taken immediately after picture x.
•S CANDAL !(t)—Returns the first scandalous picture that was taken after time t.
Describe an efficient implementation of this data structure. Show how to perform these operations,
and analyze their running times. Be succinct.
Introduction to Algorithms December 15, 2001
Massachusetts Institute of Technology 6.046J/18.410J
Singapore-MIT Alliance SMA5503
Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Handout 40
1. ordinary quicksort.
@
@
2.ma hash table where universal hashing is used.
3. a self-organizing list where the move-to-front (MTF) heuristic is used.
@
@
4.mR ANDOMIZED -S ELECT for finding the kth smallest element of an array.
1. An adversary can beat a deterministic partitioning scheme with a bad input, such
that all elements fall to one side of the pivot.
3. An adversary can always access the tail element of the list, regardless of the online
heuristic.
(c) Which of the following can be performed in worst-case linear time in the input size?
1.mbuilding a binary heap from an unsorted list of numbers
2.mdetermining if a graph is bipartite
3.mwalking a red-black tree inorder
4.msolving the single-source shortest paths problem on an acyclic directed graph
Handout 40: Practice Final Exam Solutions 2
4. Deletion on an ordinary binary search tree of height h takes Θ(h) time in worst
case, and h can be Ω(n) if the tree with n nodes is unbalanced.
(e) Which of the following algorithms discussed in 6.046 employ dynamic programming?
1. Kruskal’s algorithm for finding a minimum spanning tree
@
@
2.mthe Floyd-Warshall algorithm for solving the all-pairs shortest paths problem
3.moptimal typesetting, where the line penalty is the cube of the number of extra
spaces at the end of a line
4. the Bellman-Ford algorithm for solving the single-source shortest paths problem
@
@
(f) Which of the following correctly match a theoretical result from 6.046 with an impor-
tant technique used in a proof of the result?
1.mcorrectness of H EAPIFY . . . induction on subtree size
2. 4-competitiveness of the move-to-front (MTF) heuristic for self-organizing lists
@
@
. . . cut-and-paste argument
3.mcorrectness of Dijkstra’s algorithm for solving the single-source shortest paths
problem . . . well ordering and proof by contradiction
4.mexpected height of a randomly built binary search tree . . . indicator random vari-
ables and Chernoff bound
(g) On inputs for which both are valid, in the worst case:
2. insertion into an AVL tree asymptotically beats insertion into a 2-3-4 tree.
@
@
T (n) = 2T (n/2) + n
T (n) = 2 T (n/2) + n
≥ c n lg(n/2) + n
= c n lg n − c n + n
≥ c n lg n if c ≤ 1 .
The initial conditions are satisfiable, because T (2) ≥ 2c and T (3) ≥ 3c lg 3 , which
can be achieved by making c sufficiently close to 0.
Handout 40: Practice Final Exam Solutions 4
ĉ = c + ϕ(Tafter ) − ϕ(Tbefore )
wherere c = actual cost of the operation, Tafter = tree after the operation, Tbefore = tree before the
operation.
Let n be the number of nodes in T before the operation.
Amortized cost for insertion (actual cost c ≤ a1 lg n + a2 ):
n+1
X n
X
ĉI NSERTION = a1 lg n + a2 + k lg i − k lg i = (a1 + k) lg(n + 1) + a2
i=1 i=1
By choosing k = b1 we get: ĉD ELETE = O(1) and ĉI NSERTION = O(lg n).
Handout 40: Practice Final Exam Solutions 5
•I NSERT(x)—Inserts picture x into the data structure. Picture x has an integer field time[x]
and a boolean field scandalous[x].
•D ELETE(x)—Deletes picture x from the data structure.
•N EXT-P ICTURE(x)—Returns the picture that was taken immediately after picture x.
•S CANDAL !(t)—Returns the first scandalous picture that was taken after time t.
Describe an efficient implementation of this data structure. Show how to perform these operations,
and analyze their running times. Be succinct.
To allow us to implement these operations efficiently, we’ll use a red-black tree representation key-
ing photographs by time, and augmenting each node x with these additional fields: scandalous?[x]
and max-scand-time[x]. The field max-scand-time[x] stores the time when the latest scandalous
picture in the subtree rooted at x was taken. It contains either an integer time, or N IL if there are
no scandalous pictures in the subtree. We implement the operations (all in O(lg n) running time)
on this data structure as follows:
I NSERT(x): Place x in the tree, keyed by time[x], just like in a regular red-black tree. Update
max-scand-time fields if necessary after rotations. This can be done in O(1) time.
D ELETE(x): Just like I NSERT(x); do what’s normally done in a red-black tree, taking care to fix
max-scand-time fields after deleting node x. When node n is encountered where max-scand-time[n] =
time[x] and scandalous?[x] = T RUE, look at the node’s children and set max-scand-time[n] ←
max(max-scand-time[right[n]], max-scand-time[left[n]]).
N EXT-P ICTURE(x): Just like S UCCESSOR(x) in a red-black tree.
S CANDAL !(t): We use the max-scand-time field to search for this element. If there is no such
element in the tree, i.e. max-scand-time[root] < t, then return N IL. Otherwise we recursively
descent the tree as follows: at each node n check whether max-scand-time[left[n]] > t. If so
recursively descent to the left subtree. Otherwise, check whether time[n] > t and scandalous?[n] =
T RUE. If so return n. Otherwise, recursively descent the right subtree.
Introduction to Algorithms
Massachusetts Institute of Technology 6.046J/18.410J
Singapore-MIT Alliance SMA5503
Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Quiz 1 Solutions
6.046J/18.410J/SMA5503 Quiz 1 Solution Name 2
and the numeral of the solution to that recurrence. Points will be deducted for
Fill in the following table by specifying, for each algorithm, the letter of the recurrence for its
running time
wrong answers, so do not guess unless you are reasonably sure.
Letter Recurrence
a
b
Numeral Solution
!
"$#$%&'
(
)*+,- 1
C
c
2
CDFE
d
/ . &'89 3
CHGJIKL
e 02143 .5 76 4
C
f
: 5
C
CMDFE
.(/ ;=< 789 6
g
0>19?
h
A@B
6.046J/18.410J/SMA5503 Quiz 1 Solution Name 3
(a) Analyze the worst-case running time of the professor’s algorithm in terms of
8
and .
8 (
8
(
8
Solution: Merging the first two lists,each of
8
elements, takes
8 time,
time. Merg-
elements takes N
ing the resulting
8
elements with the third list of
and so on. Thus for a total of list, we have:
8
8
MDFE\89 . Briefly justify the running time of your, whose
(b) Briefly describe an algorithm for merging sorted lists, each of length
worst-case running time is [
rithm. (If you cannot achieve [
M
]
D \
E 9
8 , do the best you can for partial credit.)
algo-
Solution: There are several possible answers (only one is required). One method is
to repeatedly pair up the lists, and merge each pair. This method can also be seen as
a tail component of the execution merge sort, where the analysis is clear. Finally, a
8
conceptually simple method is to store a min priority queue of the minimum elements
8
of each of the lists. At each step, we output the extracted minimum of the priority
queue, and using sattelite data determine from which of the lists it came, and insert
the next element from that list into the priority queue.
6.046J/18.410J/SMA5503 Quiz 1 Solution Name 5
c^ A@.0 ^ S SS A@ed 0 ; 0 :f . L^ R@gd 0 ;=0 < f . ^
@ 0 0 . ^] .0 ^
A@ .0 ^] .0 ^ A@ 0 . ^]L.0 L^ R@gd 0 ;=0 < f . ^] .0 ^ A
h
c8i^ ^` 0
8
This is exactly the number of different ways of interleaving the list, into the single
list of elements
6.046J/18.410J/SMA5503 Quiz 1 Solution Name 6
Solution: Consider the decision tree used to make the comparisons. The worst-case
running time corresponds to the height of the tree:
(a) T F A constant ?
m
exists such that for any
m
? , there is an array of elements
such that insertion sort runs faster than merge sort on that input.
Solution: true.
(b) T F Consider the algorithm from the textbook for building a max-heap:
xM
xz|{ length y xrz
BUILD -M AX -H EAP
1 heap-size y
Oc{ } length y xz!
~ downto
2 for
3 do M AX -H EAPIFY
xhO
MDFE\
calls to MAX-HEAPIFY , and the worst-case
On an array of elements, this code runs in time in the worst case,
]
D C
E
because there are
call is
.
time for any
MDFEC is not a tight bound. In fact, Build-Max-Heap runs in time, as proven
Solution: false.
during recitation.
6.046J/18.410J/SMA5503 Quiz 1 Solution Name 8
Solution: true.
n n( nL
Letting
= n , we have:
$
@B
[ MD]E
Note: it is important to understand that
= [ MD]E .
Solution: false.
The running time is not dependent on the input that the adversary provides. This
is because radomized quicksort will randomly choose a pivot to partition, inde-
pendent of the input.
Solution: true
C
m l Jy z
m l yJ z
a*U +@% m
m +@% yJ z
6.046J/18.410J/SMA5503 Quiz 1 Solution Name 9
Solution: true.
An auxilliary sort for radix sort must be stable. Bucket sort is stable and hence
suitable for use as an auxilliary sort for radix sort.
P
P . Let |P> be the expected time for an unsuccessful search in the table
(g) T F Consider two implementations of a hash table with slots storing keys, where
Solution: false.
|P> :k P
P>
+@
.
In the worst case, in the chaining version, we need to search through all the val-
|P>Ma
| Phh .
ues in one particular slot. On the other hand, in the open addressing version, we
need to search through every slot. Hence
6.046J/18.410J/SMA5503 Quiz 1 Solution Name 10
(h) T F Let be a class of universal hash functions for a hash table of size
P |
Then, if we use a random g g to hash keys into the table, the expected
.
Solution: true.
. d .(;=< f
Number of ways to choose 2 keys out of keys is
Therefore, the expected number of collisions,
Solution: true.
the £
point requies point.
[ .That again takes [
Each of the steps takes [
. Therefore, total time
. Finally, partitioning around
is [
Introduction to Algorithms
Massachusetts Institute of Technology 6.046J/18.410J
Singapore-MIT Alliance SMA5503
Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Quiz 2 Solutions
Quiz 2 Solutions
Help the professor by designing an efficient algorithm to produce the two desired lists, including
product quantities, given the product information for today and yesterday.
Solution: We will make the following assumption in the solutions - the professor will produce
the new lists using only today’s list and yesterday’s list of product information. This means that
it is not possible for the professor to tell if the difference in the quantities of a product in the two
lists is the result of only additions of items of the product, only sales of the items of the product
or is the result of instances of the same product being both added and sold between yesterday and
today. The professor should make it clear to his clients that his lists merely give the differences in
6.046J/18.410J/SMA5503 Quiz 2 Solutions 2
the quantities of products between the two days and do not give the actual number of items sold or
added. Other reasonable assumptions are possible and may give slightly different solutions.
Assume that there are items in yesterday’s list and items in today’s list . We will give two
solutions that use amount of space.
The first solution uses hash tables and has an average runtime of . Put all the items in
yesterday’s list into a hash table with
slots. For each item in today’s list , check whether
it exists in the hash table containing yesterday’s items. If it does not exist, add the item together
with its quantity to list - this item is newly added. If the item exists, check whether the quantity
has increased, decreased or remained the same. If it has increased, add the difference to list
under the assumption that the increase is caused by additions of products. If it has decreased, add
the difference to list under the assumption that the decrease is caused by items being sold. If it
remained the same, do nothing.
Next, put all the items in today’s list into a hash table with
slots. For each item in yester-
day’s list , check whether it exists in the hash table. If it does not exist, put the item together with
its quantity into list under the assumption that it has sold out between yesterday and today. This
hash table can be stored to be reused instead of deleting it and recreating it again the following day.
Under the assumption of simple uniform hashing (or by using a universal hash function), insertions
and lookups has average runtime of
, giving a total runtime of . Some people used
perfect hashing and claimed that this gives a worst case runtime of . This is not true if
the perfect hash function has to be found each time. The technique in CLRS uses a randomized
algorithm to find the perfect hash function - this has an average (not worst case) runtime of ,
where is the size of the key set.
The second solution sorts the two lists and and uses a routine that is similar to the merge
routine used in merge sort to create the lists and . The worst case runtime is
if mergesort or heapsort is used to sort the lists.
!
First and are sorted. We then initialize and to . The following is repeated until one of the
lists runs out of items.
" If the key of # %$ is the same as the key of # &$ , then the quantities are compared. If the
# #
quantity of $ is greater than the quantity of &$ , the item is added to list together
# #
with the difference in the quantities, under the assumption that the decrease is caused by the
product being sold. If the quantity of $ is less than the quantity of &$ , the item is added
to list together with the difference in quantities, under the assumption that the increase is
caused by addition of the product. Both and are increased by 1.
" If the key of # %$ is less than the key of # &$ , this indicates that the item # %$ has been sold
out because it cannot appear later in list which is sorted. This item is added to the list
and is increased by 1.
" If the key of # %$ is greater than the key of # '$ , this indicates that the item # &$ is new as it
cannot appear later in list which is sorted. This item is added to the list and is increased
by 1.
6.046J/18.410J/SMA5503 Quiz 2 Solutions 3
Figure 2: Two no-left-turn maps. Find a path from to with as few right turns as possible, and
no left- or U-turns. You can play with these maps using a Java applet on http://www.gjnem.
demon.co.uk/noleft/ixnoleft.htm.
If runs out first, the rest of items in list are appended to list as they do not appear in and
are considered sold out. If runs out first, the rest of the items in list is appended to list as
they do not appear in and are considered as new items. The routine (excluding the sorting part)
runs in time .
The sorted list can be used the as the next day’s list, so only one list needs to be sorted each day.
If the largest key size is known, radix sort can be used and will reduce the worst case runtime to
, if '
where is the number of bits required to represent the keys and is the
size of the list to be sorted.
4 2112 2112 8
BABA BABABABA @?@? @?@? @?@? @?@? @?@? 343 787
1
BABAB
0 1
0
"!"!
1 1 1 1 1
#$# $#$# &%& '(' >=>= >=>= >=>= >=>= >=>=
$
1 1
( 1 1
1
;<
< ;
< ;
< ;
< ; < 9
: 9
: 9
: 9
: 9
: 9 :
0 1
;< ;
< ;
< ;
< ; < )
* ) * 9
: 9
: 9
: 9
: 9
: 9 :
1 1
;<
;<
;< ;
< ;<
< ; ;
< ;<
< ; ;
< ;<; <;<; +,+ /0/ :9:9 :9:9 :9:9 :9:9 :9:9 :9:9:9
< ; < , )
* ) * 0 9
: 9
: 9
: 9
: 9
:
;<
;< ;<
< ; ;<
< ; ;<; <;<; .- .- :9:9 :9:9 :9:9 :9:9 :9:9 :9:9
< 1
;< ; <
;< ;< ; <
;< ;< ; <;<;<; <;<;<; .- .- :9:9:9 :9:9:9 :9:9:9 :9:9:9 :9:9:9 :9:9:9
;< 0
Give an efficient algorithm to find a no-left-turn path through an map using the minimum
number of right turns. (Hint: For intuition, solve the left-hand map in Figure 2.)
Solution: The basic idea in solving this problem was to create a graph representation of the
problem and then use a shortest paths algorithm to find the optimal no-left-turn path. Creating
the graph typically took and the shortest path problem could be solved in as well.
Acknowledgements to Lauren Bradford, Vida Ha, and Josh Yardley who expressed some of these
ideas very clearly in their write ups.
One way to turn this problem into a graph problem is as in the figure. Our map of cells is converted
into a weighted, directed graph. For each of the m*n cells of the map, we create 4 nodes. If the
car enters a particular cell from the cell below it on the map it would enter the “south” node
associated with that particular cell. After creating the nodes, we assign directed edges to them.
Edges that correspond to a righthand turn have an edge weight of one and edges that correspond
to driving straight have a weight of zero. Edges that would represent left hand turns or uturns are
not included in the graph. An edge which goes from one orientation to another (example south to
west) represents a right turn. It will take us time to create this graph representation.
Shortest Path Algorithms
Now that we have our graph representation we need to determine the path that minimizes right
hand turns. With our edge weight representation finding the shortest path from source to sink
should be effective. Many students observed that since we had positive weight edges, we could
run Dijkstra’s algorithm to calculate the shortest path in our graph. Since Dijkstra’s algorithm runs
6.046J/18.410J/SMA5503 Quiz 2 Solutions 5
!
in time, this will give us a running time of
. However, we can do
better than this! One way to optimize this is to use 2 linked lists to optimize the min-priority-queue
in Dijkstra’s algorithm. The first list is of the elements reachable with weight 0 and the second is
elements available with weight 1. Extract-min returns the head of the first list, unless it is null, in
which case it returns the head of the second list. Since it only needs to maintain and look at these
two lists, it works in constant time, making the algorithm run in
. Another optimization
works similarly, using an auxiliary data structure to do a modified breadth first search to . That is
to say, you visit all the edges you can get to using the same number of right turns each time. The
following is a breadth first search algorithm that uses a linked list with pointers to the beginning
and end of the list.
In order to perform breadth first search you’ll first pick off the first gray node in your queue. Then
you’ll want to look at the graph information and see if the cell in front and to the right are free.
Check if the square to the front and to the right are empty. If so, then these cells, are the children
of the node you’re looking at. Depending on the children’s color, you’ll do one of three things.
1.If this node has no children, just color it black. No children means that the right and straight
ahead of you are blocked cells, so this is a dead end.
2.If the children are all gray and black, this means that the children have been discovered by
another path. Since this is a breadth first search by the number of right turn edges, we know
that if the node has already been discovered, another path was able to reach it with the same
or less number of right turns. Thus, this path cannot have a smaller number of right turns than
that earlier path that discovered it. Just color this node black just like in case 1.
3.If either child is white, color this node black. If its right child is white, color that gray and
add it to the end of the queue. If the straight ahead child is while, color it gray also, but add
it to the beginning of the queue, since you did not need to turn right to get to that node. The
cell reached by going straight has the same number of right turns as its parent cell, so both
should be considered before the thing with more right-turn edges. By adding this cell to the
beginning of the queue, you will consider this cell immediately next, which is the correct
behavior since it has the same number of right turns as the one you checked.
Since the linked list can be maintained in constant time and breadth first search runs in
!
our algorithm will run in .
Correctness
Since our graph represents driving straight with a zero weight edge and turning right with an edge
weight of one, the shortest path from s to t will be the path with no right turns. Our shortest path
algorithm cannot make any illegal turns because those edges do not exist. For the correctness of
Dijkstra’s algorithm, I refer you to CLRS.
The BFS algorithm presented above will visit every node accessible from s until it reaches t. Since
it will always visit the nodes which it can go straight before it turns right, the path which reaches t
first, will be the path with the least number of right turns.
Problem 3. Really short paths
6.046J/18.410J/SMA5503 Quiz 2 Solutions 6
If a digraph with edge-weight function
contains a negative-weight cycle,
some shortest paths may not exist. Assume that every vertex is reachable from a given
source vertex . Give an efficient algorithm that labels each with the shortest-path
distance from to or with if no shortest path from exists. Be sure to give a rigorous
argument for the correctness of your algorithm.
Solution: In dealing with single source shortest-paths problem, some shortest paths may not
exist if there is a negative-weight cycle. This is because it would then be possible to keep going
round the cycle relaxing shortest path, thus making the concept of a shortest path ill-defined.
To deal with this problem, we will have to detect the negative-weight cycle and assign every vertex
that is affected by the cycle with . Note that it’s not just the vertices in the cycle that are
affected. Those verticies outside the cycle, but which can be reached from the verticies in the cycle
will also be affected, since on the way, we can go around the cycle numerous times to relax the
shortest path.
Thus the algorithm is divided into two parts: first, we use a modified version of Bellman-Ford
algorithm to assign those verticies in the negative weight cycle to . Then, from each of those
vertices, we perform a Depth-First-Search, and assign all verticies reacheable from it with .
Specifically, we modify Bellman-Ford algorithm, as presented in CLRS on page 588, so that in-
stead of returning false in line 7, v is assigned a value of , and added to a list which we call the
Negative-Vertex-List. In line 8, instead of returning true, we perform a Depth-First-Search for the
verticies in the Negative-Vertex-List. Every vertex reached in the Depth-First-Search is assigned a
value of after it is colored black.
Proof of Correctness
Lemma 3.1: The algorithm works correctly if there are no negative-weight cycles.
Proof: This follows directly from Lemma 24.2 and Theorem 24.4 of CLRS: The only changes
we have made to the Bellman-Ford algorithm are in line 7 and line 8. But line 7 will never be
reached if there is no negative-weight cycle, as described in the proof of theorem 24.4. Also, the
modified line 8 refers only to vertices that were added in line 7, and hence will contain an empty
list since line 7 is not executed and therefore no vertex is added to the Negative-Vertex-List. Thus,
the algorithm works correctly, when there are no negative-weight cycles
Lemma 3.2: If there is a negative-weight cycle, the algorithm will detect it. Specifically, there
exists an edge on each negative-weight cycle such that the test in line 6 will be true.
Proof: This follows from the second part of the proof of Theorem 24.4 in CLRS, reproduced in a
slightly modified form below:
(1)
6.046J/18.410J/SMA5503 Quiz 2 Solutions 7
# $
# $
Assume for the purpose of contradiction that our algorithm does not find this negative-weight cycle
for
i.e. line 7 is not executed. Thus, . Summing the
inequalities around cycle gives us
# $ # $
#
$
, each vertex in appears exactly once in each of the summations # $ and
# $ , and so
Since
# $ # $
Moreover, by Corollary 24.3, $ is finite for . Thus,
#
!
which contradicts inequality (1). We conclude that our algorithm will detect a negative-weight
cycle, if there is one.
Lemma 3.3: The algorithm will correctly assign all verticies reacheable from the negative-weight
cycle with . (Remember that these verticies are precisely those which will not have shortest
path, as explained in paragraph two of this solution)
Proof: Our line 7 has already assigned a vertex in the negative-weight cycle with . It follows
from the definition of a Depth-First-Search that all the vertices reacheable from that vertex will
also be explored and hence assigned .
Theorem: The algorithm works correctly to label each with the shortest-path distance from
to or with if no shortest path from exists.
Proof: This follows directly from the three lemmas that we have just proven.
Running Time: Clearly, the running time is the same as the unmodified Bellman-Ford algorithm:
. Depth-First-Search has a running time of
. The rest of the algorithm runs in the
same time as the unmodified Bellman-Ford algorithm:
. Therefore, overall running time is
.
Problem 4. Grading
Professor Leemainerson has assembled a numerical score for each of the students in his algo-
rithms class based on final exam, quizzes (in-class and take-home), problem sets, and most im-
portantly, class participation in lecture and recitation. The term scores form a list
6.046J/18.410J/SMA5503 Quiz 2 Solutions 8
of real numbers. In order to assign a final grade of , , , or , the professor has devised the
following scheme.
The professor first determines four archetypical scores , , , and . Then, he gives each
student the grade corresponding to which archetypical score is closest to the student’s term score,
breaking ties in favor of the higher grade (because the professor is a nice guy).
To determine the four archetypical scores, the professor examines the distribution of term scores
and finds the best piecewise constant approximation of the distribution. That is, he chooses the
four archetypical scores to minimize the quantity
Solution: The professor’s grading problem can be solved in a variety of different ways. We have
time.
seem to know is that, given a sequence
,
seen correct approaches that take , , and
the value which minimizes is the median of these numbers and not their mean,
A crucial fact that several students didn’t
as many students thought2 . We can use a simple argument to prove this.
X’
212112
X
212121
212121
22112121
212121
22112121
22112121 ('( )) .--. //
334343 ""!! ""!! #$#$ #$$# &%&% 212121 ' ,+,+
22112121 ** 00
122121
22112121
22112121
122121
22112121
Delta-X
22112121
21
Figure 4: Why the median minimizes the sum
be the median 5
6 =. < Suppose that
97 :! 5 . We can write 97 ;
Proof Referring to Figure 4 , let and let
there is another value 87 such that . Without
2
The mean would minimize >@?ACBDFEHG AJILKLMON , commonly referred to as the “mean-square error”
6.046J/18.410J/SMA5503 Quiz 2 Solutions 9
loss of generality, say that 87 lies to the right of . We notice that when we change by < , the
contribution of each of the elements to the sum changes by an amount which is between <
and < , depending on the element’s position relative to the change. The contribution of each
element on the left (including the point on which the median lies) increases by + < . Since is
the median, there are
elements on the left, and their total contribution to the sum increases
by
<
. The contribution to the sum of the remaining
elements can decrease
most <
, so the decrease cannot be bigger than the increase. Hence
by at
7 cannot
be any smaller than 5 , which contradicts our original assumption that there is some other value
97 which (strictly) decreases the sum. Notice that if is even, there will be two medians,
both of which minimize the sum.
Going back to our original problem, we observe that, if the list of grades is sorted, the optimal
solution will look like Figure 5.
xA
A
A-B
xB
B
B-C
C xC
C-F
xF
F
We will now prove that in an optimal solution, the four archetypical scores
will be
the lower medians3 of the A range, the B range, the C range and the F range.
Proof Given the four ranges, let ,
, O and be their contributions to the total
sum that we want to minimize. Above, we have proved that if we let , , and be the me-
3
the term “lower” refers to the smaller of the two medians, if there are two, as is the case when the range has an
even number of elements
6.046J/18.410J/SMA5503 Quiz 2 Solutions 10
dians of the respective ranges, each of these four contributions will be minimized (independently),
and so will their sum.
A slight complication that may arise, which very few students noticed, is the situation depicted in
Figure 6 which zooms in on the A-range and the B-range. There, choosing the medians for the
range and the range may well minimize the contribution of each of the two ranges to the sum,
but we are violating the rule for choosing grades. Namely, the lowest score in the range is closer
to than it is to , so that score should have corresponded to a .
0/0/ 0/0/
A-range
,+ ,+ ,+ ,+ ,+ ,+ ,+ ,+ ,+ ,+ ,+ ,+ ,+
---
---- xA
----
----
----
----
-- ..
... ""!!""!!
... $$##$$##
&%&%&%&%
B-rage
('('('(' xB
*)*) *)*)
However, such a grade distribution cannot be optimal. This is because if we keep 1 and as
they are, and move that problematic score to the range, the contribution of that score to the sum
will decrease while the contribution of all the other scores will remain the same, thereby giving
us a lower total sum. This contradicts the assumption that what we have is an optimal solution.
Therefore, we conclude that in any optimal solution, the archetypical scores will be the medians of
the four ranges.
Now, in case there are two medians for a given range, they will both minimize the total sum. Since
we are supposed to break such ties by minimizing , we should choose the
smaller of the two medians, namely the lower median.
Given these facts, a straightforward algorithm to solve this problem is the following:
Try all
2 3
combinations of . For each choice, compute
that minimizes
and pick the combination that leads to the minimum value. In case of ties, choose the combination
. Once we find the best choice for , we can
compute the students’ grades by finding the archetypical score which is closest to each score.
The correctness of the algorithm follows from the fact that we are comparing all the candidate
solutions, and picking the one that minimizes the total sum. The bad thing is that our algorithm is
6.046J/18.410J/SMA5503 Quiz 2 Solutions 11
terribly inefficient: we need time to go over all the
2 3
H scores,
combinations of archetypical
and for each combination, we need another
time to compute
6 ,
for a total running time of
. After we have determined the archetypical scores, we can
compute the students’ grades in
time since for each grade, we just perform 4 subtractions
and pick the archetypical score which minimizes the absolute value of the difference.
We can do slightly better by observing that since the archetypical scores are the medians of the
four ranges, we can instead try all possible combinations of ranges. The only thing we need to
determine the ranges are the cutoffs. Since there are 4 ranges, there are 3 cutoffs (between A-B,
B-C, C-F). Knowing the cutoffs allows us to determine the archetypical 2 3 scores (medians) in
time4 , and proceed as before. However, there are
now “only”
combinations of cutoffs to try.
For each, we can compute
in
time, for a total running time of
.
Sticking to our strategy of trying out all possible combinations of cutoffs, we can do even better if
we observe that spending time to compute the total sum every time is overkill. The contribu-
tion of grade to the total sum is
6 and is uniquely determined2 3by
,
the indexes of the lowest and highest scores in . If we build a table with all
possible ranges
that grade can have, we can use it to compute the total sum in time for every combina-
tion of cutoffs by simply performing 4 table lookups. This would reduce the running time of our
algorithm to
, provided, of course, that we don’t need more than time to build that
table. Indeed, a simple way to build the range-cost table in time is to go over all
possible ranges, compute the median in
time, and then compute the contribution of that range
2 3 in
to the sum
time. Building the table this way requires time, and allows us to try
out all
combinations of cutoffs in
time by doing 4 constant-time table lookups for each
combination.
We can further improve the time it takes to build our lookup-table to by observing that if we
have computed Contrib , the contribution of range to the total sum, then we can compute
the Contrib
in
time because the two ranges have the same median , and so
the contribution of the second (larger) range is simply the contribution of the first one plus the two
extreme values, namely
Contrib
Contrib
This way we can compute the contributions of all the ranges that have a specific median in
time. Since there are possible medians, we can compute the contributions of all possible ranges
time.
in
In order to reduce the running time of the entire algorithm even further, we can use the dynamic
programming approach presented below.
4
assuming that the scores are sorted. If they are not, sort them using MergeSort in
small to affect the total running time of any of the algorithms we present here
E "!# M time. This is too
6.046J/18.410J/SMA5503 Quiz 2 Solutions 12
Let be the minimum total sum
H , that is, the minimum total
sum when we allocate four grades to a range . Similarly, let be the minimum total sum
when we assign only three different grades to the range and so on. Observe that is simply
the minimum value of
H and that
is just Contrib . Now, we
can define as follows:
The value of which minimizes the expression above is basically the A-B cutoff: indeed, in the
optimal solution which makes use of 4 distinct grades, all the scores from to are assigned three
different grades (B,C,F) in an optimal way, and the rest are assigned one grade (A). In general,
(2)
Thus, computing when and are available takes
time (we are going over
possible values of and taking the minimum). Computing
time. for all possible values of thus
takes
We have seen above that we can compute all combinations in
time. Given these, we can
compute and tabulate for all in
time and similarly for . As shown above,
this will enable us to compute in a total of
time. By keeping track of the values of
that led to the minimum value each time in Equation 2, we can reconstruct the cutoffs for A,B,C,F.
Knowing the cutoffs of the optimal solution, we can compute the archetypical scores by taking the
lower median of each range, for the reasons explained above. Knowing the archetypical scores, we
can assign grades in
time.
"
M OVE -C URSOR : Moves cursor forward in its file by (normal) characters if
is
positive, or backwards in its file by characters if is negative.
"
R EAD : Returns the (normal) character immediately after in ’s file.
" I NSERT : Inserts character
at and moves to immediately after .
HOLIX’s implementation of holey files treats each file as an array in which each array element
is either a normal character or a hole character, and the two types of characters can easily be dis-
tinguished. The normal characters must remain in their proper order within the file, with hole
characters interspersed. In order to support the movement of cursors, the first and last hole char-
acters of a hole store the hole’s length. (After all, the bits in the hole are otherwise unused.) Thus,
skipping over a hole takes constant time.
Give an efficient algorithm for maintaining holey files. You should try to minimize both the time
required for the operations and the total number of characters needed to support a file with normal
characters.
Solution: The best balance of space and speed of functions that we know of is the following
method. With
space, we can support the operations in the following times.
" N EW-F ILE :
" M OVE -C URSOR :
" I NSERT :
Our general strategy is to always keep only one group of holes all consecutive together, and main-
tain the invariant that always points right before the leftmost hole of the group unless the array
is full. Another strategy we use is an array that doubles in size (in amortized constant time) so
that we can continually expand to handle any number of characters. This arrays running time and
correctness was demonstrated in lecture.
Our procedures work in the following manner. HOLE - LENGTH returns to us the length of the hole
that is currently pointing to, and by our invariant, if a hole exists, will be pointing to it. It will
return 0 if no hole exists.
" N EW-F ILE :
– Allocate an array of size 2, all entries filled with holes and the pointer pointing to just
before the first hole.
" M OVE -C URSOR
:
– If is positive, we set to be
HOLE - LENTH
and shift the hole over characters
to the right by moving the characters just to the right of the hole to before the hole.
– If is negative, we set to be
and again shift the hole left displacing only
characters.
6.046J/18.410J/SMA5503 Quiz 2 Solutions 14
– Set the two holes at each end of the hole segment to display the proper hole length.
"
R EAD :
– Return the character at HOLE - LENTH .
"
I NSERT :
– If points to a character, then the array is full so we use the constant amortized time
array doubling algorithm and return the new array with all items before in the first part
of the array, followed by half an array of holes, followed by the items that were after
in the intial array. We set to point to the beginning of the holes.
– Insert the character into the hole that points to, and increment , and decrement the
hole size characters.
I argue that each of these procedures maintains our invariant and keeps the characters in order in
the proposed running times.
N EW-F ILE trivially maintains the invariant as it sets the cursor to point to the leftmost hole, runs
in constant time and keeps all characters in order.
In M OVE -C URSOR we take constant time to calculate the distance to move and then take
time to shift the displaced characters. The time to shift these characters is not dependent on the
number of holes because holes do not need to be shifted as they are all identical. Furthermore it
takes only constant time to fix the end hole lengths to their proper values. We do not change the
order of any characters, and continues to point to the beginning of the hole segment.
R EAD also maintains our invariant because it does not change the state of the system, and
returns the next real character after the hole as it should.
I NSERT also maintains the invariant. If the array is full then it will be doubled to a new
array in constant amortized time, and the pointer position will be maintain. Then the character
will simply be inserted into the first hole in constant time and incremented so that it points to the
beginning of the hole.
Clearly, this implementation requires at most space for any characters in the system and so
it uses space
.
Optimization We can optimize this algorithm further to not take as many actual operations al-
though the asymptotic running time will be the same. We do this by not moving the hole every
time a M OVE -C URSOR
is called, but instead wait until I NSERT is called to move the
hole to where we need it. Since we can store up potential for every time M OVE -C URSOR
is called, the worst we will have to move the hole to insert is as far as the cursor has moved, which
is already stored potential. In some cases we may even move the hole less if the cursor moves in
both directions and the final position is closer than other points it reached along its path and then
we save operations.