您的位置:首页 > 其它

如何理解算法的正确性

2017-05-20 19:50 671 查看

观点来自《算法导论》一书

一:循环不变式( Loop Invariants)

算法中经常用到循环还解决某些数量级很大的问题,借助数学的知识来判断算法的正确性。

We use loop invariants to help us understand why an algorithm is correct.

1. 初始化(Initialization)

循环之前,也就是第一次迭代之前,相关状态为真。

It is true prior to the first iteration of the loop.

2. 保持(Maintenance)

If it is true before an iteration of the loop, it remains true before the next iteration

3. 终止(Termination)

当前两个条件满足时,在循环终止时,这个不变式可以帮助我们判断一个算法的正确性。

When the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct.

二:循环不变式( Loop Invariants)实际上是用到了数学归纳法

数学归纳法:

1. 证明 n = 1 时的情况成立

2. 假设 n = m 情况成立

3. 依据第二个假设,证明 当 n = m + 1 时命题是否成立,得出结论。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 算法导论 数学