Difference between pages "Chapter 1" and "Chapter 2"

From The Algorithm Design Manual Solution Wiki
(Difference between pages)
Jump to navigation Jump to search
 
 
Line 1: Line 1:
Below are the exercises at the end of chapter 1 of the third edition of the Algorithm Design Manual by Steven Skiena. Student proposed answers to odd number questions are available by clicking on the question number.
+
=Algorithm Analysis=
  
=Introduction to Algorithms=
+
===Program Analysis===
  
===Finding Counter Examples===
+
:[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. 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)
  
:[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>.
+
[[2.1|Solution]]
  
  
:1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>.
+
:2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. 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)
  
  
:[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route.
+
:[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. 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)
  
 +
[[2.3|Solution]]
  
:1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns.
 
  
 +
:2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation.
 +
  conundrum(<math>n</math>)
 +
      <math>r:=0</math>
 +
      ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do''
 +
      ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do''
 +
      ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do''
 +
      <math>r:=r+1</math>
 +
      ''return''(r)
  
:[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>.
 
:Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists.
 
#Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm.
 
#Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm.
 
#Put the elements of <math>S</math> in the knapsack from largest to smallest.
 
  
 +
:[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>).
 +
    ''for'' <math> k = 1</math> to <math>n</math>
 +
        <math>x = k</math>
 +
        ''while'' (<math>x < n</math>):
 +
          ''print'' '*'
 +
          <math>x = 2x</math>
 +
:Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>\Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>.
  
:1.6. The ''set cover problem'' is as follows: given a set <math>S</math> of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>.For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>.
+
[[2.5|Solution]]
  
:Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered.
 
  
 +
:2.6. Suppose the following algorithm is used to evaluate the polynomial
 +
::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math>
 +
    <math>p:=a_0;</math>
 +
    <math>xpower:=1;</math>
 +
    for <math>i:=1</math> to <math>n</math> do
 +
    <math>xpower:=x*xpower;</math>
 +
    <math>p:=p+a_i * xpower</math>
 +
#How many multiplications are done in the worst-case? How many additions?
 +
#How many multiplications are done on the average?
 +
#Can you improve this algorithm?
  
:[[1.7]]. The ''maximum clique problem'' in a graph <math>G = (V, E)</math> asks for the largest subset <math>C</math> of vertices <math>V</math> such that there is an edge in <math>E</math> between every pair of vertices in <math>C</math>. Find a counterexample for the following algorithm: Sort the vertices of <math>G</math> from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered.
 
  
===Proofs of Correctness===
+
:2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct.
 +
  max(A)
 +
      <math>m:=A[1]</math>
 +
      ''for'' <math>i:=2</math> ''to'' n ''do''
 +
            ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math>
 +
      ''return'' (m)
  
:1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants <math> c \geq 2</math>.
+
[[2.7|Solution]]
  multiply(<math>y,z</math>)
 
  #Return the product <math>yz</math>.
 
      ''if'' <math>z=0</math> ''then'' return(0) ''else''
 
        return(multiply(<math>cy,\lfloor z/c \rfloor)+y \cdot (z\,\bmod\,c</math>))
 
  
 +
===Big Oh===
  
  
:[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial. P(x) = <math>a_nx^n + a_{n-1}x^{n-1} + \dots + a_1x + a_0</math>
+
:2.8. True or False?
    horner(<math>A,x</math>)
+
#Is <math>2^{n+1} = O (2^n)</math>?
      <math>p = A_n</math>
+
#Is <math>2^{2n} = O(2^n)</math>?
      for <math>i</math> from <math>n-1</math> to <math>0</math>
 
              <math>p = p*x+A_i</math>
 
      return <math>p</math>
 
  
  
:1.10. Prove the correctness of the following sorting algorithm.
+
:[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why.
    bubblesort (<math>A</math> : list[<math>1 \dots n</math>])
+
#<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math>
      for <math>i</math> from <math>n</math> to <math>1</math>
+
#<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math>
          for <math>j</math> from <math>1</math> to <math>i-1</math>
+
#<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math>
              if (<math>A[j] > A[j+1]</math>)
+
#<math>f(n)=n</math>; <math>g(n)=\log^2 n</math>
                  swap the values of <math>A[j]</math> and <math>A[j+1]</math>
+
#<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math>
 +
#<math>f(n)=10</math>; <math>g(n)=\log 10</math>
 +
#<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math>
 +
#<math>f(n)=2^n</math>; <math>g(n)=3^n</math>
  
 +
[[2.9|Solution]]
  
:[[1.11]]. The ''greatest common divisor of positive'' integers <math>x</math> and <math>y</math> is the largest integer <math>d</math> such that <math>d</math> divides <math>x</math> and <math>d</math> divides <math>y</math>. Euclid’s algorithm to compute <math>gcd(x, y)</math> where <math>x > y</math> reduces the task to a smaller problem:
 
  
:::::<math>gcd(x, y) = gcd(y, x mod y)</math>
+
:2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both.
 +
#<math>f(n) = (n^2 - n)/2</math>,  <math>g(n) =6n</math>
 +
#<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math>
 +
#<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math>
 +
#<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math>
 +
#<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math>
 +
#<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math>
  
:Prove that Euclid’s algorithm is correct.
 
  
===Induction===
+
:[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>?
 +
#<math>f(n) = 3n^2, g(n) = n^2</math>
 +
#<math>f(n) = 2n^4 - 3n^2 + 7, g(n) = n^5</math>
 +
#<math>f(n) = log n, g(n) = log n + 1/n</math>
 +
#<math>f(n) = 2^{klog n}, g(n) = n^k</math>
 +
#<math>f(n) = 2^n, g(n) = 2^{2n}</math>
  
:1.12. Prove that <math>\sum_{i=1}^n i</math>=<math>n(n+1)/2</math> for <math>n \geq 0</math>, by induction.
+
[[2.11|Solution]]
  
  
:[[1.13]]. Prove that <math>\sum_{i=1}^n i^2</math>=<math>n(n+1)(2n+1)/6</math> for <math>n \geq\ 0</math>, by induction.
+
:2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>.
  
  
:1.14. Prove that <math>\sum_{i=1}^n i^3</math>=<math>n^2(n+1)^2/4</math> for <math>n \geq 0</math>, by induction.
+
:2.13. Prove that <math>n^2 = O(2^n)</math>.
  
 +
[[2.13|Solution]]
  
:[[1.15]]. Prove that <math> \sum_{i=1}^n i(i+1)(i+2)=n(n+1)(n+2)(n+3)/4 </math>
 
  
 +
:2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>.
  
:1.16. Prove by induction on <math>n \geq 1</math> that for every <math>a \neq 1</math>, <math> \sum_{i=0}^n a^i =\frac{a^{n+1}-1}{a-1}</math>
 
  
 +
:[[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) <math>n^2</math>  (b) <math>n^3</math>  (c) <math>100n^2</math>  (d) <math>nlogn</math>  (e) <math>2^n</math>
  
:[[1.17]]. Prove by induction that for <math>n \geq 1</math>, <math> \sum_{i=1}^n \frac{1}{i(i+1)} = \frac{n}{n+1} </math>
+
[[2.15|Solution]]
  
  
:1.18. Prove by induction that <math>n^3+2n</math> is divisible by <math>3</math> for all <math>n \geq 0</math>.
+
: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 <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour?
 +
::(a) <math>n^2</math>  (b) <math>n^3</math> (c) <math>100n^2</math>  (d) <math>nlogn</math>  (e) <math>2^n</math> (f) <math>2^{2^n}</math>
  
  
:[[1.19]]. Prove by induction that a tree with <math>n</math> vertices has exactly <math>n-1</math> edges.
+
:[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>.
 +
#<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math>
 +
#<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math>
 +
#<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math>
  
 +
[[2.17|Solution]]
  
:1.20. Prove by mathematical induction that the sum of the cubes of the first <math>n</math> positive integers is equal to the square of the sum of these integers, i.e.
 
  
::::::::::::<math> \sum_{i=1}^n i^3 = ( \sum_{i=1}^n i )^2 </math>
+
:2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>.
  
===Estimation===
 
  
:[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library?
+
:[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>.
  
 +
[[2.19|Solution]]
  
:1.22. How many words are there in this textbook?
 
  
 +
:2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math>
  
:[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head.
 
  
 +
:[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math>
  
:1.24. Estimate how many cities and towns there are in the United States.
+
[[2.21|Solution]]
  
  
:[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer.
+
:2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math>
 +
<center><math>(n + a)^b = \Omega (n^b)</math></center>
  
  
:1.26. How many Starbucks or McDonald’s locations are there in your country?
+
:[[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.
 +
<center>
 +
<math>\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}</math>
 +
</center>
  
 +
[[2.23|Solution]]
  
:[[1.27]]. How long would it take to empty a bathtub with a drinking straw?
 
  
 +
:2.24. List the functions below from lowest to highest order. If any two or more are of the same order, indicate which.
 +
<center>
 +
<math>\begin{array}{llll}
 +
n^{\pi} & \pi^n & \binom{n}{5} & \sqrt{2\sqrt{n}} \\
 +
\binom{n}{n-4} & 2^{log^4n} & n^{5(logn)^2} & n^4\binom{n}{n-4}
 +
\\
 +
\end{array}</math>
 +
</center>
  
:1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time?
 
  
 +
:[[2.25]]. List the functions below from lowest to highest order. If any two or more are of the same order, indicate which.
 +
<center>
 +
<math>\begin{array}{llll}
 +
\sum_{i=1}^n i^i & n^n & (log n)^{log n} & 2^{(log n^2)}\\
 +
n! & 2^{log^4n} & n^{(log n)^2} & n^4 \binom{n}{n-4}\\
 +
\end{array}</math>
 +
</center>
  
:[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . .
 
::(a) if you believe that the algorithm takes time proportional to n2, and
 
::(b) if you believe that the algorithm takes time roughly proportional to n log n?
 
  
 +
[[2.25|Solution]]
  
===Implementation Projects===
 
  
:1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them?
+
: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.
 +
<center>
 +
<math>\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}</math>
 +
</center>
  
  
:[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets.
+
:[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.''
 +
#<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math>
 +
#<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math>
 +
#<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math>
 +
#<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math>
  
 +
[[2.27|Solution]]
 +
 +
 +
:2.28. True or False?
 +
#<math>2n^2+1=O(n^2)</math>
 +
#<math>\sqrt n= O(\log n)</math>
 +
#<math>\log n = O(\sqrt n)</math>
 +
#<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math>
 +
#<math>3n^2 + \sqrt n = O(n^2)</math>
 +
#<math>\sqrt n \log n= O(n) </math>
 +
#<math>\log n=O(n^{-1/2})</math>
 +
 +
 +
:[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above.
 +
#<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math>
 +
#<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math>
 +
#<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math>
 +
 +
[[2.29|Solution]].
 +
 +
 +
:2.30. For each of these questions, briefly explain your answer.
 +
::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs?
 +
::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs?
 +
::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs?
 +
::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs?
 +
::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>?
 +
 +
 +
:[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning.
 +
::(a) Is <math>3^n = O(2^n)</math>?
 +
::(b) Is <math>\log 3^n = O( \log 2^n )</math>?
 +
::(c) Is <math>3^n = \Omega(2^n)</math>?
 +
::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>?
 +
 +
[[2.31|Solution]]
 +
 +
 +
:2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>.
 +
#<math>f(n)=\sum_{i=1}^n {1\over i}</math>.
 +
#<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>.
 +
#<math>f(n)=\sum_{i=1}^n \log i</math>.
 +
#<math>f(n)=\log (n!)</math>.
 +
 +
 +
:[[2.33]]. Place the following functions into increasing asymptotic order.
 +
::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>.
 +
 +
[[2.33|Solution]]
 +
 +
 +
:2.34. Which of the following are true?
 +
#<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>.
 +
#<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>.
 +
#<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>.
 +
 +
 +
:[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>.
 +
#<math>f_1(n)= (1000)2^n + 4^n</math>.
 +
#<math>f_2(n)= n + n\log n + \sqrt n</math>.
 +
#<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>.
 +
#<math>f_4(n)= (0.99)^n + n^{100}.</math>
 +
 +
[[2.35|Solution]]
 +
 +
 +
:2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>.  Note that zero, one or more of these relations may hold for a given pair; list all correct ones.
 +
<br><center><math>
 +
\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}
 +
</math></center>
 +
 +
===Summations===
 +
 +
 +
:[[2.37]]. Find an expression for the sum of the <math>i</math>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.
 +
<center>
 +
<math>\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}</math>
 +
</center>
 +
 +
[[2.37|Solution]]
 +
 +
 +
:2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.)
 +
 +
 +
:[[2.39]]
 +
 +
[[2.39|Solution]]
 +
 +
 +
:2.40. Consider the following code fragment.
 +
<tt>
 +
  for i=1 to n do
 +
      for j=i to 2*i do
 +
        output ''foobar''
 +
</tt>
 +
:Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>.
 +
#Express <math>T(n)</math> as a summation (actually two nested summations).
 +
#Simplify the summation.  Show your work.
 +
 +
 +
:[[2.41]].Consider the following code fragment.
 +
<tt>
 +
  for i=1 to n/2 do
 +
      for j=i to n-i do
 +
        for k=1 to j do
 +
            output ''foobar''
 +
</tt>
 +
:Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>.
 +
#Express <math>T(n)</math> as three nested summations.
 +
#Simplify the summation.  Show your work.
 +
 +
[[2.41|Solution]]
 +
 +
 +
:2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?)
 +
 +
 +
:[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time.
 +
 +
[[2.43|Solution]]
 +
 +
===Logartihms===
 +
 +
 +
:2.44. Prove the following identities on logarithms:
 +
#Prove that <math>\log_a (xy) = \log_a x + \log_a y</math>
 +
#Prove that <math>\log_a x^y = y \log_a x</math>
 +
#Prove that <math>\log_a x = \frac{\log_b x}{\log_b a}</math>
 +
#Prove that <math>x^{\log_b y} = y^{\log_b x}</math>
 +
 +
 +
:[[2.45]]. Show that <math>\lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1</math>
 +
 +
[[2.45|Solution]]
 +
 +
 +
:2.46. Prove that that the binary representation of <math>n \geq 1</math> has <math>\lfloor \lg_2 n \rfloor</math> + <math>1</math> bits.
 +
 +
 +
:[[2.47]]. In one of my research papers I give a comparison-based sorting algorithm that runs in <math>O( n \log (\sqrt n) )</math>. Given the existence of an <math>\Omega(n \log n)</math> lower bound for sorting, how can this be possible?
 +
 +
 +
[[2.47|Solution]]
  
 
===Interview Problems===
 
===Interview Problems===
  
:1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it.
+
 
 +
:2.48. You are given a set <math>S</math> of <math>n</math> numbers. You must pick a subset <math>S'</math> of <math>k</math> numbers from <math>S</math> such that the probability of each element of <math>S</math> occurring in <math>S'</math> is equal (i.e., each is selected with probability <math>k/n</math>). You may make only one pass over the numbers. What if <math>n</math> is unknown?
 +
 
 +
 
 +
:[[2.49]]. 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.49|Solution]]
 +
 
 +
 
 +
:2.50. Consider the following algorithm to find the minimum element in an array of numbers <math>A[0, \ldots, n]</math>. One extra variable <math>tmp</math> is allocated to hold the current minimum value. Start from A[0]; "tmp" is compared against <math>A[1]</math>,
 +
<math>A[2]</math>, <math>\ldots</math>, <math>A[N]</math> in order. When <math>A[i]<tmp</math>, <math>tmp = A[i]</math>. What is the expected number of times that the assignment operation <math>tmp = A[i]</math> is performed?
  
  
:[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done.
+
:[[2.51]]. You are given ten bags of gold coins. Nine bags contain coins that each weigh 10 grams. One bag contains all false coins that weigh 1 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.
  
 +
[[2.51|Solution]]
  
:1.34. How many piano tuners are there in the entire world?
 
  
 +
:2.52. 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 weightings?
  
:[[1.35]]. How many gas stations are there in the United States?
 
  
 +
:[[2.53]]. Suppose we start with <math>n</math> companies that eventually merge into one big company. How many different ways are there for them to merge?
  
:1.36. How much does the ice in a hockey rink weigh?
+
[[2.53|Solution]]
  
  
:[[1.37]]. How many miles of road are there in the United States?
+
:2.54. Six pirates must divide $300 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 propose the division. Now 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.
  
  
:1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name?
+
:[[2.55]]. Reconsider the pirate problem above, where we start with only one indivisible dollar. Who gets the dollar, and how many are killed?
  
 +
[[2.55|Solution]]
  
  
 
Back to [[Chapter List]]
 
Back to [[Chapter List]]

Revision as of 18:20, 19 September 2020

Algorithm Analysis

Program Analysis

2.1. What value is returned by the following function? Express your answer as a function of [math]\displaystyle{ n }[/math]. 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 [math]\displaystyle{ n }[/math]. 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 [math]\displaystyle{ n }[/math]. 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 [math]\displaystyle{ n }[/math]. Give the worst-case running time using Big Oh notation.
  conundrum([math]\displaystyle{ n }[/math])
      [math]\displaystyle{ r:=0 }[/math]
      for [math]\displaystyle{ i:=1 }[/math] to [math]\displaystyle{ n }[/math] do
      for [math]\displaystyle{ j:=i+1 }[/math] to [math]\displaystyle{ n }[/math] do
      for [math]\displaystyle{ k:=i+j-1 }[/math] to [math]\displaystyle{ n }[/math] do
      [math]\displaystyle{ r:=r+1 }[/math]
      return(r)


2.5. Consider the following algorithm: (the print operation prints a single asterisk; the operation [math]\displaystyle{ x = 2x }[/math] doubles the value of the variable [math]\displaystyle{ x }[/math]).
   for [math]\displaystyle{  k = 1 }[/math] to [math]\displaystyle{ n }[/math]
       [math]\displaystyle{ x = k }[/math]
       while ([math]\displaystyle{ x \lt  n }[/math]):
          print '*'
          [math]\displaystyle{ x = 2x }[/math]
Let [math]\displaystyle{ f(n) }[/math] be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for [math]\displaystyle{ O(f(n)) }[/math], and [math]\displaystyle{ \Theta(f(n)) }[/math], ideally converging on [math]\displaystyle{ \Theta(f(n)) }[/math].

Solution


2.6. Suppose the following algorithm is used to evaluate the polynomial
[math]\displaystyle{ p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0 }[/math]
   [math]\displaystyle{ p:=a_0; }[/math]
   [math]\displaystyle{ xpower:=1; }[/math]
   for [math]\displaystyle{ i:=1 }[/math] to [math]\displaystyle{ n }[/math] do
   [math]\displaystyle{ xpower:=x*xpower; }[/math]
   [math]\displaystyle{ p:=p+a_i * xpower }[/math]
  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 [math]\displaystyle{ A[1..n] }[/math] is correct.
  max(A)
     [math]\displaystyle{ m:=A[1] }[/math]
     for [math]\displaystyle{ i:=2 }[/math] to n do
           if [math]\displaystyle{ A[i] \gt  m }[/math] then [math]\displaystyle{ m:=A[i] }[/math]
     return (m)

Solution

Big Oh

2.8. True or False?
  1. Is [math]\displaystyle{ 2^{n+1} = O (2^n) }[/math]?
  2. Is [math]\displaystyle{ 2^{2n} = O(2^n) }[/math]?


2.9. For each of the following pairs of functions, either [math]\displaystyle{ f(n ) }[/math] is in [math]\displaystyle{ O(g(n)) }[/math], [math]\displaystyle{ f(n) }[/math] is in [math]\displaystyle{ \Omega(g(n)) }[/math], or [math]\displaystyle{ f(n)=\Theta(g(n)) }[/math]. Determine which relationship is correct and briefly explain why.
  1. [math]\displaystyle{ f(n)=\log n^2 }[/math]; [math]\displaystyle{ g(n)=\log n }[/math] + [math]\displaystyle{ 5 }[/math]
  2. [math]\displaystyle{ f(n)=\sqrt n }[/math]; [math]\displaystyle{ g(n)=\log n^2 }[/math]
  3. [math]\displaystyle{ f(n)=\log^2 n }[/math]; [math]\displaystyle{ g(n)=\log n }[/math]
  4. [math]\displaystyle{ f(n)=n }[/math]; [math]\displaystyle{ g(n)=\log^2 n }[/math]
  5. [math]\displaystyle{ f(n)=n \log n + n }[/math]; [math]\displaystyle{ g(n)=\log n }[/math]
  6. [math]\displaystyle{ f(n)=10 }[/math]; [math]\displaystyle{ g(n)=\log 10 }[/math]
  7. [math]\displaystyle{ f(n)=2^n }[/math]; [math]\displaystyle{ g(n)=10 n^2 }[/math]
  8. [math]\displaystyle{ f(n)=2^n }[/math]; [math]\displaystyle{ g(n)=3^n }[/math]

Solution


2.10. For each of the following pairs of functions [math]\displaystyle{ f(n) }[/math] and [math]\displaystyle{ g(n) }[/math], determine whether [math]\displaystyle{ f(n) = O(g(n)) }[/math], [math]\displaystyle{ g(n) = O(f(n)) }[/math], or both.
  1. [math]\displaystyle{ f(n) = (n^2 - n)/2 }[/math], [math]\displaystyle{ g(n) =6n }[/math]
  2. [math]\displaystyle{ f(n) = n +2 \sqrt n }[/math], [math]\displaystyle{ g(n) = n^2 }[/math]
  3. [math]\displaystyle{ f(n) = n \log n }[/math], [math]\displaystyle{ g(n) = n \sqrt n /2 }[/math]
  4. [math]\displaystyle{ f(n) = n + \log n }[/math], [math]\displaystyle{ g(n) = \sqrt n }[/math]
  5. [math]\displaystyle{ f(n) = 2(\log n)^2 }[/math], [math]\displaystyle{ g(n) = \log n + 1 }[/math]
  6. [math]\displaystyle{ f(n) = 4n\log n + n }[/math], [math]\displaystyle{ g(n) = (n^2 - n)/2 }[/math]


2.11. For each of the following functions, which of the following asymptotic bounds hold for [math]\displaystyle{ f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n)) }[/math]?
  1. [math]\displaystyle{ f(n) = 3n^2, g(n) = n^2 }[/math]
  2. [math]\displaystyle{ f(n) = 2n^4 - 3n^2 + 7, g(n) = n^5 }[/math]
  3. [math]\displaystyle{ f(n) = log n, g(n) = log n + 1/n }[/math]
  4. [math]\displaystyle{ f(n) = 2^{klog n}, g(n) = n^k }[/math]
  5. [math]\displaystyle{ f(n) = 2^n, g(n) = 2^{2n} }[/math]

Solution


2.12. Prove that [math]\displaystyle{ n^3 - 3n^2-n+1 = \Theta(n^3) }[/math].


2.13. Prove that [math]\displaystyle{ n^2 = O(2^n) }[/math].

Solution


2.14. Prove or disprove: [math]\displaystyle{ \Theta(n^2) = \Theta(n^2+1) }[/math].


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) [math]\displaystyle{ n^2 }[/math] (b) [math]\displaystyle{ n^3 }[/math] (c) [math]\displaystyle{ 100n^2 }[/math] (d) [math]\displaystyle{ nlogn }[/math] (e) [math]\displaystyle{ 2^n }[/math]

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 [math]\displaystyle{ n }[/math].)Suppose you have a computer that can perform [math]\displaystyle{ 10^10 }[/math] operations per second. For each algorithm, what is the largest input size n that you can complete within an hour?
(a) [math]\displaystyle{ n^2 }[/math] (b) [math]\displaystyle{ n^3 }[/math] (c) [math]\displaystyle{ 100n^2 }[/math] (d) [math]\displaystyle{ nlogn }[/math] (e) [math]\displaystyle{ 2^n }[/math] (f) [math]\displaystyle{ 2^{2^n} }[/math]


2.17. For each of the following pairs of functions [math]\displaystyle{ f(n) }[/math] and [math]\displaystyle{ g(n) }[/math], give an appropriate positive constant [math]\displaystyle{ c }[/math] such that [math]\displaystyle{ f(n) \leq c \cdot g(n) }[/math] for all [math]\displaystyle{ n \gt 1 }[/math].
  1. [math]\displaystyle{ f(n)=n^2+n+1 }[/math], [math]\displaystyle{ g(n)=2n^3 }[/math]
  2. [math]\displaystyle{ f(n)=n \sqrt n + n^2 }[/math], [math]\displaystyle{ g(n)=n^2 }[/math]
  3. [math]\displaystyle{ f(n)=n^2-n+1 }[/math], [math]\displaystyle{ g(n)=n^2/2 }[/math]

Solution


2.18. Prove that if [math]\displaystyle{ f_1(n)=O(g_1(n)) }[/math] and [math]\displaystyle{ f_2(n)=O(g_2(n)) }[/math], then [math]\displaystyle{ f_1(n)+f_2(n) = O(g_1(n)+g_2(n)) }[/math].


2.19. Prove that if [math]\displaystyle{ f_1(N)=\Omega(g_1(n)) }[/math] and [math]\displaystyle{ f_2(n)=\Omega(g_2(n) }[/math], then [math]\displaystyle{ f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n)) }[/math].

Solution


2.20. Prove that if [math]\displaystyle{ f_1(n)=O(g_1(n)) }[/math] and [math]\displaystyle{ f_2(n)=O(g_2(n)) }[/math], then [math]\displaystyle{ f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n)) }[/math]


2.21. Prove for all [math]\displaystyle{ k \geq 1 }[/math] and all sets of constants [math]\displaystyle{ \{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R }[/math], [math]\displaystyle{ a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k) }[/math]

Solution


2.22. Show that for any real constants [math]\displaystyle{ a }[/math] and [math]\displaystyle{ b }[/math], [math]\displaystyle{ b \gt 0 }[/math]
[math]\displaystyle{ (n + a)^b = \Omega (n^b) }[/math]


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.

[math]\displaystyle{ \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\lt \varepsilon \lt 1 \\ \end{array} }[/math]

Solution


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

[math]\displaystyle{ \begin{array}{llll} n^{\pi} & \pi^n & \binom{n}{5} & \sqrt{2\sqrt{n}} \\ \binom{n}{n-4} & 2^{log^4n} & n^{5(logn)^2} & n^4\binom{n}{n-4} \\ \end{array} }[/math]


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

[math]\displaystyle{ \begin{array}{llll} \sum_{i=1}^n i^i & n^n & (log n)^{log n} & 2^{(log n^2)}\\ n! & 2^{log^4n} & n^{(log n)^2} & n^4 \binom{n}{n-4}\\ \end{array} }[/math]


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.

[math]\displaystyle{ \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} }[/math]


2.27. Find two functions [math]\displaystyle{ f(n) }[/math] and [math]\displaystyle{ g(n) }[/math] that satisfy the following relationship. If no such [math]\displaystyle{ f }[/math] and [math]\displaystyle{ g }[/math] exist, write None.
  1. [math]\displaystyle{ f(n)=o(g(n)) }[/math] and [math]\displaystyle{ f(n) \neq \Theta(g(n)) }[/math]
  2. [math]\displaystyle{ f(n)=\Theta(g(n)) }[/math] and [math]\displaystyle{ f(n)=o(g(n)) }[/math]
  3. [math]\displaystyle{ f(n)=\Theta(g(n)) }[/math] and [math]\displaystyle{ f(n) \neq O(g(n)) }[/math]
  4. [math]\displaystyle{ f(n)=\Omega(g(n)) }[/math] and [math]\displaystyle{ f(n) \neq O(g(n)) }[/math]

Solution


2.28. True or False?
  1. [math]\displaystyle{ 2n^2+1=O(n^2) }[/math]
  2. [math]\displaystyle{ \sqrt n= O(\log n) }[/math]
  3. [math]\displaystyle{ \log n = O(\sqrt n) }[/math]
  4. [math]\displaystyle{ n^2(1 + \sqrt n) = O(n^2 \log n) }[/math]
  5. [math]\displaystyle{ 3n^2 + \sqrt n = O(n^2) }[/math]
  6. [math]\displaystyle{ \sqrt n \log n= O(n) }[/math]
  7. [math]\displaystyle{ \log n=O(n^{-1/2}) }[/math]


2.29. For each of the following pairs of functions [math]\displaystyle{ f(n) }[/math] and [math]\displaystyle{ g(n) }[/math], state whether [math]\displaystyle{ f(n)=O(g(n)) }[/math], [math]\displaystyle{ f(n)=\Omega(g(n)) }[/math], [math]\displaystyle{ f(n)=\Theta(g(n)) }[/math], or none of the above.
  1. [math]\displaystyle{ f(n)=n^2+3n+4 }[/math], [math]\displaystyle{ g(n)=6n+7 }[/math]
  2. [math]\displaystyle{ f(n)=n \sqrt n }[/math], [math]\displaystyle{ g(n)=n^2-n }[/math]
  3. [math]\displaystyle{ f(n)=2^n - n^2 }[/math], [math]\displaystyle{ g(n)=n^4+n^2 }[/math]

Solution.


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


2.31. For each of the following, answer yes, no, or can't tell. Explain your reasoning.
(a) Is [math]\displaystyle{ 3^n = O(2^n) }[/math]?
(b) Is [math]\displaystyle{ \log 3^n = O( \log 2^n ) }[/math]?
(c) Is [math]\displaystyle{ 3^n = \Omega(2^n) }[/math]?
(d) Is [math]\displaystyle{ \log 3^n = \Omega( \log 2^n ) }[/math]?

Solution


2.32. For each of the following expressions [math]\displaystyle{ f(n) }[/math] find a simple [math]\displaystyle{ g(n) }[/math] such that [math]\displaystyle{ f(n)=\Theta(g(n)) }[/math].
  1. [math]\displaystyle{ f(n)=\sum_{i=1}^n {1\over i} }[/math].
  2. [math]\displaystyle{ f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil }[/math].
  3. [math]\displaystyle{ f(n)=\sum_{i=1}^n \log i }[/math].
  4. [math]\displaystyle{ f(n)=\log (n!) }[/math].


2.33. Place the following functions into increasing asymptotic order.
[math]\displaystyle{ f_1(n) = n^2\log_2n }[/math], [math]\displaystyle{ f_2(n) = n(\log_2n)^2 }[/math], [math]\displaystyle{ f_3(n) = \sum_{i=0}^n 2^i }[/math], [math]\displaystyle{ f_4(n) = \log_2(\sum_{i=0}^n 2^i) }[/math].

Solution


2.34. Which of the following are true?
  1. [math]\displaystyle{ \sum_{i=1}^n 3^i = \Theta(3^{n-1}) }[/math].
  2. [math]\displaystyle{ \sum_{i=1}^n 3^i = \Theta(3^n) }[/math].
  3. [math]\displaystyle{ \sum_{i=1}^n 3^i = \Theta(3^{n+1}) }[/math].


2.35. For each of the following functions [math]\displaystyle{ f }[/math] find a simple function [math]\displaystyle{ g }[/math] such that [math]\displaystyle{ f(n)=\Theta(g(n)) }[/math].
  1. [math]\displaystyle{ f_1(n)= (1000)2^n + 4^n }[/math].
  2. [math]\displaystyle{ f_2(n)= n + n\log n + \sqrt n }[/math].
  3. [math]\displaystyle{ f_3(n)= \log (n^{20}) + (\log n)^{10} }[/math].
  4. [math]\displaystyle{ f_4(n)= (0.99)^n + n^{100}. }[/math]

Solution


2.36. For each pair of expressions [math]\displaystyle{ (A,B) }[/math] below, indicate whether [math]\displaystyle{ A }[/math] is [math]\displaystyle{ O }[/math], [math]\displaystyle{ o }[/math], [math]\displaystyle{ \Omega }[/math], [math]\displaystyle{ \omega }[/math], or [math]\displaystyle{ \Theta }[/math] of [math]\displaystyle{ B }[/math]. Note that zero, one or more of these relations may hold for a given pair; list all correct ones.


[math]\displaystyle{ \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} }[/math]

Summations

2.37. Find an expression for the sum of the [math]\displaystyle{ i }[/math]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.

[math]\displaystyle{ \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} }[/math]

Solution


2.38. Assume that Christmas has [math]\displaystyle{ n }[/math] 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 [math]\displaystyle{ T(n) }[/math] denote the number of times `foobar' is printed as a function of [math]\displaystyle{ n }[/math].
  1. Express [math]\displaystyle{ T(n) }[/math] 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 [math]\displaystyle{ n }[/math] is even. Let [math]\displaystyle{ T(n) }[/math] denote the number of times `foobar' is printed as a function of [math]\displaystyle{ n }[/math].
  1. Express [math]\displaystyle{ T(n) }[/math] 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 [math]\displaystyle{ x \times y }[/math] means adding [math]\displaystyle{ x }[/math] a total of [math]\displaystyle{ y }[/math] times, so [math]\displaystyle{ 5 \times 4 = 5+5+5+5 = 20 }[/math]. What is the time complexity of multiplying two [math]\displaystyle{ n }[/math]-digit numbers in base [math]\displaystyle{ b }[/math] (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of [math]\displaystyle{ n }[/math] and [math]\displaystyle{ b }[/math]. Assume that single-digit by single-digit addition or multiplication takes [math]\displaystyle{ O(1) }[/math] time. (Hint: how big can [math]\displaystyle{ y }[/math] be as a function of [math]\displaystyle{ n }[/math] and [math]\displaystyle{ b }[/math]?)


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

Solution

Logartihms

2.44. Prove the following identities on logarithms:
  1. Prove that [math]\displaystyle{ \log_a (xy) = \log_a x + \log_a y }[/math]
  2. Prove that [math]\displaystyle{ \log_a x^y = y \log_a x }[/math]
  3. Prove that [math]\displaystyle{ \log_a x = \frac{\log_b x}{\log_b a} }[/math]
  4. Prove that [math]\displaystyle{ x^{\log_b y} = y^{\log_b x} }[/math]


2.45. Show that [math]\displaystyle{ \lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1 }[/math]

Solution


2.46. Prove that that the binary representation of [math]\displaystyle{ n \geq 1 }[/math] has [math]\displaystyle{ \lfloor \lg_2 n \rfloor }[/math] + [math]\displaystyle{ 1 }[/math] bits.


2.47. In one of my research papers I give a comparison-based sorting algorithm that runs in [math]\displaystyle{ O( n \log (\sqrt n) ) }[/math]. Given the existence of an [math]\displaystyle{ \Omega(n \log n) }[/math] lower bound for sorting, how can this be possible?


Solution

Interview Problems

2.48. You are given a set [math]\displaystyle{ S }[/math] of [math]\displaystyle{ n }[/math] numbers. You must pick a subset [math]\displaystyle{ S' }[/math] of [math]\displaystyle{ k }[/math] numbers from [math]\displaystyle{ S }[/math] such that the probability of each element of [math]\displaystyle{ S }[/math] occurring in [math]\displaystyle{ S' }[/math] is equal (i.e., each is selected with probability [math]\displaystyle{ k/n }[/math]). You may make only one pass over the numbers. What if [math]\displaystyle{ n }[/math] is unknown?


2.49. 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?

Solution


2.50. Consider the following algorithm to find the minimum element in an array of numbers [math]\displaystyle{ A[0, \ldots, n] }[/math]. One extra variable [math]\displaystyle{ tmp }[/math] is allocated to hold the current minimum value. Start from A[0]; "tmp" is compared against [math]\displaystyle{ A[1] }[/math],

[math]\displaystyle{ A[2] }[/math], [math]\displaystyle{ \ldots }[/math], [math]\displaystyle{ A[N] }[/math] in order. When [math]\displaystyle{ A[i]\lt tmp }[/math], [math]\displaystyle{ tmp = A[i] }[/math]. What is the expected number of times that the assignment operation [math]\displaystyle{ tmp = A[i] }[/math] is performed?


2.51. You are given ten bags of gold coins. Nine bags contain coins that each weigh 10 grams. One bag contains all false coins that weigh 1 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.52. 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 weightings?


2.53. Suppose we start with [math]\displaystyle{ n }[/math] companies that eventually merge into one big company. How many different ways are there for them to merge?

Solution


2.54. Six pirates must divide $300 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 propose the division. Now 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.


2.55. Reconsider the pirate problem above, where we start with only one indivisible dollar. Who gets the dollar, and how many are killed?

Solution


Back to Chapter List