Maximum Contiguous Subsequence Sum: One Problem, Many Solutions - PowerPoint PPT Presentation

About This Presentation
Title:

Maximum Contiguous Subsequence Sum: One Problem, Many Solutions

Description:

Problem: Given a sequence of (possibly negative) integers find ... which is slicker but perhaps harder to understand.) 2/13/06. Lecture Notes 8. 15. Program 4 ... – PowerPoint PPT presentation

Number of Views:214
Avg rating:3.0/5.0
Slides: 17
Provided by: icsU7
Category:

less

Transcript and Presenter's Notes

Title: Maximum Contiguous Subsequence Sum: One Problem, Many Solutions


1
Maximum Contiguous Subsequence Sum
One Problem, Many Solutions
Problem Given a sequence of (possibly
negative) integers find
the maximum value of the sum of a
subsequence The maximum subsequence sum is 0
if all the integers are negative.

.
Example The input is -2, 11, -4, 13, -5, 2
The answer is 20, by summing
11, -4, 13 .
2
Solution 1 --- ruminations...
Consider all possibilities for where the answer
might come from...
...
...
0 1 2 i j N-1
add
Is sum larger?
No move on
Yes remember it!
3
Analysis of solution 1
When we are sufficiently clear in our ruminations
we should be able to analyze the program even
before we write it! In fact, we are analyzing
the algorithm, not the details of the program.
  • To add up the interval of length d takes ?(d)
    steps.
  • There are (N-d1) intervals of length d
    (starting at positions 0,1,...,N-d ).
  • Time taken to add up all intervals of length d
    ?((N-d1)d)
  • Possible values of d 1,,N.
  • Calculate
    Obtain ?(N3)


(recall 1222N2? )
4
Program 1
public static int maxSubSum1( int a)
int maxSum 0 for (int i0 i lt a.length
i) for (int ji j lt a.length j)
int thisSum 0 for (int ki k lt j
k) thisSum thisSum ak if
(thisSum gt maxSum) maxSum thisSum
return maxSum
?(n3)
5
Solution 2 a simple idea for improvement
We dont need to compute interval sums from
scratch.
...
...
1 2 3 i j j1 N
add
After adding numbers in the interval i,j, the
interval i,j1 takes only one more step.
6
Program 2
public static int maxSubSum2( int a) int
maxSum 0 for (int i0 i lt a.length i)
int thisSum 0 for (int ji j lt
a.length j) thisSum thisSum aj
if (thisSum gt maxSum) maxSum
thisSum return maxSum
?(n2)
(Weve analyzed programs with this structure
before.)
7
Solution 3 divide-and-conquer, using recursion
Consider the center of the sequence
...
...
left center
right
  • Where is the max subsequence? Three
    possibilities
  • All of it is left of center --gt find by
    recursive call.
  • 2) All of it is right of center --gt find by
    recursive call.
  • 3) Across the center (see next slide).

8
Solution 3 key idea and analysis
...
...
Acenter Acenter1
Key idea when the max sum subsequence is across
the center, both Acenter and Acenter1 must
be in it so we can maximize separately the
part that is left of center and ends at
Acenter and the part that is right of center
and starts at Acenter1 . This part is
?(N)! Recurrence relation T(N) 2
T(N/2) c N Solving this gives T(N) is ?(N
log N)
9
Program 3
?(n log n)
public static int maxSubSum3( int a )
return a.length gt 0 ? maxSumRec( a, 0, a.length -
1 ) 0 private static int maxSumRec( int
a, int left, int
right ) int center ( left right ) / 2
if( left right ) //
Base case return a left gt 0 ? a left
0 return max3( maxSumRec(a, left, center),
// REC. CALL
maxAcrossCenter(a, left, right), // next slide
maxSumRec(a, center1, right) ) //
REC. CALL private static int max3( int a, int
b, int c ) return a gt b ? a gt c ? a c b gt
c ? b c
// max of three ints
10
Program 3, contd
private static int maxAcrossCenter( int a,
int left, int right
) int center ( left right ) / 2
int maxLeft 0 for( int sum0, int i
center i gt left i-- ) sum sum ai
if( sum gt maxLeft ) maxLeft sum int
maxRight 0 for( int sum0, int i center
1 i lt right i ) sum sum ai
if( sum gt maxRight ) maxRight sum return
maxLeft maxRight
11
Solution 4 an entirely different perspective
...
...
0 1 2 i j N-1
add
add
minimize
12
...
...
B
...
...
A
0 1 2 i j N-1
...
...
C
13
To solve the original problem Find indices

So, for given
Need to find least
such that i lt j
.
Compute two new arrays and
Now, the best solution is obtained by finding
14
Analysis
Looks complicated but actually the algorithm is
fast. Each array takes ?(N) steps to
compute. Finding i DiEi is minimum takes
another ?(N)steps. From this information,
finding the best interval takes ?(N) steps.
Total time ?(N)
(The book gives another ?(N) algorithm (a 5th
solution!) which is slicker but perhaps harder to
understand.)
15
Program 4
public static int maxSubSum4( int a) int
n a.length int total 0 for (int i 0
i lt n i) total total ai int b
new intn int c new intn1 for
(b00, int i1 i lt n i) bi bi-1
ai-1 for (cn0, int jn-1 j ! 0 j--)
cj cj1 aj ... continued
?(n)
16
Program 4, contd
... int d new intn int e new
intn for (d00, int i1 i lt n i)
di (di-1 lt bi) ? di-1 bi for
(en-1cn-1, int jn-2 j ! 0 j--) ej
(ej1 lt cj) ? ej1 cj int minSum
d0 e0 for (int i1 i lt n i) if
(diei lt minSum) minSum diei
return total - minSum
Write a Comment
User Comments (0)
About PowerShow.com