# 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[1] }$
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)) }$?

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

2.18

2.19

2.20

2.21

2.22

2.23

2.24

2.25

2.26

2.27

2.28

2.29

2.30

2.31

2.32

2.33

2.34

2.35

2.36

2.37
2.38
2.39
2.40
2.41
2.42
2.43

2.44
2.45
2.46
2.47

### Interview Problems

2.48
2.49
2.50
2.51
2.52
2.53
2.54
2.55

Back to Chapter List