loop invariant 循环不变量

      看到《算法导论》第二章中的用于证明插入排序的loop invariant,不太理解,搜了一下找到一个解释:

      算法导论第二章中的原文是:We state these properties ofA[1 ‥ j -1] formally as a loop invariant其中举的例子是插入排序,每次循环从数组A中取出第j个元素插入有序区A[1 .. j-1],然后递增j。这样A[1 .. j-1]的有序性始终得到保持,这就是所谓的“循环不变”了。
这个概念主要用来检验算法的正确性。
原文如下:We use loop invariants to help us understand why an algorithm is correct. We must show three things about a loop invariant:
  • Initialization: It is true prior to the first iteration of the loop.
  • Maintenance: If it is true before an iteration of the loop, it remains true before the next iteration.
  • Termination: When the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct.

1. 初始化(循环第一次迭代之前)的时候,A[1 ‥ j -1]的“有序性”是成立的;
2. 在循环的每次迭代过程中,A[1 ‥ j -1]的“有序性”仍然保持;
3. 循环结束的时候,A[1 ‥ j -1]的“有序性”仍然成立。

另外,我个人认为应该翻译为“循环不变性”,“循环不变式”是一种误导。因为这是一种性质,一种属性(property),而不是表达式。

以上引自 http://bbs.chinaunix.net/thread-750480-1-1.html

      单就这个插入排序来说的话,这个解释确实不错,其实就是对原文的翻译分析。但是对这个loop invariant的作用还是觉得模模糊糊的,书中也说是类似归纳法的一个证明方法,只是最后一步不同而已。

     一直就很头痛有关证明的东西,一些数学公式什么的,很头大,这是另一个关于loop invariant的introduction:

http://www.cs.uofs.edu/~mccloske/courses/cmps144/invariants_lec.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值