What is a loop invariant?

后端 未结 15 1277
后悔当初
后悔当初 2020-11-28 17:13

I\'m reading \"Introduction to Algorithm\" by CLRS. In chapter 2, the authors mention \"loop invariants\". What is a loop invariant?

相关标签:
15条回答
  • 2020-11-28 17:57

    There is one thing that many people don't realize right away when dealing with loops and invariants. They get confused between the loop invariant, and the loop conditional ( the condition which controls termination of the loop ).

    As people point out, the loop invariant must be true

    1. before the loop starts
    2. before each iteration of the loop
    3. after the loop terminates

    ( although it can temporarily be false during the body of the loop ). On the other hand the loop conditional must be false after the loop terminates, otherwise the loop would never terminate.

    Thus the loop invariant and the loop conditional must be different conditions.

    A good example of a complex loop invariant is for binary search.

    bsearch(type A[], type a) {
    start = 1, end = length(A)
    
        while ( start <= end ) {
            mid = floor(start + end / 2)
    
            if ( A[mid] == a ) return mid
            if ( A[mid] > a ) end = mid - 1
            if ( A[mid] < a ) start = mid + 1
    
        }
        return -1
    
    }
    

    So the loop conditional seems pretty straight forward - when start > end the loop terminates. But why is the loop correct? What is the loop invariant which proves it's correctness?

    The invariant is the logical statement:

    if ( A[mid] == a ) then ( start <= mid <= end )
    

    This statement is a logical tautology - it is always true in the context of the specific loop / algorithm we are trying to prove. And it provides useful information about the correctness of the loop after it terminates.

    If we return because we found the element in the array then the statement is clearly true, since if A[mid] == a then a is in the array and mid must be between start and end. And if the loop terminates because start > end then there can be no number such that start <= mid and mid <= end and therefore we know that the statement A[mid] == a must be false. However, as a result the overall logical statement is still true in the null sense. ( In logic the statement if ( false ) then ( something ) is always true. )

    Now what about what I said about the loop conditional necessarily being false when the loop terminates? It looks like when the element is found in the array then the loop conditional is true when the loop terminates!? It's actually not, because the implied loop conditional is really while ( A[mid] != a && start <= end ) but we shorten the actual test since the first part is implied. This conditional is clearly false after the loop regardless of how the loop terminates.

    0 讨论(0)
  • 2020-11-28 17:57

    It should be noted that a Loop Invariant can help in the design of iterative algorithms when considered an assertion that expresses important relationships among the variables that must be true at the start of every iteration and when the loop terminates. If this holds, the computation is on the road to effectiveness. If false, then the algorithm has failed.

    0 讨论(0)
  • 2020-11-28 17:57

    In simple words, it is a LOOP condition that is true in every loop iteration:

    for(int i=0; i<10; i++)
    { }
    

    In this we can say state of i is i<10 and i>=0

    0 讨论(0)
提交回复
热议问题