Prove the correctness of Binary Search using a loop invariant (which you should prove by induction). Assume that a is sorted, and that n is the number of elements in a.
int BinarySearch(int *a, int n, int x) {
int L = 0, r = n-1;
while (L <= r) {
int m = (L+r)/2;
if (a[m] == x) return m;
if (a[m] < x) L = m+1;
else r = m-1;
}
return -1; }
Assistant
To prove the correctness of the binary search algorithm, we will use a loop invariant. A loop invariant is a condition that is true before and after every iteration of the loop. To correctly prove the algorithm, we need to identify our loop invariant and show that:
User
Compute the Θ-runtime for the following program, where max(A, i, j) takes Θ(j − i + 1) time.
for(int j = 0; j < n; j++)
for(int i = 0; i <= j; i++)
cout << max(A, i, j) << endl;
Assistant
To compute the Θ-runtime of the given program, we analyze the number of times the operation max(A, i, j) is called and the time complexity of each call.
Given that max(A, i, j) takes Θ(j − i + 1) time, let's evaluate how this translates over the nested loops.
For each j from 0 to n-1 (outer loop), i goes from to (inner loop). This means that for each value, the function is called times, where takes all values in .
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Initialization: The invariant holds true before the first iteration of the loop.
Maintenance: If the invariant holds at the beginning of an iteration, it remains true at the end of that iteration.
Termination: Upon loop termination, the invariant helps us show that the algorithm is correct.
Loop Invariant for Binary Search
For our binary search algorithm, the loop invariant can be stated as:
"At the start of each iteration of the loop, the subarray a[L..r] contains all the instances of x present in the array. If x is not in the array, a[L..r] will shrink until L > r."
Proof
Initialization: Before the first iteration, L is 0 and r is n-1, which means a[L..r] includes the entire array. This satisfies our invariant because if x is in the array, it is certainly within this range.
Maintenance: To prove this by induction, we need to show that the loop invariant holds true for the next iteration assuming it is true for the current iteration.
If a[m] == x, we return m, and the search is complete.
If a[m] < x, we set L = m+1. Since the array is sorted, all elements before m are less than x and don't need to be considered, preserving the invariant.
If a[m] > x, we set r = m-1. Similarly, all elements after m are greater than x and excluding them keeps the invariant true.
Termination: The loop terminates when L > r. According to our invariant, if x is in the array, it must be within a[L..r]. When L > r, the range a[L..r] is empty, meaning we have exhaustively searched all possible locations where x could have been, thus x is not present, and we correctly return -1.
Conclusion
By demonstrating the initialization, maintenance, and termination of our loop invariant, we have proven that the binary search algorithm as provided will correctly return the index of an element x in a sorted array if present, and -1 if x is not in the array.
0
j
j
max(A, i, j)
(j - i + 1)
i
[0, j]
We accumulate the time complexity across all iterations like so:
When j=0, there is 1 (= j - 0 + 1) call to max.
When j=1, there are 2 (= j - 0 + 1) + 1 (= j - 1 + 1) calls to max.
...
When j=n-1, the pattern continues until n.
Let's compute the total complexity:
The total time T(n) for all calls is the sum of the times for each j:
[T(n) = \sum_{j=0}^{n-1} \sum_{i=0}^{j} \Theta(j - i + 1)]
Since the time complexity of max(A, i, j) can also be seen as linear with respect to its range, we can consider simplifying the inside sum:
[T(n) = \Theta(\sum_{j=0}^{n-1} \sum_{i=0}^{j} (j - i + 1))]
Using known formulas for sum of first n-1 natural numbers (\sum_{j=0}^{n-1} j = \frac{n(n-1)}{2}) and the sum of the squares (\sum_{j=0}^{n-1} j^2 = \frac{n(n-1)(2n-1)}{6}), we get: