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 `a _{1}, a_{2}, ..., a_{n}` (positive or negative) representing the quality of the harvest at each of the

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

For

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.

For

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!

- 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.)
- 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?

- (Weiss) Suppose
`T`and_{1}(N) = O(F(N))`T`. Which of the following are true?_{2}(N) = O(F(N))`T`_{1}(N) + T_{2}(N) = O(F(N))`T`_{1}(N) - T_{2}(N) = O(F(N))`T`_{1}(N)/T_{2}(N) = O(1)`T`_{1}(N) = O(T_{2}(N))

- (Weiss) Order the following functions by growth rate:
`N`,`N`,^{1/2}`N`,^{1.5}`N`,^{2}`N log N`,`N log log N`,`N log`,^{2}N`N log (N`,^{2})`2/N`,`2`,^{N}`2`,^{N/2}`37`,`N`, and^{3}`N`. Indicate which functions grow at the same rate.^{2}log N - (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++ ) sum++;

- (Weiss) Give an efficient algorithm to determine whether an integer
`i`exists such that`A`in an array of increasing integers. What is the running time of your algorithm?_{i}= i **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.