Difference between revisions of "AlgoanalysisTADM2E"
(Recovering wiki) 
m (Protected "AlgoanalysisTADM2E" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))) 
(No difference)

Latest revision as of 18:42, 11 September 2014
Algorithm Analysis
Program Analysis
21.
What value is returned by the following function?
Express your answer as a function of $ n $.
Give the worstcase running time using the Big Oh notation.
function mystery(n) r:=0 for i:=1 to n1 do for j:=i+1 to n do for k:=1 to j do r:=r+1 return(r)
22.
What value is returned by the following function?
Express your answer as a function of $ n $.
Give the worstcase 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)
23.
What value is returned by the following function?
Express your answer as a function of $ n $.
Give the worstcase 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+jk do r:=r+1 return(r)
24.
What value is returned by the following function?
Express your answer as a function of $ n $.
Give the worstcase 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+j1 $ to $ n $ do $ r:=r+1 $ return($ r $)
25.
Suppose the following algorithm is used to evaluate the polynomial
$ p(x)=a_n x^n +a_{n1} x^{n1}+ \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 worstcase? How many additions?
 How many multiplications are done on the average?
 Can you improve this algorithm?
26.
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
27.
True or False?
 Is $ 2^{n+1} = O (2^n) $?
 Is $ 2^{2n} = O(2^n) $?
28.
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 $
29.
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 $
210.
Prove that $ n^3  3n^2n+1 = \Theta(n^3) $.
211.
Prove that $ n^2 = O(2^n) $.
212.
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^2n+1 $, $ g(n)=n^2/2 $
213.
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)) $.
214.
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)) $.
215.
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)) $
216.
Prove for all $ k \geq 1 $ and all sets of
constants $ \{a_k, a_{k1}, \ldots, a_1,a_0\} \in R $,
$ a_k n^k + a_{k1}n^{k1}+....+a_1 n + a_0 = O(n^k) $
217.
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 > 3a/2 $ since $ n+a > n/2 $, and raise both sides to the $ b $.
Note the need for absolute values.
218.
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 \\ nn^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array} $
219.
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} $
220.
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)) $
221.
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}) $
222.
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^2n $
 $ f(n)=2^n  n^2 $, $ g(n)=n^4+n^2 $
223.
For each of these questions, briefly explain your answer.
(a) If I prove that an algorithm takes $ O(n^2) $ worstcase time, is it
possible that it takes $ O(n) $ on some inputs?
(b) If I prove that an algorithm takes $ O(n^2) $ worstcase time, is it
possible that
it takes $ O(n) $ on all inputs?
(c) If I prove that an algorithm takes $ \Theta(n^2) $ worstcase time, is it
possible that it takes $ O(n) $ on some inputs?
(d) If I prove that an algorithm takes $ \Theta(n^2) $ worstcase 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 $?
224.
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 ) $?
225.
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!) $.
226.
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) $.
227.
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 $,
228.
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} $.
229.
Which of the following are true?
 $ \sum_{i=1}^n 3^i = \Theta(3^{n1}) $.
 $ \sum_{i=1}^n 3^i = \Theta(3^n) $.
 $ \sum_{i=1}^n 3^i = \Theta(3^{n+1}) $.
230.
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}. $
231.
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
232.
Prove that:
$ 1^2  2^2 + 3^2  4^2 + \ldots +(1)^{k1} k^2 = (1)^{k1}k(k+1)/2 $
233.
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} $
234.
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.)
235.
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.
236.
Consider the following code fragment.
for i=1 to n/2 do for j=i to ni 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.
237.
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 singledigit by singledigit addition or multiplication takes
$ O(1) $ time.
(Hint: how big can $ y $ be as a function of $ n $ and $ b $?)
multiplication algorithms
238.
In grade school, you learned to multiply long numbers on a
digitbydigit 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 singledigit by singledigit addition or multiplication takes
$ O(1) $ time.
Logarithms
239.
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} $
240.
Show that $ \lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1 $
241.
Prove that that the binary representation of $ n \geq 1 $ has
$ \lfloor \lg_2 n \rfloor $ + $ 1 $ bits.
242.
In one of my research papers
I give a comparisonbased
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
243.
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?
244.
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?
245.
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?
246.
You have a 100story 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?
247.
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.
248.
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?
249.
Suppose we start with $ n $ companies that eventually merge into
one big company. How many different ways are there for them to merge?
250.
A Ramanujam number can be written two different ways as the
sum of two cubesi.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 $.
251.
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
seniormost 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.
252.
Reconsider the pirate problem above, where only one indivisible dollar
is to be divided. Who gets the dollar and how many are killed?