**Question 4.1-1:**What does $\textit{Find-Maximum-Subarray}$ return when all elements of $A$ are negative?

The procedure would return the single element of maximum value. This is expected since the maximum subarray must contain at least one element. This can be computed by note that $\textit{Find-Max-Crossing-Subarray}$ will always return the array of solely the midpoint and that $\textit{Find-Maximum-Subarray}$ always finds the maxium of $\{leftsum, rightsum, and crosssum\}$

**Question 4.1-2:**Write pseudocode for the brute-force method of solving the max-subarray problem. Your solution should run in $\theta(n^2)$ time.

`max_i =`

*nil*

max_j =*nil*

max_sum = -∞

for i in 0..len(A):

cur_sum = 0

for j in i..len(A):

cur_sum += A[j]

if cur_sum > max_sum:

max_sum = cur_sum

max_i = i

max_j = j

return (max_i, max_j, max_sum)**Question 4.1-3:**Implement both the brute-force and recursive algorithms for the maximum-subarray problem on your own computer. What problem size $n_0$ gives the crossover point at which the recursive algorithm beats the brute-force algorithm? Then, change the base case of the recursive algorithm to use the brute-force algorithm whenever the problem size is less than $n_0$. Does that change the crossover point?

*This question asks a question that is specific to the implementation, and the computer on which it is run. I will therefore be skipping it in this writeup.*It is worthwhile to note that it is almost guarenteed that changing he implementation to use the brute force method for values less than $n_0$ is very likely to change $n_0$.**Question 4.1-4:**Suppose we change the definition of the maximum-subarray problem to allow the result to be an empty subarray, where the sum of the values of an empty subarray is 0. How would you change any of the algorithms that do not allow empty subarrays to permit an empty subarray to be the result?

For the brute force algorithm it would be rather trivial to add a check, and if the return max_sum is > 0 return the empty array.

For the recursive divide and conquer algorithm is is sufficient to just change the $\textit{Find-Max-Crossing-Subarray}$ in a manner similar to the brute force method. If $\textit{Find-Max-Crossing-Subarray}$ return the correct value, then $\textit{Find-Maximum-Subarray}$ will do the correct thing.

**Question 4.1-5:**Develop a nonrecursive linear-time algorithm for the maximum-subarray problem.

^{[1]}If one knows a previous answer to the max-subarray problem for a given prefix of the array than any new element consists of only two cases: being part of the maximum subarray or not being part of the maximum subarray. It is easier to explain with pseudocode:

`max_start = 0`

max_end = 0

max_sum = A[0]

max_with_j = A[0]

for j in 1..len(A):

# If J is in a maximum-subarray, either j is going to being the maximum on its, or it will will add to the current max

max_with_j = max(A[j], max_with_j + x)

Determine if J is in a maximum-subarray

if max_with_j >= max_sum:

max_sum = max_with_j

max_end = j

#Set the starting value if j is the sole element of a new subarray

if max_with_j == A[j]:

max_start = j

return (max_start, max_end, cur_max)

## Tuesday, June 16, 2015

### Blogging My Way Through CLRS Section 4.1

After another long break of not writing up any CLRS answers here is section 4.1.

Subscribe to:
Post Comments (Atom)

## No comments:

## Post a Comment

Have something you want to say? You think I'm wrong? Found something I said useful?

Leave a comment!