CMSC 435 Algorithm Analysis & Design

 

Answers to Homework # 3

 

  1. Text # 4

g1(n) = 2√log(n)

g2(n) = 2n

g3(n) = n(log n)3

g4(n) = n4/3

g5(n) = nlog(n)

g6(n) = 22**n

g7(n) = 2n**2

 

Take the log of each function:

log2(g1(n)) = √log(n)

log2(g2(n)) = n

log2(g4(n)) = (4/3) log(n)

log2(g5(n)) = log2n

log2(g6(n)) = 2n

log2(g7(n)) = n2

log2(g3(n)) = log(n) + 3log(log(n))

 

Then we have the order g1, g3,  g4, g5, g2, g7, g6

 

  1. Text # 5

 

Given f(n) = O(g(n))

a)      log2(f(n)) = O(log2(g(n)) ?  true  use def. and take log of both sides except for the one case where f and g are both constant functions. f(n) = 2 and g(n) = 1

b)      2f(n) = O(2g(n))?  false  choose f(n) = 2n and g(n) = n

c)      f(n)2 = O(g(n)2) ?  true, since f(n) <= cg(n) for all n: n>= n0, we have  (f(n)2 <= c2(g(n))2 for all n: n>= n0

 

  1. Text # 6

a)      f(n) = O(n3).  The line of code that adds the entries of the array from A[i] through A[j] executes at most n2 times.  Adding the array entries from A[i] through A[j] is O(j – i + 1) operations, which is always O(n).  Storing the result in B[i, j] requires constant time.  Therefore the runtime for the algorithm is at most n2 O(n) = O(n3).

b)      Consider the times when i <= n/4 and j >= 3n/4.  In these cases j – i + 1 >= 3n/4  - n/4 + 1 > n/2.  Therefore, adding up the array entries would require at least n/2 operations, since there are more than n/2 terms to add.  How many times in the execution of the algorithm do we encounter such cases?  There are (n/4)2 pairs of (i, j) with i <= n/4 and j >= 3n/4 and the algorithm enumerates over all of them.  Therefore the algorithm must perform at least (n/2)(n/4)2 = n3/32 operations and hence is Ω(n3)

c)      Consider the following algorithm

For i = 1, 2,…, n - 1

            Set B[i, i+1] to A[i] + A[i+1]

For k = 2, 3,…, n-1

            For i = 1, 2,…, n – k

                        Set j = i + k

                        Set B[i, j] = B[i, j – 1] + A[j]

 

 

  1. Text # 7

 

Line 1 = <the Partridge family line goes here>

Line 2 = <second line of text>

Line L = <last line of text>

For i = 1, 2, …, L

            <common line goes here>

            For j = 1, 2, …, i

                        Sing lines j through 1

            EndFor

EndFor

 

The nested for-loops have a length bounded by a constant c1.  Each of the lines in the script has length at most c2 (where c2 is the maximum line length c plus the space to write the variable assignment – the common line.)  The total space required is therefore S = c1 + c2L.  The value n denotes the number of words produced when sung and n >= ∑k k = 1 + 2 + … + L = ½(L(L-1)).  Hence n >= ½(L – 1)2   So L <= 1 + √2n.  Plugging this into the bound for the script we get f(n) = S <= c1 + c2√2n = O(√n)

 

  1. Text # 8

 

a)      Suppose for simplicity that n is a perfect square.  We drop the first jar from heights that are multiples of √n until it breaks.  If it survives a drop from the top rung, we are done, else if it breaks at a height of j√n, we start with a second jar at height (j – 1) √n and go up by 1 until it breaks.  In this way we drop each of the two jars at most √n times.  If the number of rungs is not a perfect square, we go up with the first jar in increments of |_√n_|.  We get either way f2(n) = O(√n).

b)      By induction assume fk(n) <= 2kn1/k.  Begin by dropping the first jar by multiples of |_n(k-1)/k_|.  |_n(k-1)/k_| >= n(k-1)/k/2, therefore we drop the first jar at most  2n/ n(k-1)/k = 2n1/k times.  This narrows the set of rungs down to an interval of length at most n(k-1)/k.

We then apply the strategy for k – 1 jars recursively.  By induction, it uses at most 2(k – 1)( n(k-1)/k)1/(k-1) = 2(k-1)n1/k drops.  Adding in the <= 2n1/k drops made using the first jar, we get a bound of 2kn1/k .  With k -1 jars left, the second drop will be in multiples of |_( n(k-1)/k)(k-2)(/k-1)_| and (let X = n(k-1)/k) will be dropped at most 2X1/(k-1) times.  This forms an upper bound on how many times each of the succeeding jars will be dropped  to get the 2(k –1)( n(k-1)/k)(k-1)/k drops.