edited by
355 views
0 votes
0 votes

In this question, we will develop a dynamic programming algorithm for finding the maximum sum of consecutive terms of a sequence of real numbers. That is, given a sequence of real numbers $a_{1}, a_{2},\dots,a_{n},$ the algorithm computes the maximum sum $\displaystyle\sum_{i=j}^{k} a_{i}\: \text{where}\: 1 \leq j \leq k \leq n.$

  1. Show that if all terms of the sequence are nonnegative, this problem is solved by taking the sum of all terms. Then, give an example where the maximum sum of consecutive terms is not the sum of all terms.
  2. Let $M(k)$ be the maximum of the sums of consecutive terms of the sequence ending at $a_{k}.$ That is, $M(k) = \:\text{max}\:_{1\leq j\leq k}\displaystyle{} \sum_{i=j}^{k} a_{i}.$ Explain why the recurrence relation $M(k) = \text{max}(M(k − 1) + a_{k}, a_{k})$ holds for $k = 2, \dots, n.$
  3. Use part $(B)$ to develop a dynamic programming algorithm for solving this problem.
  4. Show each step your algorithm from part $(C)$ uses to find the maximum sum of consecutive terms of the sequence $2, −3, 4, 1, −2, 3.$ 
  5. Show that the worst-case complexity in terms of the number of additions and comparisons of your algorithm from part $(C)$ is linear.
edited by

Please log in or register to answer this question.

Related questions

0 votes
0 votes
0 answers
4