Prelab 2

It's all about Timing. And Lobsters.
Due by 10am, Monday 17 Sep 2012

In this prelab, you will get some practice with running time analysis and asymptotic notation. Please write or type up your solutions, and hand in a paper copy before 10am on Monday. Remember, no late prelabs allowed!


On the east coast, a little ways up north, there is a lobster-fishing community trying to maximize their lobster harvest from a straight-line waterway. As we all know, lobster sticks to magnet. Consequently, the town has fashioned a lobster harvester out of an airplane, some rope, and a very large magnet. If the magnet-plane flies low enough to the waterway, the lobsters in that segment of the waterway will stick to the magnet and be harvested. The problem is that not all segments of waterway are made equal, and not all things that stick to magnets are lobsters (for example, tin cans and robot lobsters); that is, some segments have an overall negative effect on the harvest, and some have a positive effect. Moreover, the plane cannot go up and down willy-nilly, harvesting only the positive segments. Rather, the plane can only descend and ascend exactly once, and harvest precisely those segments of the waterway lying between the points of descent and ascent. You have been commisioned by said lobster-fishing community to determine where their plane should ascend and descend so as to maximize the addititive quality of their harvest.

Lobster diagram courtesy of Mr. Tom Wexler.

More concretely (if you insist), you are given a list of n integer values a1, a2, ..., an (positive or negative) representing the quality of the harvest at each of the n segments of the waterway (from west to east, say). Your task, should you choose to accept it, is to find the maximum additive quality in any contiguous sublist of the n segments. That is, you want to find the maximum over all pairs of integers i and j where 1 ≤ i ≤ j ≤ n, of the sum ai + ai+1 + ... + aj.

For example, consider the sequence
2, 3, 4.
The contiguous subsequence with the largest sum is the entire sequence, with a sum of 9.


1, -2, 3, -4, 5, -6, 7,
the contiguous subsequence with the largest sum is just the last element, so the maximum sum is 7.


2, -5, 3, -1, 6, -6, 4, 5, 8, -4, -2, 4, -10, 12,
the contiguous subsequence with the largest sum is { 3, -1, 6, -6, 4, 5, 8 } with a sum of 19.

Note: We will allow a subsequence to have length 0. By definition, a subsequence of length 0 (an empty subsequence) has a sum of 0. The empty subsequence is a subsequence of every integer sequence, so the maximum contiguous subsequence sum of any integer sequence can never be less than 0, even for a sequence of all negative integers.

Okay, yes, this whole lobster rigamarole is just the "maximum contiguous subsequence sum" problem, described at great length in Chapter 5 of Weiss. But everything is better with lobsters involved!

  1. Write an algorithm, in pseudocode, to solve the maximum contiguous subsequence problem. Try to write it without looking at the textbook (if you've already read the chapter then just don't look at it while writing your own algorithm). What is the running time of your algorithm, using asymptotic analysis? (Pseudocode should be a mix of English and "code". Specific Java constructs like "public", semicolons, or squirrely braces have no place in pseudocode, so please leave that stuff out.)

  2. Write a second algorithm, in pseudocode, to solve the same problem. This algorithm should be sufficiently different from the previous to have a different running time. What is this new running time? Is it better or worse than the previous?


  1. (Weiss) Suppose T1(N) = O(F(N)) and T2(N) = O(F(N)). Which of the following are true?
    1. T1(N) + T2(N) = O(F(N))
    2. T1(N) - T2(N) = O(F(N))
    3. T1(N)/T2(N) = O(1)
    4. T1(N) = O(T2(N))

  2. (Weiss) Order the following functions by growth rate: N, N1/2, N1.5, N2, N log N, N log log N, N log2N, N log (N2), 2/N, 2N, 2N/2, 37, N3, and N2log N. Indicate which functions grow at the same rate.

  3. (Weiss) Occasionally, multiplying the sizes of nested loops can give an overestimate for the Big-Oh running time. This result happens when an innermost loop is infrequently executed. What is the Big-Oh running time of the following program fragment:

    for( int i=1; i <= n; i++ ) 
      for( int j=1; j <= i*i; j++ )
        if( j % i == 0 )
          for( int k=0; k < j; k++ )
  4. (Weiss) Give an efficient algorithm to determine whether an integer i exists such that Ai = i in an array of increasing integers. What is the running time of your algorithm?

  5. BONUS (Weiss) Suppose your input is an N x N matrix of numbers that is already in memory. Each individual row is increasing from left to right. Each individual column is increasing from top to bottom. Give an O(N) worst-case algorithm that decides if a number X is in the matrix.

    (Note: this is a little tricky, so try your best to come up with a good solution! If you can't come up with one that works, give me one that doesn't, analyze its running time, and give me an input on which it breaks, as well as some other ideas you may have had.)


If you adhered to the honor code in this assignment, add the following statement at the top of your prelab.

I have adhered to the Honor Code in this assignment.

Write or type your solutions and hand in a paper copy by the deadline listed at the top of this prelab. Late prelabs will not be accepted.