0% found this document useful (0 votes)
9 views15 pages

5 D&CMaxSumSubarray

Uploaded by

Tashrif Gulzer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views15 pages

5 D&CMaxSumSubarray

Uploaded by

Tashrif Gulzer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Algorithms

Lecture 5
Max Sum Subarray (Divide and Conquer)
Prantik Paul [PNP]
Lecturer
Department of Computer Science and
Engineering
1
BRAC University
Maximum Subarray Problem
• Input: an array A[1..n] of n numbers
– Assume that some of the numbers are
negative, because this problem is trivial when
all numbers are nonnegative
• Output: a nonempty subarray A[i..j] having the
largest sum S[i, j] = ai + ai+1 +... + aj
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
A 13 -3 -25 20 -3 -16 -23 18 20 -7 12 -5 -22 15 -4 7

maximum subarray 2
Maximum Subarray Problem
Target array 1 -4 3 2
: What is a
All the sub 1 1 maximum
arrays:
-4 -4 subarray?
Ans: The subarray
3 3
2
with the largest
2
-3
sum
1 -4
-4 3 -1 What is the
Max! 3 2 5 brute-force
1 -4 3 0 time?
-4 3 2 1
1 -4 3 2 2 3
Brute-Force Algorithm
All possible contiguous subarrays
● A[1..1], A[1..2], A[1..3], ..., A[1..(n-1)], A[1..n]
● A[2..2], A[2..3], ..., A[2..(n-1)], A[2..n]
● ...
● A[(n-1)..(n-1)], A[(n-1)..n]
● A[n..n]

How many of them in total? O(n2)

Algorithm: For each subarray, compute the sum.


Find the subarray that has the maximum sum. 4
Brute-Force Algorithm

Example: 2 -6 -1 3 -1
2 -2
sum from A[1]: 2 -4 -5 -2 -3 -1
-3
sum from A[2]: -6 -7 -4 -5 -3
-5
sum from A[3]: -1 2 1 3
1
sum from A[4]: 3 2 4
5
2
Brute-Force Algorithm

Outer loop: index variable i to indicate start


of subarray, for 1 ≤ i ≤ n, i.e., A[1], A[2], ...,
A[n]
● for i = 1 to n do ...

Inner loop: for each start index i, we need to


go through A[i..i], A[i..(i+1)], ..., A[i..n]
● use an index j for i ≤ j ≤ n, i.e., consider
A[i..j]
● for j = i to n do ... 6
Brute-Force Algorithm
max = -∞ Time
for i = 1 to n do complexity
begin ?
sum = 0 O(n2)
for j = i to n do
begin
sum = sum + A[j]
if sum > max
then max = sum
end
end
7
Divide-and-Conquer Algorithm
Possible locations of a maximum subarray A[i..j] of
A[low..high], where mid = ⎣(low + high)/2⎦
▪ entirely in A[low..mid] (low ≤ i ≤ j ≤ mid)
▪ entirely in A[mid+1..high] (mid < i ≤ j ≤
high)
▪ crossing the midpoint (low ≤ i ≤ mid < j
crossing the midpoint
≤ high)
low mid
high

entirely in entirely in
A[low..mid] A[mid+1..high]
Possible locations of subarrays of A[low..high] 8
Divide-and-Conquer Algorithm
FIND-MAX-CROSSING-SUBARRAY (A, low, mid, high)
left-sum = -∞ // Find a maximum subarray of the form A[i..mid]
sum = 0
for i = mid downto low
sum = sum + A[i ]
if sum > left-sum
left-sum = sum
max-left = i
right-sum = -∞ // Find a maximum subarray of the form A[mid + 1 .. j ]
sum =0
for j = mid +1 to high
sum = sum + A[j]
if sum > right-sum
right-sum = sum
max-right = j
// Return the indices and the sum of the two subarrays
9
return (max-left, max-right, left-sum + right-sum)
Divide-and-Conquer Algorithm

A[mid+1..j]
low i mid
high
mid +1 j
A[i..mi
d]
A[i..j] comprises two subarrays A[i..mid] and
A[mid+1..j]

10
Divide-and-Conquer Algorithm
mid =5
1 2 3 4 5 6 7 8 9 10
13 -3 -25 20 -3 -16 -23 18 20 -7
A
S[5 .. 5] =
-3
S[4 .. 5] =
17 ⇐ (max-left = 4)
S[3 .. 5] = mid-8=5
S[2 .. 5] = 1 2 3 -11
4 5 6 7 8 9 10
S[1 .. 5] = 13 -3 2 -25 20 -3 -16 -23 18 20 -7
A
S[6 .. 6] = -16
S[6 .. 7] = -39
S[6 .. 8] = -21
S[6 .. 9] = (max-right = 9) ⇒ -
1
S[6..10] = - 11
⇒ maximum subarray crossing
8 mid is S[4..9] = 16
Divide-and-Conquer Algorithm
FIND-MAXIMUM-SUBARRAY (A, low, high)
if high == low
return (low, high, A[low]) // base case: only one element
else mid =
(left-low, left-high, left-sum) =
FIND-MAXIMUM-SUBARRAY(A, low, mid)
(right-low, right-high, right-sum) =
FIND-MAXIMUM-SUBARRAY(A, mid + 1, high)
(cross-low, cross-high, cross-sum) =
FIND-MAX-CROSSING-SUBARRAY(A, low, mid, high)
if left-sum ≧ right-sum and left-sum ≧ cross-sum
return (left-low, left-high, left-sum)
elseif right-sum ≧ left-sum and right-sum ≧ cross-sum
return (right-low, right-high, right-sum)
else return (cross-low, cross-high, cross-sum)
Initial call: FIND-MAXIMUM-SUBARRAY (A, 1, n) 12
Divide-and-Conquer Algorithm
Analyzing time complexity
FIND-MAX-CROSSING-SUBARRAY : Θ(n), where n = high
− low + 1

FIND-MAXIMUM-SUBARRAY

T(n) = 2T(n/2) + Θ(n)


= Θ(n lg n) (similar to merge-sort) 13
Practice

-2 1 -3 4 -1 2 1 -5 4
Resources

● CLRS Book:
○ Chapter 4.1 - The Maximum Subarray Problem

15

You might also like