Algo-analysis-TADM2E

From Algorithm Wiki
Revision as of 18:22, 11 September 2014 by Algowikiadmin (talk | contribs) (Recovering wiki)
Jump to: navigation, search

Algorithm Analysis

Program Analysis


2-1. What value is returned by the following function? Express your answer as a function of $ n $. Give the worst-case running time using the Big Oh notation.

  function 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.1)


2-2. What value is returned by the following function? Express your answer as a function of $ n $. Give the worst-case running time using Big Oh notation.

   function 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)

(Solution 2.2)


2-3. What value is returned by the following function? Express your answer as a function of $ n $. Give the worst-case running time using Big Oh notation.

   function 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.3)


2-4. What value is returned by the following function? Express your answer as a function of $ n $. Give the worst-case running time using Big Oh notation.

  function conundrum($ n $)
      $ r:=0 $
      for $ i:=1 $ to $ n $ do
      for $ j:=i+1 $ to $ n $ do
      for $ k:=i+j-1 $ to $ n $ do
      $ r:=r+1 $
       return($ r $)


2-5. Suppose the following algorithm is used to evaluate the polynomial $ p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0 $

   $ p:=a_0; $
   $ xpower:=1; $
   for $ i:=1 $ to $ n $ do
   $ xpower:=x*xpower; $
   $ p:=p+a_i * xpower $
   end
  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?

(Solution 2.5)


2-6. Prove that the following algorithm for computing the maximum value in an array $ A[1..n] $ is correct.

  function max(A)
     $ m:=A[1] $
     for $ i:=2 $ to n do
           if $ A[i] > m $ then $ m:=A[i] $
     return (m)

(Solution 2.6)


Big Oh


2-7. True or False?

  1. Is $ 2^{n+1} = O (2^n) $?
  2. Is $ 2^{2n} = O(2^n) $?

(Solution 2.7)


2-8. For each of the following pairs of functions, either $ f(n) $ is in $ O(g(n)) $, $ f(n) $ is in $ \Omega(g(n)) $, or $ f(n)=\Theta(g(n)) $. Determine which relationship is correct and briefly explain why.

  1. $ f(n)=\log n^2 $; $ g(n)=\log n $ + $ 5 $
  2. $ f(n)=\sqrt n $; $ g(n)=\log n^2 $
  3. $ f(n)=\log^2 n $; $ g(n)=\log n $
  4. $ f(n)=n $; $ g(n)=\log^2 n $
  5. $ f(n)=n \log n + n $; $ g(n)=\log n $
  6. $ f(n)=10 $; $ g(n)=\log 10 $
  7. $ f(n)=2^n $; $ g(n)=10 n^2 $
  8. $ f(n)=2^n $; $ g(n)=3^n $

(Solution 2.8)


2-9. For each of the following pairs of functions $ f(n) $ and $ g(n) $, determine whether $ f(n) = O(g(n)) $, $ g(n) = O(f(n)) $, or both.

  1. $ f(n) = (n^2 - n)/2 $, $ g(n) =6n $
  2. $ f(n) = n +2 \sqrt n $, $ g(n) = n^2 $
  3. $ f(n) = n \log n $, $ g(n) = n \sqrt n /2 $
  4. $ f(n) = n + \log n $, $ g(n) = \sqrt n $
  5. $ f(n) = 2(\log n)^2 $, $ g(n) = \log n + 1 $
  6. $ f(n) = 4n\log n + n $, $ g(n) = (n^2 - n)/2 $

(Solution 2.9)


2-10. Prove that $ n^3 - 3n^2-n+1 = \Theta(n^3) $.


2-11. Prove that $ n^2 = O(2^n) $.

(Solution 2.11)


2-12. For each of the following pairs of functions $ f(n) $ and $ g(n) $, give an appropriate positive constant $ c $ such that $ f(n) \leq c \cdot g(n) $ for all $ n > 1 $.

  1. $ f(n)=n^2+n+1 $, $ g(n)=2n^3 $
  2. $ f(n)=n \sqrt n + n^2 $, $ g(n)=n^2 $
  3. $ f(n)=n^2-n+1 $, $ g(n)=n^2/2 $


2-13. Prove that if $ f_1(n)=O(g_1(n)) $ and $ f_2(n)=O(g_2(n)) $, then $ f_1(n)+f_2(n) = O(g_1(n)+g_2(n)) $.

(Solution 2.13)


2-14. Prove that if $ f_1(N)=\Omega(g_1(n)) $ and $ f_2(n)=\Omega(g_2(n)) $, then $ f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n)) $.


2-15. Prove that if $ f_1(n)=O(g_1(n)) $ and $ f_2(n)=O(g_2(n)) $, then $ f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n)) $

(Solution 2.15)


2-16. Prove for all $ k \geq 1 $ and all sets of constants $ \{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R $, $ a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k) $


2-17. Show that for any real constants $ a $ and $ b $, $ b > 0 $ Big Oh notation $ (n+a)^b = \Theta(n^b) $ To show $ f(n) = \Theta(g(n)) $, we must show $ O $ and $ \Omega $. Go back to the definition!

  1. Big $ O $ -- Must show that $ (n+a)^b \leq c_1 \cdot n^b $ for all $ n > n_0 $. When is this true? If $ c_1 = 2^b $, this is true for all $ n > |a| $ since $ n+a < 2n $, and raise both sides to the $ b $.
  2. Big $ \Omega $ -- Must show that $ (n+a)^b \geq c_2 \cdot n^b $ for all $ n > n_0 $. When is this true? If $ c_2 = (1/2)^b $, this is true for all $ n > 3|a|/2 $ since $ n+a > n/2 $, and raise both sides to the $ b $.

Note the need for absolute values.

(Solution 2.17)


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

$ \begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array} $


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

$ \begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array} $

(Solution 2.19)


2-20. Find two functions $ f(n) $ and $ g(n) $ that satisfy the following relationship. If no such $ f $ and $ g $ exist, write None.

  1. $ f(n)=o(g(n)) $ and $ f(n) \neq \Theta(g(n)) $
  2. $ f(n)=\Theta(g(n)) $ and $ f(n)=o(g(n)) $
  3. $ f(n)=\Theta(g(n)) $ and $ f(n) \neq O(g(n)) $
  4. $ f(n)=\Omega(g(n)) $ and $ f(n) \neq O(g(n)) $


2-21. True or False?

  1. $ 2n^2+1=O(n^2) $
  2. $ \sqrt n= O(\log n) $
  3. $ \log n = O(\sqrt n) $
  4. $ n^2(1 + \sqrt n) = O(n^2 \log n) $
  5. $ 3n^2 + \sqrt n = O(n^2) $
  6. $ \sqrt n \log n= O(n) $
  7. $ \log n=O(n^{-1/2}) $

(Solution 2.21)


2-22. For each of the following pairs of functions $ f(n) $ and $ g(n) $, state whether $ f(n)=O(g(n)) $, $ f(n)=\Omega(g(n)) $, $ f(n)=\Theta(g(n)) $, or none of the above.

  1. $ f(n)=n^2+3n+4 $, $ g(n)=6n+7 $
  2. $ f(n)=n \sqrt n $, $ g(n)=n^2-n $
  3. $ f(n)=2^n - n^2 $, $ g(n)=n^4+n^2 $


2-23. For each of these questions, briefly explain your answer.
(a) If I prove that an algorithm takes $ O(n^2) $ worst-case time, is it possible that it takes $ O(n) $ on some inputs?
(b) If I prove that an algorithm takes $ O(n^2) $ worst-case time, is it possible that it takes $ O(n) $ on all inputs?
(c) If I prove that an algorithm takes $ \Theta(n^2) $ worst-case time, is it possible that it takes $ O(n) $ on some inputs?
(d) If I prove that an algorithm takes $ \Theta(n^2) $ worst-case time, is it possible that it takes $ O(n) $ on all inputs?
(e) Is the function $ f(n) = \Theta(n^2) $, where $ f(n) = 100 n^2 $ for even $ n $ and $ f(n) = 20 n^2 - n \log_2 n $ for odd $ n $?

(Solution 2.23)


2-24. For each of the following, answer yes, no, or can't tell. Explain your reasoning.
(a) Is $ 3^n = O(2^n) $?
(b) Is $ \log 3^n = O( \log 2^n ) $?
(c) Is $ 3^n = \Omega(2^n) $?
(d) Is $ \log 3^n = \Omega( \log 2^n ) $?


2-25. For each of the following expressions $ f(n) $ find a simple $ g(n) $ such that $ f(n)=\Theta(g(n)) $.

  1. $ f(n)=\sum_{i=1}^n {1\over i} $.
  2. $ f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil $.
  3. $ f(n)=\sum_{i=1}^n \log i $.
  4. $ f(n)=\log (n!) $.

(Solution 2.25)


2-26. Place the following functions into increasing asymptotic order. $ f_1(n) = n^2\log_2n $, $ f_2(n) = n(\log_2n)^2 $, $ f_3(n) = \sum_{i=0}^n 2^i $, $ f_4(n) = \log_2(\sum_{i=0}^n 2^i) $.


2-27. Place the following functions into increasing asymptotic order. If two or more of the functions are of the same asymptotic order then indicate this. $ f_1(n)=\sum_{i=1}^n \sqrt i $, $ f_2(n)= (\sqrt n)\log n $, $ f_3(n)= n\sqrt {\log n} $, $ f_4(n) = 12n^{3\over 2} + 4n $,

(Solution 2.27)


2-28. For each of the following expressions $ f(n) $ find a simple $ g(n) $ such that $ f(n)=\Theta(g(n)) $. (You should be able to prove your result by exhibiting the relevant parameters, but this is not required for the homework.)

  1. $ f(n)=\sum_{i=1}^n 3i^4 + 2i^3 - 19i + 20 $.
  2. $ f(n)=\sum_{i=1}^n 3(4^i) + 2(3^i) -i^{19} + 20 $.
  3. $ f(n)=\sum_{i=1}^n 5^i + 3^{2i} $.


2-29. Which of the following are true?

  1. $ \sum_{i=1}^n 3^i = \Theta(3^{n-1}) $.
  2. $ \sum_{i=1}^n 3^i = \Theta(3^n) $.
  3. $ \sum_{i=1}^n 3^i = \Theta(3^{n+1}) $.

(Solution 2.29)


2-30. For each of the following functions $ f $ find a simple function $ g $ such that $ f(n)=\Theta(g(n)) $.

  1. $ f_1(n)= (1000)2^n + 4^n $.
  2. $ f_2(n)= n + n\log n + \sqrt n $.
  3. $ f_3(n)= \log (n^{20}) + (\log n)^{10} $.
  4. $ f_4(n)= (0.99)^n + n^{100}. $


2-31. For each pair of expressions $ (A,B) $ below, indicate whether $ A $ is $ O $, $ o $, $ \Omega $, $ \omega $, or $ \Theta $ of $ B $. Note that zero, one or more of these relations may hold for a given pair; list all correct ones.
$ \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} $

(Solution 2.31)


Summations


2-32. Prove that: $ 1^2 - 2^2 + 3^2 - 4^2 + \ldots +(-1)^{k-1} k^2 = (-1)^{k-1}k(k+1)/2 $


2-33. Find an expression for the sum of the $ i $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.

$ \begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array} $

(Solution 2.33)


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


2-35. Consider the following code fragment.

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

Let $ T(n) $ denote the number of times `foobar' is printed as a function of $ n $.

  1. Express $ T(n) $ as a summation (actually two nested summations).
  2. Simplify the summation. Show your work.

(Solution 2.35)


2-36. 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 $ n $ is even. Let $ T(n) $ denote the number of times `foobar' is printed as a function of $ n $.

  1. Express $ T(n) $ as three nested summations.
  2. Simplify the summation. Show your work.

(Solution 2.36)


2-37. When you first learned to multiply numbers, you were told that $ x \times y $ means adding $ x $ a total of $ y $ times, so $ 5 \times 4 = 5+5+5+5 = 20 $. What is the time complexity of multiplying two $ n $-digit numbers in base $ b $ (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of $ n $ and $ b $. Assume that single-digit by single-digit addition or multiplication takes $ O(1) $ time. (Hint: how big can $ y $ be as a function of $ n $ and $ b $?) multiplication algorithms

(Solution 2.37)


2-38. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that $ 127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397 $. Analyze the time complexity of multiplying two $ n $-digit numbers with this method as a function of $ n $ (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes $ O(1) $ time.

(Solution 2.38)



Logarithms


2-39. Prove the following identities on logarithms:

  1. Prove that $ \log_a (xy) = \log_a x + \log_a y $
  2. Prove that $ \log_a x^y = y \log_a x $
  3. Prove that $ \log_a x = \frac{\log_b x}{\log_b a} $
  4. Prove that $ x^{\log_b y} = y^{\log_b x} $

(Solution 2.39)


2-40. Show that $ \lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1 $


2-41. Prove that that the binary representation of $ n \geq 1 $ has $ \lfloor \lg_2 n \rfloor $ + $ 1 $ bits.

(Solution 2.41)


2-42. In one of my research papers I give a comparison-based sorting algorithm that runs in $ O( n \log (\sqrt n) ) $. Given the existence of an $ \Omega(n \log n) $ lower bound for sorting, how can this be possible?

(Solution 2.42)

Interview Problems


2-43. You are given a set $ S $ of $ n $ numbers. You must pick a subset $ S' $ of $ k $ numbers from $ S $ such that the probability of each element of $ S $ occurring in $ S' $ is equal (i.e., each is selected with probability $ k/n $). You may make only one pass over the numbers. What if $ n $ is unknown?

(Solution 2.43)


2-44. We have 1,000 data items to store on 1,000 nodes. Each node can store copies of exactly three different items. Propose a replication scheme to minimize data loss as nodes fail. What is the expected number of data entries that get lost when three random nodes fail?


2-45. Consider the following algorithm to find the minimum element in an array of numbers $ A[0, \ldots, n] $. One extra variable $ tmp $ is allocated to hold the current minimum value. Start from A[0]; "tmp" is compared against $ A[1] $, $ A[2] $, $ \ldots $, $ A[N] $ in order. When $ A[i]<tmp $, $ tmp = A[i] $. What is the expected number of times that the assignment operation $ tmp = A[i] $ is performed?

(Solution 2.45)


2-46. You have a 100-story building and a couple of marbles. You must identify the lowest floor for which a marble will break if you drop it from this floor. How fast can you find this floor if you are given an infinite supply of marbles? What if you have only two marbles?


2-47. You are given 10 bags of gold coins. Nine bags contain coins that each weigh 10 grams. One bag contains all false coins that weigh one gram less. You must identify this bag in just one weighing. You have a digital balance that reports the weight of what is placed on it.

(Solution 2.47)


2-48. You have eight balls all of the same size. Seven of them weigh the same, and one of them weighs slightly more. How can you find the ball that is heavier by using a balance and only two weighings?


2-49. Suppose we start with $ n $ companies that eventually merge into one big company. How many different ways are there for them to merge?

(Solution 2.49)


2-50. A Ramanujam number can be written two different ways as the sum of two cubes---i.e., there exist distinct $ a $, $ b $, $ c $, and $ d $ such that $ a^3 + b^3 = c^3 + d^3 $. Generate all Ramanujam numbers where $ a,b,c,d<n $.


2-51. Six pirates must divide $300 dollars among themselves. The division is to proceed as follows. The senior pirate proposes a way to divide the money. Then the pirates vote. If the senior pirate gets at least half the votes he wins, and that division remains. If he doesn't, he is killed and then the next senior-most pirate gets a chance to do the division. Now you have to tell what will happen and why (i.e., how many pirates survive and how the division is done)? All the pirates are intelligent and the first priority is to stay alive and the next priority is to get as much money as possible.

(Solution 2.51)


2-52. Reconsider the pirate problem above, where only one indivisible dollar is to be divided. Who gets the dollar and how many are killed?

(Solution 2.52)