Docsity

Homework 1 Solutions - Algorithm Design | ECS 122A, Assignments of Computer Science

Toggle sidebar
bg1
HW 1
Due Wds Oct 12 ECS 122A, Fall 2005
Problem 1:
The algorithm remains mostly the same. We divide the points into two sets of size dn/2eand bn/2c,PLand
PR, with PLon or to the left of a vertical dividing line and PRon or to the right of the vertical dividing line.
We recursively find the smallest circle containing at least three points in each of PLand in PR. Let δbe
the smaller of the diameters of these two circles. A circle of diameter < δ which contains points of both PL
and PRmust be completely contained in a vertical strip of width 2δaround the vertical dividing line, and
within this strip it must be contained in a rectangle of height δ. Such a rectangle consists of two squares,
one on the left of the dividing line and one on the right. To prove that the algorithm runs in O(nlgn) time,
we need only to prove that such a rectangle can contain at most a constant number of points of PLor PR.
This seems intuitively true. If we try to put a whole bunch of points into a δ×δsquare, say 15, we notice
that we are forced to have some three points contained in a circle of diameter less than δ. On the other
hand, it is perfectly possible for two points to be very close together, so we can’t just use the argument from
the closest pair algorithm that we gave in class.
There are many correct ways to prove that a δ×δsquare cannot contain more than a constant number
of points if no three points are allowed to be in a circle of diameter < δ. An easy one is illustrated in the
picture above. Every point in the square is contained in at least one of the three circles, each of which has
diameter (1 −1/100)δ. But each of the circles may contain at most two points. So the square can contain
at most eight points.
Problem 2:
Show that the recurrence T(n) = T(n/2) + a(nlg n) solves to O(nlg2n). Assume that T(1) is O(1).
To prove that T(n) = O(nlg2n), we prove that there exist two constants c > 0n > n0, for all n > n0,
T(n)≤cn lg2n. We make the inductive assumption that this bound holds for n/2, that is, that T(n/2) ≤
c(n/2) lg2(n/2). Substituting this into the recurrence yields
T(n)=2T(n/2) + a(nlg n)
≤2c(n/2) lg2(n/2) + a(nlg n)
=cn(lg n−1)2+an lg n
=cn lg2n+ (an lg n−2cn lg n+cn)
≤cn lg2n+nlg n(a−c) (1)
≤cn lg2n, (2)
where (1) holds when n≥2, and (2) holds as long as c > a. So we choose c=a+ 1 and n0= 2.
Problem 3:
Show that logkn=O(log2n), for any constant k > 1.
We know that k= 2lg2k, so
2lg2n=n=klgkn= (2lg2k)lgkn= 2lg2klgkn
So lg2n= lg2klgkn, thus we can have lgknis O(lg2) with constant c= 1/log2k. By the way, 1/lg2k= lgk2.
Can you prove this?
1
pf3

Partial preview of the text

Download Homework 1 Solutions - Algorithm Design | ECS 122A and more Assignments Computer Science in PDF only on Docsity!

HW 1

Due Wds Oct 12 ECS 122A, Fall 2005

Problem 1:

The algorithm remains mostly the same. We divide the points into two sets of size dn/ 2 e and bn/ 2 c, PL and PR, with PL on or to the left of a vertical dividing line and PR on or to the right of the vertical dividing line. We recursively find the smallest circle containing at least three points in each of PL and in PR. Let δ be the smaller of the diameters of these two circles. A circle of diameter < δ which contains points of both PL and PR must be completely contained in a vertical strip of width 2δ around the vertical dividing line, and within this strip it must be contained in a rectangle of height δ. Such a rectangle consists of two squares, one on the left of the dividing line and one on the right. To prove that the algorithm runs in O(nlgn) time, we need only to prove that such a rectangle can contain at most a constant number of points of PL or PR. This seems intuitively true. If we try to put a whole bunch of points into a δ × δ square, say 15, we notice that we are forced to have some three points contained in a circle of diameter less than δ. On the other hand, it is perfectly possible for two points to be very close together, so we can’t just use the argument from the closest pair algorithm that we gave in class.

There are many correct ways to prove that a δ × δ square cannot contain more than a constant number of points if no three points are allowed to be in a circle of diameter < δ. An easy one is illustrated in the picture above. Every point in the square is contained in at least one of the three circles, each of which has diameter (1 − 1 /100)δ. But each of the circles may contain at most two points. So the square can contain at most eight points.

Problem 2:

Show that the recurrence T (n) = T (n/2) + a(n lg n) solves to O(n lg^2 n). Assume that T (1) is O(1). To prove that T (n) = O(n lg^2 n), we prove that there exist two constants c > 0 n > n 0 , for all n > n 0 , T (n) ≤ cn lg^2 n. We make the inductive assumption that this bound holds for n/2, that is, that T (n/2) ≤ c(n/2) lg^2 (n/2). Substituting this into the recurrence yields

T (n) = 2 T (n/2) + a(n lg n) ≤ 2 c(n/2) lg^2 (n/2) + a(n lg n) = cn(lg n − 1)^2 + an lg n = cn lg^2 n + (an lg n − 2 cn lg n + cn) ≤ cn lg^2 n + n lg n(a − c) (1) ≤ cn lg^2 n, (2)

where (1) holds when n ≥ 2, and (2) holds as long as c > a. So we choose c = a + 1 and n 0 = 2.

Problem 3:

Show that logk n = O(log 2 n), for any constant k > 1. We know that k = 2lg^2 k, so

2 lg^2 n^ = n = klgk^ n^ = (2lg^2 k)lgk^ n^ = 2lg^2 k^ lgk^ n

So lg 2 n = lg 2 k lgk n, thus we can have lgk n is O(lg 2 ) with constant c = 1/ log 2 k. By the way, 1/ lg 2 k = lgk 2. Can you prove this?

Problem 4:

Prove that an array is Monge if and only if for all i = 1, 2 , ..., m − 1 and j = 1, 2 , ..., n − 1, we have Ai,j + Ai+1,j+1 ≤ Ai,j+1 + Ai+1,j. Base case: if a 2 × 2 array is Monge, Ai,j + Ai+1,j+1 ≤ Ai,j+1 + Ai+1,j. This is obvious by inspection. Now we’ll show that it’s true for an m × 2, for all m > 2, by induction. Our inductive assumption is that it’s true for an m × 2 array. Thus for any row i, Ai, 1 + Am, 2 ≤ Am, 1 + Ai, 2. If we add a new row m + 1 which is Monge with respect to row m, Am, 1 + Am+1, 2 ≤ Am+1, 1 + Am, 2. Then Ai, 1 − Ai, 2 ≤ Am, 1 − Am, 2 , and Am+1, 1 − Am+1, 2 ≥ Am, 1 − Am, 2. Thus Ai, 1 − Ai, 2 ≤ Am, 1 − Am, 2 ≤ Am+1, 1 − Am+1, 2 , and Ai, 1 + Am+1, 2 ≤ Ai, 2 + Am+1, 1. Therefore for it is true for all (m + 1) × 2 arrays. The same argument can be applied for 2 × n arrays. To show it’s true for m × n arrays, we follow a similar inductive proof: Base case: A m × 2 array is Monge, for all i = 1, 2 , ..., m − 1, if we have Ai, 1 + Ai+1, 2 ≤ Ai, 2 + Ai+1, 2 , as we showed above. Our inductive assumption is that a m × n array is Monge. We need to show that if for all i = 1, 2 , ..., m − 1 and j = 1, 2 , ..., n, Ai,j + Ai+1,j+1 ≤ Ai,j+1 + Ai+1,j , the m × n + 1 array is also Monge: We know that for any i ∈ 1 , 2 , ..., m − 1 and for any j ∈ 1 , 2 , ..., n − 1, k ∈ j + 1, j + 2, ..., n − 1, Ai,j +Am,k ≤ Am,j + Ai,k. Add a column n + 1, ensuring that it is Monge with respect to column n: Aj,n + Ak,n+1 ≤ Aj,n+1 + Ak,n. Then Ai,n − Ak,n ≤ Aj,n+1 − Ak,n+1. By the inductive assumption, we know that Aj,i + Ak,n ≤ Ak,i − Aj,n, so we have Aj,i − Ak,i ≤ Aj,n − Ak,n. Thus Aj,i − Ak,i ≤ Aj,n+1 − Ak,n+1, and Aj,i + Ak,n+1 ≤ Aj,n+1 + Ak,i.

4.b.

We know that the 2×2 subarray

∣ is not Monge, because 23+7^ >^ 6+22. Changing the upper-right

element will do, without breaking any constraints. Thus, for example, the following array is Monge:∣ ∣ ∣ ∣ ∣ ∣ ∣ ∣ ∣ ∣ 37 23 24 32 21 6 7 10 53 34 30 31 32 13 9 6 43 21 15 8

4.c. Prove that f (1) ≤ f (2) ≤ ... ≤ f (m) for any m × n Monge array.

Proof: Assume that for some m × n Monge array, there exists some row i such that f (i) is not less than f (i + 1). Let j = f (i + 1), and k = f (i). Since f (i) is the index of the minimum value of row i, and f (i + 1) is the index of the minimum value of row i + 1, there can be no pair of values in rows i, i + 1 whose sum is less than or equal to Ai+1,j + Ai,k. But due to the definition of Monge, we know Ai,j + Ai+1,k+1 ≤ Ai+1,j + Ai,k. Therefore there cannot be a Monge array such that f (i) > f (i + 1).

4.d. Explain how to compute the leftmost minimum in the odd-numbered rows

of A in O(m + n) time.

We know from section 4.c that the leftmost minimum of row i is less than or equal to the leftmost minimum of i + 1. We further know the leftmost minimum of the even rows. Given f (2), we find f (1) as follows: begin with column f (2), and scan leftwards in row 1 until we find its minimum. Similarly, for row 3, we scan leftwards until we reach index f (1). We must scan all n columns, and at worst we must scan all m rows. Thus this algorithm takes O(n + m) time.