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 Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle n} . 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 Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle n} . 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 Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle n} . 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 Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle n} . Give the worst-case running time using Big Oh notation.
  conundrum(Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle n}
)
      Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle r:=0}

      for Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle i:=1}
 to Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle n}
 do
      for Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle j:=i+1}
 to Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle n}
 do
      for Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle k:=i+j-1}
 to Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle n}
 do
      Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle r:=r+1}

      return(r)


2.5. Consider the following algorithm: (the print operation prints a single asterisk; the operation Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle x = 2x} doubles the value of the variable Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle x} ).
   for Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle  k = 1}
 to Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle n}

       Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle x = k}

       while (Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle x < n}
):
          print '*'
          Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle x = 2x}

Let Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle f(n)} be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle O(f(n))} , and Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle /Theta(f(n))} , ideally converging on Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle \Theta(f(n))} .

Solution


2.6. Suppose the following algorithm is used to evaluate the polynomial
Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0}
   Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle p:=a_0;}

   Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle xpower:=1;}

   for Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle i:=1}
 to Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle n}
 do
   Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle xpower:=x*xpower;}

   Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle p:=p+a_i * xpower}

  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 Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle A[1..n]} is correct.
  max(A)
     Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle m:=A[1]}

     for Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle i:=2}
 to n do
           if Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle A[i] > m}
 then Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle m:=A[i]}

     return (m)

Solution

Big Oh

2.8. True or False?
  1. Is Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle 2^{n+1} = O (2^n)} ?
  2. Is Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle 2^{2n} = O(2^n)} ?


2.9. For each of the following pairs of functions, either Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle f(n )} is in Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle O(g(n))} , Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle f(n)} is in Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle \Omega(g(n))} , or Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle f(n)=\Theta(g(n))} . Determine which relationship is correct and briefly explain why.
  1. Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle f(n)=\log n^2} ; Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle g(n)=\log n} + Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle 5}
  2. Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle f(n)=\sqrt n} ; Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle g(n)=\log n^2}
  3. Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle f(n)=\log^2 n} ; Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle g(n)=\log n}
  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: .


2.15. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one?
(a) (b) (c) (d) (e)

Solution


2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size .)Suppose you have a computer that can perform operations per second. For each algorithm, what is the largest input size n that you can complete within an hour?
(a) (b) (c) (d) (e) (f)


2.17. For each of the following pairs of functions and , give an appropriate positive constant such that for all .
  1. ,
  2. ,
  3. ,

Solution


2.18. Prove that if and , then .


2.19. Prove that if and , then .

Solution


2.20. Prove that if and , then


2.21. Prove for all and all sets of constants ,

Solution


2.22. Show that for any real constants and ,


2.23. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which.

Solution


2.24


2.25

Solution


2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which.


2.27. Find two functions and that satisfy the following relationship. If no such and exist, write None.
  1. and
  2. and
  3. and
  4. and

Solution


2.28. True or False?


2.29. For each of the following pairs of functions and , state whether , , , or none of the above.
  1. ,
  2. ,
  3. ,

Solution.


2.30. For each of these questions, briefly explain your answer.


(a) If I prove that an algorithm takes worst-case time, is it possible that it takes on some inputs?


(b) If I prove that an algorithm takes worst-case time, is it possible that it takes on all inputs?


(c) If I prove that an algorithm takes worst-case time, is it possible that it takes on some inputs?


(d) If I prove that an algorithm takes worst-case time, is it possible that it takes on all inputs?


(e) Is the function , where for even and for odd ?


2.31. For each of the following, answer yes, no, or can't tell. Explain your reasoning.


(a) Is ?


(b) Is ?


(c) Is ?


(d) Is ?

Solution


2.32. For each of the following expressions find a simple such that .
  1. .
  2. .
  3. .
  4. .


2.33. Place the following functions into increasing asymptotic order.
, , , .

Solution


2.34. Which of the following are true?
  1. .
  2. .
  3. .


2.35. For each of the following functions find a simple function such that .
  1. .
  2. .
  3. .

Solution


2.36. For each pair of expressions below, indicate whether is , , , , or of . Note that zero, one or more of these relations may hold for a given pair; list all correct ones.


Summations

2.37. Find an expression for the sum of the th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0.

Solution


2.38. Assume that Christmas has days. Exactly how many presents did my true love send me? (Do some research if you do not understand this question.)


2.39

Solution


2.40. Consider the following code fragment.

  for i=1 to n do
     for j=i to 2*i do
        output foobar

Let denote the number of times `foobar' is printed as a function of .
  1. Express as a summation (actually two nested summations).
  2. Simplify the summation. Show your work.


2.41.Consider the following code fragment.

  for i=1 to n/2 do
     for j=i to n-i do
        for k=1 to j do
           output foobar

Assume is even. Let denote the number of times `foobar' is printed as a function of .
  1. Express as three nested summations.
  2. Simplify the summation. Show your work.

Solution


2.42. When you first learned to multiply numbers, you were told that means adding a total of times, so . What is the time complexity of multiplying two -digit numbers in base (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of and . Assume that single-digit by single-digit addition or multiplication takes

time. (Hint: how big can be as a function of and ?)


2.43. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that . Analyze the time complexity of multiplying two -digit numbers with this method as a function of (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes time.

[2.43|Solution]]

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