0% found this document useful (0 votes)
12 views

4 D and C - Maximum Subarray

This document discusses the divide and conquer approach to solving the maximum subarray sum problem. It introduces the problem, describes dividing the array in half recursively computing maximum sums in left, right, and crossing subarrays, and provides examples of applying this approach.

Uploaded by

ctxxfx5ykz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

4 D and C - Maximum Subarray

This document discusses the divide and conquer approach to solving the maximum subarray sum problem. It introduces the problem, describes dividing the array in half recursively computing maximum sums in left, right, and crossing subarrays, and provides examples of applying this approach.

Uploaded by

ctxxfx5ykz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Design and Analysis of Algorithms

Divide and Conquer


(Maximum Subarray Sum Problem)

Dr. D. P. Acharjya
Professor, SCOPE
Office: SJT Annex 201E
Email: [email protected]

1/23/2024 Dr. D. P. Acharjya 1


Maximum Subarray Problem
 Given an array of integers, our objective is to
find the contiguous subarray containing at least
one number has the maximum sum value.
 It is otherwise known as maximum subarray
sum problem.
 For example consider the input:

 Output:
Max Sum: 60
23 January 2024 Dr. D. P. Acharjya 2
Various Approaches
 Brute Force Method
 Identifying all subarrays and then calculating the
maximum sum.
 Divide and Conquer (Syllabus)
 Divide, Conquer and Combine
 Greedy Method
 Dynamic Programming
 Kadane’s Algorithm

23 January 2024 Dr. D. P. Acharjya 3


Divide and Conquer Approach
1. Divide the array of numbers into two equal halves.
2. Recursively compute the Maximum subarray sum in left half
(Left Sum)
3. Recursively compute the Maximum subarray sum in right half
(Right Sum)
4. Recursively compute the Maximum subarray sum for the
subarray crosses the mid point (Cross Sum)
- Find the maximum sum starting from mid and ending at
some point on left of mid.
- Find the maximum sum starting from (mid+1) and ending
at some point on right of (mid +1).
- Finally add the two and return.
5. Calculate the Maximum of Step 2, Step 3, Step 4
23 January 2024 Dr. D. P. Acharjya 4
Numerical Illustration …

1/23/2024 Dr. D. P. Acharjya 5


Continued …
 Left sum = 10
 Right sum = -30
 Cross Sum
 Left sum = -20 Left sub sum =
 Right sum = 10 Max{10, 10+(-20)} = 10
 Cross Sum Right sub sum = -30
Left sub sum = -20 Total = -30 + 10 = -20
Right sub sum = 10 Final sum = max{10,-30, -
20} = 10
Total = -20 + 10 = -10
Final sum = max{-20,10,-10}
= 10
1/23/2024 Dr. D. P. Acharjya 6
Continued …

 Left sum = 40
 Right sum = -10
 Cross Sum
Left sub sum = 40
Right sub sum = -10
Total = 40 - 10 = 30
Final sum = Max{Left sum, Right sum, Cross sum}
= max{40,-10,30} = 40
1/23/2024 Dr. D. P. Acharjya 7
Continued …
 Left sum = 10
 Right sum = 40
 Cross Sum
Left sub sum =
Max{-30, -30+10, -30+10-20}
= Max{-30, -20, - 40}= -20
Right sub sum =
Max{40, 40-10} = Max{40, 30}
= 40
Total = -20 + 40 = 20
Final sum = max{10, 40, 20} = 40
1/23/2024 Dr. D. P. Acharjya 8
Continued …

 Left sum = 20  Left sum = -50


 Right sum = 10  Right sum = 40
 Cross Sum  Cross Sum
Left sub sum =Max{20}= 20 Left sub sum =Max{-50}= -50
Right sub sum = Max{10}=10 Right sub sum = Max{40}=40
Total = 20 + 10 = 30 Total = -50 + 40 = -10
Final sum = max{20, 10, 30} = 30 Final sum = max{-50, 40, -10} = 40

1/23/2024 Dr. D. P. Acharjya 9


Continued …
 Left sum = 30
 Right sum = 40
 Cross Sum
Left sub sum =
Max{10, 10+20}
= Max{10, 30}= 30
Right sub sum =
Max{-50, -50+40}
= Max{-50, -10}= -10
Total = 30 - 10 = 20
Final sum = max{30, 40, 20} = 40
1/23/2024 Dr. D. P. Acharjya 10
Continued …

1/23/2024 Dr. D. P. Acharjya 11


Continued …
 Left sum = 40
 Right sum = 40
 Cross Sum
Left sub sum =
Max{-10, -10+40, -10+40-30, -10+40-30+10, -10+40-30+10-20 }
= Max{-10, 30, 0, 10, -10}= 30
Right sub sum =
Max{20, 20+10, 20+10-50, 20+10-50+40}
= Max{20, 30, -20, 20}= 30
Total cross sum = 30 + 30 = 60
Final sum = max{40, 40, 60} = 60 (Solution)
1/23/2024 Dr. D. P. Acharjya 12
Continued …

 The Recurrence Relation


T(n) = 2 T(n / 2) + (n)
 On Solving we get
T(n) = (n log n)

1/23/2024 Dr. D. P. Acharjya 13

You might also like