Questions tagged [loop-invariant]

In formal program verification, loop invariants are expressed in formal predicate logic and used to prove properties of loops and, by extension, algorithms employing loops (usually correctness properties). A loop invariant should be true on entry into a loop and is guaranteed to remain true after every iteration of the loop.

In formal program verification, in particular in the Floyd-Hoare approach, loop invariants are expressed in formal predicate logic and used to prove properties of loops and, by extension, algorithms employing loops (usually correctness properties). A loop invariant should be true on entry into a loop and is guaranteed to remain true after every iteration of the loop. This means that on exit from the loop both the loop invariant and the loop termination condition can be guaranteed.

Because of the fundamental similarity of loops and recursive programs, proving partial correctness of loops with invariants is very similar to proving correctness of recursive programs via induction. In fact, the loop invariant is often the inductive property- the induction hypothesis- one has to prove of a recursive program that is equivalent to a given loop.

121 questions
288
votes
16 answers

What is a loop invariant?

I'm reading "Introduction to Algorithm" by CLRS. In chapter 2, the authors mention "loop invariants". What is a loop invariant?
Attilah
  • 16,382
  • 34
  • 132
  • 197
33
votes
7 answers

Loop invariant of linear search

As seen on Introduction to Algorithms (http://mitpress.mit.edu/algorithms), the exercise states the following: Input: Array A[1..n] and a value v Output: Index i, where A[i] = v or NIL if v does not found in A Write pseudocode for LINEAR-SEARCH, …
Clash
  • 4,485
  • 9
  • 42
  • 63
16
votes
5 answers

What is the best way of determining a loop invariant?

When using formal aspects to create some code is there a generic method of determining a loop invariant or will it be completely different depending on the problem?
filinep
  • 509
  • 2
  • 9
  • 17
15
votes
1 answer

Proving the 100 Prisoners and a lightbulb with Dafny

Consider the standard strategy to solve the 100 prisoners and a lightbulb problem. Here's my attempt to model it in Dafny: method strategy(P: set, Special: T) returns (count: int) requires |P| > 1 && Special in P ensures count == (|P| -…
Hugo Sereno Ferreira
  • 8,665
  • 6
  • 41
  • 88
10
votes
3 answers

Hoare Logic Loop Invariant

I'm looking at Hoare Logic and I'm having problems understanding the method of finding the loop invariant. Can someone explain the method used to calculate the loop invariant? And what should a loop invariant should contain to be a "useful" one? I'm…
nunopolonia
  • 13,237
  • 3
  • 24
  • 28
5
votes
1 answer

When to use low < high or low + 1 < high for loop invariant

I've read multiple articles including Jon Bentleys chapter on binary search. This is what I understand about CORRECT binary search logic and it works in the simple tests I did: binarysearch (arr, low, high, k) 1. while (low < high) 2.…
4
votes
1 answer

Loop fission/invariant optimization not performed, why?

I am trying to learn more about assembly and which optimizations compilers can and cannot do. I have a test piece of code for which I have some questions. See it in action here: https://godbolt.org/z/pRztTT, or check the code and assembly…
Tomas Creemers
  • 2,505
  • 11
  • 14
4
votes
1 answer

(Dafny) Adding elements of an array into another - loop invariant

I have a function sum that takes two arrays a and b as inputs and modifies b such that b[i] = a[0] + a[1] + ... + a[i]. I wrote this function and want to verify it with Dafny. However, Dafny tells me that my loop invariant might not be maintainted…
Dory
  • 129
  • 7
4
votes
2 answers

How to find a loop invariant

I know that the loop invariant is meant to prove the correctness of a problem but i can't quite understand how to come up with one, no matter how trivial the problem is. Here is an example, Can someone point out what are the step i should consider…
Bobby
  • 476
  • 5
  • 16
4
votes
1 answer

Can we prove correctness of algorithms using loop invariants in which we prove that it is true after the first iteration rather than before?

CLRS says that 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…
Garima
  • 53
  • 5
4
votes
3 answers

Explaining a loop invariant

This is a question from a past exam paper. Why does the loop invariant say i<=n when the loop test says i
nsc010
  • 385
  • 2
  • 5
  • 12
3
votes
2 answers

Loop Invariant for function to compute factorials

I'm having a hard time correctly identifying a loop invariant for the following function: F(y) X <-- 1 while (y > 1) do x <-- x * y y <-- y - 1 return (x) I've identified the loop invariant to be x = 1 OR x = y! as…
Brownbay
  • 4,880
  • 3
  • 21
  • 30
3
votes
1 answer

What loop invariants to use for an integer logarithm?

As I am having my first steps in C formal verification with Frama-C, I am trying to formally verify an integer binary logarithm function written as follows: //@ logic integer pow2(integer n) = (n == 0)? 1 : 2 * pow2(n - 1); /*@ requires n > 0; …
3
votes
1 answer

failure to optimize seemingly obvious loop invariant (but volatile qualifier did magic)

Godbolt Link: https://godbolt.org/g/Hv6MAL typedef int cell; cell y; const cell *phys_addr = (const cell*)0x12340; int main() { for (int i = 0; i < 20; i++) { for (int j = 0; j < 30; j++) { for (int k = 0; k < 50; k++) { …
Yashas
  • 964
  • 1
  • 9
  • 29
3
votes
3 answers

Using loop invariants to prove the correctness of heap sort

What are loop invariants and how do I use them to prove the correctness of the heap sort algorithm?
dawnoflife
  • 1,352
  • 8
  • 39
  • 58
1
2 3
8 9