线性搜索的循环不变式

发布于 2024-10-31 07:53:44 字数 880 浏览 7 评论 0原文

正如算法简介 (http://mitpress.mit.edu/algorithms) 所示,该练习指出以下内容:

输入:数组A[1..n]和值v

输出:索引i,其中 A[i] = vNIL(如果在 A 中未找到 v) >

为线性搜索编写伪代码, 它扫描序列, 寻找 v。使用循环不变量, 证明你的算法是正确的。 (确保你的循环不变 满足三个必要的属性 – 初始化、维护、 终止。)

我创建算法没有问题,但我不明白的是我如何决定我的循环不变式是什么。我想我理解了循环不变的概念,即在循环开始之前、每次迭代结束/开始时始终为真的条件,并且在循环结束时仍然为真。这通常是目标,例如,在 插入排序< /a>,迭代 j,从 j = 2 开始,A[1..j-1] 元素始终已排序。这对我来说很有意义。但是对于线性搜索呢?我想不出任何东西,循环不变式听起来太简单了。我是不是理解错了什么?我只能想到一些明显的东西,比如(它要么是 NIL,要么是 0 到 n 之间)。预先非常感谢!

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

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

Output: Index i, where A[i] = v or NIL if v does not found in A

Write 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, iterating over j, starting at j = 2, the A[1..j-1] elements are always sorted. This makes sense to 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一笑百媚生 2024-11-07 07:53:44
LINEAR-SEARCH(A, ν)
1  for i = 1 to A.length
2      if A[i] == ν 
3          return i
4  return NIL 

循环不变式:for 循环的第 i 次迭代开始时(第 1-4 行),

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

初始化:

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

是真的,因为任何关于空集的陈述都是真的(空洞的真理)。

维护:假设循环不变式在 for 循环的第 i 次迭代开始时为 true。如果A[i] == ν,则当前迭代是最后一次(参见终止部分),因为第3行被执行;否则,如果 A[i] ≠ ν,

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

这意味着不变循环在下一次迭代开始时(第 i+1 次)仍然为真。

终止:for循环可能因两个原因而结束:

  1. 返回i(第3行),如果A[i] == ν;
  2. i == A.length + 1for 循环的最后一次测试),在这种情况下,我们位于 A.length 的开头+ 第 1 次迭代,因此循环不变式为

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

    并且返回NIL值(第4行)。

在这两种情况下,LINEAR-SEARCH 都会按预期结束。

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.

等待我真够勒 2024-11-07 07:53:44

在查看了索引 i 后,还没有找到 v,那么关于 v 之前数组的部分,您能说些什么呢? i 以及关于 i 之后的数组部分?

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?

情仇皆在手 2024-11-07 07:53:44

线性搜索的情况下,循环变体将是用于保存索引(输出)的后备存储。

让我们将后备存储命名为 index ,它最初设置为 NIL。循环变体应符合三个条件:

  • 初始化:此条件对于索引变量成立。因为,它包含 NIL,可以是第一次迭代之前的结果为 true。
  • 维护:索引将保持 NIL,直到找到项目 v。在迭代之前和下一次迭代之后也是如此。As,比较条件成功后将在循环内设置。
  • 终止:索引将包含 NIL 或项目 v 的数组索引

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.

.

爱的故事 2024-11-07 07:53:44

循环不变式将

永远是 0 <= i < k,其中k是循环迭代变量的当前值,
A[i] != v

循环终止时:

如果 A[k] == v,则循环终止并输出 k

if A[k] != v,且 k + 1 == n(列表的大小) then 循环以 nil 值终止

正确性证明:留作练习

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

A君 2024-11-07 07:53:44

假设您有一个长度为 i 的数组,索引范围为 [0...i-1],并且该算法正在检查 v 是否存在于该数组中。
我不完全确定,但我认为循环不变式如下:
如果 j 是 v 的索引,则 [0..j-1] 将是一个没有 v 的数组。

初始化:在从 0..i-1 迭代之前,检查当前数组(无),不包含 v。

维护:在 j 处找到 v 时,[0..j-1] 中的数组将是一个没有 v 的数组。

终止:当循环在 j 处找到 v 时终止,[0..j-1] 中的数组将是一个没有 v 的数组。 .j-1] 将是一个没有 j 的数组。

如果数组本身没有v,那么j = i-1,上面的条件仍然成立。

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.

讽刺将军 2024-11-07 07:53:44

线性搜索的不变量是 i 之前的每个元素都不等于搜索键。二分搜索的合理不变量可能是范围 [low, high),low 之前的每个元素都小于键,而 high 之后的每个元素都大于或等于。
请注意,二分搜索有一些变体,其不变量和属性略有不同 - 这是“下限”二分搜索的不变量,它返回等于或大于键的任何元素的最低索引。

资料来源:https://www.reddit.com/r/compsci/comments /wvyvs/what_is_a_loop_invariant_for_linear_search/

对我来说似乎是正确的。

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.

你的背包 2024-11-07 07:53:44

我编写的 LS 算法是 -

LINEARSEARCH(A, v)
  for i=0 to A.length-1
    if(A[i] == v)
      return i
  return NIL

我对循环不变式做出了自己的假设,以检查线性搜索的正确性:

  1. 在初始化时 - 在 i = 0 处,我们在 i = 0 处搜索 v。

  2. 在连续迭代中 - 我们在寻找 v 直到 i

    A.length-1

  3. 终止时 - i = A.length 直到 A.length 我们一直在寻找 v。

The LS algorithm that I wrote is-

LINEARSEARCH(A, v)
  for i=0 to A.length-1
    if(A[i] == v)
      return i
  return NIL

I made my own assumptions for loop invariant for checking correctness of Linear Search:

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文