You are on page 1of 5

Introduction to Algorithms: 6.

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 le 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 nd 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.

Problem 1-1. [15 points] Asymptotic Practice For each group of functions, sort the functions in increasing order of asymptotic (big-O) complexity: (a) [5 points] Group 1: f1 (n) f2 (n) f3 (n) f4 (n) (b) [5 points] Group 2: f1 (n) = 22 f2 (n) = 2100000n 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
1000000

= = = =

n0.999999 log n 10000000n 1.000001n n2

f4 (n) =
i=1

(i + 1)

2 Problem 1-2. [15 points] Recurrence Relation Resolution For each of the following recurrence relations, pick the correct asymptotic runtime:

Problem Set 1

(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. (log n). (n). (n log n). (n log2 n). (n2 ). (2n ). for c 2, for c 2, and

(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. (log n). (n). (n log n). (n log2 n). (n2 ). (2n ). for c 2, for c 2, and

(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. (log n). (n). (n log n). (n log2 n). (n2 ). (2n ). = = = = (x) (x) + S (x, y/2), (y ) (y ) + T (x/2, y ). for c 2, for c 2, and

Problem Set 1

Peak-Finding
In Lecture 1, you saw the peak-nding 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 le called ps1.zip. In the le algorithms.py, there are four different algorithms which have been written to solve the peak-nding problem, only some of which are correct. Your goal is to gure out which of these algorithms are correct and which are efcient. 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 Efciency (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 ).

(b) [4 points] What is the worst-case runtime of algorithm2 on a problem of size n n? 1. (log n). 2. (n).

4 3. 4. 5. 6. (n log n). (n log2 n). (n2 ). (2n ).

Problem Set 1

(c) [4 points] What is the worst-case runtime of algorithm3 on a problem of size n n? 1. 2. 3. 4. 5. 6. (log n). (n). (n log n). (n log2 n). (n2 ). (2n ).

(d) [4 points] What is the worst-case runtime of algorithm4 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 1-5. [19 points] Peak-Finding Proof Please modify the proof below to construct a proof of correctness for the most efcient correct algorithm among algorithm2, algorithm3, and algorithm4. The following is the proof of correctness for algorithm1, which was sketched in Lecture 1. We wish to show that algorithm1 will always return a peak, as long as the problem is not empty. To that end, we wish to prove the following two statements: 1. If the peak problem is not empty, then algorithm1 will always return a location. Say that we start with a problem of size m n. The recursive subproblem examined by algorithm1 will have dimensions m n/2 or m (n n/2 1). Therefore, the number of columns in the problem strictly decreases with each recursive call as long as n > 0. So algorithm1 either returns a location at some point, or eventually examines a subproblem with a non-positive number of columns. The only way for the number of columns to become strictly negative, according to the formulas that determine the size of the subproblem, is to have n = 0 at some point. So if algorithm1 doesnt return a location, it must eventually examine an empty subproblem. We wish to show that there is no way that this can occur. Assume, to the contrary, that algorithm1 does examine an empty subproblem. Just prior to this, it must examine

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 nds 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 le giving a counterexample (i.e. a matrix for which the algorithm returns a location that is not a peak).

You might also like