Algorithm Analysis for finding the largest and continuous subvector Problems

Source: Internet
Author: User

1. Problem description: this is a problem that we can see in Chapter 2nd "Algorithm Design Technology" of programming Pearl (version 8th. The problem is described as follows: "The input is the vector x with n floating point numbers, and the output is the largest sum of any continuous subvectors of the input vector. For example, if the input vector contains the following 10 elements: (31,-, 59, 26,-,-93,-), the output of this program is x [2... 6] is the sum of 187." When all numbers are positive, the problem is easily solved. The largest subvector is the input vector itself. However, if the input vector contains a negative number, it cannot be processed properly. In addition, to make the definition of the problem more complete, we believe that when all input values are negative, the largest sum of subvectors is an empty vector and the sum is 0. 2 problem analysis 2.1 the simplest and most direct algorithm to see the problem. The simplest and most direct algorithm that comes to mind is a double nested loop that traverses all consecutive subvectors and records the largest sum encountered, and continuous updates. The pseudo code of this algorithm is: copy the Code 1 maxSum = 0; 2 for I = 0-> n-1 3 subVecSum = 0; 4 for j = I + 1-> n-1 5 subVecSum = subVecSum + x [j]; 6 if (subVecSum> maxSum) then 7 maxSum = subVecSum; 8 endif 9 endfor10 endfor copying code is a simple and crude algorithm. The algorithm complexity is O (n2), which is too inefficient. How can we improve it? In the preceding double-layer loop, apart from the continuous subvectors containing the first element of the input vector, the subvectors are scanned at least twice. For example, a subvector (-) is scanned twice, and a subvector (31,-) is scanned for processing once (no special processing or recording ), it was processed again when scanning the subvector (-) itself. In fact, the number of times a continuous subvector is processed depends on the position of the first element of the subvector in the input vector, if the first element of a continuous sub-vector is at the m-bit (starting from 1) of the input vector, the sub-vector is processed m times, and the m-1 times are not recorded. Through the above analysis, we found that most of the efficiency of this simple algorithm is consumed by the repeated processing of continuous subvectors. So the idea of algorithm optimization is how to reduce the repeated processing of continuous subvectors? 2.2 In the scanning algorithm, sum (I... j) is used to represent the sum of x [I... j] in a continuous subvector. Let's assume that the continuous subvector x [m... the sum of k] is the largest, that is, sum (m-1... k) ≤ sum (m... k) ≤ sum (m... k + 1) that is, sum (h... k) <sum (m... k) returns hε [m, k) (1) sum (l... k) ≤ sum (m... k) returns l ε [0, m) (2) for expression (1), we get the following derivation: 0 + sum (h... k) <sum (m... h) + sum (h... k) subtract sum (h... k), and 0 <sum (m... h) So, we get Conclusion 1: If the sum of a continuous subvector is greater than zero, the sum of continuous subvectors prefixed with this subvector may be larger. For expression (2), we get the following derivation: sum (l... m − 1) + sum (m... k) ≤ 0 + sum (m... k) subtract sum (m... k) to get sum (l... m-1) ≤ 0, so we get the conclusion 2: If the sum of a continuous subvector is less than or equal to zero, the sum of continuous subvectors prefixed with this subvector cannot be greater than the sum of subvectors after the subvector prefix is removed. Based on conclusions 1 and 2, we can obtain the following algorithm that only needs to scan the input vector x. Copy code 1 maxVecBegin = maxVecEnd =-1; 2 maxSum = 0; 3 cursorVecBegin = cursorVecEnd = 0; 4 cursorVecSum = 0; 5 while (cursorVecEnd <n) 6 do 7 cursorVecSum = cursorVecSum + x [cursorVecEnd]; 8 if (cursorVecSum> maxSum) then 9 maxSum = cursorVecSum; 10 maxVecBegin = cursorVecBegin; 11 maxVecEnd = cursorVecEnd; 12 else if (cursorVecSum <= 0) then13 cursorVecSum = 0; 14 cursorVecBegin = cursorVecEnd + 1; 1 5 endif16 cursorVecEnd = cursorVecEnd + 1; 17 The endwhile copy the code to maxSum record the largest sum of continuous subvectors, maxVecBegin and maxVecEnd records, and the maximum start and end positions of continuous subvectors. In addition, a cursor vector is used to scan the input vector. The end position of the cursor vector moves from the first element of the input vector to the end element, the starting position of the cursor vector may need to be adjusted backward as the scanning condition occurs. Each time you move the cursor vector's end position, cursorVecEnd, to a backward position, do the following: 1. calculate the sum of the cursor vectors and cursorVecSum; 2. based on the cursor vector and cursorVecSum, do the following: • The sum of the cursor vectors, cursorVecSum, is greater than the largest and maxVecSum, and updates the largest and maxVecSum as the sum of the cursor vectors and cursorVecSum, and update the largest and continuous subvectors to the cursor vector (that is, maxVecBegin = cursorVecBegin and maxVecEnd = cursorVecEnd); • or, if the sum of the cursor vectors is less than or equal to zero, discard the cursor vector, after the starting position of the cursor vector is adjusted to the current Scanning position, namely, cursorVecBegin = cursorVecEnd + 1 (according to conclusion 2); • otherwise, the current cursor vector is retained, move the end position of the cursor vector to the next position (according to conclusion 1 ). We use the input vector example (31,-, 26,-, 58, 97,-93,-) given in the Problem description to verify the preceding calculation method. Table lattice (1) sets cursorVecEnd to list the value of cursorVecSum in each round of loop based on the baseline of the game, and the values of cursorVecBegin, cursorVecSum, maxVecBegin, maxVecEnd, and maxVecSum after the cycle. Table 1 after the loop, cursorVecEnd cursorVecSum cursorVecBegin cursorVecSum maxVecBegin maxVecEnd maxVecSum0 31 0 31 0 0 0 0 311-10 2 0 0 312 59 2 59 2 593 85 2 85 2 3 854 32 2 32 2 3 855 90 2 90 2 5 906 187 2 187 2 6 1877 94 2 94 2 6 1878 71 2 71 2 6 1879 155 2 155 2 6 187 according to the final result, and the largest continuous subvector is x [2... 6], total 187. This algorithm only scans the input vector once, and the complexity of this algorithm is O (n ). This is a linear algorithm and is already optimal. 3. problem cause: this problem occurs in a pattern recognition problem faced by Ulf Grenander of Brown University. The initial form of the problem is the following two-dimensional form. "Given the real number array of n × n, calculate the maximum sum of the rectangle sub-array ." In the two-dimensional form, the maximum sum subarray is the maximum likelihood estimator of a specific pattern in digital images. Because it takes too much time to solve a two-dimensional problem, Grenander simplifies it into a one-dimensional problem to gain a deep understanding of its structure. I just remembered this question recently. I was asked this two-dimensional question by an interviewer when I graduated to work for a job two years ago. At that time, I spoke about it in a random manner, and the interview was of course a tragedy. Find a free time and try again later.

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.