






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Introduction to Algorithms: 6. Massachusetts Institute of Technology September 16, 2011 Professors Erik Demaine and Srini Devadas Problem Set 1 Solutions
Problem 1-1. [15 points] Asymptotic Practice
For each group of functions, sort the functions in increasing order of asymptotic (big-O) complex- ity:
(a) [5 points] Group 1:
f 1 (n) = n^0.^999999 log n f 2 (n) = 10000000 n f 3 (n) = 1. 000001 n f 4 (n) = n^2
Solution: The correct order of these functions is f 1 (n), f 2 (n), f 4 (n), f 3 (n). To see why f 1 (n) grows asymptotically slower than f 2 (n), recall that for any c > 0 , log n is O(nc). Therefore we have:
f 1 (n) = n^0.^999999 log n = O(n^0.^999999 · n^0.^000001 ) = O(n) = O(f 2 (n))
The function f 2 (n) is linear, while the function f 4 (n) is quadratic, so f 2 (n) is O(f 4 (n)). Finally, we know that f 3 (n) is exponential, which grows much faster than quadratic, so f 4 (n) is O(f 3 (n)). (b) [5 points] Group 2:
f 1 (n) = 22 1000000 f 2 (n) = 2100000 n n f 3 (n) = 2 f 4 (n) =
n
n
Solution: The correct order of these functions is f 1 (n), f 4 (n), f 3 (n), f 2 (n). The vari- able n never appears in the formula for f 1 (n), so despite the multiple exponentials, f 1 (n) is constant. Hence, it is asymptotically smaller than f 4 (n), which does grow with n. We may (^) (rewrite
) the formula for^ f^4 (n)^ to be^ f^4 (n) =^ n^ n^ =^ n
(c) [5 points] Group 3: √ f 1 (n) = n n f 2 (n) = 2 n f 3 (n) = n^10 n
· 2 n/^2
f 4 (n) =
(i + 1) i= Solution: The correct ordering of these functions is f 4 (n), f 1 (n), f 3 (n), f 2 (n). To see why, we first use the rules of arithmetic series to derive a simpler formula for f 4 (n): n f 4 (n) =
∑ (^) n((n + 1) + 2) n(n + 3) (i + 1) = = = Θ(n^2 ) i=1^2 √ This is clearly asymptotically smaller than f 1 ( n) = n n. Next, we want to compare f 1 (n), f 2 (n), and f 3 (n). To do so, we transform both f 1 (n) and f 3 (n) so that they look more like f 3 (n): √ f 1 (n) = n n^ = (2lg^ n^
√ ) n^
√ = 2 n·lg^ n f 3 (n) = n^10 · 2 n/^2
10 = 2lg(n^ )^ · 2 n/^2 = 2n/2+10 lg^ n
The exponent of the 2 in f 1 (n) is a function that grows more slowly than linear time; the exponent of the 2 in f 3 (n) is a function that grows linearly with n. Therefore, f 1 (n) = O(f 3 (n)). Finally, we wish to compare f 3 (n) with f 2 (n). Both have a linear function of n in their exponent, so it’s tempting to say that they behave the same asymptotically, but they do not. If c is any constant and g(x) is a function, then 2 cg(x)^ = (2c)g(x). Hence, changing the constant of the function in the exponent is the same as changing the base of the exponent, which does affect the asymptotic running time. Hence, f 3 (n) is O(f 2 (n)), but f 2 (n) is not O(f 3 (n)).
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) for c ≤ 2 , T (c, y) = Θ(y) for c ≤ 2 , and T (x, y) = Θ(x + y) + T (x/ 2 , y/2).
(c) [5 points] Select the correct asymptotic complexity of an algorithm with runtime T (n, n) where
T (x, c) = Θ(x) for c ≤ 2 , T (x, y) = Θ(x) + S(x, y/2), S(c, y) = Θ(y) for c ≤ 2 , and S(x, y) = Θ(y) + T (x/ 2 , y).
T (x, y) = Θ(x) + Θ(y/2) + T (x/ 2 , y/2).
We can rewrite this to eliminate the constants and get the recurrence T (x, y) = Θ(x + y) + T (x/ 2 , y/2). This is precisely the same recurrence relation as seen in part (a) of this problem, so it must have the same complexity.
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?
(b) [4 points] Is algorithm2 correct?
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?
examined by algorithm1 will have dimensions m × bn/ 2 c or m × (n − bn/ 2 c − 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 even- tually 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 de- termine the size of the subproblem, is to have n = 0 at some point. So if algorithm doesn’t 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 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.
val(r 1 , c 1 ) < val(r 1 , c 2 ) ≤ val(r 2 , c 2 ) < val(r 2 , c 1 )
But in order for algorithm1 to return (r 1 , c 1 ) as a peak, the value at (r 1 , c 1 ) must have been the greatest in its column, making val(r 1 , c 1 ) ≥ val(r 2 , c 1 ). Hence, we have a contradiction.
Solution: To prove that algorithm4 is correct, we make the following modifications to the proof of correctness for algorithm1:
The result of these changes, written more rigorously, is the following proof:
We wish to show that the algorithm will always return a peak. To that end, we wish to prove the following two statements:
MIT OpenCourseWare http://ocw.mit.edu
6.00 6 Introduction to Algorithms Fall 2011
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.