0% found this document useful (0 votes)
5 views

Proof of Correctness - Search Algorithms

Uploaded by

Satvik Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Proof of Correctness - Search Algorithms

Uploaded by

Satvik Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Proof of Correctness - Search Algorithms

Dr Upendra Pratap Singh


September 2024

1 Linear Search
To prove the correctness of the linear search algorithm using loop invariants,
we need to establish the loop invariant and demonstrate its correctness through
initialization, maintenance, and termination.

Problem Statement

Given an array A of size n and a target value x, the goal of the linear search is
to determine whether x is present in A and, if so, return its index; otherwise,
return −1.

Loop Invariant

At the start of each iteration of the loop, for every index k such that 0 ≤ k < i,
A[k] ̸= x (i.e., the target x is not found in the portion of the array from index
0 to (i − 1).

Proof Using Loop Invariants

Initialization

The loop invariant must be true before the first iteration of the loop. At the
beginning of the loop (when i = 0), no indices have been checked yet. Thus,
the portion of the array from index 0 to (i − 1) is an empty set, and by default,
x cannot be in this (empty) portion. Therefore, the loop invariant holds before
the first iteration.

Maintenance

We must show that if the loop invariant holds at the start of the i − th it-
eration, it also holds at the start of the (i + 1) − th iteration.

1. Assume that at the start of iteration i, the loop invariant holds, meaning
that for all k such that 0 ≤ k < i, A[k] ̸= x.

1
2. During the i − th iteration, the algorithm checks whether A[i] = x:
(a) If A[i] = x, the search is complete, and the algorithm returns i, so
the loop terminates.
(b) If A[i] ̸= x, the loop invariant still holds because the portion of the
array from index 0 to i now satisfies the condition that A[k] ̸= x for
all k such that 0 ≤ k ≤ i.

Thus, the loop invariant is maintained after each iteration.

Termination

The loop terminates when either:


1. The algorithm finds x at some index i, in which case it returns i. At this
point, the algorithm is correct because it has found the target value.
2. The algorithm reaches the end of the array without finding x. This hap-
pens when i = n. By the loop invariant, for every index k such that
0 ≤ k < n, A[k] ̸= x. Therefore, x is not in the array, and the algorithm
correctly returns −1.

Conclusion

Since the loop invariant holds at initialization, is maintained during each it-
eration, and leads to a correct conclusion at termination, we have proven that
the linear search algorithm is correct by using loop invariants.

2 Binary Search
To prove the correctness of the binary search algorithm using loop invariants, we
follow the process of defining a loop invariant and proving that it holds through
initialization, maintenance, and termination.

Problem Statement

Given a sorted array A of size n and a target value x, the goal of binary search
is to determine whether x is present in A. If x is present, the algorithm returns
its index; otherwise, it returns −1.

Loop Invariant

At the start of each iteration of the loop, if x is in the array, then x is in


the subarray A[low...high], where low and high represent the current bounds
of the search space.

Proof Using Loop Invariants

2
Initialization

The loop invariant must hold before the first iteration of the loop. At the
beginning (before the first iteration), low = 0 and high = n − 1, meaning that
the entire array A[0...n − 1] is the search space. If x is in the array, it must be
in this range.

Thus, the loop invariant holds true at initialization.


Maintenance

We must show that if the loop invariant holds at the start of the i − th it-
eration, it also holds at the start of the (i + 1) − th iteration.
1. Assume that the loop invariant holds at the start of the i − th iteration.
That is, if x is in the array, it is in the subarray A[low...high].
2. In the i − th iteration, we calculate the midpoint mid = (low + high)/2.
Then, three cases arise:
(a) Case 1: A[mid] = x. The algorithm has found x, so it returns mid.
The search is correct, and the loop terminates.
(b) Case 2: A[mid] < x. Since the array is sorted, x can only be in the
right half of the current subarray, i.e., in the range A[mid + 1...high].
Therefore, we set low = mid + 1, and the loop invariant still holds
because if x is present, it must be in the updated search space.
(c) Case 3: A[mid] > x. Similarly, since the array is sorted, x can
only be in the left half of the current subarray, i.e., in the range
A[low...mid − 1]. Therefore, we set high = mid − 1, and the loop
invariant still holds because if x is present, it must be in the updated
search space.
Thus, the loop invariant is maintained after each iteration.
Termination

The loop terminates when low > high. At this point, the search space becomes
empty because low exceeds high. By the loop invariant, if x were in the array,
it would have to be in the subarray A[low...high]. However, since the search
space is empty, x cannot be in the array, and the algorithm correctly returns −1.

Alternatively, the loop could terminate if A[mid] = x during one of the


iterations, in which case the algorithm correctly returns the index mid.
Conclusion

Since the loop invariant holds at initialization, is maintained during each it-
eration, and leads to a correct conclusion at termination, we have proven that
the binary search algorithm is correct by using loop invariants.

You might also like