MIT6 006F11 ps1
MIT6 006F11 ps1
006
Massachusetts Institute of Technology
Professors Erik Demaine and Srini Devadas
September 8, 2011
Problem Set 1
Problem Set 1
Both theory and programming questions are due Thursday, September 15 at 11:59PM.
Please download the .zip archive for this problem set, and refer to the README.txt file for
instructions on preparing your solutions. Remember, your goal is to communicate. Full credit will
be given only to a correct solution which is described clearly. Convoluted and obtuse descriptions
might receive low marks, even when they are correct. Also, aim for concise solutions, as it will
save you time spent on write-ups, and also help you conceptualize the key idea of the problem.
We will provide the solutions to the problem set 10 hours after the problem set is due, which
you will use to find any errors in the proof that you submitted. You will need to submit a critique
of your solutions by Tuesday, September 20th, 11:59PM. Your grade will be based on both your
solutions and your critique of the solutions.
=
=
=
=
n0.999999 log n
10000000n
1.000001n
n2
f1 (n) = 22
n
f2 (n) =
2100000
n
f3 (n) =
2
f4 (n) = n n
(c) [5 points] Group 3:
f1 (n) = n n
f2 (n) = 2n
f3 (n) = n10 2n/2
n
X
f4 (n) =
(i + 1)
i=1
Problem Set 1
Problem 1-2. [15 points] Recurrence Relation Resolution
For each of the following recurrence relations, pick the correct asymptotic runtime:
(a) [5 points] Select the correct asymptotic complexity of an algorithm with runtime
T (n, n) where
T (x, c) = (x)
T (c, y) = (y)
T (x, y) = (x + y) + T (x/2, y/2).
1.
2.
3.
4.
5.
6.
for c 2,
for c 2, and
(log n).
(n).
(n log n).
(n log2 n).
(n2 ).
(2n ).
(b) [5 points] Select the correct asymptotic complexity of an algorithm with runtime
T (n, n) where
T (x, c) = (x)
T (c, y) = (y)
T (x, y) = (x) + T (x, y/2).
1.
2.
3.
4.
5.
6.
for c 2,
for c 2, and
(log n).
(n).
(n log n).
(n log2 n).
(n2 ).
(2n ).
(c) [5 points] Select the correct asymptotic complexity of an algorithm with runtime
T (n, n) where
T (x, c)
T (x, y)
S(c, y)
S(x, y)
1.
2.
3.
4.
5.
6.
=
=
=
=
(x)
(x) + S(x, y/2),
(y)
(y) + T (x/2, y).
(log n).
(n).
(n log n).
(n log2 n).
(n2 ).
(2n ).
for c 2,
for c 2, and
Problem Set 1
Peak-Finding
In Lecture 1, you saw the peak-finding problem. As a reminder, a peak in a matrix is a location
with the property that its four neighbors (north, south, east, and west) have value less than or equal
to the value of the peak. We have posted Python code for solving this problem to the website in
a file called ps1.zip. In the file algorithms.py, there are four different algorithms which
have been written to solve the peak-finding problem, only some of which are correct. Your goal is
to figure out which of these algorithms are correct and which are efficient.
Problem 1-3. [16 points] Peak-Finding Correctness
(a) [4 points] Is algorithm1 correct?
1. Yes.
2. No.
(b) [4 points] Is algorithm2 correct?
1. Yes.
2. No.
(c) [4 points] Is algorithm3 correct?
1. Yes.
2. No.
(d) [4 points] Is algorithm4 correct?
1. Yes.
2. No.
Problem 1-4. [16 points] Peak-Finding Efficiency
(a) [4 points] What is the worst-case runtime of algorithm1 on a problem of size
n n?
1.
2.
3.
4.
5.
6.
(log n).
(n).
(n log n).
(n log2 n).
(n2 ).
(2n ).
Problem Set 1
3.
4.
5.
6.
(n log n).
(n log2 n).
(n2 ).
(2n ).
(log n).
(n).
(n log n).
(n log2 n).
(n2 ).
(2n ).
(log n).
(n).
(n log n).
(n log2 n).
(n2 ).
(2n ).
Problem Set 1
a subproblem of size m 1 or m 2. If the problem is of size m 1, then calculating
the maximum of the central column is equivalent to calculating the maximum of the
entire problem. Hence, the maximum that the algorithm finds must be a peak, and it
will halt and return the location. If the problem has dimensions m 2, then there are
two possibilities: either the maximum of the central column is a peak (in which case
the algorithm will halt and return the location), or it has a strictly better neighbor in the
other column (in which case the algorithm will recurse on the non-empty subproblem
with dimensions m 1, thus reducing to the previous case). So algorithm1 can
never recurse into an empty subproblem, and therefore algorithm1 must eventually
return a location.
2. If algorithm1 returns a location, it will be a peak in the original problem. If
algorithm1 returns a location (r1 , c1 ), then that location must have the best value
in column c1 , and must have been a peak within some recursive subproblem. Assume,
for the sake of contradiction, that (r1 , c1 ) is not also a peak within the original problem.
Then as the location (r1 , c1 ) is passed up the chain of recursive calls, it must eventually
reach a level where it stops being a peak. At that level, the location (r1 , c1 ) must be
adjacent to the dividing column c2 (where |c1 c2 | = 1), and the values must satisfy the
inequality val(r1 , c1 ) < val(r1 , c2 ).
Let (r2 , c2 ) be the location of the maximum value found by algorithm1 in the dividing column. As a result, it must be that val(r1 , c2 ) val(r2 , c2 ). Because the algorithm
chose to recurse on the half containing (r1 , c1 ), we know that val(r2 , c2 ) < val(r2 , c1 ).
Hence, we have the following chain of inequalities:
val(r1 , c1 ) < val(r1 , c2 ) val(r2 , c2 ) < val(r2 , c1 )
But in order for algorithm1 to return (r1 , c1 ) as a peak, the value at (r1 , c1 ) must
have been the greatest in its column, making val(r1 , c1 ) val(r2 , c1 ). Hence, we have
a contradiction.
Problem 1-6. [19 points] Peak-Finding Counterexamples
For each incorrect algorithm, upload a Python file giving a counterexample (i.e. a matrix for which
the algorithm returns a location that is not a peak).
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.