如何理解算法的正确性
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 时命题是否成立,得出结论。
相关文章推荐
- 如何写出正确的二分查找?——利用循环不变式理解二分查找及其变体的正确性以及构造方式
- 【算法】并查集如何理解。
- KMP 算法(1):如何理解 KMP 讲得特别好
- 如何证明算法的正确性?
- KMP 算法(1):如何理解 KMP
- 如何写出正确的二分查找?——利用循环不变式理解二分查找及其变体的正确性以及构造方式
- KMP 算法(1):如何理解 KMP
- KMP 算法(1):如何理解 KMP
- 如何写出正确的二分查找?——利用循环不变式理解二分查找及其变体的正确性以及构造方式
- 如何理解算法中的渐进符号?
- 算法笔记 (6)线性表如何理解
- KMP 算法(1):如何理解 KMP
- 对算法导论中用循环不变式证明算法正确性的理解
- 【SDN】Openflow协议中对LLDP算法的理解--如何判断非OF区域的存在
- 如何理解算法
- KMP 算法(1):如何理解 KMP
- 【基础算法】希尔排序-如何着手理解一个排序算法
- 关于FFT的一些理解,以及如何手工计算FFT加深理解和验证正确性
- 在不知道算法原理的情况下,如何阅读理解算法的matlab程序