Docsity

Asymptotic Practice Problem Set 1 with Solutions, Exercises of Algorithms and Programming

Toggle sidebar
bg1
Introduction to Algorithms: 6.006
Massachusetts Institute of Technology September 16, 2011
Professors Erik Demaine and Srini Devadas Problem Set 1 Solutions
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:
f1(n) = n0.999999 log n
f2(n) = 10000000n
f3(n) = 1.000001n
f4(n) = n2
Solution: The correct order of these functions is f1(n), f2(n), f4(n), f3(n). To see
why f1(n)grows asymptotically slower than f2(n), recall that for any c > 0,log nis
O(nc). Therefore we have:
f(n) = n0.999999 0
1log n=O(n.999999 ·n0.000001) = O(n) = O(f2(n))
The function f2(n)is linear, while the function f4(n)is quadratic, so f2(n)is O(f4(n)).
Finally, we know that f3(n)is exponential, which grows much faster than quadratic,
so f4(n)is O(f3(n)).
(b) [5 points] Group 2:
f1(n) = 221000000
f2(n) = 2100000n
n
f3(n) = 2
f4(n) =
n√
n
Solution: The correct order of these functions is f1(n), f4(n), f3(n), f2(n). The vari-
able nnever appears in the formula for f1(n), so despite the multiple exponentials,
f1(n)is constant. Hence, it is asymptotically smaller than f4(n), which does grow
with n. We may
rewrite √
the formula for f4(n)to be f4(n) = n n =n1.5. The
value of f3(n) = n
2is given by the formula n(n−1)/2, which is Θ(n2). Hence,
f1.5 2
4(n) = n=O(n) = O(f3(n)). Finally, f2(n)is exponential, while f3(n)is
quadratic, meaning that f3(n)is O(f2(n)).
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Asymptotic Practice Problem Set 1 with Solutions and more Exercises Algorithms and Programming in PDF only on Docsity!

Introduction to Algorithms: 6. Massachusetts Institute of Technology September 16, 2011 Professors Erik Demaine and Srini Devadas Problem Set 1 Solutions

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

  1. (^5). The value of f 3 (n) = n 2 is given by the formula n(n − 1)/ 2 , which is Θ(n^2 ). Hence, f 4 (n) = n^1.^5 = O(n^2 ) = O(f 3 (n)). Finally, f 2 (n) is exponential, while f 3 (n) is quadratic, meaning that f 3 (n) is O(f 2 (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).

  1. Θ(log n).
  2. Θ(n).
  3. Θ(n log n).

(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).

  1. Θ(log n).
  2. Θ(n).
  3. Θ(n log n).
  4. Θ(n log^2 n).
  5. Θ(n^2 ).
  6. Θ(2n). Solution: The correct answer here is Θ(n). To see why, we want to first eliminate the mutually recursive recurrence relations. To do so, we will replace all references to the function S(x, y) with the definition of S(x, y). This yields the following recurrence relation for T (x, 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.

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. Solution: Yes. This is the Θ(n log n) algorithm whose proof was sketched in Lecture
  3. A more rigorous version of the proof of correctness was included in this homework.

(b) [4 points] Is algorithm2 correct?

  1. Yes.
  2. No. Solution: Yes. This is the same as the greedy ascent algorithm presented in Lecture 1. The algorithm will always eventually return a location, because the value of location that it stores strictly increases with each recursive call, and there are only a finite number of values in the grid. Hence, it will eventually return a value, which is always a peak. (c) [4 points] Is algorithm3 correct?
  3. Yes.
  4. No. Solution: No. To see that this is true, try running the algorithm on the counterexample given in the solutions to Problem 1-6. (d) [4 points] Is algorithm4 correct?
  5. Yes.
  6. No. Solution: Yes. See Problem 1-5 for a proof of correctness.

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. Θ(log n).
  2. Θ(n).
  3. Θ(n log n).
  4. Θ(n log^2 n).
  5. Θ(n^2 ).
  6. Θ(2n). Solution: The worst-case runtime of algorithm1 is Θ(n log n), as explained in Lecture 1. (b) [4 points] What is the worst-case runtime of algorithm2 on a problem of size n × n?
  7. Θ(log n).
  8. Θ(n).
  9. Θ(n log n).
  10. Θ(n log^2 n).
  11. Θ(n^2 ).

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.

  1. If algorithm1 returns a location, it will be a peak in the original problem. If algorithm1 returns a location (r 1 , c 1 ), then that location must have the best value in column c 1 , and must have been a peak within some recursive subproblem. Assume, for the sake of contradiction, that (r 1 , c 1 ) is not also a peak within the original problem. Then as the location (r 1 , c 1 ) 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 (r 1 , c 1 ) must be adjacent to the dividing column c 2 (where |c 1 − c 2 | = 1), and the values must satisfy the inequality val(r 1 , c 1 ) < val(r 1 , c 2 ). Let (r 2 , c 2 ) be the location of the maximum value found by algorithm1 in the divid- ing column. As a result, it must be that val(r 1 , c 2 ) ≤ val(r 2 , c 2 ). Because the algorithm chose to recurse on the half containing (r 1 , c 1 ), we know that val(r 2 , c 2 ) < val(r 2 , c 1 ). Hence, we have the following chain of inequalities:

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:

  1. There is more variation in the sizes of the recursive subproblems, because we can split by rows as well as by columns.
  1. Because we can split by rows or columns, it is no longer true that the number of columns is always strictly decreasing. With every step, either the number of rows or the number of columns strictly decreases, as long as both are greater than zero. However, this does still mean that the algorithm must either return a location, or examine an empty subproblem.
  2. It is no longer true that the size of the problem just prior to examining an empty subproblem is m × 1 or m × 2. If the algorithm splits on rows instead of columns just prior to examining an empty subproblem, then the size of the problem must be 1 × n or 2 × n. Even so, the analyses of the two cases (splitting on rows versus splitting on columns) are nearly identical.
  3. The second part of the proof diverges more from the proof given for algorithm1. We no longer know that the value returned by the algorithm is the maximum in some row or column of the original problem. Instead, we base our argument about the correctness of the algorithm on the use of the bestSeen variable, which contains the location of the best value seen so far in the matrix. This lets us know that we will never choose to return a location that looks like a peak within the current subproblem, but is adjacent to some greater value just outside the subproblem.

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:

  1. If the peak problem is not empty, then the algorithm will always return a lo- cation. Say that we start with a problem of size m × n. Depending on whether the algorithm is splitting the rows or the columns, the recursive subproblem examined by the algorithm will have dimensions bm/ 2 c × n, (m − bm/ 2 c − 1) × n, m × bn/ 2 c, or m × (n − bn/ 2 c − 1). Therefore, with each recursive call, either the number of rows or the number of columns strictly decreases, as long as both are greater than zero. So the algorithm either halts and returns at some point, or eventually examines a subproblem with a non-positive number of rows or columns. The only way for the number of rows or columns to become strictly negative, according to the formulas that determine the size of the subproblem, is to have m = 0 or n = 0 at some point. So if the algorithm does not returns 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 the algorithm does examine an empty subproblem. Without loss of generality, say that the algorithm split the columns just prior to this. Then at that point in the algorithm it must have examined 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 return the location), or it has a strictly better neighbor in the other column (in which case the algorithm will recurse on the subproblem with

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.