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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章