Maximum Subarray Sum (Kadane’s Algorithm)
May 20, 2023
Maximum Subarray Sum algorithm, commonly known as Kadane’s algorithm, is a dynamic programming algorithm that solves the problem of finding the contiguous subarray within a one-dimensional array of integers that has the largest sum. The algorithm is widely used in various applications such as data analysis, computer vision, image processing, and machine learning.
Brief history and development
The algorithm was first introduced by Jay Kadane in 1984, a professor of Statistics at Carnegie Mellon University. The algorithm was initially developed to solve the problem of finding the maximum sum of stock prices in the stock market. Later on, the algorithm gained popularity in other fields such as computer science, mathematics, and engineering due to its simplicity and efficiency.
Purpose and usage of the Algorithm
The main purpose of the Maximum Subarray Sum algorithm is to find the contiguous subarray within an array that has the largest sum. This is a fundamental problem in computer science, statistics, and machine learning, and it has various use cases such as:
- In computer vision and image processing, the algorithm is used for object recognition and detection, where the features of an object are represented by a set of numerical values.
- In machine learning, the algorithm is used to train deep neural networks by optimizing the weights and biases of complex models.
- In statistics, the algorithm is used to analyze time-series data, where the goal is to find the trend or pattern in the data.
Key concepts and principles
The key concept behind Kadane’s algorithm is dynamic programming, where the problem is divided into smaller subproblems, and the solution to the original problem is obtained by combining the solutions to the subproblems.
The algorithm maintains two variables: max_so_far and max_ending_here. The max_so_far variable stores the maximum sum of the subarray seen so far, while the max_ending_here variable stores the maximum sum of the subarray that ends at the current position. The algorithm iterates through the input array and updates these variables accordingly.
To find the maximum sum subarray, the algorithm compares the current element with the sum of the current element and the previous maximum sum subarray ending at the previous position. If the current element is greater than the sum, the algorithm starts a new subarray at the current position, and updates the max_so_far variable if the new sum is greater than the previous maximum sum. Otherwise, the algorithm extends the previous subarray by adding the current element to the previous maximum sum subarray, and updates the max_so_far variable if the new sum is greater than the previous maximum sum.
Pseudocode and implementation details
The pseudocode for Kadane’s algorithm is as follows:
max_so_far = 0
max_ending_here = 0
for i in range(len(arr)):
max_ending_here = max(arr[i], max_ending_here + arr[i])
max_so_far = max(max_so_far, max_ending_here)
return max_so_far
The implementation of the algorithm in Python is as follows:
def kadane(arr):
max_so_far = 0
max_ending_here = 0
for i in range(len(arr)):
max_ending_here = max(arr[i], max_ending_here + arr[i])
max_so_far = max(max_so_far, max_ending_here)
return max_so_far
The time complexity of Kadane’s algorithm is O(n), where n is the size of the input array. The space complexity of the algorithm is O(1), as it only requires two variables to maintain the maximum sum.
Examples and use cases
Let’s consider an example of finding the maximum sum subarray from the input array [-2, 1, -3, 4, -1, 2, 1, -5, 4]. The maximum sum subarray is [4, -1, 2, 1], with a sum of 6.
Using Kadane’s algorithm, we can find the maximum sum subarray as follows:
arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
print(kadane(arr)) # Output: 6
Another use case of Kadane’s algorithm is finding the maximum sum subarray in a two-dimensional array. The algorithm can be applied to each row of the matrix to find the maximum sum subarray in each row, and then the maximum of these values can be found to obtain the maximum sum subarray in the entire matrix.
Advantages and disadvantages
The advantages of Kadane’s algorithm are:
- The algorithm has a simple and intuitive implementation.
- The algorithm has a time complexity of O(n), which is efficient for large input sizes.
- The algorithm has a space complexity of O(1), which is constant and does not depend on the size of the input.
The disadvantages of Kadane’s algorithm are:
- The algorithm only works for arrays of integers, and cannot be applied to other types of data such as strings or floating-point numbers.
- The algorithm only finds the maximum sum subarray and does not provide any information about the indices of the subarray or the actual elements in the subarray.
Related algorithms or variations
There are various related algorithms or variations of Kadane’s algorithm, such as:
- Maximum Sum Increasing Subsequence: This algorithm finds the increasing subsequence within an array that has the largest sum.
- Maximum Sum Decreasing Subsequence: This algorithm finds the decreasing subsequence within an array that has the largest sum.
- Maximum Product Subarray: This algorithm finds the contiguous subarray within an array that has the largest product.