Introduction to Algorithms December 20, 2005 Massachusetts Institute of Technology 6.046J/18.410J

advertisement
Introduction to Algorithms
Massachusetts Institute of Technology
Professors Erik D. Demaine and Charles E. Leiserson
December 20, 2005
6.046J/18.410J
Final Exam
Final Exam
�
�
�
�
�
�
�
�
�
�
Do not open this exam booklet until you are directed to do so. Read all the instructions on
this page.
When the exam begins, write your name on every page of this exam booklet.
This exam contains 11 problems, some with multiple parts. You have 180 minutes to earn
180 points.
This exam booklet contains 20 pages, including this one. Two extra sheets of scratch paper
are attached. Please detach them before turning in your exam at the end of the examination
period.
This exam is closed book. You may use two handwritten A4 or crib sheets. No
calculators or programmable devices are permitted.
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 may be separated for grading.
Do not waste time and paper rederiving facts that we have studied. It is sufficient to cite
known results.
Do not spend too much time on any one 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!
Name:
6.046J/18.410J Final Exam
Name
2
Grade sheet: Do not write in the spaces below.
Problem Title
Parts Points Grade Grader
1
Recurrences
3
15
2
Algorithms and running times
1
9
3
Substitution method
1
10
4
True or False, and Justify
7
35
5
Red-black trees
3
15
6
Wiggly arrays
1
10
7
Difference constraints
2
12
8
Amortized increment
1
12
9
Minimum spanning trees
1
12
10 Multithreaded scheduling
1
10
11 Transposing a matrix
4
40
Total
180
6.046J/18.410J Final Exam
Name
Problem 1. Recurrences [15 points] (3 parts)
3
Give a tight asymptotic upper bound ( � notation) on the solution to each of the following recur­
rences. You need not justify your answers.
(a)
(b)
(c)
% !" %
where
.
$#%'&
if $()%+*
if is a variable independent from .
6.046J/18.410J Final Exam
Name
4
Problem 2. Algorithms and running times [9 points]
Match each algorithm below with the tightest asymptotic upper bound for its worst-case running
time by inserting one of the letters A, B, ��� , I into the corresponding box. For sorting algorithms,
is the number of input elements. For matrix algorithms, the input matrix has size . For
graph algorithms, the number of vertices is , and the number of edges is !" .
You need not justify your answers. Some running times may be used multiple times or not at all.
Because points will be deducted for wrong answers, do not guess unless you are reasonably sure.
Insertion sort
A: �
Heapsort
B: �
B UILD -H EAP
C: �
Strassen’s
D: �
Bellman-Ford
E: �
Depth-first search
F: �
Floyd-Warshall
G: �
Johnson’s
H: �
Prim’s
I: �
6.046J/18.410J Final Exam
Name
5
Problem 3. Substitution method [10 points]
Use the substitution method to prove a tight asymptotic lower bound ( -notation) on the solution
to the recurrence
�
) �
6.046J/18.410J Final Exam
Name
6
Problem 4. True or False, and Justify [35 points] (7 parts)
Circle T or F for each of the following statements to indicate whether the statement is true or
false, respectively. If the statement is correct, briefly state why. If the statement is wrong, explain
why. The more content you provide in your justification, the higher your grade, but be brief. Your
justification is worth more points than your true-or-false designation.
T F Let � , � , and � be three sorted arrays of real numbers (all distinct). In the comparison
model, constructing a balanced binary search tree of the set � � � requires time.
�
T F Let be a complete binary tree with nodes. Finding a path from the root of to a given
vertex using breadth-first search takes � time.
6.046J/18.410J Final Exam
Name
7
of integers, building a max-heap out of the elements of
T F Given an unsorted array �� �� � can be performed asymptotically faster than building a red-black tree out of the elements
of � .
T F Suppose we use a hash function to hash distinct keys into an array of length .
Assuming
simple uniform hashing, the expected number of colliding pairs of elements is
! .
6.046J/18.410J Final Exam
Name
T F Every sorting network with inputs has depth
�
8
.
T F If a dynamic-programming problem satisfies the optimal-substructure property, then a lo­
cally optimal solution is globally optimal.
6.046J/18.410J Final Exam
&
Name
9
be a directed graph with negative-weight edges, but no negative-weight
T F Let � to all faster
cycles. Then, one can compute all shortest paths from a source than Bellman-Ford using the technique of reweighting.
6.046J/18.410J Final Exam
Name
10
Problem 5. Red-black trees [15 points] (3 parts)
& & &� & & & � &
to the nodes of the binary search tree below so
(a) Assign the keys that they satisfy the binary-search-tree property.
nil
nil
nil
nil
nil
nil
nil
nil
nil
(b) Explain why this binary search tree cannot be colored to form a legal red-black tree.
6.046J/18.410J Final Exam
Name
(c) The binary search tree can be transformed into a red-black tree by performing a single
rotation. Draw the red-black tree that results, labeling each node with “red” or “black.”
Include the keys from part (a).
11
6.046J/18.410J Final Exam
Name
Problem 6. Wiggly arrays [10 points]
(
(
12
(
An array � � �� is wiggly if � � �� � � � �� � ��� � � � � � .
Given an unsorted array � �� of real numbers, describe an efficient algorithm that outputs
a permutation � � � � of such that � is a wiggly array.
6.046J/18.410J Final Exam
Name
13
Problem 7. Difference constraints [12 points] (2 parts)
Consider the following linear-programming system of difference constraints (note that one con­
straint is an equality):
� � � � � � � � � ( � ( � ( � � (
� (
� ( � (
� (
�
(a) Draw the constraint graph for these constraints.
(b) Solve for the unknowns � , � , � , � , and � , or explain why no solution exists.
6.046J/18.410J Final Exam
Name
14
Problem 8. Amortized increment [12 points]
An array of bits (each array element is
or ) stores a binary number
To add (modulo ) to , we use the following procedure:
.
&
I NCREMENT 1 2 while and 3
do 4
5 if 6
then Given a number , define the potential of to be the number of ’s in the binary representation
of . For example, , because . Use a potential-function argument to prove
that the amortized cost of an increment is , where the initial value in the counter is .
6.046J/18.410J Final Exam
Name
15
Problem 9. Minimum spanning trees [12 points]
&
� , and
does,
Let � be a connected, undirected graph with &edge-weight
& & & function
assume all & edge weights are distinct. Consider a cycle ��� in � , where& and let be the edge in the cycle with the largest edge weight. Prove that not belong to the minimum spanning tree of � .
6.046J/18.410J Final Exam
Name
Problem 10. Multithreaded scheduling [10 points]
16
A greedy scheduler runs a multithreaded computation in � seconds on
seconds on processors. Is it possible that the computation has work length � ? Justify your answer.
�
�
processors and in � and critical-path
6.046J/18.410J Final Exam
Name
17
Problem 11. Transposing a matrix [40 points] (4 parts)
� �
Let
be an
T
:
� & & T RANS 1 for to
2
do for 3
do
matrix, where
is an exact power of . The following code computes
to& &
� � � �
%
Consider the cache-oblivious two-level memory model with a cache of
elements and blocks of
elements. Assume that both& matrices
& & & and
& & are & stored& in& row-major
& & order,
& & that& is, &the linear
& &
order& of & in& memory
is
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
&
� � �� � , and similarly for .
� �
� �
�
(a) Analyze the number MT �
�
�
�
of memory transfers incurred by T RANS when
� %
.
6.046J/18.410J Final Exam
Name
18
Now, consider the following divide-and-conquer algorithm for computing the transpose:
� & &
�
�
�
�
�
�
R-T RANS 1 if &
&
� 2
then � 3
else Partition into four "
4
Partition into& four& "
5
R-T RANS & & " 6
R-T RANS & & " 7
R-T RANS & & " 8
R-T RANS " " "
submatrices
submatrices
Assume that the cost of partitioning is �
.
(b) Give and solve a recurrence
% for the number MT R-T RANS when
.
, and � .
� , , � , , � , and
.
of memory transfers incurred by
6.046J/18.410J Final Exam
Name
19
The following multithreaded algorithm computes the transpose in parallel:
� & & �
�
P-T RANS 1 if &
&
� 2
then � 3
else Partition into four " 4
Partition into four "
& 5
spawn P-T RANS &
6
spawn P-T RANS & 7
spawn P-T RANS & 8
spawn P-T RANS 9
sync
�
�
�
�
" & "
& " & " & " " submatrices
submatrices
, and � .
� , , � , , � , and
.
(c) Give and solve recurrences describing the work and critical-path length "
of P-T RANS . What is the asymptotic parallelism of the algorithm?
6.046J/18.410J Final Exam
Name
20
Professor Kellogg inadvertantly places two additional sync statements into his code as follows:
& &
K-T RANS 1 if &
&
2
then 3
else Partition into four "
4
Partition into four "
&
5
spawn K-T RANS 6
sync
&
7
spawn K-T RANS 8
sync
&
9
spawn K-T RANS &
10
spawn K-T RANS 11
sync
" & " "
& "
&
& "
"
submatrices
submatrices
, and .
, , , , , and
.
(d) Give and solve recurrences describing the work and critical-path length "
of K-T RANS . What is the asymptotic parallelism of this algorithm?
SCRATCH PAPER — Please detach this page before handing in your exam.
SCRATCH PAPER — Please detach this page before handing in your exam.
Download