Maxsubarray
Maxsubarray
Divide-and-Conquer Algorithms
1 / 10
Divide-and-Conquer algorithms – Overview
2 / 10
Divide-and-Conquer algorithms – Overview
The divide-and-conquer (DC) strategy solves a problem by
1. Breaking the problem into subproblems that are themselves smaller
instances of the same type of problem (”divide”),
2. Recursively solving these subproblems (”conquer”),
3. Appropriately combining their answers (”combine”)
2 / 10
Divide-and-Conquer algorithms – Overview
The divide-and-conquer (DC) strategy solves a problem by
1. Breaking the problem into subproblems that are themselves smaller
instances of the same type of problem (”divide”),
2. Recursively solving these subproblems (”conquer”),
3. Appropriately combining their answers (”combine”)
2 / 10
The maximum-subarray problem
3 / 10
The maximum-subarray problem
Problem statement:
Input: an array A[1...n] of (positive/negative) numbers.
3 / 10
The maximum-subarray problem
Problem statement:
Input: an array A[1...n] of (positive/negative) numbers.
Output:
(1) Indices i and j such that the subarray A[i...j] has the
greatest sum of any nonempty contiguous subarray of A, and
(2) the sum of the values in A[i...j].
3 / 10
The maximum-subarray problem
Problem statement:
Input: an array A[1...n] of (positive/negative) numbers.
Output:
(1) Indices i and j such that the subarray A[i...j] has the
greatest sum of any nonempty contiguous subarray of A, and
(2) the sum of the values in A[i...j].
Note: Maximum subarray might not be unique, though its value is, so we
speak of a maximum subarray, rather than the maximum subarray.
3 / 10
The maximum-subarray problem
Example 1: stock prices and changes
Day 0 1 2 3 4
Price 10 11 7 10 6
Change (= A[...]) 1 -4 3 -4
4 / 10
The maximum-subarray problem
Example 1: stock prices and changes
Day 0 1 2 3 4
Price 10 11 7 10 6
Change (= A[...]) 1 -4 3 -4
maximum-subarray: A[3] (i = j = 3) and Sum = 3
4 / 10
The maximum-subarray problem
Example 1: stock prices and changes
Day 0 1 2 3 4
Price 10 11 7 10 6
Change (= A[...]) 1 -4 3 -4
maximum-subarray: A[3] (i = j = 3) and Sum = 3
Day 0 1 2 3 4 5 6
Price 10 11 7 10 14 12 18
Change (= A[...]) 1 -4 3 4 -2 6
4 / 10
The maximum-subarray problem
Example 1: stock prices and changes
Day 0 1 2 3 4
Price 10 11 7 10 6
Change (= A[...]) 1 -4 3 -4
maximum-subarray: A[3] (i = j = 3) and Sum = 3
Day 0 1 2 3 4 5 6
Price 10 11 7 10 14 12 18
Change (= A[...]) 1 -4 3 4 -2 6
maximum-subarray: A[3...6] (i = 3, j = 6) and Sum = 11.
4 / 10
The maximum-subarray problem
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
100 113 110 85 105 102 86 63 81 101 94 106 101 79 94 90 97
A 13 -3 -25 20 -3 -16 -23 18 20 -7 12 -5 -22 15 -4 7
5 / 10
The maximum-subarray problem
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
100 113 110 85 105 102 86 63 81 101 94 106 101 79 94 90 97
A 13 -3 -25 20 -3 -16 -23 18 20 -7 12 -5 -22 15 -4 7
I maximum-subarray: A[i...j]?
5 / 10
The maximum-subarray problem
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
100 113 110 85 105 102 86 63 81 101 94 106 101 79 94 90 97
A 13 -3 -25 20 -3 -16 -23 18 20 -7 12 -5 -22 15 -4 7
I maximum-subarray: A[i...j]?
I Answer: A[8...11] and sum = 43!
5 / 10
The maximum-subarray problem
Algorithm 1. Solve by brute-force
6 / 10
The maximum-subarray problem
Algorithm 1. Solve by brute-force
I Check all subarrays
6 / 10
The maximum-subarray problem
Algorithm 1. Solve by brute-force
I Check all subarrays
I Total number of subarrays A[i...j]:
n n! 1
= = n(n − 1) = Θ(n2 )
2 2!(n − 2)! 2
6 / 10
The maximum-subarray problem
Algorithm 1. Solve by brute-force
I Check all subarrays
I Total number of subarrays A[i...j]:
n n! 1
= = n(n − 1) = Θ(n2 )
2 2!(n − 2)! 2
6 / 10
The maximum-subarray problem
Algorithm 2. Solve by Divide-and-Conquer
7 / 10
The maximum-subarray problem
Algorithm 2. Solve by Divide-and-Conquer
I Generic problem:
Find a maximum subarray of A[low...high]
with initial call: low = 1 and high = n
7 / 10
The maximum-subarray problem
Algorithm 2. Solve by Divide-and-Conquer
I Generic problem:
Find a maximum subarray of A[low...high]
with initial call: low = 1 and high = n
I DC strategy:
1. Divide A[low...high] into two subarrays of as equal size as possible by
finding the midpoint mid
2. Conquer:
(a) finding maximum subarrays of A[low...mid] and A[mid + 1...high]
(b) finding a max-subarray that crosses the midpoint
3. Combine: returning the max of the three
7 / 10
The maximum-subarray problem
Algorithm 2. Solve by Divide-and-Conquer
I Generic problem:
Find a maximum subarray of A[low...high]
with initial call: low = 1 and high = n
I DC strategy:
1. Divide A[low...high] into two subarrays of as equal size as possible by
finding the midpoint mid
2. Conquer:
(a) finding maximum subarrays of A[low...mid] and A[mid + 1...high]
(b) finding a max-subarray that crosses the midpoint
3. Combine: returning the max of the three
I Correctness: This strategy works because any subarray must either lie
entirely in one side of midpoint or cross the midpoint.
7 / 10
The maximum-subarray problem
MaxSubarray(A,low,high)
if high == low // base case: only one element
return (low, high, A[low])
else
// divide
mid = floor( (low + high)/2 )
// conquer
(leftlow,lefthigh,leftsum) = MaxSubarray(A,low,mid)
(rightlow,righthigh,rightsum) = MaxSubarray(A,mid+1,high)
(xlow,xhigh,xsum) = MaxXingSubarray(A,low,mid,high)
// combine
if leftsum >= rightsum and leftsum >= xsum
return (leftlow,lefthigh,leftsum)
else if rightsum >= leftsum and rightsum >= xsum
return (rightlow,righthigh,rightsum)
else
return (xlow,xhigh,xsum)
end if
end if
8 / 10
The maximum-subarray problem
MaxXingSubarray(A,low,mid,high)
leftsum = -infty; sum = 0 // Find max-subarray of A[i..mid]
for i = mid downto low
sum = sum + A[i]
if sum > leftsum
leftsum = sum
maxleft = i
end if
end for
rightsum = -infty; sum = 0 // Find max-subarray of A[mid+1..j]
for j = mid+1 to high
sum = sum + A[j]
if sum > rightsum
rightsum = sum
maxright = j
end if
end for
// Return the indices i and j and the sum of two subarrays
return (maxleft,maxright,leftsum+rightsum)
9 / 10
The maximum-subarray problem
Remarks:
1. Initial call: MaxSubarray(A,1,n)
2. Base case is when the subarray has only 1 element.
10 / 10
The maximum-subarray problem
Remarks:
1. Initial call: MaxSubarray(A,1,n)
2. Base case is when the subarray has only 1 element.
3. Divide by computing mid.
Conquer by the two recursive calls to MaxSubarray. and a call to
MaxXingSubarray
Combine by determining which of the three results gives the maximum
sum.
10 / 10
The maximum-subarray problem
Remarks:
1. Initial call: MaxSubarray(A,1,n)
2. Base case is when the subarray has only 1 element.
3. Divide by computing mid.
Conquer by the two recursive calls to MaxSubarray. and a call to
MaxXingSubarray
Combine by determining which of the three results gives the maximum
sum.
4. Complexity:
n
T (n) = 2 · T + Θ(n) + Θ(1)
2
= Θ(n lg n)
10 / 10