Difference between revisions of "Algo-analysis-TADM2E"

From Algorithm Wiki
Jump to: navigation, search
(Recovering wiki)
 
(Recovering wiki)
Line 5: Line 5:
 
'''Program Analysis'''
 
'''Program Analysis'''
  
<br>2-1.  
+
<br>2-1.  
 
What value is returned by the following function?
 
What value is returned by the following function?
Express your answer as a function of &lt;math&gt;n&lt;/math&gt;.
+
Express your answer as a function of <math>n</math>.
 
Give the worst-case running time using the Big Oh notation.
 
Give the worst-case running time using the Big Oh notation.
 
   '''function''' mystery(''n'')
 
   '''function''' mystery(''n'')
Line 19: Line 19:
 
[[TADM2E 2.1|(Solution 2.1)]]  
 
[[TADM2E 2.1|(Solution 2.1)]]  
  
&lt;br&gt;2-2.  
+
<br>2-2.  
 
What value is returned by the following function?
 
What value is returned by the following function?
Express your answer as a function of &lt;math&gt;n&lt;/math&gt;.
+
Express your answer as a function of <math>n</math>.
 
Give the worst-case running time using Big Oh notation.
 
Give the worst-case running time using Big Oh notation.
 
     '''function''' pesky(n)
 
     '''function''' pesky(n)
Line 33: Line 33:
 
[[TADM2E 2.2|(Solution 2.2)]]  
 
[[TADM2E 2.2|(Solution 2.2)]]  
  
&lt;br&gt;2-3.  
+
<br>2-3.  
 
What value is returned by the following function?
 
What value is returned by the following function?
Express your answer as a function of &lt;math&gt;n&lt;/math&gt;.
+
Express your answer as a function of <math>n</math>.
 
Give the worst-case running time using Big Oh notation.
 
Give the worst-case running time using Big Oh notation.
 
     '''function''' prestiferous(n)
 
     '''function''' prestiferous(n)
Line 48: Line 48:
 
[[TADM2E 2.3|(Solution 2.3)]]  
 
[[TADM2E 2.3|(Solution 2.3)]]  
  
&lt;br&gt;2-4.  
+
<br>2-4.  
 
What value is returned by the following function?
 
What value is returned by the following function?
Express your answer as a function of &lt;math&gt;n&lt;/math&gt;.
+
Express your answer as a function of <math>n</math>.
 
Give the worst-case running time using Big Oh notation.
 
Give the worst-case running time using Big Oh notation.
   ''function'' conundrum(&lt;math&gt;n&lt;/math&gt;)
+
   ''function'' conundrum(<math>n</math>)
       &lt;math&gt;r:=0&lt;/math&gt;
+
       <math>r:=0</math>
       ''for'' &lt;math&gt;i:=1&lt;/math&gt; ''to'' &lt;math&gt;n&lt;/math&gt; ''do''
+
       ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do''
       ''for'' &lt;math&gt;j:=i+1&lt;/math&gt; ''to'' &lt;math&gt;n&lt;/math&gt; ''do''
+
       ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do''
       ''for'' &lt;math&gt;k:=i+j-1&lt;/math&gt; ''to'' &lt;math&gt;n&lt;/math&gt; ''do''
+
       ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do''
       &lt;math&gt;r:=r+1&lt;/math&gt;
+
       <math>r:=r+1</math>
         return(&lt;math&gt;r&lt;/math&gt;)
+
         return(<math>r</math>)
  
&lt;br&gt;2-5.  
+
<br>2-5.  
 
Suppose the following algorithm is used to evaluate the polynomial
 
Suppose the following algorithm is used to evaluate the polynomial
&lt;math&gt; p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0&lt;/math&gt;
+
<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math>
     &lt;math&gt;p:=a_0;&lt;/math&gt;
+
     <math>p:=a_0;</math>
     &lt;math&gt;xpower:=1;&lt;/math&gt;
+
     <math>xpower:=1;</math>
     for &lt;math&gt;i:=1&lt;/math&gt; to &lt;math&gt;n&lt;/math&gt; do
+
     for <math>i:=1</math> to <math>n</math> do
     &lt;math&gt;xpower:=x*xpower;&lt;/math&gt;
+
     <math>xpower:=x*xpower;</math>
     &lt;math&gt;p:=p+a_i * xpower&lt;/math&gt;
+
     <math>p:=p+a_i * xpower</math>
 
     end
 
     end
 
#How many multiplications are done in the worst-case? How many additions?
 
#How many multiplications are done in the worst-case? How many additions?
Line 75: Line 75:
 
[[TADM2E 2.5|(Solution 2.5)]]  
 
[[TADM2E 2.5|(Solution 2.5)]]  
  
&lt;br&gt;2-6.  
+
<br>2-6.  
 
Prove that the following algorithm for computing the maximum value in an
 
Prove that the following algorithm for computing the maximum value in an
array &lt;math&gt;A[1..n]&lt;/math&gt; is correct.
+
array <math>A[1..n]</math> is correct.
 
   ''function'' max(A)
 
   ''function'' max(A)
       &lt;math&gt;m:=A[1]&lt;/math&gt;
+
       <math>m:=A[1]</math>
       ''for'' &lt;math&gt;i:=2&lt;/math&gt; ''to'' n ''do''
+
       ''for'' <math>i:=2</math> ''to'' n ''do''
             ''if'' &lt;math&gt;A[i] &gt; m&lt;/math&gt; ''then'' &lt;math&gt;m:=A[i]&lt;/math&gt;
+
             ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math>
 
       ''return'' (m)
 
       ''return'' (m)
 
[[TADM2E 2.6|(Solution 2.6)]]  
 
[[TADM2E 2.6|(Solution 2.6)]]  
Line 89: Line 89:
 
'''Big Oh'''
 
'''Big Oh'''
  
&lt;br&gt;2-7.  
+
<br>2-7.  
 
True or False?
 
True or False?
#Is &lt;math&gt;2^{n+1} = O (2^n)&lt;/math&gt;?
+
#Is <math>2^{n+1} = O (2^n)</math>?
#Is &lt;math&gt;2^{2n} = O(2^n)&lt;/math&gt;?
+
#Is <math>2^{2n} = O(2^n)</math>?
  
 
[[TADM2E 2.7|(Solution 2.7)]]  
 
[[TADM2E 2.7|(Solution 2.7)]]  
  
&lt;br&gt;2-8.  
+
<br>2-8.  
For each of the following pairs of functions, either &lt;math&gt;f(n)&lt;/math&gt; is in
+
For each of the following pairs of functions, either <math>f(n)</math> is in
&lt;math&gt;O(g(n))&lt;/math&gt;, &lt;math&gt;f(n)&lt;/math&gt; is in &lt;math&gt;\Omega(g(n))&lt;/math&gt;, or &lt;math&gt;f(n)=\Theta(g(n))&lt;/math&gt;.
+
<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.
 
Determine which relationship is correct and briefly explain why.
#&lt;math&gt;f(n)=\log n^2&lt;/math&gt;; &lt;math&gt;g(n)=\log n&lt;/math&gt; + &lt;math&gt;5&lt;/math&gt;
+
#<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math>
#&lt;math&gt;f(n)=\sqrt n&lt;/math&gt;; &lt;math&gt;g(n)=\log n^2&lt;/math&gt;
+
#<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math>
#&lt;math&gt;f(n)=\log^2 n&lt;/math&gt;; &lt;math&gt;g(n)=\log n&lt;/math&gt;
+
#<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math>
#&lt;math&gt;f(n)=n&lt;/math&gt;; &lt;math&gt;g(n)=\log^2 n&lt;/math&gt;
+
#<math>f(n)=n</math>; <math>g(n)=\log^2 n</math>
#&lt;math&gt;f(n)=n \log n + n&lt;/math&gt;; &lt;math&gt;g(n)=\log n&lt;/math&gt;
+
#<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math>
#&lt;math&gt;f(n)=10&lt;/math&gt;; &lt;math&gt;g(n)=\log 10&lt;/math&gt;
+
#<math>f(n)=10</math>; <math>g(n)=\log 10</math>
#&lt;math&gt;f(n)=2^n&lt;/math&gt;; &lt;math&gt;g(n)=10 n^2&lt;/math&gt;
+
#<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math>
#&lt;math&gt;f(n)=2^n&lt;/math&gt;; &lt;math&gt;g(n)=3^n&lt;/math&gt;
+
#<math>f(n)=2^n</math>; <math>g(n)=3^n</math>
  
 
[[TADM2E 2.8|(Solution 2.8)]]  
 
[[TADM2E 2.8|(Solution 2.8)]]  
  
&lt;br&gt;2-9.  
+
<br>2-9.  
For each of the following pairs of functions &lt;math&gt;f(n)&lt;/math&gt; and &lt;math&gt;g(n)&lt;/math&gt;, determine
+
For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine
 
whether
 
whether
&lt;math&gt;f(n) = O(g(n))&lt;/math&gt;, &lt;math&gt;g(n) = O(f(n))&lt;/math&gt;, or both.
+
<math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both.
#&lt;math&gt;f(n) = (n^2 - n)/2&lt;/math&gt;&lt;math&gt;g(n) =6n&lt;/math&gt;
+
#<math>f(n) = (n^2 - n)/2</math><math>g(n) =6n</math>
#&lt;math&gt;f(n) = n +2 \sqrt n&lt;/math&gt;, &lt;math&gt;g(n) = n^2&lt;/math&gt;
+
#<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math>
#&lt;math&gt;f(n) = n \log n&lt;/math&gt;, &lt;math&gt;g(n) = n \sqrt n /2&lt;/math&gt;
+
#<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math>
#&lt;math&gt;f(n) = n + \log n&lt;/math&gt;, &lt;math&gt;g(n) = \sqrt n&lt;/math&gt;
+
#<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math>
#&lt;math&gt;f(n) = 2(\log n)^2&lt;/math&gt;, &lt;math&gt;g(n) = \log n + 1&lt;/math&gt;
+
#<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math>
#&lt;math&gt;f(n) = 4n\log n + n&lt;/math&gt;, &lt;math&gt;g(n) = (n^2 - n)/2&lt;/math&gt;
+
#<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math>
  
 
[[TADM2E 2.9|(Solution 2.9)]]  
 
[[TADM2E 2.9|(Solution 2.9)]]  
  
&lt;br&gt;2-10.  
+
<br>2-10.  
Prove that &lt;math&gt;n^3 - 3n^2-n+1 = \Theta(n^3)&lt;/math&gt;.
+
Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>.
  
&lt;br&gt;2-11.  
+
<br>2-11.  
Prove that &lt;math&gt;n^2 = O(2^n)&lt;/math&gt;.
+
Prove that <math>n^2 = O(2^n)</math>.
  
 
[[TADM2E 2.11|(Solution 2.11)]]  
 
[[TADM2E 2.11|(Solution 2.11)]]  
  
&lt;br&gt;2-12.  
+
<br>2-12.  
For each of the following pairs of functions &lt;math&gt;f(n)&lt;/math&gt; and &lt;math&gt;g(n)&lt;/math&gt;, give
+
For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give
an appropriate positive constant &lt;math&gt;c&lt;/math&gt;
+
an appropriate positive constant <math>c</math>
such that &lt;math&gt;f(n) \leq c \cdot g(n)&lt;/math&gt; for all &lt;math&gt;n &gt; 1&lt;/math&gt;.
+
such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>.
#&lt;math&gt;f(n)=n^2+n+1&lt;/math&gt;, &lt;math&gt;g(n)=2n^3&lt;/math&gt;
+
#<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math>
#&lt;math&gt;f(n)=n \sqrt n + n^2&lt;/math&gt;, &lt;math&gt;g(n)=n^2&lt;/math&gt;
+
#<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math>
#&lt;math&gt;f(n)=n^2-n+1&lt;/math&gt;, &lt;math&gt;g(n)=n^2/2&lt;/math&gt;
+
#<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math>
  
&lt;br&gt;2-13.  
+
<br>2-13.  
Prove that if &lt;math&gt;f_1(n)=O(g_1(n))&lt;/math&gt; and &lt;math&gt;f_2(n)=O(g_2(n))&lt;/math&gt;, then
+
Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then
&lt;math&gt;f_1(n)+f_2(n) = O(g_1(n)+g_2(n))&lt;/math&gt;.
+
<math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>.
  
 
[[TADM2E 2.13|(Solution 2.13)]]  
 
[[TADM2E 2.13|(Solution 2.13)]]  
  
&lt;br&gt;2-14.  
+
<br>2-14.  
Prove that if &lt;math&gt;f_1(N)=\Omega(g_1(n))&lt;/math&gt; and &lt;math&gt;f_2(n)=\Omega(g_2(n))&lt;/math&gt;, then
+
Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n))</math>, then
&lt;math&gt;f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))&lt;/math&gt;.
+
<math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>.
  
&lt;br&gt;2-15.  
+
<br>2-15.  
Prove that if &lt;math&gt;f_1(n)=O(g_1(n))&lt;/math&gt; and &lt;math&gt;f_2(n)=O(g_2(n))&lt;/math&gt;, then
+
Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then
&lt;math&gt;f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))&lt;/math&gt;
+
<math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math>
  
 
[[TADM2E 2.15|(Solution 2.15)]]  
 
[[TADM2E 2.15|(Solution 2.15)]]  
  
&lt;br&gt;2-16.  
+
<br>2-16.  
Prove for all &lt;math&gt;k \geq 1&lt;/math&gt; and all sets of
+
Prove for all <math>k \geq 1</math> and all sets of
constants &lt;math&gt;\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R&lt;/math&gt;,
+
constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>,
&lt;math&gt; a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)&lt;/math&gt;
+
<math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math>
  
&lt;br&gt;2-17.  
+
<br>2-17.  
 
Show that for any real constants
 
Show that for any real constants
&lt;math&gt;a&lt;/math&gt; and &lt;math&gt;b&lt;/math&gt;, &lt;math&gt;b &gt; 0&lt;/math&gt;
+
<math>a</math> and <math>b</math>, <math>b > 0</math>
 
''Big Oh notation''
 
''Big Oh notation''
&lt;math&gt; (n+a)^b = \Theta(n^b) &lt;/math&gt;
+
<math> (n+a)^b = \Theta(n^b) </math>
To show &lt;math&gt;f(n) = \Theta(g(n))&lt;/math&gt;, we
+
To show <math>f(n) = \Theta(g(n))</math>, we
must show &lt;math&gt;O&lt;/math&gt; and &lt;math&gt;\Omega&lt;/math&gt;.
+
must show <math>O</math> and <math>\Omega</math>.
 
''Go back to the definition!''
 
''Go back to the definition!''
#''Big &lt;math&gt;O&lt;/math&gt;'' -- Must show that &lt;math&gt;(n+a)^b \leq c_1 \cdot n^b&lt;/math&gt; for all &lt;math&gt;n &gt; n_0&lt;/math&gt;. When is this true? If &lt;math&gt;c_1 = 2^b&lt;/math&gt;, this is true for all &lt;math&gt;n &gt; |a|&lt;/math&gt; since &lt;math&gt;n+a &lt; 2n&lt;/math&gt;, and raise both sides to the &lt;math&gt;b&lt;/math&gt;.
+
#''Big <math>O</math>'' -- Must show that <math>(n+a)^b \leq c_1 \cdot n^b</math> for all <math>n > n_0</math>. When is this true? If <math>c_1 = 2^b</math>, this is true for all <math>n > |a|</math> since <math>n+a < 2n</math>, and raise both sides to the <math>b</math>.
#''Big &lt;math&gt;\Omega&lt;/math&gt;'' -- Must show that &lt;math&gt;(n+a)^b \geq c_2 \cdot n^b&lt;/math&gt; for all &lt;math&gt;n &gt; n_0&lt;/math&gt;. When is this true? If &lt;math&gt;c_2 = (1/2)^b&lt;/math&gt;, this is true for all &lt;math&gt;n &gt; 3|a|/2&lt;/math&gt; since &lt;math&gt;n+a &gt; n/2&lt;/math&gt;, and raise both sides to the &lt;math&gt;b&lt;/math&gt;.
+
#''Big <math>\Omega</math>'' -- Must show that <math>(n+a)^b \geq c_2 \cdot n^b</math> for all <math>n > n_0</math>. When is this true? If <math>c_2 = (1/2)^b</math>, this is true for all <math>n > 3|a|/2</math> since <math>n+a > n/2</math>, and raise both sides to the <math>b</math>.
 
Note the need for absolute values.
 
Note the need for absolute values.
  
 
[[TADM2E 2.17|(Solution 2.17)]]  
 
[[TADM2E 2.17|(Solution 2.17)]]  
  
&lt;br&gt;2-18.  
+
<br>2-18.  
 
List the functions below from the lowest to the highest order.
 
List the functions below from the lowest to the highest order.
 
If any two or more are of the same order, indicate which.
 
If any two or more are of the same order, indicate which.
&lt;center&gt;
+
<center>
&lt;math&gt;\begin{array}{llll}
+
<math>\begin{array}{llll}
n &amp; 2^n &amp; n \lg n &amp; \ln n \\
+
n & 2^n & n \lg n & \ln n \\
n-n^3+7n^5 &amp; \lg n &amp; \sqrt n &amp; e^n \\
+
n-n^3+7n^5 & \lg n & \sqrt n & e^n \\
n^2+\lg n &amp; n^2 &amp; 2^{n-1} &amp; \lg \lg n \\
+
n^2+\lg n & n^2 & 2^{n-1} &  \lg \lg n \\
n^3 &amp; (\lg n)^2 &amp; n! &amp; n^{1+\varepsilon} where 0&lt; \varepsilon &lt;1
+
n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1
 
\\
 
\\
\end{array}&lt;/math&gt;
+
\end{array}</math>
&lt;/center&gt;
+
</center>
  
&lt;br&gt;2-19.  
+
<br>2-19.  
 
List the functions below from the lowest to the highest order.
 
List the functions below from the lowest to the highest order.
 
If any two or more are of the same order, indicate which.
 
If any two or more are of the same order, indicate which.
&lt;center&gt;
+
<center>
&lt;math&gt;\begin{array}{lll}
+
<math>\begin{array}{lll}
\sqrt{n} &amp; n &amp; 2^n \\
+
\sqrt{n} & n & 2^n \\
n \log n &amp; n - n^3 + 7n^5 &amp; n^2 + \log n \\
+
n \log n &  n - n^3 + 7n^5 &  n^2 + \log n \\
n^2 &amp; n^3 &amp; \log n \\
+
n^2 &  n^3 &  \log n \\
n^{\frac{1}{3}} + \log n &amp; (\log n)^2 &amp; n! \\
+
n^{\frac{1}{3}} + \log n & (\log n)^2 &  n! \\
\ln n &amp; \frac{n}{\log n} &amp; \log \log n \\
+
\ln n & \frac{n}{\log n} &  \log \log n \\
({1}/{3})^n &amp; ({3}/{2})^n &amp; 6 \\
+
({1}/{3})^n &  ({3}/{2})^n &  6 \\
\end{array}&lt;/math&gt;
+
\end{array}</math>
&lt;/center&gt;
+
</center>
  
 
[[TADM2E 2.19|(Solution 2.19)]]  
 
[[TADM2E 2.19|(Solution 2.19)]]  
  
&lt;br&gt;2-20.  
+
<br>2-20.  
Find two functions &lt;math&gt;f(n)&lt;/math&gt; and &lt;math&gt;g(n)&lt;/math&gt; that satisfy the following
+
Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following
relationship. If no such &lt;math&gt;f&lt;/math&gt; and &lt;math&gt;g&lt;/math&gt; exist, write ''None.''
+
relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.''
#&lt;math&gt;f(n)=o(g(n))&lt;/math&gt; and &lt;math&gt;f(n) \neq \Theta(g(n))&lt;/math&gt;
+
#<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math>
#&lt;math&gt;f(n)=\Theta(g(n))&lt;/math&gt; and &lt;math&gt;f(n)=o(g(n))&lt;/math&gt;
+
#<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math>
#&lt;math&gt;f(n)=\Theta(g(n))&lt;/math&gt; and &lt;math&gt;f(n) \neq O(g(n))&lt;/math&gt;
+
#<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math>
#&lt;math&gt;f(n)=\Omega(g(n))&lt;/math&gt; and &lt;math&gt;f(n) \neq O(g(n))&lt;/math&gt;
+
#<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math>
  
&lt;br&gt;2-21.  
+
<br>2-21.  
 
True or False?
 
True or False?
#&lt;math&gt;2n^2+1=O(n^2)&lt;/math&gt;
+
#<math>2n^2+1=O(n^2)</math>
#&lt;math&gt;\sqrt n= O(\log n)&lt;/math&gt;
+
#<math>\sqrt n= O(\log n)</math>
#&lt;math&gt;\log n = O(\sqrt n)&lt;/math&gt;
+
#<math>\log n = O(\sqrt n)</math>
#&lt;math&gt;n^2(1 + \sqrt n) = O(n^2 \log n)&lt;/math&gt;
+
#<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math>
#&lt;math&gt;3n^2 + \sqrt n = O(n^2)&lt;/math&gt;
+
#<math>3n^2 + \sqrt n = O(n^2)</math>
#&lt;math&gt;\sqrt n \log n= O(n) &lt;/math&gt;
+
#<math>\sqrt n \log n= O(n) </math>
#&lt;math&gt;\log n=O(n^{-1/2})&lt;/math&gt;
+
#<math>\log n=O(n^{-1/2})</math>
  
 
[[TADM2E 2.21|(Solution 2.21)]]  
 
[[TADM2E 2.21|(Solution 2.21)]]  
  
&lt;br&gt;2-22.  
+
<br>2-22.  
For each of the following pairs of functions &lt;math&gt;f(n)&lt;/math&gt; and &lt;math&gt;g(n)&lt;/math&gt;, state
+
For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state
whether &lt;math&gt;f(n)=O(g(n))&lt;/math&gt;, &lt;math&gt;f(n)=\Omega(g(n))&lt;/math&gt;, &lt;math&gt;f(n)=\Theta(g(n))&lt;/math&gt;, or none
+
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.
 
of the above.
#&lt;math&gt;f(n)=n^2+3n+4&lt;/math&gt;, &lt;math&gt;g(n)=6n+7&lt;/math&gt;
+
#<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math>
#&lt;math&gt;f(n)=n \sqrt n&lt;/math&gt;, &lt;math&gt;g(n)=n^2-n&lt;/math&gt;
+
#<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math>
#&lt;math&gt;f(n)=2^n - n^2&lt;/math&gt;, &lt;math&gt;g(n)=n^4+n^2&lt;/math&gt;
+
#<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math>
  
&lt;br&gt;2-23.  
+
<br>2-23.  
 
For each of these questions, briefly explain your answer.
 
For each of these questions, briefly explain your answer.
&lt;br&gt;
+
<br>
(a) If I prove that an algorithm takes &lt;math&gt;O(n^2)&lt;/math&gt; worst-case time, is it
+
(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it
possible that it takes &lt;math&gt;O(n)&lt;/math&gt; on some inputs?
+
possible that it takes <math>O(n)</math> on some inputs?
&lt;br&gt;
+
<br>
(b) If I prove that an algorithm takes &lt;math&gt;O(n^2)&lt;/math&gt; worst-case time, is it
+
(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it
 
possible that
 
possible that
it takes &lt;math&gt;O(n)&lt;/math&gt; on all inputs?
+
it takes <math>O(n)</math> on all inputs?
&lt;br&gt;
+
<br>
(c) If I prove that an algorithm takes &lt;math&gt;\Theta(n^2)&lt;/math&gt; worst-case time, is it
+
(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it
possible that it takes &lt;math&gt;O(n)&lt;/math&gt; on some inputs?
+
possible that it takes <math>O(n)</math> on some inputs?
&lt;br&gt;
+
<br>
(d) If I prove that an algorithm takes &lt;math&gt;\Theta(n^2)&lt;/math&gt; worst-case time,
+
(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time,
is it possible that it takes &lt;math&gt;O(n)&lt;/math&gt; on all inputs?
+
is it possible that it takes <math>O(n)</math> on all inputs?
&lt;br&gt;
+
<br>
(e) Is the function &lt;math&gt;f(n) = \Theta(n^2)&lt;/math&gt;, where &lt;math&gt;f(n) = 100 n^2&lt;/math&gt; for even &lt;math&gt;n&lt;/math&gt;
+
(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 &lt;math&gt;f(n) = 20 n^2 - n \log_2 n&lt;/math&gt; for odd &lt;math&gt;n&lt;/math&gt;?
+
and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>?
  
 
[[TADM2E 2.23|(Solution 2.23)]]  
 
[[TADM2E 2.23|(Solution 2.23)]]  
  
&lt;br&gt;2-24.  
+
<br>2-24.  
 
For each of the following, answer ''yes'', ''no'', or ''can't tell''.
 
For each of the following, answer ''yes'', ''no'', or ''can't tell''.
 
Explain your reasoning.
 
Explain your reasoning.
&lt;br&gt;
+
<br>
(a) Is &lt;math&gt;3^n = O(2^n)&lt;/math&gt;?
+
(a) Is <math>3^n = O(2^n)</math>?
&lt;br&gt;
+
<br>
(b) Is &lt;math&gt;\log 3^n = O( \log 2^n )&lt;/math&gt;?
+
(b) Is <math>\log 3^n = O( \log 2^n )</math>?
&lt;br&gt;
+
<br>
(c) Is &lt;math&gt;3^n = \Omega(2^n)&lt;/math&gt;?
+
(c) Is <math>3^n = \Omega(2^n)</math>?
&lt;br&gt;
+
<br>
(d) Is &lt;math&gt;\log 3^n = \Omega( \log 2^n )&lt;/math&gt;?
+
(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>?
  
&lt;br&gt;2-25.  
+
<br>2-25.  
For each of the following expressions &lt;math&gt;f(n)&lt;/math&gt;
+
For each of the following expressions <math>f(n)</math>
find a simple &lt;math&gt;g(n)&lt;/math&gt; such that
+
find a simple <math>g(n)</math> such that
&lt;math&gt;f(n)=\Theta(g(n))&lt;/math&gt;.
+
<math>f(n)=\Theta(g(n))</math>.
#&lt;math&gt;f(n)=\sum_{i=1}^n {1\over i}&lt;/math&gt;.
+
#<math>f(n)=\sum_{i=1}^n {1\over i}</math>.
#&lt;math&gt;f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil&lt;/math&gt;.
+
#<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>.
#&lt;math&gt;f(n)=\sum_{i=1}^n \log i&lt;/math&gt;.
+
#<math>f(n)=\sum_{i=1}^n \log i</math>.
#&lt;math&gt;f(n)=\log (n!)&lt;/math&gt;.
+
#<math>f(n)=\log (n!)</math>.
  
 
[[TADM2E 2.25|(Solution 2.25)]]  
 
[[TADM2E 2.25|(Solution 2.25)]]  
  
&lt;br&gt;2-26.  
+
<br>2-26.  
 
Place the following functions into increasing asymptotic order.
 
Place the following functions into increasing asymptotic order.
&lt;math&gt;f_1(n) = n^2\log_2n&lt;/math&gt;,
+
<math>f_1(n) = n^2\log_2n</math>,
&lt;math&gt;f_2(n) = n(\log_2n)^2&lt;/math&gt;,
+
<math>f_2(n) = n(\log_2n)^2</math>,
&lt;math&gt;f_3(n) = \sum_{i=0}^n 2^i&lt;/math&gt;,
+
<math>f_3(n) = \sum_{i=0}^n 2^i</math>,
&lt;math&gt;f_4(n) = \log_2(\sum_{i=0}^n 2^i)&lt;/math&gt;.
+
<math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>.
  
&lt;br&gt;2-27.  
+
<br>2-27.  
 
Place the following functions into increasing asymptotic
 
Place the following functions into increasing asymptotic
 
order.  If two or more of the functions are of the same
 
order.  If two or more of the functions are of the same
 
asymptotic order then indicate this.
 
asymptotic order then indicate this.
&lt;math&gt;f_1(n)=\sum_{i=1}^n  \sqrt i&lt;/math&gt;,
+
<math>f_1(n)=\sum_{i=1}^n  \sqrt i</math>,
&lt;math&gt;f_2(n)=  (\sqrt n)\log n&lt;/math&gt;,
+
<math>f_2(n)=  (\sqrt n)\log n</math>,
&lt;math&gt;f_3(n)=  n\sqrt {\log n}&lt;/math&gt;,
+
<math>f_3(n)=  n\sqrt {\log n}</math>,
&lt;math&gt;f_4(n) = 12n^{3\over 2} + 4n&lt;/math&gt;,
+
<math>f_4(n) = 12n^{3\over 2} + 4n</math>,
  
 
[[TADM2E 2.27|(Solution 2.27)]]  
 
[[TADM2E 2.27|(Solution 2.27)]]  
  
&lt;br&gt;2-28.  
+
<br>2-28.  
For each of the following expressions &lt;math&gt;f(n)&lt;/math&gt;
+
For each of the following expressions <math>f(n)</math>
find a simple &lt;math&gt;g(n)&lt;/math&gt; such that
+
find a simple <math>g(n)</math> such that
&lt;math&gt;f(n)=\Theta(g(n))&lt;/math&gt;.
+
<math>f(n)=\Theta(g(n))</math>.
 
(You should be able to prove your
 
(You should be able to prove your
 
result by exhibiting the relevant
 
result by exhibiting the relevant
 
parameters, but this is not
 
parameters, but this is not
 
required for the homework.)
 
required for the homework.)
#&lt;math&gt;f(n)=\sum_{i=1}^n 3i^4 + 2i^3 - 19i + 20&lt;/math&gt;.
+
#<math>f(n)=\sum_{i=1}^n 3i^4 + 2i^3 - 19i + 20</math>.
#&lt;math&gt;f(n)=\sum_{i=1}^n 3(4^i) + 2(3^i) -i^{19} + 20&lt;/math&gt;.
+
#<math>f(n)=\sum_{i=1}^n 3(4^i) + 2(3^i) -i^{19} + 20</math>.
#&lt;math&gt;f(n)=\sum_{i=1}^n 5^i + 3^{2i}&lt;/math&gt;.
+
#<math>f(n)=\sum_{i=1}^n 5^i + 3^{2i}</math>.
  
&lt;br&gt;2-29.  
+
<br>2-29.  
 
Which of the following are true?
 
Which of the following are true?
#&lt;math&gt;\sum_{i=1}^n 3^i = \Theta(3^{n-1})&lt;/math&gt;.
+
#<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>.
#&lt;math&gt;\sum_{i=1}^n 3^i = \Theta(3^n)&lt;/math&gt;.
+
#<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>.
#&lt;math&gt;\sum_{i=1}^n 3^i = \Theta(3^{n+1})&lt;/math&gt;.
+
#<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>.
  
 
[[TADM2E 2.29|(Solution 2.29)]]  
 
[[TADM2E 2.29|(Solution 2.29)]]  
  
&lt;br&gt;2-30.  
+
<br>2-30.  
For each of the following functions &lt;math&gt;f&lt;/math&gt; find
+
For each of the following functions <math>f</math> find
a simple function &lt;math&gt;g&lt;/math&gt; such that &lt;math&gt;f(n)=\Theta(g(n))&lt;/math&gt;.
+
a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>.
#&lt;math&gt;f_1(n)= (1000)2^n + 4^n&lt;/math&gt;.
+
#<math>f_1(n)= (1000)2^n + 4^n</math>.
#&lt;math&gt;f_2(n)= n + n\log n + \sqrt n&lt;/math&gt;.
+
#<math>f_2(n)= n + n\log n + \sqrt n</math>.
#&lt;math&gt;f_3(n)= \log (n^{20}) + (\log n)^{10}&lt;/math&gt;.
+
#<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>.
#&lt;math&gt;f_4(n)= (0.99)^n + n^{100}.&lt;/math&gt;
+
#<math>f_4(n)= (0.99)^n + n^{100}.</math>
  
&lt;br&gt;2-31.  
+
<br>2-31.  
For each pair of expressions &lt;math&gt;(A,B)&lt;/math&gt; below,
+
For each pair of expressions <math>(A,B)</math> below,
indicate whether &lt;math&gt;A&lt;/math&gt; is &lt;math&gt;O&lt;/math&gt;, &lt;math&gt;o&lt;/math&gt;, &lt;math&gt;\Omega&lt;/math&gt;, &lt;math&gt;\omega&lt;/math&gt;, or &lt;math&gt;\Theta&lt;/math&gt; of
+
indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of
&lt;math&gt;B&lt;/math&gt;.  Note that zero, one or more of these relations may hold for a
+
<math>B</math>.  Note that zero, one or more of these relations may hold for a
 
given pair; list all correct ones.
 
given pair; list all correct ones.
&lt;br&gt;&lt;math&gt;
+
<br><math>
 
\begin{array}{lcc}
 
\begin{array}{lcc}
         &amp; A                    &amp; B \\
+
         & A                    & B \\
(a)    &amp; n^{100}              &amp; 2^n \\
+
(a)    & n^{100}              & 2^n \\
(b)    &amp; (\lg n)^{12}        &amp; \sqrt{n} \\
+
(b)    & (\lg n)^{12}        & \sqrt{n} \\
(c)    &amp; \sqrt{n}              &amp; n^{\cos (\pi n/8)} \\
+
(c)    & \sqrt{n}              & n^{\cos (\pi n/8)} \\
(d)    &amp; 10^n                  &amp; 100^n \\
+
(d)    & 10^n                  & 100^n \\
(e)    &amp; n^{\lg n}            &amp; (\lg n)^n \\
+
(e)    & n^{\lg n}            & (\lg n)^n \\
(f)    &amp; \lg{(n!)}            &amp; n \lg n
+
(f)    & \lg{(n!)}            & n \lg n
 
\end{array}
 
\end{array}
&lt;/math&gt;
+
</math>
  
 
[[TADM2E 2.31|(Solution 2.31)]]  
 
[[TADM2E 2.31|(Solution 2.31)]]  
Line 345: Line 345:
 
'''Summations'''
 
'''Summations'''
  
&lt;br&gt;2-32.  
+
<br>2-32.  
 
Prove that:
 
Prove that:
&lt;math&gt; 1^2 - 2^2 + 3^2 - 4^2 + \ldots +(-1)^{k-1} k^2 = (-1)^{k-1}k(k+1)/2 &lt;/math&gt;
+
<math> 1^2 - 2^2 + 3^2 - 4^2 + \ldots +(-1)^{k-1} k^2 = (-1)^{k-1}k(k+1)/2 </math>
  
&lt;br&gt;2-33.  
+
<br>2-33.  
Find an expression for the sum of the &lt;math&gt;i&lt;/math&gt;th row of the following triangle,
+
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
 
and prove its correctness. Each entry is the sum of the three
 
entries directly above it.
 
entries directly above it.
 
All non existing entries are considered 0.
 
All non existing entries are considered 0.
&lt;center&gt;
+
<center>
&lt;math&gt;\begin{array}{ccccccccc}
+
<math>\begin{array}{ccccccccc}
&amp;&amp;&amp;&amp;1&amp;&amp;&amp;&amp; \\
+
&&&&1&&&& \\
&amp;&amp;&amp;1&amp;1&amp;1&amp;&amp;&amp;\\
+
&&&1&1&1&&&\\
&amp;&amp;1&amp;2&amp;3&amp;2&amp;1&amp;&amp;\\
+
&&1&2&3&2&1&&\\
&amp;1&amp;3&amp;6&amp;7&amp;6&amp;3&amp;1&amp;\\
+
&1&3&6&7&6&3&1&\\
1&amp;4&amp;10&amp;16&amp;19&amp;16&amp;10&amp;4&amp;1\\
+
1&4&10&16&19&16&10&4&1\\
\end{array}&lt;/math&gt;
+
\end{array}</math>
&lt;/center&gt;
+
</center>
  
 
[[TADM2E 2.33|(Solution 2.33)]]  
 
[[TADM2E 2.33|(Solution 2.33)]]  
  
&lt;br&gt;2-34.  
+
<br>2-34.  
Assume that Christmas has &lt;math&gt;n&lt;/math&gt; days.
+
Assume that Christmas has <math>n</math> days.
 
Exactly how many presents did my ''true love'' send me?
 
Exactly how many presents did my ''true love'' send me?
 
(Do some research if you do not understand this question.)
 
(Do some research if you do not understand this question.)
  
&lt;br&gt;2-35.  
+
<br>2-35.  
 
Consider the following code fragment.
 
Consider the following code fragment.
&lt;tt&gt;
+
<tt>
 
   for i=1 to n do
 
   for i=1 to n do
 
       for j=i to 2*i do
 
       for j=i to 2*i do
 
         output ''foobar''
 
         output ''foobar''
&lt;/tt&gt;
+
</tt>
Let &lt;math&gt;T(n)&lt;/math&gt; denote the number of times `foobar' is printed
+
Let <math>T(n)</math> denote the number of times `foobar' is printed
as a function of &lt;math&gt;n&lt;/math&gt;.
+
as a function of <math>n</math>.
#Express &lt;math&gt;T(n)&lt;/math&gt; as a summation (actually two nested summations).
+
#Express <math>T(n)</math> as a summation (actually two nested summations).
 
#Simplify the summation.  Show your work.
 
#Simplify the summation.  Show your work.
  
 
[[TADM2E 2.35|(Solution 2.35)]]  
 
[[TADM2E 2.35|(Solution 2.35)]]  
  
&lt;br&gt;2-36.  
+
<br>2-36.  
 
Consider the following code fragment.
 
Consider the following code fragment.
&lt;tt&gt;
+
<tt>
 
   for i=1 to n/2 do
 
   for i=1 to n/2 do
 
       for j=i to n-i do
 
       for j=i to n-i do
 
         for k=1 to j do
 
         for k=1 to j do
 
             output ''foobar''
 
             output ''foobar''
&lt;/tt&gt;
+
</tt>
Assume &lt;math&gt;n&lt;/math&gt; is even.
+
Assume <math>n</math> is even.
Let &lt;math&gt;T(n)&lt;/math&gt; denote the number of times `foobar' is printed
+
Let <math>T(n)</math> denote the number of times `foobar' is printed
as a function of &lt;math&gt;n&lt;/math&gt;.
+
as a function of <math>n</math>.
#Express &lt;math&gt;T(n)&lt;/math&gt; as three nested summations.
+
#Express <math>T(n)</math> as three nested summations.
 
#Simplify the summation.  Show your work.
 
#Simplify the summation.  Show your work.
  
 
[[TADM2E 2.36|(Solution 2.36)]]  
 
[[TADM2E 2.36|(Solution 2.36)]]  
  
&lt;br&gt;2-37.  
+
<br>2-37.  
 
When you first learned to multiply numbers, you were
 
When you first learned to multiply numbers, you were
told that &lt;math&gt;x \times y&lt;/math&gt; means adding &lt;math&gt;x&lt;/math&gt; a total of &lt;math&gt;y&lt;/math&gt; times,
+
told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times,
so &lt;math&gt;5 \times 4 = 5+5+5+5 = 20&lt;/math&gt;.
+
so <math>5 \times 4 = 5+5+5+5 = 20</math>.
What is the time complexity of multiplying two &lt;math&gt;n&lt;/math&gt;-digit numbers in base &lt;math&gt;b&lt;/math&gt;
+
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)
 
(people work in base 10, of course, while computers work in base 2)
using the repeated addition method, as a function of &lt;math&gt;n&lt;/math&gt; and &lt;math&gt;b&lt;/math&gt;.
+
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
 
Assume that single-digit by single-digit addition or multiplication takes
&lt;math&gt;O(1)&lt;/math&gt; time.
+
<math>O(1)</math> time.
(Hint: how big can &lt;math&gt;y&lt;/math&gt; be as a function of &lt;math&gt;n&lt;/math&gt; and &lt;math&gt;b&lt;/math&gt;?)
+
(Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?)
 
''multiplication algorithms''
 
''multiplication algorithms''
  
 
[[TADM2E 2.37|(Solution 2.37)]]  
 
[[TADM2E 2.37|(Solution 2.37)]]  
  
&lt;br&gt;2-38.  
+
<br>2-38.  
 
In grade school, you learned to multiply long numbers on a
 
In grade school, you learned to multiply long numbers on a
digit-by-digit basis, so that &lt;math&gt;127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397&lt;/math&gt;.
+
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 &lt;math&gt;n&lt;/math&gt;-digit numbers with
+
Analyze the time complexity of multiplying two <math>n</math>-digit numbers with
this method as a function of &lt;math&gt;n&lt;/math&gt; (assume constant base size).
+
this method as a function of <math>n</math> (assume constant base size).
 
Assume that single-digit by single-digit addition or multiplication takes
 
Assume that single-digit by single-digit addition or multiplication takes
&lt;math&gt;O(1)&lt;/math&gt; time.
+
<math>O(1)</math> time.
  
 
[[TADM2E 2.38|(Solution 2.38)]]  
 
[[TADM2E 2.38|(Solution 2.38)]]  
Line 430: Line 430:
 
'''Logarithms'''
 
'''Logarithms'''
  
&lt;br&gt;2-39.  
+
<br>2-39.  
 
Prove the following identities on logarithms:
 
Prove the following identities on logarithms:
#Prove that &lt;math&gt;\log_a (xy) = \log_a x + \log_a y&lt;/math&gt;
+
#Prove that <math>\log_a (xy) = \log_a x + \log_a y</math>
#Prove that &lt;math&gt;\log_a x^y = y \log_a x&lt;/math&gt;
+
#Prove that <math>\log_a x^y = y \log_a x</math>
#Prove that &lt;math&gt;\log_a x = \frac{\log_b x}{\log_b a}&lt;/math&gt;
+
#Prove that <math>\log_a x = \frac{\log_b x}{\log_b a}</math>
#Prove that &lt;math&gt;x^{\log_b y} = y^{\log_b x}&lt;/math&gt;
+
#Prove that <math>x^{\log_b y} = y^{\log_b x}</math>
  
 
[[TADM2E 2.39|(Solution 2.39)]]  
 
[[TADM2E 2.39|(Solution 2.39)]]  
  
&lt;br&gt;2-40.  
+
<br>2-40.  
Show that &lt;math&gt;\lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1&lt;/math&gt;
+
Show that <math>\lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1</math>
  
&lt;br&gt;2-41.  
+
<br>2-41.  
Prove that that the binary representation of &lt;math&gt;n \geq 1&lt;/math&gt; has
+
Prove that that the binary representation of <math>n \geq 1</math> has
&lt;math&gt;\lfloor \lg_2 n \rfloor&lt;/math&gt; + &lt;math&gt;1&lt;/math&gt; bits.
+
<math>\lfloor \lg_2 n \rfloor</math> + <math>1</math> bits.
  
 
[[TADM2E 2.41|(Solution 2.41)]]  
 
[[TADM2E 2.41|(Solution 2.41)]]  
  
&lt;br&gt;2-42.  
+
<br>2-42.  
 
In one of my research papers
 
In one of my research papers
 
I give a comparison-based
 
I give a comparison-based
sorting algorithm that runs in &lt;math&gt;O( n \log (\sqrt n) )&lt;/math&gt;.
+
sorting algorithm that runs in <math>O( n \log (\sqrt n) )</math>.
 
Given the existence of an
 
Given the existence of an
&lt;math&gt;\Omega(n \log n)&lt;/math&gt; lower bound for sorting, how can this be possible?
+
<math>\Omega(n \log n)</math> lower bound for sorting, how can this be possible?
  
 
[[TADM2E 2.42|(Solution 2.42)]]
 
[[TADM2E 2.42|(Solution 2.42)]]
Line 459: Line 459:
 
'''Interview Problems'''
 
'''Interview Problems'''
  
&lt;br&gt;2-43.  
+
<br>2-43.  
You are given a set &lt;math&gt;S&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; numbers.
+
You are given a set <math>S</math> of <math>n</math> numbers.
You must pick a subset &lt;math&gt;S'&lt;/math&gt; of &lt;math&gt;k&lt;/math&gt; numbers from &lt;math&gt;S&lt;/math&gt;
+
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 &lt;math&gt;S&lt;/math&gt; occurring in &lt;math&gt;S'&lt;/math&gt;
+
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 &lt;math&gt;k/n&lt;/math&gt;).
+
is equal (i.e., each is selected with probability <math>k/n</math>).
 
You may make only one pass over the numbers.
 
You may make only one pass over the numbers.
What if &lt;math&gt;n&lt;/math&gt; is unknown?
+
What if <math>n</math> is unknown?
  
 
[[TADM2E 2.43|(Solution 2.43)]]  
 
[[TADM2E 2.43|(Solution 2.43)]]  
  
&lt;br&gt;2-44.  
+
<br>2-44.  
 
We have 1,000 data items to store on
 
We have 1,000 data items to store on
 
1,000 nodes.
 
1,000 nodes.
Line 477: Line 477:
 
of data entries that get lost when three random nodes fail?
 
of data entries that get lost when three random nodes fail?
  
&lt;br&gt;2-45.  
+
<br>2-45.  
 
Consider the following algorithm to find the minimum element in
 
Consider the following algorithm to find the minimum element in
an array of numbers &lt;math&gt;A[0, \ldots, n]&lt;/math&gt;.
+
an array of numbers <math>A[0, \ldots, n]</math>.
One extra variable &lt;math&gt;tmp&lt;/math&gt; is allocated to hold the current minimum
+
One extra variable <math>tmp</math> is allocated to hold the current minimum
value. Start from A[0]; &quot;tmp&quot; is compared against &lt;math&gt;A[1]&lt;/math&gt;, &lt;math&gt;A[2]&lt;/math&gt;, &lt;math&gt;\ldots&lt;/math&gt;,
+
value. Start from A[0]; "tmp" is compared against <math>A[1]</math>, <math>A[2]</math>, <math>\ldots</math>,
&lt;math&gt;A[N]&lt;/math&gt;
+
<math>A[N]</math>
 
in order.
 
in order.
When &lt;math&gt;A[i]&lt;tmp&lt;/math&gt;, &lt;math&gt;tmp = A[i]&lt;/math&gt;.
+
When <math>A[i]<tmp</math>, <math>tmp = A[i]</math>.
 
What is the expected number of times that the
 
What is the expected number of times that the
assignment operation &lt;math&gt;tmp = A[i]&lt;/math&gt; is performed?
+
assignment operation <math>tmp = A[i]</math> is performed?
  
 
[[TADM2E 2.45|(Solution 2.45)]]  
 
[[TADM2E 2.45|(Solution 2.45)]]  
  
&lt;br&gt;2-46.  
+
<br>2-46.  
 
You have a 100-story building and a couple of marbles. You must
 
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
 
identify the lowest floor for which a marble will break if you drop it
Line 498: Line 498:
 
What if you have only two marbles?
 
What if you have only two marbles?
  
&lt;br&gt;2-47.  
+
<br>2-47.  
 
You are given 10 bags of gold coins. Nine bags contain coins that each
 
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.
 
weigh 10 grams. One bag contains all false coins that weigh one gram less.
Line 506: Line 506:
 
[[TADM2E 2.47|(Solution 2.47)]]  
 
[[TADM2E 2.47|(Solution 2.47)]]  
  
&lt;br&gt;2-48.  
+
<br>2-48.  
 
You have eight balls all of the same size. Seven of them weigh the same,
 
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
 
and one of them weighs slightly more. How can you find the ball that
 
is heavier by using a balance and only two weighings?
 
is heavier by using a balance and only two weighings?
  
&lt;br&gt;2-49.  
+
<br>2-49.  
Suppose we start with &lt;math&gt;n&lt;/math&gt; companies that eventually merge into
+
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?
 
one big company. How many different ways are there for them to merge?
  
 
[[TADM2E 2.49|(Solution 2.49)]]  
 
[[TADM2E 2.49|(Solution 2.49)]]  
  
&lt;br&gt;2-50.  
+
<br>2-50.  
 
A ''Ramanujam number'' can be written two different ways as the
 
A ''Ramanujam number'' can be written two different ways as the
sum of two cubes---i.e., there exist distinct &lt;math&gt;a&lt;/math&gt;, &lt;math&gt;b&lt;/math&gt;, &lt;math&gt;c&lt;/math&gt;, and &lt;math&gt;d&lt;/math&gt;
+
sum of two cubes---i.e., there exist distinct <math>a</math>, <math>b</math>, <math>c</math>, and <math>d</math>
such that &lt;math&gt;a^3 + b^3 = c^3 + d^3&lt;/math&gt;.
+
such that <math>a^3 + b^3 = c^3 + d^3</math>.
Generate all Ramanujam numbers where &lt;math&gt;a,b,c,d&lt;n&lt;/math&gt;.
+
Generate all Ramanujam numbers where <math>a,b,c,d<n</math>.
  
&lt;br&gt;2-51.  
+
<br>2-51.  
 
Six pirates must divide $300 dollars among
 
Six pirates must divide $300 dollars among
 
themselves. The division is to proceed as follows. The senior
 
themselves. The division is to proceed as follows. The senior
Line 537: Line 537:
 
[[TADM2E 2.51|(Solution 2.51)]]  
 
[[TADM2E 2.51|(Solution 2.51)]]  
  
&lt;br&gt;2-52.  
+
<br>2-52.  
 
Reconsider the pirate problem above, where only one indivisible dollar
 
Reconsider the pirate problem above, where only one indivisible dollar
 
is to be divided. Who gets the dollar and how many are killed?
 
is to be divided. Who gets the dollar and how many are killed?
  
 
[[TADM2E 2.52|(Solution 2.52)]]
 
[[TADM2E 2.52|(Solution 2.52)]]

Revision as of 18:22, 11 September 2014

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)