通过循环不变式(归纳法)证明正确性

发布于 2025-01-08 15:48:18 字数 653 浏览 0 评论 0原文

我编写了自己的简单小函数(为了方便起见,使用 php),并希望有人可以帮助通过归纳法构建证明,这样我就可以掌握它的基本技巧。

function add_numbers($max) {
  //assume max >= 2
  $index=1;
  $array=array(0);
  while ($index != $max) {
     //invariant: ∀ k:1 .. index-1, array[k]=array[k-1]+1
     $array[$index] = $array[$index-1]+1;
     $index += 1;
  }
}

结果是每个索引处的值与索引本身相同,尽管只是因为 a[0] 被初始化为 0。

我相信目标是(或应该是)证明不变式(这本身可能是可疑的) ,但希望能明白这一点)对于 k+1 成立。

谢谢

编辑:示例: http://homepages.ius.edu/ rwisman/C455/html/notes/Chapter2/LoopInvariantProof.htm

I wrote my own trivial little function (php for convenience) and was hoping someone could help structure a proof by induction for it, just so I can get a very basic hang of it.

function add_numbers($max) {
  //assume max >= 2
  $index=1;
  $array=array(0);
  while ($index != $max) {
     //invariant: ∀ k:1 .. index-1, array[k]=array[k-1]+1
     $array[$index] = $array[$index-1]+1;
     $index += 1;
  }
}

The result being that the value at each index is the same as the index itself, though only because a[0] was initialized to 0.

I believe the goal is (or should be) to prove that the invariant (which may itself be suspect, but hopefully gets the point across) holds for k+1.

Thanks

edit: examples: http://homepages.ius.edu/rwisman/C455/html/notes/Chapter2/LoopInvariantProof.htm

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

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

发布评论

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

评论(1

得不到的就毁灭 2025-01-15 15:48:18

也许是这样的,尽管这有点迂腐。

不变:当索引 = n 时,对于 n >= 1(在循环顶部检查条件),array[i] = i 对于 0 <= i <名词

证明:证明是通过归纳法。在基本情况 n = 1 中,循环第一次检查条件,主体尚未执行,并且我们从代码的早期部分得到了 array[0] = 0 的外部保证。假设该不变量对于所有 n 到 k 都成立。对于 k + 1,我们分配 array[k] = array[k-1] + 1。根据归纳假设,array[k-1] = k-1,因此分配给 array[k] 的值为 (k-1 )+1 = k。因此,该不变量适用于 n 的下一个值,并且通过归纳,每个值都成立(在循环的顶部)。

编辑:

function add_numbers($max) {
  //assume max >= 2
  $index=1;
  $array=array(63);
  while ($index != $max) {
     //invariant: ∀ k:1 .. index-1, array[k]=array[k-1]+1
     $array[$index] = $array[$index-1]+1;
     $index += 1;
  }
}

不变:当索引 = n 时,对于 n >= 1 (在检查条件的循环顶部), array[i] = i + 63 for 0 <= i < <名词

证明:证明是通过归纳法。在基本情况 n = 1 中,循环第一次检查条件,主体尚未执行,并且我们从代码的早期部分得到了 array[0] = 63 的外部保证。假设该不变量对于所有 n 到 k 都成立。对于 k + 1,我们赋值 array[k] = array[k-1] + 1。根据归纳假设,array[k-1] = (k-1) + 63 = k + 62,所以赋给 array 的值[k]为(k+62)+1=k+63。因此,该不变量适用于 n 的下一个值,并且通过归纳,每个值都成立(在循环的顶部)。

Something like this, maybe, although this is a little pedantic.

Invariant: when index = n, for n >= 1 (at the top of the loop where it checks the condition), array[i] = i for 0 <= i < n.

Proof: The proof is by induction. In the base case n = 1, the loop is checking the condition for the first time, the body has not executed, and we have an outside guarantee that array[0] = 0, from earlier in the code. Assume the invariant holds for all n up to k. For k + 1, we assign array[k] = array[k-1] + 1. From the induction hypothesis, array[k-1] = k-1, so the value assigned array[k] is (k-1)+1 = k. Thus the invariant holds for the next, and by induction every, value of n (at the top of the loop).

EDIT:

function add_numbers($max) {
  //assume max >= 2
  $index=1;
  $array=array(63);
  while ($index != $max) {
     //invariant: ∀ k:1 .. index-1, array[k]=array[k-1]+1
     $array[$index] = $array[$index-1]+1;
     $index += 1;
  }
}

Invariant: when index = n, for n >= 1 (at the top of the loop where it checks the condition), array[i] = i + 63 for 0 <= i < n.

Proof: The proof is by induction. In the base case n = 1, the loop is checking the condition for the first time, the body has not executed, and we have an outside guarantee that array[0] = 63, from earlier in the code. Assume the invariant holds for all n up to k. For k + 1, we assign array[k] = array[k-1] + 1. From the induction hypothesis, array[k-1] = (k-1) + 63 = k + 62, so the value assigned array[k] is (k+62)+1 = k+63. Thus the invariant holds for the next, and by induction every, value of n (at the top of the loop).

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