CSSE 230
Data Structures and Algorithm Analysis

Homework 2 (62 points)

To Be Turned In

These are to be turned in to the HW2 Drop Box. See submission instructions in HW1.   After you have submitted, click on the drop box again to verify that your submission was successful.

A single late day may be used or earned for a homework: you must turn in all parts early to earn a late day; if you turn in any part late, you will use a late day.

The following formulas for series sums may be helpful:

Problem numbers [in square brackets] are the numbers from the 3rd edition of the Weiss book.

  1. (10 points). Use the formal definition of big-Oh (see Weiss section 5.4) to show that N2 + 3N + 8 is O(N2). That is, find appropriate constants N0 and c, and show that they work.
  2. (20 points) Weiss Problem 5.3[5.3], parts a–d. For each of these statements: if the statement is always true, explain why using the N0 and c idea from the formal definition of big-Oh. [Hint: the given bounds on T1 and T2 tell you something about the existence of an N0 and c for each of those functions: we could call these numbers N01, c1, N02, and c2] If the statement can be false, give an example of functions T1 T2, and F for which it is false. (Hints: For algorithm analysis we assume that all the functions have non-negative values; an algorithm can’t execute in negative time.  Note that the use of big-O instead of big-theta here is intentional; some of the answers would be different if big-theta was used instead.
  3. (12 points) For each of the following four code fragments, similar to Weiss problem 5.20 [5.15], first give the exact number of times the "sum++" statement executes in terms of n. Then use that to give a Big-Theta running time (by ignoring the low-order terms).
    1. for (int i=0; i <= n+2; i++)
      for (int j=0; j < n * n; j++)
      sum++;
    2. for (int i=0; i < n; i++)
      for (int j=i; j < n; j++)
      sum++;
    3. for (int i=0; i < n; i++)
      for (int j=0; j < i * n; j++)
      for (int k=0; k < n; k++)
      sum++;
    4. for (int i=n; i >= 1; i = i / 2)
      sum++;
  4. (10 points) Weiss Problem 5.21 [5.16]. You only need to do the analysis, not an implementation. This one is more complex than any of the parts of problem 5.20 [5.15]. As with our analysis of the cubic algorithm for MCSS in class, you should first derive a (closed-form, no-summation notation) formula for the exact number of times that the sum++ statement executes. Be sure to explain what you are doing, not just write equations. Use your derived formula to state a big-Theta estimate; you can use the usual shortcut of dropping lower-order terms and constant factors only at this last stage.

    You do not have to implement and run the code.

  5. (10 points) Weiss Exercise 5.30 [5.23]. Note that your method is supposed to efficiently search the sorted (increasing) array a of integers, looking for an integer i for which a[i] == i and return true if and only if there is such an i.

    You may assume that the array has no duplicate entries, and therefore is strictly increasing. I.e. a[j-1] < a[j] for all j with 0 < j < a.length. You may not assume that all of the integers in the array are non-negative, because then the problem would be boring.

    Show your algorithm and the results of your analysis. Obviously you should try to come up with an efficient algorithm; some of the credit for this problem will be based on your algorithm's efficiency. (Think about it carefully—is O(N) "efficient" for this task? Anyone can write a loop from 0 to a.length.)

For Your Consideration

These problems are for you to think about and convince yourself that you could do them. It would be good practice to actually do them, but you are not required to turn them in.