Introduction to Algorithms Exercise 2.3-7

Today is the first day of the May 1st holiday. After the holiday, there is an exam. I originally planned to review the high numbers today, but unfortunately, I didn't seem to be in the right state today, so I started to read "Introduction to Algorithms".

Exercise 2.3-7: Design an algorithm that, given a set S of n integers and another given integer X, can determine in Θ(nlgn) time whether there are two elements in S such that Their sum is exactly X.

The time complexity of Θ(nlgn) makes it easy to think of using quicksort, merge sort and other Θ(nlgn) algorithms to sort first, because unordered sequences are too intractable. Then use two subscripts
i=0;
j=Array.length-1;
to narrow the judgment range by comparing the sum of the elements in the two positions each time. The pseudo code is as follows

checkSum(A,x)
{
    //进行升序排序,可以换成任意一个时间为Θ(nlgn)的排序算法
    mergeSort(A,A.length)
    i=0;
    j=A.length-1;
    while(i<j)
        if(A[i]+A[j]>x)
            j--;
        else
            if(A[i]+A[j]<x)
                i++;
            else
                return true;
    return false;
}

The following is a proof of the above algorithm using cyclic inequalities

  • Initialization: There may be two elements a, b in the given array A, such that a+b==x is true. After sorting, if a and b exist, the two are obviously in A[0...A.length-1]. Here, it is assumed that the array is sorted in ascending order. Let i=0 and j=A.length-1.
  • Keep: Before the first iteration (that is, just after the array is sorted), if a and b exist, then both are obviously in A[i...j]

    • If A[i] + A[j] > x, it means that at least one of A[i] and A[j] is not the a, b we are looking for. Since the arrays are arranged in ascending order, A[j] must be greater than or equal to A[i]. At this time, you should find an element smaller than A[j], that is, the previous element of A[j]. If a and b exist, there must be a and b in A[i...j-1], so the value of j is subtracted by one. If a and b exist at this time, then a and b must be in A[i...j]. .
    • If A[i] + A[j] < x, it means that at least one of A[i] and A[j] is not the a, b we are looking for. Since the arrays are arranged in ascending order, A[j] must be greater than or equal to A[i]. At this time, an element larger than A[i] should be found, that is, the next element of A[i]. If a and b exist, there must be a and b in A[i+1...j], so the value of i is added by one. If a and b exist at this time, then a and b must be in A[i...j] .
    • If A[i] + A[j] == x is true, it means that A[i] A[j] is the a, b we are looking for, and returns the true value directly.
    • It can be seen from the above process that if a and b exist before the first iteration, they must be in A[i...j-1] or have been found. At the end of each iteration, if a and b exist, they must also be in A[i...j] -1], or have been found. Each iteration narrows the lookup.
  • Termination: Each iteration will reduce the range where a and b are located. When i = j, there is only one element in the range A[i...j], which obviously does not meet the requirement that the sum is x, when i > j , there is no element in A[i...j] (here i is the starting point, j is the end point), obviously it does not meet the requirement that the sum is x, then directly return false to indicate that two elements that meet the requirements are not found.

Time complexity proof:

  • The time complexity of merge sort is stable Θ(nlgn)
  • The average and worst time complexity of the loop part of the subsequent 7~14 lines are Θ(n)
  • If the two algorithms are juxtaposed, the time complexity of the checkSum algorithm is stable Θ(nlgn)

Summary: Algorithm problems still need to think more and use tools to prove them, such as circular inequalities, mathematical tools or other methods.

Tucao: It's almost one o'clock in the morning, the ghost knows what time I can get up, I have to review and prepare for the mid-term exam. . . . . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325798551&siteId=291194637