Presentation is loading. Please wait.

Presentation is loading. Please wait.

ALG0183 Algorithms & Data Structures Lecture 6 The maximum contiguous subsequence sum problem. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy.

Similar presentations


Presentation on theme: "ALG0183 Algorithms & Data Structures Lecture 6 The maximum contiguous subsequence sum problem. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy."— Presentation transcript:

1 ALG0183 Algorithms & Data Structures Lecture 6 The maximum contiguous subsequence sum problem. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Weiss Chapter 5.3 There are many algorithms to solve this problem and their performances vary dramatically.

2 The maximum contiguous subsequence sum problem. Given (possibly negative) integers A 1, A 2,...,A N, find (and identify the sequence corresponding to) the maximum value of: The maximum contiguous subsequence sum is zero if all the integers are negative. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 2

3 Examples If the input is {-2, 11, -4, 13, -5, 2} the maximum sum is 20. If the input is {1, -3, 4, -2, -1, 6} the maximum sum is 7. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 3 The case where all the numbers are positive is not interesting.

4 “Always consider emptiness” Weiss If all the integers were negative, the algorithm could return the smallest negative integer as the maximum contiguous subsequence sum. This would, however, be ignoring the empty subsequence of zero integers whose sum is 0. – Remember, the empty set is a subset of any set: ∀ A: ∅ ⊆ A 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 4 “Be aware that emptiness is always a possibility and that in many instances it is not a special case at all.” Weiss

5 The obvious O(N 3 ) algorithm. exhaustive search or brute-force An obvious solution is work out the sum of all possible subsequences, updating maximum sum as required. A loop is needed to iterate through all the possible starting subscripts of the subsequences. An inner loop is needed to iterate through all the possible ending subscripts of the subsequences. For each subsequence, a loop is needed to compute the sum of the elements. We have a loop inside a loop inside a loop. – Informally, Big-Oh is O(N 3 ). 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 5

6 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 6 A cubic maximum contiguous subsequence sum algorithm.

7 MaxSumTest.java the beginning 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 7 static means “any changes to a static member are made to all objects” private means “cannot be accessed by members of another class”

8 A more formal calculation of Big-Oh. The summation is carried out the following number of times: 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 8 How do we get the RHS from the LHS?

9 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 9 expand terms in brackets 1 is added to itself (j-i+1) times 1 +2+3+...+(n-i+1)

10 An improved O(N 2 ) algorithm. To improve Big-Oh from O(N 3 ) to O(N 2 ) requires removing a loop (if possible). For a value of i, the summation includes just one more element from the previous calculation. This previous calculation is thrown away by the O(N 3 ) algorithm. Making use of the previous calculation allows us to remove a loop and make the algorithm O(N 2 ). 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 10

11 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 11 A quadratic maximum contiguous subsequence sum algorithm.

12 Theorem 5.2 Weiss Let A i,j be any sequence with sum S i,j j, then A i,q is not the maximum subsequence. – A larger subsequence can be obtained by excluding the negative subsequence A i,j. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 12 Figure 5.6 © Addison-Wesley A maximum subsequence cannot begin with a negative number.

13 Observation 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 13 “All contiguous subsequences that border the maximum contiguous subsequence must have negative (or 0) sums (otherwise, we would include them).” Weiss

14 Observation 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 14 At some i, at the first value of j for which the sum of the subsequence becomes negative, we know that: a) The maximum occured between i and j-1, or b) The maximum started before i, or c) The maximum starts after j. Any new maximum will occur after index j, so i can be updated to j+1. For cases a) and b) we have already met and computed the maximum. maximum - maximum subsequence

15 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 15 At some i, we begin the usual process of summing subsequences. At some j, here when -7 is read, the sum is found to be negative. There is no point in considering further sequences starting at 1, 2, or 3. a negative subsequence Any subsequence that begins with a negative subsequence can be improved by eliminating that negative subsequence.

16 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 16 A linear maximum contiguous subsequence sum algorithm.

17 Lecture activity: trace maxSubSum3 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 17 thisSummaxSumseqStartseqEnd j=025-963-54 j=125-963-54 j=225-963-54 j=325-963-54 j=425-963-54 j=525-963-54 j=625-963-54 j=725-963-54

18 From O(N 3 ) to O(N 2 ) to O(N): be amazed! Of course, it is usually not possible to improve an algorithm´s performance quite so much. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 18 Figure 5.1 ©Addison Wesley


Download ppt "ALG0183 Algorithms & Data Structures Lecture 6 The maximum contiguous subsequence sum problem. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy."

Similar presentations


Ads by Google