I'm reading "Introduction to Algorithm" by CLRS. In chapter 2, the authors mention "loop invariants". What is a loop invariant?
相关问题
- Finding k smallest elements in a min heap - worst-
- binary search tree path list
- High cost encryption but less cost decryption
- How to get a fixed number of evenly spaced points
- Space complexity of validation of a binary search
相关文章
- What are the problems associated to Best First Sea
- Coin change DP solution to keep track of coins
- Algorithm for partially filling a polygonal mesh
- Robust polygon normal calculation
- Algorithm for maximizing coverage of rectangular a
- How to measure complexity of a string?
- Select unique/deduplication in SSE/AVX
- How to smooth the blocks of a 3D voxel world?
In simple words, it is a LOOP condition that is true in every loop iteration:
In this we can say state of i is
i<10 and i>=0
I like this very simple definition: (source)
By itself, a loop invariant doesn't do much. However, given an appropriate invariant, it can be used to help prove the correctness of an algorithm. The simple example in CLRS probably has to do with sorting. For example, let your loop invariant be something like, at the start of the loop, the first
i
entries of this array are sorted. If you can prove that this is indeed a loop invariant (i.e. that it holds before and after every loop iteration), you can use this to prove the correctness of a sorting algorithm: at the termination of the loop, the loop invariant is still satisfied, and the counteri
is the length of the array. Therefore, the firsti
entries are sorted means the entire array is sorted.An even simpler example: Loops Invariants, Correctness, and Program Derivation.
The way I understand a loop invariant is as a systematic, formal tool to reason about programs. We make a single statement that we focus on proving true, and we call it the loop invariant. This organizes our logic. While we can just as well argue informally about the correctness of some algorithm, using a loop invariant forces us to think very carefully and ensures our reasoning is airtight.
Previous answers have defined a Loop Invariant in a very good way.
Now let me try to explain how authors of CLRS used Loop Invariants to prove correctness of Insertion Sort.
Insertion Sort algorithm(as given in Book):
Loop Invariant in this case (Source: CLRS book): Subarray[1 to j-1] is always sorted.
Now let us check this and prove that algorithm is correct.
Initialization: Before the first iteration j=2. So Subarray [1:1] is the array to be tested.As it has only one element so it is sorted.Thus Invariant is satisfied.
Maintanence: This can be easily verified by checking the invariant after each iteration.In this case it is satisfied.
Termination: This is the step where we will prove the correctness of algorithm.
When the loop terminates then value of j=n+1. Again Loop invariant is satisfied.This means that Subarray[1 to n] should be sorted.
This is what we want to do with our Algorithm.Thus our Algorithm is correct.
A loop invariant is an assertion that is true before and after loop execution.
Beside all of the good answers, I guess a great example from How to Think About Algorithms, by Jeff Edmonds can illustrate the concept very well:
Invariant in this case means a condition that must be true at a certain point in every loop iteration.
In contract programming, an invariant is a condition that must be true (by contract) before and after any public method is called.