线性搜索的循环不变式
正如算法简介 (http://mitpress.mit.edu/algorithms) 所示,该练习指出以下内容:
输入:数组A[1..n]
和值v
输出:索引i
,其中 A[i] = v
或 NIL
(如果在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
循环不变式:在
for
循环的第i
次迭代开始时(第 1-4 行),初始化:
是真的,因为任何关于空集的陈述都是真的(空洞的真理)。
维护:假设循环不变式在
for
循环的第 i 次迭代开始时为 true。如果A[i] == ν
,则当前迭代是最后一次(参见终止部分),因为第3行被执行;否则,如果 A[i] ≠ ν,这意味着不变循环在下一次迭代开始时(第 i+1 次)仍然为真。
终止:
for
循环可能因两个原因而结束:返回i
(第3行),如果A[i] == ν
;i == A.length + 1
(for
循环的最后一次测试),在这种情况下,我们位于A.length 的开头+ 第 1 次迭代,因此循环不变式为
并且返回
NIL
值(第4行)。在这两种情况下,
LINEAR-SEARCH
都会按预期结束。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.在查看了索引
i
后,还没有找到v
,那么关于v
之前数组的部分,您能说些什么呢?i
以及关于i
之后的数组部分?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
?在线性搜索的情况下,循环变体将是用于保存索引(输出)的后备存储。
让我们将后备存储命名为 index ,它最初设置为 NIL。循环变体应符合三个条件:
。
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 :
.
循环不变式将
永远是 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
假设您有一个长度为 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.
线性搜索的不变量是 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.
我编写的 LS 算法是 -
我对循环不变式做出了自己的假设,以检查线性搜索的正确性:
在初始化时 - 在 i = 0 处,我们在 i = 0 处搜索 v。
在连续迭代中 - 我们在寻找 v 直到 i
A.length-1
终止时 - i = A.length 直到 A.length 我们一直在寻找 v。
The LS algorithm that I wrote is-
I made my own assumptions for loop invariant for checking correctness of Linear Search:
At Initialisation- at i = 0, we are searching for v at i = 0.
At successive iterations- we are looking for v till i < A.length-1
At termination- i = A.length and till A.length we kept looking for v.