Data Structures and Algorithms in java 4th Edition Drozdek Solutions Manual

Page 1

// Instructor’s Solutions Manual

// © 2013 Cengage Learning, all rights reserved.

2

COMPLEXITY ANALYSIS

1. (a) The function f never exceeds the value of a certain constant c .

(b) f is a constant function.

(c) The function f never exceeds the value of the power function n c for some constant c

2. In the following answers, these two definitions are used:

1 ()fn is 1 (())Ogn if there exist positive numbers c1 and N1 such that 111 ()() fncgn  for all 1nN  ;

2 ()fn is 2 (())Ogn if there exist positive numbers c2 and N2 such that 222 ()() fncgn  for all 2 nN  ;

(a) From the above definitions, we have

111212 ()max(()())forallmax(), fncgngnnNN 

221212 ()max(()())forallmax(), fncgngnnNN  which implies that

12121212 ()()()max(()())forallmax(). fnfnccgngnnNN 

Hence for 312 ccc  and 31212312 max()()()max(()()) NNNfnfncgngn  for all 3 nN  , that is, 12()() fnfn  is 12 (max(()())). Ogngn 

(b) If 12()(), gngn  then for 12 max(),ccc  12()() cgncgn  and 122 ()()2(), cgncgncgn  which implies that 12 (())(()) OgnOgn  is 2 (()).Ogn

(c) The rule of product, 12()() fnfn  is 12 (()())Ogngn  is true, since 121212 ()()()() fnfnccgngn  for all 12 max().nNN 

(d) (())Ocgn is ((()) Ogn means that any function f which is ()Ocg is also ()Og . Function f is ()Ocg if there are two constants 1c and N so that 1 ()() fnccgn  for all nN  ; in this case, for a constant 212()(); cccfncgn  thus by choosing properly a constant 2c (whose value depends on the value of c and 1c ), f is ().Og

// DataStructures&AlgorithmsInJava
4th ed.)
(Adam Drozdek,
2-1
Full Download: http://testbanktip.com/download/data-structures-and-algorithms-in-java-4th-edition-drozdek-solutions-manual/ Download all pages and all chapters
Data Structures and Algorithms in

// Instructor’s Solutions Manual

// © 2013 Cengage Learning, all rights reserved.

(e) A constant c is O(1) if there exist positive numbers 1c and N such that 1 1 cc for all ; nN  that is, the constant function c is independent of n, and we can simply set 1 cc 

3. (a) 2 1 n i i   is 3 ()On if a constant can be found such that 23 1 ; n i icn    but 223 1 , n i innn    thus c = 1 for any n.

(b) Function lg k ann  is () k On if there is a c and N for which lg kk anncn  for all nN  The inequality becomes lg anc  for 1 N  , and since lg0an we can put c = a. But there is no positivec for which lg can  (it holds for c = 0), thus lg k ann  is not (). k n 

(c) Function 11 lg nnn  is 11 () n  if two constants can be found such that 111111 12 lg . cnnnncn  These inequalities can be transformed into 1 12 1lg ; cnnc   by the rule of L’Hospital, 11 lg(lg) nnnn   has the same limit as (lg)(1)(10lg)11 , enen  which is 0. Hence, 121110lg cce  .

(d) 2 n is ()On if there is a c and N for which 2 n cn  for all nN  If N = 1, then 2 n cn  implies that 2(123), n cn i.e., 2222 123 2. n c  

For the other part of the exercise: ! n is O(2n) if there are constants c and N such that !2 n nc  for . nN  The inequality !2 n nc  implies

1232 , n nc  i.e., 3 12 2222 ,nc    for all n’s. But such a constant c cannot be found.

(e) We can find such a c that for some N and all ,22 , nannNc   if 2/22 nana c  

(f) We cannot find such a c that for some N and all ,22 , nannNc   because there exists no constant 2/22. nan cna  

(g) Because 2lg , n n  then lg 2 ; aan n  therefore, if lg lg 22 , ann  then lg 2 an n  Now we have to find such a c that for some N and all lg ,2 , nanNcn  or lg lg 22 , nan c  i.e., lg lg 2/2 , nan c  which is possible because the function lg lg 2/2nan is decreasing.

4. (a) Let  11 ,fnan  and  22 ;fnan  then both f1 and f2 are O(n), but  1212 fnfnaan  is not  0 . OnnO  Hence, 12 fnfn is not  12()().Oggnn

(b) Take the same functions as before;  1212 // fnfnaan  is not  /1 . OnnO  Therefore, 12 / fnfn is not   12 / Ogngn

5. For functions   22 12,, , fnanOnfncndgnn  both f1(n) and f2(n) are O(g(n)), but f1 is not O(f2).

DataStructures&AlgorithmsInJava
//
(Adam Drozdek, 4th ed.)
2-2


// Instructor’s Solutions Manual

// © 2013 Cengage Learning, all rights reserved.

6. (a) It is not true that if  1 fn is   gn  then  2 fn is  (2) gn  , if, for example,  ,fnn  and

2.gnn 

(b)  fngn  is not

min, , fngn  if, for example,  ,fnn  and  1 . gn n 

(c) 2na is not  2 , On because there is no constant  1 222nanna c  for any n and 1; a  for 1 a  the function  1 2 na is decreasing so that a c meeting the specified condition can be found.

7. If the line for (i = 0, length = 1; i < n-1; i++) is replaced by the line for (i = 0, length = 1; i < n-1 && length < n-i; i++) in the algorithm for finding the longest subarray with numbers in increasing order, then the best case, when all numbers in the array are in decreasing order, remains O(n) since the inner loop is executed just once for each of the 1 n executions of the outer loop. For the ordered array, the outer loop is executed just once and the inner loop 1 n times, which makes it another best case.

But the algorithm is still O(n2). For example, if the array is [5 4 3 2 1 1 2 3 4 5], i.e., the first half of the array is in descending order, then the outer loop executes n/2 times and for each iteration 1,,/2in  , the inner loop iterates i times, which makes O(n2) iterations in total. This inefficiency is due to the fact that the inner loop remembers only the length of the longest subarray, not its position, thereby checking subarrays of this subarray, e.g., after checking the subarray [5 4 3 2 1], it also checks its subarrays [4 3 2 1], [3 2 1], etc. To improve the algorithm more and make it O(n), the inner loop for (i1 = i2 = k = i; k < n-1 && a[k] < a[k+1]; k++, i2++); should be changed to for (i1 = i2 = k; k < n-1 && a[k] < a[k+1]; k++, i2++);

8. The complexity of selectkth() is

9. The algorithm for adding matrices requires n 2 assignments. Note that the counter i for the inner loop does not depend on the counter j for the outer loop and both of them take values 0,,1 n  .

All three counters, i, j, and k in the algorithm for matrix multiplication are also independent of each other, hence the complexity of the algorithms is n 3

To transpose a matrix,  21 2 01 3 nn iji On    assignments are required.

10. (a) The autoincrement cnt1++ is executed exactly n 2 times.

DataStructures&AlgorithmsInJava
//
(Adam Drozdek, 4th ed.)
2-3

 

  2 1221/2 nnnknkkOn 
(b)  2 1 n i iOn   

DataStructuresandAlgorithmsinjava4thEditionDrozdekSolutionsManual

FullDownload:http://testbanktip.com/download/data-structures-and-algorithms-in-java-4th-edition-drozdek-solutions-manual/

12. To prove the conjecture, the best thing to do is to devise an amortized cost that remains constant for each increment step. For example,

of bits in set to 1 amCostincrementxx

If the cost of setting one bit is one unit, then after setting a 1 bit there is one unit left for setting this bit back to 0, therefore, there is no need to charge anything for setting bits to 0. Note that the amortized cost for one increment is always 2.

Another definition is

number of flipped bitsnumber of 1s added to amCostincrementxx

The following table illustrates the application of this definition to increments of a 3-bit binary number.

13. For an alternative

For an alternative A = p1, generate the expression:

12 , App  generate an expression

Downloadallpagesandallchaptersat:TestBankTip.com

// DataStructures&AlgorithmsInJava (Adam Drozdek, 4th ed.) // Instructor’s Solutions Manual
© 2013 Cengage Learning, all rights reserved. 2-4 (c)  lg 1 lg n i inn    (d)  lg 1 2 ni i n    11.    121214173 424288 nnnnnnn n   
//
  2number

  

Number Flipped bits Added 1s Amortized cost 000 001 1 1 2 010 2 0 2 011 1 1 2 100 3 1 2 101 1 1 2 110 2 0 2 111 1 1 2


 
 1111 Apyzpyzpyzpyz  
1212 . Appzppz

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.
Data Structures and Algorithms in java 4th Edition Drozdek Solutions Manual by Anitax80 - Issuu