Prove the correctness of the divide and conquer algorithm by using the loop without changing expression

Source: Internet
Author: User

The statement is not copied from a textbook, but is intended by the author. It is likely to be wrong. Please ignore your criticism. Do not use the content in this article when you answer the question. Otherwise, you will be charged for the result of 0 points. In addition, the syntax of the sample code in this article uses the pseudo-code syntax in introduction to algorithms.

Introduction to divide and conquer Law

The divide and conquer method is a recursive method for solving problems. It consists of three steps:
-Decomposition: divides the problem into several subproblems.
-Solution: solve each subproblem recursively until the subproblem is small enough to be solved directly.
-Merge: Merge the solutions of each subproblem into the solution of the entire problem.

For example, we want to implement a sum algorithm SUM (A, p, r ). The input of the algorithm is an array A with A length of n, and the START subscript p and end subscript r for summation. The algorithm returns the result. The pseudocode is as follows:

SUM (A, p, r)
1 if p ≥ r
2 then return A [p]
3 else return SUM (A, p, R-1) + A [r]

If A = [,], SUM (A,) returns 10.

The figure below gives you a more intuitive understanding of the running status of the algorithm.

Decomposition Process
[1, 2, 3, 4]

/\

[1, 2, 3] 4

/\

[1, 2] 3

/\

[1] 2

/

1

Merge Process
1 2

\/

3 3

\/

6 4

\/

10

Verify the correctness of the sharding Algorithm

The non-variant pattern can also be used to prove the correctness of the divide and conquer algorithm. But the specific operation is slightly different-we do not prove it from the first recursion, but from the first direct solution (that is, the last recursive call. The general process is
Initialization: After the function is directly solved, the cycle is not changed.
Persistence: it is assumed that all recursive calls within the function meet the cycle-changing pattern. After the function itself is returned, the cycle-changing pattern remains true.
End: After the function call of the "outermost layer" returns, the algorithm results must be correct.

It is proved that SUM (A, p, r) is correct.

As an example, it proves the correctness of the SUM () function.
First, let's give A non-variant loop: For an array A with A given length of n (n ≥ 1), for any integer p and r with A value of 1 ≤ p ≤ r ≤ n, the return value of SUM (A, p, r) is.
Initialization: When p = r, the SUM () function returns A [p] directly. In addition, when r = p, = A [p]. The cycle is not changed.
Keep: Assuming SUM (A, p, r) internally executes the recursive call SUM (A, p, R-1) to satisfy the cycle without variation, that is, SUM (A, p, R-1) the return value is. Since return SUM (A, p, R-1) + A [r] is executed in row 3rd, the return value of SUM (A, p, r) satisfies the cycle-changing formula.
Termination: When the SUM () function of the outermost layer returns, the return value must be yes, and the algorithm is correct.

A more complex example

This is the question 7-3 in introduction to algorithms and STOOGE sorting.

STOOGE-SORT (A, I, j)
1 if A [I]> A [j]
2 then exchange A [I] «A [j]
3 if I + 1 ≥ j
4 then return
5 k ¬ ë (j-I + 1)/3 û # rounded down
6 STOOGE-SORT (A, I, j-k) # previous 2/3
7 STOOGE-SORT (A, I + k, j) # post 2/3
8 STOOGE-SORT (A, I, j-k) # The first 2/3

This is a well-known sorting algorithm. It is not because of its speed. In fact, it is slower than insertion sorting. What's amazing about it is that the normal finger-breaking method cannot prove its correctness. Let's prove its correctness with a non-variant loop.

Loop unchanged: the array A [I. j] is ordered every time STOOGE-SORT (A, I, j) returns.

Initialization: When I + 1 is greater than or equal to j, recursion is not performed and the function returns immediately. Because 1st and 2 rows are executed, A [I] ≤ A [j] can be ensured. Because I + 1 = j, the array A [I. j] is ordered. The cycle is not changed.

Persistence: assume that all recursive calls within STOOGE-SORT (A, I, j) to STOOGE-SORT () meet the cycle unchanged, that is, STOOGE-SORT (A, I, j-k) enables A [I .. j-k] is ordered. STOOGE-SORT (A, I + k, j) enables A [I + k .. j] ordered.
Required k = bytes (j-I + 1)/3 bytes
∴ J-I + 1 ≥ 3 k
Round (j-I + 1)-2 k ≥ k

When A [I .. j-k] And A [I + k .. j] the overlapping part is A [I + k .. j-k], total (j-k)-(I + k) + 1 = (j-k + 1)-2 k ≥ k Elements
And A [I .. j-k] And A [I + k .. j] The non-overlapping part is A [j-k + 1 .. j] A total of j-(j-k + 1) + 1 = k elements. That is, the number of elements in the overlapping part of A [I. j-k] And A [I + k. j] is greater than or equal to the number of elements in the non-overlapping part.
Worker executes STOOGE-SORT (A, I, j-k) and STOOGE-SORT (A, I + k, j) in rows 6th and 7 ), make A [I .. j-k] And A [I + k .. j] After ordering, A [j-k + 1 .. the element in j] is A [I .. j] has the largest k elements and is ordered.
After the worker executes the STOOGE-SORT (A, I, j-k) of 8th rows, the sequence of A [I.. j-k] can be ensured.
In summary, A [I. j] is ordered after STOOGE-SORT (A, I, j) returns. The cycle is not changed.
 
Terminate: After the STOOGE-SORT (A, 1, n) of the outermost layer is returned, array A can be ordered and the algorithm is correct.

Certificate completion

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.