Difference between revisions of "Algo-analysis-TADM2E"
(Recovering wiki) |
(Recovering wiki) |
||
Line 5: | Line 5: | ||
'''Program Analysis''' | '''Program Analysis''' | ||
− | + | <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 | + | 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)]] | ||
− | + | <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 | + | 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)]] | ||
− | + | <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 | + | 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)]] | ||
− | + | <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 | + | 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( | + | ''function'' conundrum(<math>n</math>) |
− | + | <math>r:=0</math> | |
− | ''for'' | + | ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' |
− | ''for'' | + | ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' |
− | ''for'' | + | ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' |
− | + | <math>r:=r+1</math> | |
− | return( | + | return(<math>r</math>) |
− | + | <br>2-5. | |
Suppose the following algorithm is used to evaluate the polynomial | 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 | + | for <math>i:=1</math> to <math>n</math> do |
− | + | <math>xpower:=x*xpower;</math> | |
− | + | <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)]] | ||
− | + | <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 | + | array <math>A[1..n]</math> is correct. |
''function'' max(A) | ''function'' max(A) | ||
− | + | <math>m:=A[1]</math> | |
− | ''for'' | + | ''for'' <math>i:=2</math> ''to'' n ''do'' |
− | ''if'' | + | ''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''' | ||
− | + | <br>2-7. | |
True or False? | True or False? | ||
− | #Is | + | #Is <math>2^{n+1} = O (2^n)</math>? |
− | #Is | + | #Is <math>2^{2n} = O(2^n)</math>? |
[[TADM2E 2.7|(Solution 2.7)]] | [[TADM2E 2.7|(Solution 2.7)]] | ||
− | + | <br>2-8. | |
− | For each of the following pairs of functions, either | + | 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. | Determine which relationship is correct and briefly explain why. | ||
− | # | + | #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> |
− | # | + | #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> |
− | # | + | #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> |
− | # | + | #<math>f(n)=n</math>; <math>g(n)=\log^2 n</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> |
[[TADM2E 2.8|(Solution 2.8)]] | [[TADM2E 2.8|(Solution 2.8)]] | ||
− | + | <br>2-9. | |
− | For each of the following pairs of functions | + | For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine |
whether | 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> |
[[TADM2E 2.9|(Solution 2.9)]] | [[TADM2E 2.9|(Solution 2.9)]] | ||
− | + | <br>2-10. | |
− | Prove that | + | Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. |
− | + | <br>2-11. | |
− | Prove that | + | Prove that <math>n^2 = O(2^n)</math>. |
[[TADM2E 2.11|(Solution 2.11)]] | [[TADM2E 2.11|(Solution 2.11)]] | ||
− | + | <br>2-12. | |
− | For each of the following pairs of functions | + | For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give |
− | an appropriate positive constant | + | an appropriate positive constant <math>c</math> |
− | such that | + | 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> |
− | + | <br>2-13. | |
− | Prove that if | + | 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>. | |
[[TADM2E 2.13|(Solution 2.13)]] | [[TADM2E 2.13|(Solution 2.13)]] | ||
− | + | <br>2-14. | |
− | Prove that if | + | 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>. | |
− | + | <br>2-15. | |
− | Prove that if | + | 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> | |
[[TADM2E 2.15|(Solution 2.15)]] | [[TADM2E 2.15|(Solution 2.15)]] | ||
− | + | <br>2-16. | |
− | Prove for all | + | Prove for all <math>k \geq 1</math> and all sets of |
− | constants | + | 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> | |
− | + | <br>2-17. | |
Show that for any real constants | Show that for any real constants | ||
− | + | <math>a</math> and <math>b</math>, <math>b > 0</math> | |
''Big Oh notation'' | ''Big Oh notation'' | ||
− | + | <math> (n+a)^b = \Theta(n^b) </math> | |
− | To show | + | To show <math>f(n) = \Theta(g(n))</math>, we |
− | must show | + | must show <math>O</math> and <math>\Omega</math>. |
''Go back to the definition!'' | ''Go back to the definition!'' | ||
− | #''Big | + | #''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 | + | #''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)]] | ||
− | + | <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. | ||
− | + | <center> | |
− | + | <math>\begin{array}{llll} | |
− | n & | + | n & 2^n & n \lg n & \ln n \\ |
− | n-n^3+7n^5 & | + | n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ |
− | n^2+\lg n & | + | n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ |
− | n^3 & | + | n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 |
\\ | \\ | ||
− | \end{array} | + | \end{array}</math> |
− | + | </center> | |
− | + | <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. | ||
− | + | <center> | |
− | + | <math>\begin{array}{lll} | |
− | \sqrt{n} & | + | \sqrt{n} & n & 2^n \\ |
− | n \log n & | + | n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ |
− | n^2 & | + | n^2 & n^3 & \log n \\ |
− | n^{\frac{1}{3}} + \log n & | + | n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ |
− | \ln n & | + | \ln n & \frac{n}{\log n} & \log \log n \\ |
− | ({1}/{3})^n & | + | ({1}/{3})^n & ({3}/{2})^n & 6 \\ |
− | \end{array} | + | \end{array}</math> |
− | + | </center> | |
[[TADM2E 2.19|(Solution 2.19)]] | [[TADM2E 2.19|(Solution 2.19)]] | ||
− | + | <br>2-20. | |
− | Find two functions | + | Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following |
− | relationship. If no such | + | 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> |
− | + | <br>2-21. | |
True or False? | 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> |
[[TADM2E 2.21|(Solution 2.21)]] | [[TADM2E 2.21|(Solution 2.21)]] | ||
− | + | <br>2-22. | |
− | For each of the following pairs of functions | + | For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state |
− | whether | + | 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. | ||
− | # | + | #<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> |
− | + | <br>2-23. | |
For each of these questions, briefly explain your answer. | For each of these questions, briefly explain your answer. | ||
− | + | <br> | |
− | (a) If I prove that an algorithm takes | + | (a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it |
− | possible that it takes | + | possible that it takes <math>O(n)</math> on some inputs? |
− | + | <br> | |
− | (b) If I prove that an algorithm takes | + | (b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it |
possible that | possible that | ||
− | it takes | + | it takes <math>O(n)</math> on all inputs? |
− | + | <br> | |
− | (c) If I prove that an algorithm takes | + | (c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it |
− | possible that it takes | + | possible that it takes <math>O(n)</math> on some inputs? |
− | + | <br> | |
− | (d) If I prove that an algorithm takes | + | (d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, |
− | is it possible that it takes | + | is it possible that it takes <math>O(n)</math> on all inputs? |
− | + | <br> | |
− | (e) Is the function | + | (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 | + | 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)]] | ||
− | + | <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. | ||
− | + | <br> | |
− | (a) Is | + | (a) Is <math>3^n = O(2^n)</math>? |
− | + | <br> | |
− | (b) Is | + | (b) Is <math>\log 3^n = O( \log 2^n )</math>? |
− | + | <br> | |
− | (c) Is | + | (c) Is <math>3^n = \Omega(2^n)</math>? |
− | + | <br> | |
− | (d) Is | + | (d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? |
− | + | <br>2-25. | |
− | For each of the following expressions | + | For each of the following expressions <math>f(n)</math> |
− | find a simple | + | 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>. |
[[TADM2E 2.25|(Solution 2.25)]] | [[TADM2E 2.25|(Solution 2.25)]] | ||
− | + | <br>2-26. | |
Place the following functions into increasing asymptotic order. | 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>. | |
− | + | <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. | ||
− | + | <math>f_1(n)=\sum_{i=1}^n \sqrt i</math>, | |
− | + | <math>f_2(n)= (\sqrt n)\log n</math>, | |
− | + | <math>f_3(n)= n\sqrt {\log n}</math>, | |
− | + | <math>f_4(n) = 12n^{3\over 2} + 4n</math>, | |
[[TADM2E 2.27|(Solution 2.27)]] | [[TADM2E 2.27|(Solution 2.27)]] | ||
− | + | <br>2-28. | |
− | For each of the following expressions | + | For each of the following expressions <math>f(n)</math> |
− | find a simple | + | find a simple <math>g(n)</math> such that |
− | + | <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.) | ||
− | # | + | #<math>f(n)=\sum_{i=1}^n 3i^4 + 2i^3 - 19i + 20</math>. |
− | # | + | #<math>f(n)=\sum_{i=1}^n 3(4^i) + 2(3^i) -i^{19} + 20</math>. |
− | # | + | #<math>f(n)=\sum_{i=1}^n 5^i + 3^{2i}</math>. |
− | + | <br>2-29. | |
Which of the following are true? | 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>. |
[[TADM2E 2.29|(Solution 2.29)]] | [[TADM2E 2.29|(Solution 2.29)]] | ||
− | + | <br>2-30. | |
− | For each of the following functions | + | For each of the following functions <math>f</math> find |
− | a simple function | + | 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> |
− | + | <br>2-31. | |
− | For each pair of expressions | + | For each pair of expressions <math>(A,B)</math> below, |
− | indicate whether | + | 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. | given pair; list all correct ones. | ||
− | + | <br><math> | |
\begin{array}{lcc} | \begin{array}{lcc} | ||
− | & | + | & A & B \\ |
− | (a) & | + | (a) & n^{100} & 2^n \\ |
− | (b) & | + | (b) & (\lg n)^{12} & \sqrt{n} \\ |
− | (c) & | + | (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ |
− | (d) & | + | (d) & 10^n & 100^n \\ |
− | (e) & | + | (e) & n^{\lg n} & (\lg n)^n \\ |
− | (f) & | + | (f) & \lg{(n!)} & n \lg n |
\end{array} | \end{array} | ||
− | + | </math> | |
[[TADM2E 2.31|(Solution 2.31)]] | [[TADM2E 2.31|(Solution 2.31)]] | ||
Line 345: | Line 345: | ||
'''Summations''' | '''Summations''' | ||
− | + | <br>2-32. | |
Prove that: | Prove that: | ||
− | + | <math> 1^2 - 2^2 + 3^2 - 4^2 + \ldots +(-1)^{k-1} k^2 = (-1)^{k-1}k(k+1)/2 </math> | |
− | + | <br>2-33. | |
− | Find an expression for the sum of the | + | 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. | ||
− | + | <center> | |
− | + | <math>\begin{array}{ccccccccc} | |
− | & | + | &&&&1&&&& \\ |
− | & | + | &&&1&1&1&&&\\ |
− | & | + | &&1&2&3&2&1&&\\ |
− | & | + | &1&3&6&7&6&3&1&\\ |
− | 1& | + | 1&4&10&16&19&16&10&4&1\\ |
− | \end{array} | + | \end{array}</math> |
− | + | </center> | |
[[TADM2E 2.33|(Solution 2.33)]] | [[TADM2E 2.33|(Solution 2.33)]] | ||
− | + | <br>2-34. | |
− | Assume that Christmas has | + | 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.) | ||
− | + | <br>2-35. | |
Consider the following code fragment. | Consider the following code fragment. | ||
− | + | <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'' | ||
− | + | </tt> | |
− | Let | + | Let <math>T(n)</math> denote the number of times `foobar' is printed |
− | as a function of | + | as a function of <math>n</math>. |
− | #Express | + | #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)]] | ||
− | + | <br>2-36. | |
Consider the following code fragment. | Consider the following code fragment. | ||
− | + | <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'' | ||
− | + | </tt> | |
− | Assume | + | Assume <math>n</math> is even. |
− | Let | + | Let <math>T(n)</math> denote the number of times `foobar' is printed |
− | as a function of | + | as a function of <math>n</math>. |
− | #Express | + | #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)]] | ||
− | + | <br>2-37. | |
When you first learned to multiply numbers, you were | When you first learned to multiply numbers, you were | ||
− | told that | + | told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, |
− | so | + | so <math>5 \times 4 = 5+5+5+5 = 20</math>. |
− | What is the time complexity of multiplying two | + | 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 | + | 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 | ||
− | + | <math>O(1)</math> time. | |
− | (Hint: how big can | + | (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)]] | ||
− | + | <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 | + | 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 | + | Analyze the time complexity of multiplying two <math>n</math>-digit numbers with |
− | this method as a function of | + | 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 | ||
− | + | <math>O(1)</math> time. | |
[[TADM2E 2.38|(Solution 2.38)]] | [[TADM2E 2.38|(Solution 2.38)]] | ||
Line 430: | Line 430: | ||
'''Logarithms''' | '''Logarithms''' | ||
− | + | <br>2-39. | |
Prove the following identities on logarithms: | Prove the following identities on logarithms: | ||
− | #Prove that | + | #Prove that <math>\log_a (xy) = \log_a x + \log_a y</math> |
− | #Prove that | + | #Prove that <math>\log_a x^y = y \log_a x</math> |
− | #Prove that | + | #Prove that <math>\log_a x = \frac{\log_b x}{\log_b a}</math> |
− | #Prove that | + | #Prove that <math>x^{\log_b y} = y^{\log_b x}</math> |
[[TADM2E 2.39|(Solution 2.39)]] | [[TADM2E 2.39|(Solution 2.39)]] | ||
− | + | <br>2-40. | |
− | Show that | + | Show that <math>\lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1</math> |
− | + | <br>2-41. | |
− | Prove that that the binary representation of | + | Prove that that the binary representation of <math>n \geq 1</math> has |
− | + | <math>\lfloor \lg_2 n \rfloor</math> + <math>1</math> bits. | |
[[TADM2E 2.41|(Solution 2.41)]] | [[TADM2E 2.41|(Solution 2.41)]] | ||
− | + | <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 | + | sorting algorithm that runs in <math>O( n \log (\sqrt n) )</math>. |
Given the existence of an | Given the existence of an | ||
− | + | <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''' | ||
− | + | <br>2-43. | |
− | You are given a set | + | You are given a set <math>S</math> of <math>n</math> numbers. |
− | You must pick a subset | + | 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 | + | 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 | + | 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 | + | What if <math>n</math> is unknown? |
[[TADM2E 2.43|(Solution 2.43)]] | [[TADM2E 2.43|(Solution 2.43)]] | ||
− | + | <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? | ||
− | + | <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 | + | an array of numbers <math>A[0, \ldots, n]</math>. |
− | One extra variable | + | One extra variable <math>tmp</math> is allocated to hold the current minimum |
− | value. Start from A[0]; | + | 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. | in order. | ||
− | When | + | 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 | + | assignment operation <math>tmp = A[i]</math> is performed? |
[[TADM2E 2.45|(Solution 2.45)]] | [[TADM2E 2.45|(Solution 2.45)]] | ||
− | + | <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? | ||
− | + | <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)]] | ||
− | + | <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? | ||
− | + | <br>2-49. | |
− | Suppose we start with | + | 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)]] | ||
− | + | <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 | + | sum of two cubes---i.e., there exist distinct <math>a</math>, <math>b</math>, <math>c</math>, and <math>d</math> |
− | such that | + | such that <math>a^3 + b^3 = c^3 + d^3</math>. |
− | Generate all Ramanujam numbers where | + | Generate all Ramanujam numbers where <math>a,b,c,d<n</math>. |
− | + | <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)]] | ||
− | + | <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)
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)
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)
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
- 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?
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)
Big Oh
2-7.
True or False?
- Is $ 2^{n+1} = O (2^n) $?
- Is $ 2^{2n} = O(2^n) $?
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.
- $ f(n)=\log n^2 $; $ g(n)=\log n $ + $ 5 $
- $ f(n)=\sqrt n $; $ g(n)=\log n^2 $
- $ f(n)=\log^2 n $; $ g(n)=\log n $
- $ f(n)=n $; $ g(n)=\log^2 n $
- $ f(n)=n \log n + n $; $ g(n)=\log n $
- $ f(n)=10 $; $ g(n)=\log 10 $
- $ f(n)=2^n $; $ g(n)=10 n^2 $
- $ f(n)=2^n $; $ g(n)=3^n $
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.
- $ f(n) = (n^2 - n)/2 $, $ g(n) =6n $
- $ f(n) = n +2 \sqrt n $, $ g(n) = n^2 $
- $ f(n) = n \log n $, $ g(n) = n \sqrt n /2 $
- $ f(n) = n + \log n $, $ g(n) = \sqrt n $
- $ f(n) = 2(\log n)^2 $, $ g(n) = \log n + 1 $
- $ f(n) = 4n\log n + n $, $ g(n) = (n^2 - n)/2 $
2-10.
Prove that $ n^3 - 3n^2-n+1 = \Theta(n^3) $.
2-11.
Prove that $ n^2 = O(2^n) $.
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 $.
- $ f(n)=n^2+n+1 $, $ g(n)=2n^3 $
- $ f(n)=n \sqrt n + n^2 $, $ g(n)=n^2 $
- $ 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)) $.
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)) $
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!
- 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 $.
- 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.
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} $
2-20.
Find two functions $ f(n) $ and $ g(n) $ that satisfy the following
relationship. If no such $ f $ and $ g $ exist, write None.
- $ f(n)=o(g(n)) $ and $ f(n) \neq \Theta(g(n)) $
- $ f(n)=\Theta(g(n)) $ and $ f(n)=o(g(n)) $
- $ f(n)=\Theta(g(n)) $ and $ f(n) \neq O(g(n)) $
- $ f(n)=\Omega(g(n)) $ and $ f(n) \neq O(g(n)) $
2-21.
True or False?
- $ 2n^2+1=O(n^2) $
- $ \sqrt n= O(\log n) $
- $ \log n = O(\sqrt n) $
- $ n^2(1 + \sqrt n) = O(n^2 \log n) $
- $ 3n^2 + \sqrt n = O(n^2) $
- $ \sqrt n \log n= O(n) $
- $ \log n=O(n^{-1/2}) $
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.
- $ f(n)=n^2+3n+4 $, $ g(n)=6n+7 $
- $ f(n)=n \sqrt n $, $ g(n)=n^2-n $
- $ 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 $?
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)) $.
- $ f(n)=\sum_{i=1}^n {1\over i} $.
- $ f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil $.
- $ f(n)=\sum_{i=1}^n \log i $.
- $ f(n)=\log (n!) $.
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 $,
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.)
- $ f(n)=\sum_{i=1}^n 3i^4 + 2i^3 - 19i + 20 $.
- $ f(n)=\sum_{i=1}^n 3(4^i) + 2(3^i) -i^{19} + 20 $.
- $ f(n)=\sum_{i=1}^n 5^i + 3^{2i} $.
2-29.
Which of the following are true?
- $ \sum_{i=1}^n 3^i = \Theta(3^{n-1}) $.
- $ \sum_{i=1}^n 3^i = \Theta(3^n) $.
- $ \sum_{i=1}^n 3^i = \Theta(3^{n+1}) $.
2-30.
For each of the following functions $ f $ find
a simple function $ g $ such that $ f(n)=\Theta(g(n)) $.
- $ f_1(n)= (1000)2^n + 4^n $.
- $ f_2(n)= n + n\log n + \sqrt n $.
- $ f_3(n)= \log (n^{20}) + (\log n)^{10} $.
- $ 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} $
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} $
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 $.
- Express $ T(n) $ as a summation (actually two nested summations).
- Simplify the summation. Show your work.
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 $.
- Express $ T(n) $ as three nested summations.
- Simplify the summation. Show your work.
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
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.
Logarithms
2-39.
Prove the following identities on logarithms:
- Prove that $ \log_a (xy) = \log_a x + \log_a y $
- Prove that $ \log_a x^y = y \log_a x $
- Prove that $ \log_a x = \frac{\log_b x}{\log_b a} $
- Prove that $ x^{\log_b y} = y^{\log_b x} $
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.
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?
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?
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?
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.
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?
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.
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?