# Algorithm Analysis

### Program Analysis

2.1. What value is returned by the following function? Express your answer as a function of $\displaystyle{ n }$. Give the worst-case running time using the Big Oh notation.
  mystery(n)
r:=0
for i:=1 to n-1 do
for j:=i+1 to n do
for k:=1 to j do
r:=r+1
return(r)


2.2. What value is returned by the following function? Express your answer as a function of $\displaystyle{ n }$. Give the worst-case running time using Big Oh notation.
   pesky(n)
r:=0
for i:=1 to n do
for j:=1 to i do
for k:=j to i+j do
r:=r+1
return(r)


2.3. What value is returned by the following function? Express your answer as a function of $\displaystyle{ n }$. Give the worst-case running time using Big Oh notation.
   prestiferous(n)
r:=0
for i:=1 to n do
for j:=1 to i do
for k:=j to i+j do
for l:=1 to i+j-k do
r:=r+1
return(r)


2.4. What value is returned by the following function? Express your answer as a function of $\displaystyle{ n }$. Give the worst-case running time using Big Oh notation.
  conundrum($\displaystyle{ n }$)
$\displaystyle{ r:=0 }$
for $\displaystyle{ i:=1 }$ to $\displaystyle{ n }$ do
for $\displaystyle{ j:=i+1 }$ to $\displaystyle{ n }$ do
for $\displaystyle{ k:=i+j-1 }$ to $\displaystyle{ n }$ do
$\displaystyle{ r:=r+1 }$
return(r)


2.5. Consider the following algorithm: (the print operation prints a single asterisk; the operation $\displaystyle{ x = 2x }$ doubles the value of the variable $\displaystyle{ x }$).
   for $\displaystyle{ k = 1 }$ to $\displaystyle{ n }$
$\displaystyle{ x = k }$
while ($\displaystyle{ x \lt n }$):
print '*'
$\displaystyle{ x = 2x }$

Let $\displaystyle{ f(n) }$ be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for $\displaystyle{ O(f(n)) }$, and $\displaystyle{ \Theta(f(n)) }$, ideally converging on $\displaystyle{ \Theta(f(n)) }$.

2.6. Suppose the following algorithm is used to evaluate the polynomial
$\displaystyle{ p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0 }$
   $\displaystyle{ p:=a_0; }$
$\displaystyle{ xpower:=1; }$
for $\displaystyle{ i:=1 }$ to $\displaystyle{ n }$ do
$\displaystyle{ xpower:=x*xpower; }$
$\displaystyle{ p:=p+a_i * xpower }$

1. How many multiplications are done in the worst-case? How many additions?
2. How many multiplications are done on the average?
3. Can you improve this algorithm?

2.7. Prove that the following algorithm for computing the maximum value in an array $\displaystyle{ A[1..n] }$ is correct.
  max(A)
$\displaystyle{ m:=A }$
for $\displaystyle{ i:=2 }$ to n do
if $\displaystyle{ A[i] \gt m }$ then $\displaystyle{ m:=A[i] }$
return (m)


### Big Oh

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

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

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

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

2.12. Prove that $\displaystyle{ n^3 - 3n^2-n+1 = \Theta(n^3) }$.

2.13. Prove that $\displaystyle{ n^2 = O(2^n) }$.

2.14. Prove or disprove: $\displaystyle{ \Theta(n^2) = \Theta(n^2+1) }$.

2.15. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one?
(a) $\displaystyle{ n^2 }$ (b) $\displaystyle{ n^3 }$ (c) $\displaystyle{ 100n^2 }$ (d) $\displaystyle{ nlogn }$ (e) $\displaystyle{ 2^n }$

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

(a) If I prove that an algorithm takes $\displaystyle{ O(n^2) }$ worst-case time, is it possible that it takes $\displaystyle{ O(n) }$ on some inputs?
(b) If I prove that an algorithm takes $\displaystyle{ O(n^2) }$ worst-case time, is it possible that it takes $\displaystyle{ O(n) }$ on all inputs?
(c) If I prove that an algorithm takes $\displaystyle{ \Theta(n^2) }$ worst-case time, is it possible that it takes $\displaystyle{ O(n) }$ on some inputs?
(d) If I prove that an algorithm takes $\displaystyle{ \Theta(n^2) }$ worst-case time, is it possible that it takes $\displaystyle{ O(n) }$ on all inputs?
(e) Is the function $\displaystyle{ f(n) = \Theta(n^2) }$, where $\displaystyle{ f(n) = 100 n^2 }$ for even $\displaystyle{ n }$ and $\displaystyle{ f(n) = 20 n^2 - n \log_2 n }$ for odd $\displaystyle{ n }$?

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

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

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

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

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

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

$\displaystyle{ \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} }$

### Summations

2.37. Find an expression for the sum of the $\displaystyle{ 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.

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

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

2.39

2.40. Consider the following code fragment.

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


Let $\displaystyle{ T(n) }$ denote the number of times foobar' is printed as a function of $\displaystyle{ n }$.
1. Express $\displaystyle{ T(n) }$ as a summation (actually two nested summations).
2. Simplify the summation. Show your work.

2.41.Consider the following code fragment.

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


Assume $\displaystyle{ n }$ is even. Let $\displaystyle{ T(n) }$ denote the number of times foobar' is printed as a function of $\displaystyle{ n }$.
1. Express $\displaystyle{ T(n) }$ as three nested summations.
2. Simplify the summation. Show your work.

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

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

### Logartihms

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

2.45. Show that $\displaystyle{ \lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1 }$

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

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

### Interview Problems

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

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

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

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

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

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

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

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

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

Back to Chapter List