As seen on Introduction to Algorithms (http://mitpress.mit.edu/algorithms), the exercise states the following:
Input: Array A[1...n]
Output: i, where A[i]=v or NIL when not found
Write a pseudocode for LINEAR-SEARCH, which scans through the sequence, looking for v. Using a loop invariant, prove that your algorithm is correct. (Make sure that your loop invariant fulfills the three necessary properties – initialization, maintenance, termination.)
I have no problem creating the algorithm, but what I don't get is how can I decide what's my loop invariant. I think I understood the concept of loop invariant, that is, a condition that is always true before the beginning of the loop, at the end/beginning of each iteration and still true when the loop ends. This is usually the goal, so for example, at insertion sort, itearting over j, starting at j=2, the [1, j-1] elements are always sorted. This makes sense for me. But for a linear search? I can't think of anything, it just sounds too simple to think of a loop invariant. Did I understand something wrong? I can only think of something obvious like (it's either NIL or between 0 and n). Thanks a lot in advance!
Assume that you have an array of length i, indexed from [0...i-1], and the algorithm is checking if v is present in this array. I'm not totally sure, but I think, the loop invariant is as follows: If j is the index of v, then [0..j-1] will be an array that does not have v.
Initialization : Before iterating from 0..i-1, the current array checked (none), does not consist of v.
Maintenance : On finding v at j, array from [0..j-1] will be an array without v.
Termination : As the loop terminates on finding v at j, [0..j-1] will be an array without j.
If the array itself does not have v, then j = i-1, and the above conditions will still hold true.
The LS algorithm that I wrote is-
I made my own assumptions for loop invariant for checking correctness of Linear Search..... Maybe its totally wrong so I need suggestions on my assumptions.
1) At Initialisation- at i = 0, we are searching for v at i = 0.
2) At successive iterations- we are looking for v till i < A.length-1
3) At termination- i = A.length and till A.length we kept looking for v.
The invariant for linear search is that every element before i is not equal to the search key. A reasonable invariant for binary search might be for a range [low, high), every element before low is less than the key and every element after high is greater or equal. Note that there are a few variations of binary search with slightly different invariants and properties - this is the invariant for a "lower bound" binary search which returns the lowest index of any element equal to or greater than the key.
Source:https://www.reddit.com/r/compsci/comments/wvyvs/what_is_a_loop_invariant_for_linear_search/
Seems correct to me.
After you have looked at index
i
, and not foundv
yet, what can you say aboutv
with regard to the part of the array beforei
and with regard to the part of the array afteri
?Loop invariant: at the start of the
i
th iteration of thefor
loop (lines 1–4),Initialization:
which is true, as any statement regarding the empty set is true (vacuous truth).
Maintenance: let's suppose the loop invariant is true at the start of the
i
th iteration of thefor
loop. IfA[i] == ν
, the current iteration is the final one (see the termination section), as line 3 is executed; otherwise, ifA[i] ≠ ν
, we havewhich means that the invariant loop will still be true at the start of the next iteration (the
i+1
th).Termination: the
for
loop may end for two reasons:return i
(line 3), ifA[i] == ν
;i == A.length + 1
(last test of thefor
loop), in which case we are at the beginning of theA.length + 1
th iteration, therefore the loop invariant isand the
NIL
value is returned (line 4).In both cases,
LINEAR-SEARCH
ends as expected.In the case of linear search, the loop variant will be the backing store used for saving the index(output) .
Lets name the backing store as index which is initially set to NIL.The loop variant should be in accordance with three conditions :
.