What is a loop invariant?

后端 未结 15 1275
后悔当初
后悔当初 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:32

    In simple words, a loop invariant is some predicate (condition) that holds for every iteration of the loop. For example, let's look at a simple for loop that looks like this:

    int j = 9;
    for(int i=0; i<10; i++)  
      j--;
    

    In this example it is true (for every iteration) that i + j == 9. A weaker invariant that is also true is that i >= 0 && i <= 10.

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

    In Linear Search (as per exercise given in book), we need to find value V in given array.

    Its simple as scanning the array from 0 <= k < length and comparing each element. If V found, or if scanning reaches length of array, just terminate the loop.

    As per my understanding in above problem-

    Loop Invariants(Initialization): V is not found in k - 1 iteration. Very first iteration, this would be -1 hence we can say V not found at position -1

    Maintainance: In next iteration,V not found in k-1 holds true

    Terminatation: If V found in k position or k reaches the length of the array, terminate the loop.

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

    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:

    EXAMPLE 1.2.1 "The Find-Max Two-Finger Algorithm"

    1) Specifications: An input instance consists of a list L(1..n) of elements. The output consists of an index i such that L(i) has maximum value. If there are multiple entries with this same value, then any one of them is returned.

    2) Basic Steps: You decide on the two-finger method. Your right finger runs down the list.

    3) Measure of Progress: The measure of progress is how far along the list your right finger is.

    4) The Loop Invariant: The loop invariant states that your left finger points to one of the largest entries encountered so far by your right finger.

    5) Main Steps: Each iteration, you move your right finger down one entry in the list. If your right finger is now pointing at an entry that is larger then the left finger’s entry, then move your left finger to be with your right finger.

    6) Make Progress: You make progress because your right finger moves one entry.

    7) Maintain Loop Invariant: You know that the loop invariant has been maintained as follows. For each step, the new left finger element is Max(old left finger element, new element). By the loop invariant, this is Max(Max(shorter list), new element). Mathe- matically, this is Max(longer list).

    8) Establishing the Loop Invariant: You initially establish the loop invariant by point- ing both fingers to the first element.

    9) Exit Condition: You are done when your right finger has finished traversing the list.

    10) Ending: In the end, we know the problem is solved as follows. By the exit condi- tion, your right finger has encountered all of the entries. By the loop invariant, your left finger points at the maximum of these. Return this entry.

    11) Termination and Running Time: The time required is some constant times the length of the list.

    12) Special Cases: Check what happens when there are multiple entries with the same value or when n = 0 or n = 1.

    13) Coding and Implementation Details: ...

    14) Formal Proof: The correctness of the algorithm follows from the above steps.

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

    A loop invariant is an assertion that is true before and after loop execution.

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

    The Loop Invariant Property is a condition that holds for every step of a loops execution (ie. for loops, while loops, etc.)

    This is essential to a Loop Invariant Proof, where one is able to show that an algorithm executes correctly if at every step of its execution this loop invariant property holds.

    For an algorithm to be correct, the Loop Invariant must hold at:

    Initialization (the beginning)

    Maintenance (each step after)

    Termination (when it's finished)

    This is used to evaluate a bunch of things, but the best example is greedy algorithms for weighted graph traversal. For a greedy algorithm to yield an optimal solution (a path across the graph), it must reach connect all nodes in the lowest weight path possible.

    Thus, the loop invariant property is that the path taken has the least weight. At the beginning we haven't added any edges, so this property is true (it isn't false, in this case). At each step, we follow the lowest weight edge (the greedy step), so again we're taking the lowest weight path. At the end, we have found the lowest weighted path, so our property is also true.

    If an algorithm doesn't do this, we can prove that it isn't optimal.

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

    The meaning of invariant is never change

    Here the loop invariant means "The change which happen to variable in the loop(increment or decrement) is not changing the loop condition i.e the condition is satisfying " so that the loop invariant concept has came

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