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

5.2 Exercises

1.Find a real number c and an N0 2 N such that n2 + 5n < cn2 for all n 2 N with n N0:

1. c = 2; N0 = 6

3.Find a real number c and an N0 2 N such that n2 + 3n < cn3 for all n 2 N with n N0:

3. c = 1; N0 = 1

5.(a) Find a real number c and an N0 2 N such that n2 < cn3 for all n 2 N with n N0:

(b)Find a real number c and an N0 2 N such that 5n < cn3 for all n 2 N with n N0:

5. (a) c = 1; N0 = 1

5.(b) c = 1; N0 = 4

7.Using the proof of the Corollary 1 to Theorem 1 as a model, nd a real number c and an N0 2 N such that 7n4 2 O(n4) for all n 2 N with n N0:

7.c = 7; N0 = 1

9.(a) Find a real number c and an N0 2 N such that 2n2 + 7n 2 O(n3).

(b) Find a real number c and an N0 2 N such that 3n2 7n 2 O(n3).

(c) Using part (a), part (b), and Theorem 3, nd a real number c and an N0 2 N to prove that 5n2 2 O(n3):

(d) Using part (a), part (b), and Theorem 3, nd a real number c and an N0 2 N to prove that j n2 14n j 2 O(n3):

(e) Using part (a), part (b), and Theorem 3, nd a real number c and an N0 2 N to prove that 5(2n2 + 7n) + 3(3n2 7n) 2 O(n3):

9.(a) c = 1; N0 = 4

9.(b) c = 1; N0 = 3

9.(c) c = 1 + 1; N0 = maxf3; 4g = 4

9. (d) c = 1 + 1; N0 = maxf3; 4g = 4 9. (e) c = 8; N0 = 4

11.(a) Find a real number c and an N0 2 N such that 2n2 5n 2 O(n3 +5n2):

(b)Find a real number c and an N0 2 N such that 6n3 + 5n2 2 O(n4):

(c)Using part (a), part (b), and Theorem 2, nd a real number c and an N0 2 N to prove that 2n2 5n 2 O(n4):

11.(a) c = 1; N0 = 3

11. (b) c = 12; N0 = 1

11.(c) c = 1; N0 = 3

13.Using the proof of Theorem 6 as a guide:

(a)Find a real number c and an N0 2 N such that n2 + 5n + 3 2 O(n3).

(b)Show that n3 62O(n2 + 5n + 3):

13. (a) c = 3=2; N0 = 20

13. (b) Choose k > 1 such that kc > N0. Then (kc)3 c((kc)2 +5(kc)+3). This is a contradiction.

15. Prove that pn 2 O(n) and that n 62O(pn).

15. Let n0 = 1 and c = 1: pn pnpn = 1 n for all n n0:

Suppose c and N0 exist such that n < cpn for all n > N0. But then, there is cm for some m > 2 such that

p

cm < c or cm < c

which is a contradiction.

17.Prove that ln(n) 2 O(n) but that n 62O(ln(n)) for n 2 N:

17. To show ln(n) 2 O(n) let c = 1 and N0 = 1: For the converse let c and N0 be given. Now, for n N0 chooses en > n > N0. For en we are led to the contradiction en < cn:

19.Prove that for all b; c > 1; O(logb) = O(logc):

19.

(loga b)(logb c) = (loga c)

21. Prove Theorem 4.

21. (a) If O(F ) O(G); then by Theorem 1 we have F 2 O(F ): By set inclusion, it follows that F 2 O(G):

Conversely, suppose that F G 2 O(G). Let H 2 O(F ). By Theorem 2, H 2 O(G). So, O(F ) O(G).

(b)Use part (a) twice.

(c)Theorem 1 implies that the relation is re exive. Part (b) implies that

the relation is symmetric. For transitivity, let (F; G); (G; H) 2 . Then F 2 O(G) and G 2 O(H), so F 2 O(H) by Theorem 2. Similarly,

H2 O(F ), since H 2 O(G) and G 2 O(F ). Therefore,(F; G) 2 .

23.Prove Theorem 8.

23. We show, in fact, that jxr j jxsj for x 1: We prove this by de ning a function H(x) = xs xr and proving that H(x) 0 for x 1:

First, observe that H(1) = 0: We now prove that H(x) is increasing for x 1; and this will imply the required condition on H(x):

To see that H(x) is increasing, we take the derivative of H and show that it is non-negative.

H0(x) = sxs 1 rxr 1

> rxs 1 rxr 1 (since s > r) = rxr 1(xs r 1):

We wish to show that H0(x) 0 for x 1: In the last expression, rxr 1 is clearly positive for x 1: The other factor is xs r 1: Since s r > 0 and x > 1; we have xs r 1 0: Thus, we have shown that H 0(x) > 0 for x > 1: This proves that H(x) is increasing for x 1; and thus that xs asymptotically dominates xr :

To show that G(x) = xr does not asymptotically dominate F (x) = xs; we must show that for each c > 0 there is no real number xc such that jxsj jxr j for all x xc: Let c > 0: Choose xc large enough so that the following inequality holds for all x xc and (s r) log(x) > log(c): Then

s log(x) r log(x) s log(x) log(xs) xs

>log(c)

>log(c) + r log(x)

>log(cxr )

>cxr

for all x > xc: But this implies that G(x) = xr does not asymptotically dominate F (x) = xs:

(b) We show rst that cax asymptotically dominates cbx: Since for all x > 0, we have ax > bx it follows from the assumption that c > 0 that cax > cbx for all x > 0, proving that cax asymptotically dominates cba:

We now show that cbx does not asymptotically dominate cax: If cbx did asymptotically dominate cax; then there would exist real numbers x0 and d such that cax dcbx for all x > x0: Taking logarithms base c of each side of this inequality, we get logc(d) + bx ax for all x > x0: But this

does not hold for x > logc (d) : This contradiction proves that cbx does not

(a b)

asymptotically dominate cax:

25. Consider a polynomial

P (n) = aknk + ak 1nk 1 + ::: + a2n2 + a1n + a0

as a function from R to R: Show how to compute a number N so that all roots of P (n) = 0 occur between N and N . Prove that your answer is correct.

25. First, suppose that ak = 1. For x > M , let M = maxf1; Pk 1 j ai jg+

i=0

1:

 

k

 

 

X

 

f (x) =

aixi; (ak = 1)

 

i=0

 

 

k 1

 

 

X

 

=

xk +

aixi

 

i=0

 

 

k 1

 

X

xk j

aixi j

 

i=0

 

k 1

 

 

X

 

xk jaijxk 1

 

i=0

 

 

 

k 1

 

 

X

xk 1(M j ai j)

 

 

i=0

>

0

 

For f (x) = ak xk +ak 1xk 1 + +a1x+a0 put g(x) = xk +Pk 1(ai=ak )xi:

i=0

By the above argument, if > maxf1; Pk 1 j ai=ak jg + 1; then g( ) > 0:

i=0

So f ( ) = ak g( ) is greater than zero if ak > 0 and less than zero if ak < 0: In either case f ( ) 6= 0: Thus g( ) 6= 0 when

k 1

X

> M = maxf1; j ai jg + 1

i=0

27. For functions F; G : N ! (0; 1), F is said to be (G) if

lim F (x) = c

x!1 G(x)

for some c 2 (0; 1).

(a)Show that if F 2 (G), then O(G) = O(F ):

(b)Use this result to give an alternate proof of Theorem 6.

(c)Find functions F; G : N ! [0; 1) where O(F ) = O(G) but

lim F (x)

x!1 G(x)

does not exist.

27. (a) There exists a real number xc such that for all x > xc we have

F (x) < c +

G(x)

where is an arbitrarily small positive real number. Therefore,

F (x) < (c + )G(x):

Use c + and xc in the de nition of asymptotically dominates to show

F 2 O(G):

Since G(x) > 0 for all x, we can invert the fraction and conclude G 2 O(F ):

27. (b) Let F (x) = P (x) and G(x) = ak nk:

lim P (x) = 1:

x!1 ak xk

27. (c) Let F (x) = 10 + sin(x) and G(x) = 10 + cos(x): Because of the oscillation, there is no xed number that F (x)=G(x) approaches as x ! 1.

29. Find functions F; G : N ! [0; 1) where F 2 O(G) and G 62O(F ) but

lim F (x)

x!1 G(x)

does not exist.

29. Let F (x) = x and G(x) = x sin(x): This works because j G(x) j is zero in nitely often.

31.Is it true that for all functions F , O(F ) = O(F + 1)? Is it true for all increasing functions F ?

31. No. F (n) = 1 is increasing and the answer is still no.

5.4Exercises

1.Calculate how many times statement S is executed in each block of code. Simplify all your answers.

(a)I = 1

while I N

S

I = 2 I

(b)Counter1 = 1

while Counter1 N 1

Counter2 = 1

while Counter2 N 1

S

Counter2 = Counter2 + 1 Counter1 = Counter1 + 1

(c)Counter1 = 1

while Counter1 < X Counter2 = 1

while Counter2 X

S

Counter2 = Counter2 + 2 Counter1 = Counter1 + 1

(d)Counter1 = 1

while Counter1 N Counter2 = 1

while Counter2 N S

Counter2 = Counter2 + 1 Counter3 = 1

while Counter3 N Counter4 = 1

while Counter4 N S

Counter4 = Counter4 + 1 Counter3 = Counter3 + 1

Counter1 = Counter1 + 1

(e)Counter1 = 1

while Counter1 X Counter2 = 1

while Counter2 X

S

Counter2 = 2 Counter2

Counter1 = Counter1 + 1

(f)Counter1 = 1

while Counter1 N 1 Counter2 = 1

while Counter2 Counter1

S

Counter2 = Counter2 + 1 Counter1 = Counter1 + 1

1.

(a) O(log2(N )

 

 

 

 

1.

(b)

N 1

P

N 1 1

 

 

 

1.

(c)

PX

 

 

1

 

 

 

 

 

 

1

 

 

1

 

 

 

 

 

 

P N P

X=2

 

 

N

 

N

 

 

N

 

 

 

 

 

1

h

1

 

 

 

 

 

1 1i

1.

(d)

1

1

 

1 +

1

P

 

 

PX

P

 

 

P

 

 

1.

(f)

PN P

log(X)

S

 

 

 

1.

(e)

1

 

1

 

 

1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1

i

 

 

 

 

 

 

 

i=1

j=1

 

 

 

 

3.Show that the following Selection Sort algorithm, on any input list of size n, makes exactly n(n 1)=2 comparisons and exactly n 1 calls to Swap.

ALGORITHM: Selection Sort

INPUT: A list of distinct values List [1]; : : : ; List [N ] OUTPUT: List [1]; : : : ; List [N ] with values in increasing order

SelectionSort( List ; N )

for Position = 1 to N 1 do

Small = List [P osition]

Place = Position

for I = Position + 1 to N do if (Small > List [I]) then

Small = List [I] Place = I

Swap(List [Position],List [Place])

3. Count comparisons with c and swaps with s:

N 1

N

N 1

n 1 N

X X

X

X j X

((

c) + s) =

s +

c

i=1

j=i+1

i=1

i=1 =i+1

n 1

X

= (N 1)s + c(N i)

i=1

N 1

X

= (N 1)s + cN (N 1) c i

i=1

=(N 1)s + cN (N 1) cN (N 1)=2

=(N 1)s + (N (N 1)=2)c

5.Write an algorithm in pseudocode that follows the description in Example 2 for the special case where the formula is in CNF. For sake of simplicity, assume each proposition letter is a lower case letter (a-z), but try to ignore the fact that there are only 26 such letters. Also, assume that each of the symbols (^; _; :) is also just one character, some ASCII character other than a through z. (Hint: Suppose you are given the formula from that example,

= (a _ b _ c _ d _ :b) ^ (c _ d _ :c _ e _ f )

and the example truth assignment of T to a; :b; c; :d; e; and :f . Your program should scan through from left to right, keeping track of (i) the value so far of the current clause { the disjunction of all the literals seen so far in that clause | and (ii) the value so far of the entire formula | the conjunction of the values of all the clauses completed so far.)

Since this can be done with just one scan through the formula, you may be tempted to think the algorithm is linear time, but it is not. Why? For formulas not in CNF, the algorithm is also O(n2) but more complicated: One must rst parse the formula to get an expression tree and then keep track of truth values on the tree. The student may have seen such problems in a data structures or algorithms course.

5.

Algorithm CNF-SAT

Inputs: A list of clauses, c1; c2; : : : ; cm (Each clause is a list of literals.)

A list I of literals set to TRUE.

Output: I( )

Set SATsoFar = TRUE.

For each clause ci

Initialize trueSoFar = FALSE.

For each literal j in ci Search for j in list I

If j 2 I,

set trueSoFar = TRUE. If not trueSoFar

set SATsoFar = FALSE.

The time consumption here: we must search through | search through every element of a list of lists. This takes O of the sum of the length of the lists. For each literal encountered, we search to see whether 2 I | a search through list I each time. The time for that search is O(jIj). Thus the entire time is O(j j jIj). Since every I contains one entry for each variable in , jIj j j. Thus the entire time is O(j j2). (With the right random-access data structures this can be done in time O(j j).)

7.It is tempting to compute the complexity of an algorithm by counting statements, as we did with the BubbleSort example, but only keeping track of the number of steps along the way up to O(*). This turns out not to work with loops. For example, it is possible that each time through the loop, the number of statements executed is in O(1), but that the number of statements executed by a loop of length n is not in O(n). Find an example. (Hint: each time through the loop, the number of statements executed may be in O(n), but the constants c and N0 may change.)

7.The point here is simply that, if we ignore multiplicative constants, we must mean the same constant each time through the loop. Consider simple-mindedly adding

(1)+ (1 + 2)+(1 + 2 + 3) + (1 + 2 + 3 + 4)

+(1 + 2 + 3 + 4 + 5) + + (1 + 2 + 3 + 4 + + n)

In the rst pass through the outer loop, compute 1; in the second pass, 1+2; in the third, 1+2+3; etc. One could say that each pass through the outer loop takes O(1) additions: 0 1 additions for the rst, 1 1 for the second, 2 1 for the third, and so forth. Then careless O arithmetic would let us infer that the whole algorithm takes time O(n 1). But that is false: the algorithm makes n(n 1)=2 + (n + 1) additions. If one counted the number of steps exactly and took O at the end, the bad counting above just wouldn't arise.

9.Is it reasonable to consider all polynomial-time algorithms to be practical? Why, or why not?

9. For moderately large inputs, even polynomial time algorithms where the degree of the polynomial are relatively small is not very practical. For example, F (x) = x10 with x = 106 is 1060!!

11.What is the output of each of the following blocks of code?

(a)cntr = 0

for i = 0 to N do cntr = cntr + 1

print cntr

(b)cntr = 0

for i = 0 to N do

for j = 0 to N do cntr = cntr + 1

print cntr

(c)cntr = 0

for i = 0 to N do

for j = i to N do

for k = j to N do cntr = cntr + 1

print cntr

11. (a) N + 1

11. (b) (N + 1)2

11.(c) (N + 1)3 N (N + 1)2 + 16 N (N + 1)(N + 2)

5.6End of Chapter Material

Starting to Review

1.Express in words what G 2 O(F ) means. Express in words what G 62 O(F ) means.

1. G is an element of order F if for some real number c and some integer N0, it is true that for all integers n such that n N0, G(n) is less that c times F (n):

G is not an element of order F if for any real number c and any integer N0, there is an integer n such that n N0 and G(n) > cF (n):

3.Let F (n) = n and G(n) = 2n + 3 be functions de ned on [0; 1): Find a real number c and N0 2 N such that F 2 O(G) with

3. c = 3; N0 = 3.

5.Show that O(1) O(log (n)):

5. Let c = 1 and N0 = dee: Then for every n N0; 1 < c log(n):

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