Chapter 2

From The Algorithm Design Manual Solution Wiki
Jump to navigation Jump to search

Algorithm Analysis

Program Analysis

2.1. What value is returned by the following function? Express your answer as a function of . Give the worst-case running time using the Big Oh notation.
  mystery(n)
      r:=0
      for i:=1 to n-1 do
          for j:=i+1 to n do
              for k:=1 to j do
                  r:=r+1
       return(r)

Solution


2.2. What value is returned by the following function? Express your answer as a function of . Give the worst-case running time using Big Oh notation.
   pesky(n)
       r:=0
       for i:=1 to n do
           for j:=1 to i do
               for k:=j to i+j do
                   r:=r+1
       return(r)


2.3. What value is returned by the following function? Express your answer as a function of . Give the worst-case running time using Big Oh notation.
   prestiferous(n)
       r:=0
       for i:=1 to n do
           for j:=1 to i do
               for k:=j to i+j do
                   for l:=1 to i+j-k do
                       r:=r+1
       return(r) 

Solution


2.4. What value is returned by the following function? Express your answer as a function of . Give the worst-case running time using Big Oh notation.
  conundrum()
      
      for  to  do
      for  to  do
      for  to  do
      
      return(r)


2.5. Consider the following algorithm: (the print operation prints a single asterisk; the operation doubles the value of the variable ).
   for  to 
       
       while ():
          print '*'
          
Let be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for , and , ideally converging on .

Solution


2.6. Suppose the following algorithm is used to evaluate the polynomial
   
   
   for  to  do
   
   
  1. How many multiplications are done in the worst-case? How many additions?
  2. How many multiplications are done on the average?
  3. Can you improve this algorithm?


2.7. Prove that the following algorithm for computing the maximum value in an array is correct.
  max(A)
     
     for  to n do
           if  then 
     return (m)

Solution

Big Oh

2.8. True or False?
  1. Is ?
  2. Is ?


2.9. For each of the following pairs of functions, either is in , is in , or . Determine which relationship is correct and briefly explain why.
  1. ; +
  2. ;
  3. ;
  4. ;
  5. ;
  6. ;
  7. ;
  8. ;

Solution


2.10. For each of the following pairs of functions and , determine whether , , or both.
  1. ,
  2. ,
  3. ,
  4. ,
  5. ,
  6. ,


2.11. For each of the following functions, which of the following asymptotic bounds hold for ?

Solution


2.12. Prove that .


2.13. Prove that .

Solution


2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)<\math>.


2.15

Solution


2.16


2.17

Solution


2.18


2.19

Solution


2.20


2.21

Solution


2.22


2.23

Solution


2.24


2.25

Solution


2.26


2.27

Solution


2.28


2.29

Solution


2.30


2.31

Solution


2.32


2.33

Solution


2.34


2.35

Solution


2.36

Summations

2.37
2.38
2.39
2.40
2.41
2.42
2.43

Logartihms

2.44
2.45
2.46
2.47

Interview Problems

2.48
2.49
2.50
2.51
2.52
2.53
2.54
2.55


Back to Chapter List