Submitted:
16 October 2023
Posted:
17 October 2023
You are already at the latest version
Abstract
Keywords:
1. History
2. Growing champions
- (a)
- a maximal adjacent subsequence cannot have a starting sub-subsequence with a negative sum. Eliminating such a starting sub-subsequence and starting over must result in a larger sum for the subsequence, so the original subsequence cannot be optimal.
- (b)
- after eliminating starting sub-subsequences with negative sums, the ensuing sub-subsequence must start non-negatively, so including it in the subsequence must increase (zeros don’t affect the sum) the resulting sum. So an optimal subsequence must start immediately after the elimination of starting sub-subsequences with negative sums.
| Algorithm 1 Linear algorithm based on Champ |
|
MaxSoFar : = − inf
Champ : = − inf
For I = 1 to N do
Champ : =X[I] + Max(0.0, Champ)
MaxSoFar : = Max(MaxSoFar, Champ).
|
| Algorithm 2 Algorithm 1 modified to report the start and end of the first interval whose sum is maximum. |
|
MaxSoFar : = − inf
Champ : = − inf
Start : = 1
End : = 1
Cstart : =1
For I = 1 to N do
if Champ < 0 then
Cstart : = I
Champ : = X[I]
else Champ : = X[I] + Champ
if MaxSoFar < Champ
Start : = Cstart
End : = I
MaxSoFar : = Champ.
|
3. The linear algorithm Bentley gave me credit for
| Algorithm 3 Same as Algorithm 4 in Bentley [5] |
|
MaxSoFar : = 0.0
MaxEndingHere : = 0.0
for I = 1 to N do
MaxEndingHere : =Max(0.0, MaxEndingHere + X[I])
MaxSoFar : = Max(MaxSoFar, MaxEndingHere).
|
| Algorithm 4 Algorithm 3 modified to report the start and end of an optimal subsequence. |
|
MaxSoFar : = 0
MaxEndingHere : = 0
Start : = 1
End : = 1
Mstart : =1
for I = 1 to N do
if MaxEndingHere + X[I] < 0.0 then
Mstart : = I
MaxEndingHere : = 0.0
else MaxEndHere : = MaxEndhere + X[I]
if MaxSoFar < MaxEndingHere then
MaxSoFar : = MaxEndingHere
Start = MStart
End = I.
|
4. Discussion
Appendix A. Proof of 4
References
- von Neumann, J.; Morgenstern, O. Theory of Games and Economic Behavior; Princeton University Press: New Jersey, 1944. [Google Scholar]
- Savage, L.J. Foundations of Statistics; J. Wiley and Sons: New York, 1954. [Google Scholar]
- Dantzig, G. Linear Programming and Extensions; Princeton University Press, 1963. [Google Scholar]
- Klee, V.; Minty, G.J. How good is the simplex algorithm? In Inequalities III (Proceedings of the Third Symposium on Inequalities held at the University of California, Los Angeles, Calif., September 1–9, 1969, dedicated to the memory of Theodore S. Motzkin), Shisha, Oved (ed.); Academic Press: New York-London:, 1972; pp. 159–175. [Google Scholar]
- Bentley, J. Algorithm Design Techniques. Communications of the ACM 1984, (9) 27, 865–871. [Google Scholar] [CrossRef]
- Aygun, R.S. Using Maximum Sum Subarrays for Approximate String Matching. Annals of Data Science 2017, 4, 503–531. [Google Scholar] [CrossRef]
| Algorithm 1 | Algorithm 3 | |
|---|---|---|
| Time | 0(n) | 0(n) |
| Space | 0(1) | 0(1) |
| Correct? | Yes | Yes |
| Modify to report start and end | Algorithm 2 | Algorithm 4 |
| Negative input | Either empty set or | |
| largest element | Empty set only |
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |
© 2023 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (http://creativecommons.org/licenses/by/4.0/).