This problem is listed under several book solution manuals, but they\'re wrong.
ID: 3709219 • Letter: T
Question
This problem is listed under several book solution manuals, but they're wrong. Give algorithm in pseudocode
A contiguous subsequence of a list S is a subsequence made up of consecutive elements of S For instance, if S is 5, 15, -30,10, -5, 40, 10, then 15,-30,10 is a contiguous subsequence but 5,15, 40 is not. Give a linear-time algorithm for the following task Input: A list of numbers, a1, a2,..,an. Output: The contiguous subsequence of maximum sum (a subsequence of length zero has sum zero) For the preceding example, the answer would be 10,-5,40,10, with a sum of 55 (Hint: For eachj e 11, 2,..., n), consider contiguous subsequences ending exactly at position j.)Explanation / Answer
ANSWER
GIVEN BY
Let S be the uninterrupted basics present in a bordering sub-sequence list S.
Give a linear-time algorithm for dilemma
Input:
amount list, al to a„.
Output:
The contiguous sequence of minimum total; if we like, we can presently return the whole, slightly than the sub-sequence.
Note:
a sub-sequence of span 0 has sum 0.
Sub-problem:
Let F(i) be the total of the highest sum adjoining sub-sequence that ends exactly at a,. (But is perhaps of length 0) We crave maxiF(i).
Recursive Formula:
The sub-sequence crucial F(i) either has
(1)length
(2) contains the unsurpassed sub-sequence finishing at a,.1, succeed by element al. Hence, F(1) = max (0, + F(i - 1)). For stability F(0) = 0.
Algorithm:
F(0) = 0 for i = 1 to n: F(i) = max (0,a; + F (i - 1)) return maxi F(i)
Running Time: 0 (n)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.