Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
35
Добавлен:
20.03.2016
Размер:
716.61 Кб
Скачать

7.Let F (n) = n2 n + 550 and G(n) = 59n + 50 be function de ned on [0; 1): Determine n such that F takes less time at n than G.

7. Factor n2 n + 550 = 59n + 50 to see for n such that 10 < n < 50, we have G(n) > F (n):

9.Explain in words how the complexity of a sequence of statements is computed.

9. The complexity of a sequence of statements is computed by totaling the complexity for each of the statements.

11.Explain in words how the complexity of a loop of the form for i = 0 to n is computed.

11. The complexity of a loop of the form for i = 0 to n is found by adding together, one term for each of the n + 1 executions of the body of the loop and the complexity of the statements implied by the loop construct that determines if the loop is executed again or not. We must also add the complexity of the statements implied by the loop construct that allows the execution of the body of the loop the rst time.

Review Questions

Prove the following set of inclusions.

O(1) O(log(n)) O(pn) O(n) O(n log(n))

O(n2) O(2n) O(n 2n) O(3n) O(n!) O(nn) by solving Exercises 1 through 10. (Remember to show inequality.)

1.Prove that O(1) O(log(n)):

1. inclusion: ln(n) > 1 for all n > e: proper: For any c, ln(n) > c 1 for all n > ec:

3.Prove that O(pn) O(n):

3. inclusion: n > pn for all n > 1: proper: For any c > 0; c pN for all n > c2:

5.Prove that O(n log(n)) O(n2):

5. inclusion: 2) and 3) show that n > ln(n) for all n > 4; hence n2 > n ln(n) for all n > 4: proper: b) and c) show that for any c > 0; n > c ln(n) for all n > (2c)2; hence n2 > c n ln(n) for all n > (2c)2:

7.Prove that O(2n) O(n 2n):

7. Use Exercise 27 in Section 5.2.

9.Prove that O(3n) O(n!):

9. inclusion: For n > 3e; we have n=e > 2; so by Stirling's formula (this is an interesting formula to know about-you can prove the result directly)

3n < (n=e)n < n!:

proper: For any c > 0; choose n > maxfc3; 3eg: Then, c 3n < (n=e)n pn < n!:

Using Discrete Mathematics in Computer Science

1.Let F1; F2; G1; G2 : N ! N: Prove the following, or nd counterexamples:

(a)If F1 2 O(G1) and F2 2 O(G2), then F1 + F2 2 O(G1 + G2)

(b)If F1 2 O(G1) and F2 2 O(G2), then j F1 F2 j 2 O(j G1 G2 j)

(c) If F1 2 O(G1) and F2 2 O(G2), then F1 F2 2 O(G1 G2) where

F1 F2(x) = F1(x) F2(x):

(d)If F1 2 O(G1) and F2 2 O(G2), then F1 F2 2 O(G1 G2) where F1=F2(x) = F1(x)=F2(x) provided F2(x); G2 (x) 6= 0:

(e)If F1 2 O(G1) and F2 2 O(G2); then F1 F2 2 O(G1 G2).

1. (a) Since F1 2 O(G1); there are real numbers c1 and x1 such that

j F1(x) j c1j G1(x) j for all x > x1: Similarly, since F2 2

O(G2); there

are real numbers c2 and x2 such that j F2(x) j c2j G2(x) j

for all x > x2:

Let c = maxfc1; c2g and xc = maxfx1; x2g: Then j F1(x) + F2(x) j j F1(x) j + j F2(x) j c1j G1(x) j + c2j G2(x) j cjG1(x) + G2(x)j This string of inequalities is correct because all the functions have [0; 1] as their range. The conclusion follows with c and xc as described.

1. (b) Let F1(x) = x3, F2(x) = x2, G1(x) = G2(x) = x3: The result does not follow.

1. (c) Since F1 2 O(G1); there are real numbers c1 and x1 such that

j F1(x) j c1j G1(x) j for all x > x1: Similarly, since F2 2 O(G2); there are real numbers c2 and x2 such that j F2(x) j c2j G2(x) j for all x > x2: Let

xc = maxfx1; x2g and c = c1c2: Then j F1(x)F2 (x) j = j F1(x) j j F2 (x) j c1j G1(x) j c2 j G2(x) j = cj G1(x) G2(x) j: The inequalities follow because the range of all the functions is [0; N]:

1. (d) The functions de ned in (b) will provide a counterexample.

3.(a) Find the complexity of the following algorithm for computing factorials:

ALGORITHM: Compute n!

INPUT: n 2 N OUTPUT: n! x = 1

for i = 1 to n x = x i

print x

(b)Find the complexity of the following algorithm for computing the mean of n values.

ALGORITHM: Mean

INPUT: The number of values n and an array a[1::n] containing them

OUTPUT: the mean of the values a[1]; a[2]; :::; a[n]

sumOfValues = 0 for i = 1 to n

sumOfValues = sumOfValues + a[i] print sumOfValues=n

3. (a) The key operation is multiplication. O(ComputeF actorial(n)) = O(n)

3. (b) The key operation will be addition.

n

X

O(M ean) = 1 = n

i=1

5.Find the complexity for each of the following algorithms that evaluate a polynomial of degree n at a value x0. Assume multiplication is the key operation.

(a)

ALGORITHM: Evaluate P (x) = a0 + a1x + a2x2 + + anxn at x0

INPUT: n 2 N, the coe cients of P stored in a[0::n], and a real number x0

OUTPUT: P (x0)

Poly = a[0] x = 1

for i = 1 to n x = x0 x

Poly = a[i] x + Poly print Poly

(b)

ALGORITHM: Evaluate P (x) = a0 + a1x + a2x2 + + anxn at x0

INPUT: n 2 N, the coe cients of P stored in a[0..n], and a real number x0

OUTPUT: P (x0)

Poly = a[0] for i = 0 to n

x = 1

for j = 1 to i x = x x0

Poly = Poly + a[i] x print Poly

5. (a) The key operation is multiplication.

n

X

O(P (x0)) = O(1 + 2) = O(2n + 1)

i=1

5. (b) The key operation is multiplication.

n

i

n

X X

X

O(P (x0)) = O( (

1 + 1)) = O(2

i) = O(n(n + 1))

i=1 j=1

i=1

7.The Insertion Sort algorithm, that follows, is a good sorting algorithm to use when (i) the size of the list is very small (say < 10) or (ii) the list is already almost in order. In this respect, it is like bubble sort, but it is normally a bit better.

ALGORITHM: InsertionSort

INPUT: A list List of values, List[1]; : : : ; List[N ].

OUTPUT: The same elements, but in increasing order.

for positionToFix = 2 to N do

 

valueToPlace = List[positionToFix]

 

candidatePos = positionToFix

 

/* locate place to put valueToPlace,

*/

/* relative to previous elements,

*/

/* and open up room to insert it there. */

while candidatePos > 1 and List[candidatePos 1] > valueToPlace List[candidatePos] = List[candidatePos 1]

candidatePos = candidatePos 1 List[candidatePos] = valueToPlace

Calculate the complexity of theInsertionSort algorithm on an input List of size N . Let comparison of list elements be the key operation. (Hint: For this version, which sorts the list into increasing order, the worst-case behavior occurs when the list is in decreasing order.)

7. Suppose the input is the list fN; N 1; N 2; : : : ; 3; 2; 1g.

Show that the number of comparisons is less than N (N 1)=2. The outer loop is executed N 1 times. Each time through the outer loop, the inner loop is executed at most positionToFix 1 times, since candidatePos is decremented by 1 each time it the inner loop is run and stops when candidatePos reaches 1. The list element comparisons are all made in the inner loop, once each time through that loop. Thus the total number of list element comparisons is at most

0 + 1 + : : : + (N 1) = N (N 1) 2

Now, if the list is originally exactly out of order, say [N; N 1; N 2; : : : ; 3; 2; 1] then the inner loop test List[candidatePos 1] > valueToPlace can never be satis ed: each new element we insert is smaller than all previously inserted elements. Thus the maximum above is actually achieved in this case.

9.Challenge: This returns to an issue raised at the end of Section 2.5.6. Show that, for formulas in CNF, the shortest equivalent DNF 0 may be exponentially longer than . Stated more precisely:

(i) Count the length of a formula as the total number of symbols in the formula where a proposition letter pi is counted as one symbol and, if a single symbol occurs more than once in the formula, each occurrence is counted. So, the length of

(((p9;876;543;210 _ p9;876;543;210) _ p9;876;543;210) ^ (p1 _ :p9;876;543;210 ))

is 18: There are 5 occurrences of proposition letters, 4 open parentheses, 4 closed parentheses, 1 : sign, 3 _'s, and 1 ^.

(ii)For each CNF formula , compute the length dnf l( ) of the shortest DNF equivalent to . Now, for an integer n, de ne dnf l(n) to be the maximum value of dnf l( ) for all formulas of length less than or equal to n.

(iii)Show that for some real number r, rn 2 O(DN F L).

9.Consider the CNF formula

n = (p1 _ q1) ^ (p2 _ q2) ^ ^ (pn _ qn);

which abbreviates

(( ((p1 _ q1) ^ (p2 _ q2)) ^ )(pn _ qn)):

So n has 2n occurrences of proposition letters, 2n 1 ^'s and _'s, and 4n 2 proposition letters | 8n 3 symbols in all.

The simple-minded way to construct a DNF equivalent of n is to use the distributive law to distribute the _'s over the ^'s. This results in a formula with 2n terms:

(p1 ^ p2 ^ ^ pn 1 ^ pn) _

(p1 ^ p2 ^ ^ pn 1 ^ qn)

_

(p1 ^ p2 ^ ^ qn 1 ^ pn)

_

(p1 ^ p2 ^ ^ qn 1 ^ qn)

_ _

(q1 ^ q2 ^ ^ qn 1 ^ qn):

 

That formula has n2n occurrences of proposition letters, n2n 1 occurrences of ^ and _, and 2n2n 2 occurrences of parentheses | for 4n2n 3 symbols in all. We shall argue below that there is no shorter DNF formula equivalent to n.

First, assuming that result, compare log2 of the length of that shortest DNF formula with the length of the n: for all integers n > 264,

log2(2n 2n 3) > log2(2n 2n) = logn(n 2n+1) = (n + 1) log2(n) > 64(n + 1)

> 8(8n 3):

Thus 8 2m < 8(DN F L(m)) for all values m of the form 8n 3 | for, say all m > 267. It follows that 2m < 8(DN F L(m)) for all m > 267, giving the exponential bound.

Now we need to check that we did indeed nd a minimum length DNF equivalent for n. A DNF equivalent would be a formula of the form

1 ^ 2 ^ ^ k

where each i is a term | a conjunction of literals. (Indeed, it is the minimum length equivalent, but there is no reason for us to argue that here.)

Lemma 1. Each non-self-contradictory i must contain either pi or qi (or both) for each i n.

Proof: Suppose some i contains neither pj nor qj for some j. Pick any truth assignment I satisfying i (we assumed i is non-contradictory). De ne truth assignment I0 to be the same I except that I 0(pj ) = I0(qj ) = F . Since neither pj nor qj occurs in i, I0 must also satisfy i. But I0 does not satisfy pj _ qj , so I0 does not satisfy n, contradicting the assumption that n was logically equivalent to 1 ^ 2 ^ ^ k .

Lemma 2. There must at least 2n non-self-contradictory i's.

Proof. Consider the 2n truth assignments Ik which assign exactly one of each pair pi; qi to true. Each Ik satis es n, so it must satisfy some non-self-contradictory i. But suppose two of these truth assignments, Ik and Ik0 satisfy the same i: since Ik 6= Ik0 , there must be some j where Ik (pj ) = T Ik (qj ) = F , Ik0 (pj ) = F , and Ik0 (qj ) = T . But, by Lemma 1, either pj or qj must occur in i, and thus must be a conjunct of i. But then Ik and Ik0 cannot both satisfy i. Thus there must be at least 2k non-self-contradictory i's.

From Lemmas 1 and 2, we have (i) that each non-self-contradictory termi of the DNF formula must have at least n conjuncts, and (ii) that there must be at least 2n non-self-contradictory terms. Thus every DNF equivalent to n must be at least as long as the one we constructed.

11.Consider the following two algorithms to check whether a number n is prime. Let computation of Mod be the key operation.

The rst algorithm directly re ects the de nition of \prime." Primality testing has been used in cryptography (secret coding), particularly in building \public key cryptograms."

ALGORITHM: SimplestPrimalityTest

INPUT: An integer n > 1

OUTPUT: \prime" or \composite"

for d = 2 to n 1

if M od(n; d) = 0 output \composite" and stop. output prime.

ALGORITHM: ShortenedPrimalityTest

INPUT: an integer n > 1

OUTPUT: \prime" or \composite"

d = 2.

while (d2 n)

if M od(n; d) = 0 output \composite" and stop. d = d + 1.

output prime.

(a)Show that if n is a k digit prime number, SimplestPrimalityTest executes the key operation approximately 10k times.

(b)Suppose that cracking a public key cryptogram depends on nding

the smallest 50-digit prime. Suppose that a single execution mod of takes 0.01 nanoseconds (10 11 seconds)1 | and that the rest of the above program takes no time at all. Approximately how long would it take, in years, to verify that a 50 digit number is prime?

(c)If n is a k digit prime number, approximately how many times does ShortenedPrimalityTest execute the key operation? How much does this speed up our time from part (b)?

How much does this speed up our time from part (b)?

(d)Show that ShortenedPrimalityTest correctly determines whether the input integer n is prime.

1 That is sure to be an unrealistic assumption, no matter how fast microprocessors get: with numbers of this size, the amount of time to do one mod operation will almost certainly depend upon the length of the number.

In fact, Manindra Agrawal, Neeraj Kayal, and Nitin Saxena of the Indian Institute of Technology showed that primality can be tested in polynomial time in the number of digits in n.

More recent work has been based, not on testing primality, but on factoring large integers, which seems to be harder.

11. (a) The program has a single loop, over all integers from 2 to n 1; that gives at most n 3 repetitions in all. Since n was assumed to be prime, the loop will not terminate early, so there will be exactly n 3 repetitions. The mod operation is performed once each repetition, so it will be computed exactly n 3 times.

Since n is a k digit number, 10k n 10 10 k. So the number of times mod is calculated is between 10k 3 and 10 10k 3. Thus, for our O approximations, \approximately 10k" is a good approximation.

11. (b) The calculation is assumed to take about 1050 10 11 seconds | 1050 10 11=(60 60 24 365) years, about 1031 years. By comparison, the universe is believed to be less than 2 1010 years old.

11. (c) The loop would now be executed only up to the square root of n times | around 1025 times. That gives us about 106 years, \only" a few million years.

11. (d) The algorithm assumes that, if n is not prime, n has a divisor ( 1 and) the square root of n. We need to prove that that is true. Working toward a contradiction, assume that n is not prime, but that all its factors (other than 1 and n) are greater than the square root of n. Since n is

composite, it can be factored into the product of two smaller numbers, say n = d1d2; then d1; d2 > pn. But in that case, d1 d2 > pn pn = n,

contradicting the assumption that n = d1d2.

Соседние файлы в папке Student Solutions Manual