ALG0183 Algorithms & Data Structures Lecture 6 The maximum contiguous subsequence sum problem....
-
Upload
chester-beasley -
Category
Documents
-
view
230 -
download
1
Transcript of ALG0183 Algorithms & Data Structures Lecture 6 The maximum contiguous subsequence sum problem....
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
1
ALG0183 Algorithms & Data Structures
Lecture 6The maximum contiguous subsequence sum problem.
8/25/2009
Weiss Chapter 5.3
There are many algorithms to solve this problem and their performances vary dramatically.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
2
The maximum contiguous subsequence sum problem.
• Given (possibly negative) integers A1, A2,...,AN, 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
j
kk i
A
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
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
The case where all the numbers are positive is not interesting.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
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
“Be aware that emptiness is always a possibility and that in many instances it is not a special case at all.” Weiss
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
5
The obvious O(N3) 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(N3).
8/25/2009
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
68/25/2009
A cubic maximum contiguous subsequence sum algorithm.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
7
MaxSumTest.javathe beginning
8/25/2009
static means “any changes to a static member are made to all objects”private means “cannot be accessed by members of another class”
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
8
A more formal calculation of Big-Oh.
• The summation is carried out the following number of times:
8/25/2009
3 2
1
3 21
6
jN N
i j i k i
N N N
1
( 1)
2
n
x
n nx
2
1
( 1)(2 1)
6
n
x
n n nx
1
1n
x
n
How do we get the RHS from the LHS?
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
98/25/2009
1
( 1)
2
n
x
n nx
2
1
( 1)(2 1)
6
n
x
n n nx
1 1j
k i
j i
( 1)( 2)( 1)
2
n
j i
n i n ij i
expand terms in brackets
1 is added to itself (j-i+1) times
1
1n
x
n
1 +2+3+...+(n-i+1)
A lot of work.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
10
An improved O(N2) algorithm.
• To improve Big-Oh from O(N3) to O(N2) 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(N3) algorithm.
• Making use of the previous calculation allows us to remove a loop and make the algorithm O(N2).
8/25/2009
1j j
k j kk i k i
A A A
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
118/25/2009
A quadratic maximum contiguous subsequence sum algorithm.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
12
Theorem 5.2 Weiss
• Let Ai,j be any sequence with sum Si,j < 0. If q > j, then Ai,q is not the maximum subsequence.– A larger subsequence can be obtained by
excluding the negative subsequence Ai,j.
8/25/2009
Figure 5.6 © Addison-Wesley
A maximum subsequence cannot begin with a negative number.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
13
Observation
8/25/2009
“All contiguous subsequences that border the maximum contiguous subsequence must have negative (or 0) sums (otherwise, we would include them).”
Weiss
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
14
Observation
8/25/2009
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, orb) The maximum started before i, orc) 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
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
158/25/2009
...,1,2,3,-7,...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.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
168/25/2009
A linear maximum contiguous subsequence sum algorithm.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
17
Lecture activity: trace maxSubSum3
8/25/2009
thisSum maxSum seqStart seqEnd
j=0 2 5 -9 6 -1 3 -5 4
j=1 2 5 -9 6 -1 3 -5 4
j=2 2 5 -9 6 -1 3 -5 4
j=3 2 5 -9 6 -1 3 -5 4
j=4 2 5 -9 6 -1 3 -5 4
j=5 2 5 -9 6 -1 3 -5 4
j=6 2 5 -9 6 -1 3 -5 4
j=7 2 5 -9 6 -1 3 -5 4