Loop invariant of linear search

前端 未结 7 1749
醉梦人生
醉梦人生 2021-01-31 09:16

As seen on Introduction to Algorithms (http://mitpress.mit.edu/algorithms), the exercise states the following:

Input: Array A[1..n] and a v

相关标签:
7条回答
  • 2021-01-31 09:22

    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 :

    • Initialization : This condition holds true for index variable.since, it contains NIL which could be a result outcome and true before the first iteration.
    • Maintenance : index will hold NIL until the item v is located. It is also true before the iteration and after the next iteration.As, it will be set inside the loop after comparison condition succeeds.
    • Termination : index will contain NIL or the array index of the item v.

    .

    0 讨论(0)
  • 2021-01-31 09:23
    LINEAR-SEARCH(A, ν)
    1  for i = 1 to A.length
    2      if A[i] == ν 
    3          return i
    4  return NIL 
    

    Loop invariant: at the start of the ith iteration of the for loop (lines 1–4),

    ∀ k ∈ [1, i) A[k] ≠ ν.  
    

    Initialization:

    i == 1 ⟹ [1, i) == Ø ⟹ ∀ k ∈ Ø A[k] ≠ ν,
    

    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 ith iteration of the for loop. If A[i] == ν, the current iteration is the final one (see the termination section), as line 3 is executed; otherwise, if A[i] ≠ ν, we have

    ∀ k ∈ [1, i) A[k] ≠ ν and A[i] ≠ ν ⟺ ∀ k ∈ [1, i+1) A[k] ≠ ν,
    

    which means that the invariant loop will still be true at the start of the next iteration (the i+1th).

    Termination: the for loop may end for two reasons:

    1. return i (line 3), if A[i] == ν;
    2. i == A.length + 1 (last test of the for loop), in which case we are at the beginning of the A.length + 1th iteration, therefore the loop invariant is

      ∀ k ∈ [1, A.length + 1) A[k] ≠ ν ⟺ ∀ k ∈ [1, A.length] A[k] ≠ ν
      

      and the NIL value is returned (line 4).

    In both cases, LINEAR-SEARCH ends as expected.

    0 讨论(0)
  • 2021-01-31 09:27

    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.

    0 讨论(0)
  • 2021-01-31 09:31

    Loop invariant would be

    forevery 0 <= i < k, where k is the current value of the loop iteration variable, A[i] != v

    On loop termination:

    if A[k] == v, then the loop terminates and outputs k

    if A[k] != v, and k + 1 == n (size of list) then loop terminates with value nil

    Proof of Correctness: left as an exercise

    0 讨论(0)
  • 2021-01-31 09:37

    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.

    0 讨论(0)
  • 2021-01-31 09:42

    After you have looked at index i, and not found v yet, what can you say about v with regard to the part of the array before i and with regard to the part of the array after i?

    0 讨论(0)
提交回复
热议问题