Inductive method for solving the maximum continuous subsequence

Source: Internet
Author: User

(Link: http://www.karottc.com/blog/2014/09/07/maximum-consecutive-subsequence/)

Problem

The following describes the problem of finding the maximum continuous subsequence:

Given a real number sequence x1, x2 ,..., xn (not necessarily a positive number), find a (continuous) subsequence Xi, Xi + 1 ,..., XJ, so that the sum of its values is the largest and largest in all consecutive subsequences.

This problem is the largest subsequence problem. The desired sequence is called --Maximum subsequence. The following mathematical induction method is used to analyze and solve this problem. The best goal of solving this problem is:An algorithm that can scan this sequence only once to obtain the largest subsequence.

Inductive Analysis

According to the above questions, we can directly obtain general inductive assumptions:

Inductive hypothesis:We know how to find the largest subsequence of a sequence with a size less than N.

The following assumes some variables:

  • Given SequenceS (n)= (X1, x2,..., xn ).
  • SequenceS (n)The sub-sequence of isS' (N).
  • SequenceS (n)The largest subsequence of isS'm (N)= (XI, Xi + 1,..., XJ ).
  • SequenceS (n)The largest suffix subsequence of isS' E (n)= (XK, XK + 1,..., xn ).

Now we start to use mathematical induction to prove:

  1. When n = 1,S (1)= X1. If X1 is <0, * s 'M (1)= NULL; otherwise, * s 'M (1)= X1.
  2. When N = n-1,S (n-1)= (X1, x2,..., xn-1), supposeS'm (n-1)= (XI, Xi + 1,..., XJ ).
  3. When N = n,S (n)= (X1, x2,..., xn) = (S (n-1), Xn), and thenS'm (n-1)Push and exportS'm (N)In three cases:

    1. S'm (n-1)= (XI, Xi + 1,..., XJ) = NULL. If XN <0, * s'm (N)= NULL; otherwise, * s'm (N)= Xn.
    2. S'm (n-1)= (XI, Xi + 1,..., XJ) & J = n-1, at this timeS'm (n-1)=S 'E (n-1)If XN> 0,S'm (N)=S'm (n-1)+ Xn = (XI, Xi + 1,..., XJ) + Xn, otherwiseS'm (N)=S'm (n-1)= (XI, Xi + 1,..., XJ ).
    3. S'm (n-1)= (XI, Xi + 1,..., XJ) & J <n-1, at this timeS'm (n-1)>S 'E (n-1)To obtainS'm (N)=S'm (n-1)OrS'm (N)=S 'E (n-1)+ Xn.
  4. That is, the above evidence: can be obtainedS (n)= (X1, x2,..., xn) the maximum continuous subsequence.Enhanced induction hypothesisThis is also the key to this proof:

    More inductive assumptions:We know how to find the largest subsequence with a scale less than N and the largest subsequence with a suffix.

We know.S'm (N)AndS' E (n)These two sequences make the algorithm clear. We add XN to the largest suffix subsequence. If the sum of XN is greater than the original largest subsequence, a new largest subsequence (also a suffix) is obtained. Otherwise, reserve the largest subsequence before. However, the process of solving the problem is not over yet. We also need to find a new sub-sequence with the largest suffix, because we cannot simply add XN to the previous largest suffix, it is possible that the sum of the largest suffixes ending with XN is negative. In this case, we need to set null (empty set) as the maximum suffix (and the subsequent xn + 1 are also taken into account ).

Specific Algorithm Implementation

According to the above analysis process, the specific c ++ code is attached below:

Int maximum_consecutive_subsequence (int * X, int N) {int global_max = 0; // sum of the largest subsequences and INT suffix_max = 0; // sum of the subsequences with the largest suffix, each iteration updates int I = 0; for (I = 0; I <n; I ++) {If (X [I] + suffix_max> global_max) {suffix_max = suffix_max + X [I]; global_max = suffix_max;} else if (X [I] + suffix_max> 0) {suffix_max = suffix_max + X [I];} else {suffix_max = 0 ;}} return global_max ;}

For more detailed code, click here.

2014.09.07


Inductive method for solving the maximum continuous subsequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.