Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the maximum sum of a sub-sequence using dynamic programming?

I'm re-reading Skiena's Algorithm Design Manual to catch up on some stuff I've forgotten since school, and I'm a little baffled by his descriptions of Dynamic Programming. I've looked it up on Wikipedia and various other sites, and while the descriptions all make sense, I'm having trouble figuring out specific problems myself. Currently, I'm working on problem 3-5 from the Skiena book. (Given an array of n real numbers, find the maximum sum in any contiguous subvector of the input.) I have an O(n^2) solution, such as described in this answer. But I'm stuck on the O(N) solution using dynamic programming. It's not clear to me what the recurrence relation should be.

I see that the subsequences form a set of sums, like so:

S = {a,b,c,d}

a    a+b    a+b+c    a+b+c+d
     b      b+c      b+c+d
            c        c+d
                     d

What I don't get is how to pick which one is the greatest in linear time. I've tried doing things like keeping track of the greatest sum so far, and if the current value is positive, add it to the sum. But when you have larger sequences, this becomes problematic because there may be stretches of negative numbers that would decrease the sum, but a later large positive number may bring it back to being the maximum.

I'm also reminded of summed area tables. You can calculate all the sums using only the cumulative sums: a, a+b, a+b+c, a+b+c+d, etc. (For example, if you need b+c, it's just (a+b+c) - (a).) But don't see an O(N) way to get it.

Can anyone explain to me what the O(N) dynamic programming solution is for this particular problem? I feel like I almost get it, but that I'm missing something.

like image 622
user1118321 Avatar asked Dec 27 '11 22:12

user1118321


People also ask

What is maximum sum increasing subsequence?

Maximum Sum Increasing subsequence is a subsequence of a given list of integers, whose sum is maximum and in the subsequence, all elements are sorted in increasing order.

How do you find the longest increasing subsequence with a difference K?

Fill the dp array with the maximum value between current dp indices and current max_length indices+1. Fill the max_length array with the maximum value between current dp indices and current max_length indices. Longest sub sequence length is the maximum value in dp array.

How do you print the longest increasing subsequence?

We have discussed how to find length of Longest Increasing consecutive subsequence. To print the subsequence, we store index of last element. Then we print consecutive elements ending with last element.

How do you find the subsequence of an array?

Approach: For every element in the array, there are two choices, either to include it in the subsequence or not include it. Apply this for every element in the array starting from index 0 until we reach the last index. Print the subsequence once the last index is reached.


1 Answers

You should take a look to this pdf back in the school in http://castle.eiu.edu here it is:

enter image description here

The explanation of the following pseudocode is also int the pdf. enter image description here

like image 128
edgarmtze Avatar answered Oct 01 '22 17:10

edgarmtze