|
|
| Line 1: |
Line 1: |
| In [[computer science]], the '''maximum subarray problem''' is the task of finding the contiguous subarray within a one-dimensional [[array data structure|array]] of numbers (containing at least one positive number) which has the largest sum. For example, for the sequence of values −2, 1, −3, 4, −1, 2, 1, −5, 4; the contiguous subarray with the largest sum is 4, −1, 2, 1, with sum 6.
| | Hi there, I am Alyson Pomerleau and I believe it seems quite good when you say it. To play lacross is something he would [http://www.edmposts.com/build-a-beautiful-organic-garden-using-these-ideas/ psychic solutions by lynne] no means give up. Some time in the past she chose to reside in Alaska and her mothers and fathers live nearby. Since he was eighteen he's been operating as an information [http://alles-herunterladen.de/excellent-advice-for-picking-the-ideal-hobby/ free psychic reading] officer but he ideas on changing it.<br><br>Feel free to surf to my blog post :: real psychics - [http://Www.Sirudang.com/siroo_Notice/2110 click here to investigate] - |
| | |
| The problem was first posed by [[Ulf Grenander]] of [[Brown University]] in 1977, as a simplified model for [[maximum likelihood]] estimation of patterns in digitized images. A [[linear time]] [[algorithm]] was found soon afterwards by [[Jay Kadane]] of [[Carnegie-Mellon University]] {{harv|Bentley|1984}}.
| |
| | |
| ==Kadane's algorithm==
| |
| Kadane's algorithm consists of a scan through the array values, computing at each position the maximum subarray ending at that position. This subarray is either empty (in which case [[empty sum|its sum is zero]]) or consists of one more element than the maximum subarray ending at the previous position. Thus, the problem can be solved with the following code, expressed here in [[Python (programming language)|Python]]:
| |
| | |
| <source lang="python">
| |
| def max_subarray(A):
| |
| max_ending_here = max_so_far = 0
| |
| for x in A:
| |
| max_ending_here = max(0, max_ending_here + x)
| |
| max_so_far = max(max_so_far, max_ending_here)
| |
| return max_so_far
| |
| </source>
| |
| | |
| A variation of the problem that does not allow zero-length subarrays to be returned in the case that the entire array consists of negative numbers can be solved with the following code:
| |
| | |
| <source lang="python">
| |
| def max_subarray(A):
| |
| max_ending_here = max_so_far = A[0]
| |
| for x in A[1:]:
| |
| max_ending_here = max(x, max_ending_here + x)
| |
| max_so_far = max(max_so_far, max_ending_here)
| |
| return max_so_far
| |
| </source>
| |
| | |
| Here is a version in [[C++]] which can optionally return the beginning and ending indices of the maximum subarray:
| |
| | |
| <source lang="cpp">
| |
| int sequence(std::vector<int> const & numbers)
| |
| {
| |
| // Initialize variables here
| |
| int max_so_far = numbers[0], max_ending_here = numbers[0];
| |
| | |
| // OPTIONAL: These variables can be added in to track the position of the subarray
| |
| // size_t begin = 0;
| |
| // size_t begin_temp = 0;
| |
| // size_t end = 0;
| |
| | |
| // Find sequence by looping through
| |
| for(size_t i = 1; i < numbers.size(); i++)
| |
| {
| |
| // calculate max_ending_here
| |
| if(max_ending_here < 0)
| |
| {
| |
| max_ending_here = numbers[i];
| |
|
| |
| // begin_temp = i;
| |
| }
| |
| else
| |
| {
| |
| max_ending_here += numbers[i];
| |
| }
| |
| | |
| // calculate max_so_far
| |
| if(max_ending_here >= max_so_far )
| |
| {
| |
| max_so_far = max_ending_here;
| |
|
| |
| // begin = begin_temp;
| |
| // end = i;
| |
| }
| |
| }
| |
| return max_so_far ;
| |
| }
| |
| </source>
| |
| | |
| The algorithm can also be easily modified to keep track of the starting and ending indices of the maximum subarray (see commented code).
| |
| | |
| Because of the way this algorithm uses optimal substructures (the maximum subarray ending at each position is calculated in a simple way from a related but smaller and overlapping subproblem, the maximum subarray ending at the previous position) this algorithm can be viewed as a simple example of [[dynamic programming]].
| |
| | |
| The runtime complexity of Kadane's algorithm is <math>\mathcal{O}(n)</math>.
| |
| | |
| ==Divide and conquer==
| |
| A [[divide and conquer algorithm]] is introduced in (Bentley, Jon (1984)); its time complexity is <math>\mathcal{O}(n \log n)</math>.
| |
| | |
| ==Generalizations==
| |
| Similar problems may be posed for higher dimensional arrays, but their solution is more complicated; see, e.g., {{harvtxt|Takaoka|2002}}. {{harvtxt|Brodal|Jørgensen|2007}} showed how to find the ''k'' largest subarray sums in a one-dimensional array, in the optimal time bound <math>\mathcal{O}(n + k)</math>.
| |
| | |
| == See also ==
| |
| | |
| * [[Subset sum problem]]
| |
| | |
| ==References==
| |
| *{{citation
| |
| | first = Jon | last = Bentley | authorlink = Jon Bentley
| |
| | title = Programming pearls: algorithm design techniques
| |
| | journal = [[Communications of the ACM]]
| |
| | volume = 27 | issue = 9 | year = 1984 | doi = 10.1145/358234.381162 | pages = 865–873}}.
| |
| *{{citation
| |
| | first1 = Gerth Stølting | last1 = Brodal | first2 = Allan Grønlund | last2 = Jørgensen
| |
| | contribution = A linear time algorithm for the ''k'' maximal sums problem
| |
| | title = Mathematical Foundations of Computer Science 2007
| |
| | publisher = Springer-Verlag
| |
| | series = Lecture Notes in Computer Science
| |
| | volume = 4708 | year = 2007 | pages = 442–453 | doi = 10.1007/978-3-540-74456-6_40}}.
| |
| *{{citation
| |
| | first = T. | last = Takaoka
| |
| | title = Efficient algorithms for the maximum subarray problem by distance matrix multiplication
| |
| | journal = Electronic Notes in Theoretical Computer Science | volume = 61 | year = 2002
| |
| | url = http://www.cosc.canterbury.ac.nz/tad.takaoka/cats02.pdf}}.
| |
| | |
| == External links ==
| |
| * [http://www.algorithmist.com/index.php/Kadane's_Algorithm www.algorithmist.com]
| |
| * [http://alexeigor.wikidot.com/kadane alexeigor.wikidot.com]
| |
| * [http://www.cs.waikato.ac.nz/Teaching/COMP317B/Week_1/AlgorithmDesignTechniques.pdf Algorithm Design Techniques]
| |
| | |
| [[Category:Optimization algorithms and methods]]
| |
| [[Category:Dynamic programming]]
| |
| [[Category:Articles with example Python code]]
| |
Hi there, I am Alyson Pomerleau and I believe it seems quite good when you say it. To play lacross is something he would psychic solutions by lynne no means give up. Some time in the past she chose to reside in Alaska and her mothers and fathers live nearby. Since he was eighteen he's been operating as an information free psychic reading officer but he ideas on changing it.
Feel free to surf to my blog post :: real psychics - click here to investigate -