Homework 4 Homework 4

22C:44 Algorithms
Due Tuesday, March 12, 2002


  1. Sort the following functions so that they are ordered by growth rate (slowest growing first): 3Ön, 100n2 + logn, 400n - n3 + n5, (logn)2, loglogn, 2n+1, 3n, 2(2n), n3, n logn2, n!, 1000 logn, n + cos(n)
  2. Show carefully, using the definition of Big-Oh, that (n+4)2 = O(n2). To do this, you need to choose particular values for constants c and n0 of the definition of Big-Oh.
  3. You are given programs A, B, C, and D. 80n2 is an upper bound on the worst-case running time of program A. B is Q(n logn) in the worst case, and Q(n) in the best and average cases. The running time of program C is characterized exactly by T(n) = 2n. The worst-case running time of D is W(1).

    NOTE: Make sure you include short explanations with each of your answers below.

  4. Suppose functions f and g are defined as follows
    f(n) = ì
    í
    î
    n3
    if n mod 3 = 0
    n
    otherwise

    g(n) = ì
    í
    î
    n2
    if n mod 2 = 0
    1
    otherwise
    Recall that n mod c = 0 means that n is a multiple of c.

    Give tight upper (O) and lower(W) bounds for T(n) in each of the following cases.


      a. T(n) = f(n) + g(n)
      b. T(n) = f(n) / g(n)
      c. T(n) = f(g(n))

    HINT: It might be helpful to elminate the f's and g's from T(n). That is, rewrite T(n) in terms of n only. For example, if we have T(n) = f(n) + g(n) then we can rewrite T(n) as
    T(n) = ì
    ï
    ï
    í
    ï
    ï
    î
    n3 + 1
    if n mod 3 = 0, but n mod 2 ¹ 0
    n3 + n2
    if n mod 6 = 0
    n2 + n
    if n mod 2 = 0, but n mod 3 ¹ 0
    n + 1
    otherwise

  5. Give a tight bound on the running time of each the following (i.e. find f(n) such that the program is Q(f(n)). Assume n ³ 1.

      a.
                              for (i = 5; i< n*n; i++) {
                                  j = 1;
                                  while (j < n) {
                                      print(i*j + i*i - 3);
                                      A[i,j] = j-i;
                                      j = j + 2;
                                  }
                              }
      
      




      b.
                              for (i = 1; i < n/2; i++) 
                                B[i] = i*i*i;
                              i = 1; 
                              while (i < n) {
                                 for (j = 1; j < n; j++) 
                                     for (k = 1; k < n; k++)  	
                                         A[j,k] := i * j;
                                 i = i * 2;
                              }
                              for (i = 1; i < n; i++) 
                                for (j = 1; j < n; j++)
                                  A[i] += A[j + i] * i + j;
      
      
  6. Determine f(n) such that if
    T(1) = 1
    T(n) = 4 T(n/2) + n
    for n ³ 2
    then T(n) is Q(f(n)). Use the ``recursion tree'' or ``iteration'' method for solving recurrences.



File translated from TEX by TTH, version 3.05.
On 7 Mar 2002, 09:46.