Exercise 2.3-7: design an algorithm that, for a given set of n integers and another given integer x, the algorithm can determine in Time whether there are two elements in S, making their and just x.
problem-solving ideas: The first thing to think about is to use a sort algorithm to sort the elements in S. There are two ways to deal with this, and the first idea is to traverse all the elements a in the already ordered S and use
two points The lookup method finds the X-a in S, and if it can be found, then it is true that there are two elements in S and for X, the algorithm terminates. This approach is clearly a requirement of satisfaction; the second way of thinking is my own
Come up with an algorithm, this algorithm is also very simple, but its correctness is not very good proof.
Idea 1:
CheckSum (A[1...N], X) 1 mergesort (A)//small to large sort 2 for i = 1 to n 3 if BinarySearch (A[1...N], x-a[i])!=–1//Assuming BinarySearch returns when the specified element is not found-1 4 return True 5 return False |
Idea 2:
CheckSum (A[1...N], X) 1 mergesort (A)//small to large sort 2 i = 1 3 J = N 4 while (I < j) 5 if (A[i] + a[j] = = X) 6 return True 7 else if (A[i] + a[j] > X) 8 j-- 9 else if (A[i] + a[j] < X) Ten i++ return False |
The correctness of the algorithm proves:
The correctness of idea 1 is obvious, and the correctness of the idea 2 is not so intuitive, and it may be confusing to find out whether the idea of 2 will miss something. The following is the beginning of the proof of idea 2,
But because I am also a beginner, prove the process is not very rigorous and normative.
First, assuming that the "no" in S contains two elements of X, then the test condition of line 5th of Idea 2 will never come true, then the final algorithm must return false. Therefore, to prove the correctness of idea 2
Converted into proof:
If there are two elements a and B in S, which makes a+b==x, then the algorithm must return true. (*)
In order to prove (*) is established, the following first proves that the idea 2 algorithm satisfies the following characteristics in the execution process:
If there are two elements a and B in S, making a+b==x (a<=b), then in the algorithm execution process sink of idea 2, before each iteration begins (that is, before the 4th line of the algorithm executes), A[I...J] contains A and B. (#)
The following inductive method is used to prove the establishment of the (#): (To make IK,JK, respectively, the K-round iteration before the beginning of the first and the value of I and J, A[IK...JK] to indicate the beginning of the K-round iteration A[I...J])
Prove: 1, the 1th round of iterations before the beginning, i1=1,j1=n. A[IK...JK] i.e. A[1...n],a and B are clearly contained in A[1...N], the conclusion is established. 2. A[ik...jk] contains A and B before the start of the K-round iteration. Follow the steps of the algorithm: (1) If a[ik]+a[jk]==x, the algorithm returns TRUE, the algorithm terminates. (2) ifA[IK]+A[JK] > X, the algorithm makes the JK value minus 1, that is, before the start of the k+1 round iteration, ik+1 = Ik,jk+1 = jk-1. The following evidence Ming A[JK] cannot be any of a or B: (2.1) Because of a<b, andA[IK...JK] contains a and b, so A[JK] cannot be a. (2.2) Suppose a[jk] equals B, because a[] is from small to large sort, so, there must be: A[JK]+A[JK-1] >A[jk]+a[jk-2] > B+a[jk-1] > B+a[jk-2] > B+a[jk-3] > ... > b+a[ik+1] >b+a[ik] > X & nbsp; that is, a cannot be any of the elements in A[ik...jk-1], and the premise: A[IK...JK] contains A and b contradictions, so false Wrong settingError, so A[JK] is not B. Because A[IK...JK] contains a and b, and has proved that A[JK] is not a or B, and ik+1 = ik,jk+1 = jk-1 , so, in the section K + Before 1-round iterations begin, a[ik+1...jk+1] must contain A and B. (3) ifA[IK]+A[JK] < X, the same can be k+1 before the beginning of the iteration a[ik+1,jk+1] must contain A and B. 3, from 1, and 2, know, each iteration before the beginning, A[I...J] contains A and B. |
Now (#) has been proved, and the (#) card (*) is very intuitive. Because A[I...J] always contains a and b, and the size of each iteration A[I...J] is small, the worst case scenario is that the iteration is performed to I+1=j
time, because A[i,j] contains A and B, so a[i] must be a,a[j] must be B, the algorithm detects a[i]+a[j] = a+b=x, the algorithm returns TRUE.
Summary: the above is the whole content, theoretically speaking 2 should be faster than the idea 1 (although they are all ). But obviously, the correctness of idea 1 is more intuitive.
Introduction to Algorithms 2.3-7 checks whether there are two numbers in the collection and the x--algorithm and proofs for the specified