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.

148 questions
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,572
  • 10
  • 42
  • 62
3
votes
1 answer

GCC not performing loop invariant code motion

I decided to check the result of loop invariant code motion optimization using g++. However, when I compiled the following code with -fmove-loop-invariants and analysed its assembly, I saw that k + 17 calculation is still performed in the loop…
LVK
  • 288
  • 4
  • 13
3
votes
1 answer

Understanding Loop Invariants in Java

I am learning loop invariants and I have been challenging myself with harder and harder questions. Today I solved a question, but I am unsure if my solution is correct. Can anyone please verify my answer, and please explain rather than just giving a…
Jean de Toit
  • 127
  • 9
3
votes
1 answer

What is the relationship between loop invariant and weakest precondition

Given a loop invariant, Wikipedia lists, a nice way to produce the weakest preconditions for a loop (from http://en.wikipedia.org/wiki/Predicate_transformer_semantics): wp(while E inv I do S, R) = I \wedge \forall y. ((E \wedge I) \implies…
3
votes
1 answer

Loop invariant proof

I am running into an issue coming up with the post-condition and showing partial correctness of this piece of code. { m = A ≥ 0 } x:=0; odd:=1; sum:=1; while sum<=m do x:=x+1; odd:=odd+2; sum:=sum+odd end while { Postcondition } I'm not…
Ross Verry
  • 115
  • 8
2
votes
0 answers

How does one pick the proper loop invariant to prove an algorithm's correctness?

We started with loop invariants last week and our professor proposed this question to work on at home. I've been following along the slides/lectures but I am having so much trouble with identifying a loop invariant which can help me prove the…
b0to
  • 21
  • 3
2
votes
1 answer

Optimal placement of assert statements to assure correctness using invariant

I'm trying to understand invariants in programming via real examples written in Python. I'm confused about where to place assert statements to check for invariants. My research has shown different patterns for where to check for invariants. For…
Robin Andrews
  • 3,514
  • 11
  • 43
  • 111
2
votes
1 answer

Why my loop invariant might not be preserved by any iteration?

I'm trying to write a code in Spark that computes the value of the polynomial using the Horner method. The variable Result is calculated by Horner, and the variable Z is calculated in the classical way. I've done a lot of different tests and my loop…
s7e0n5g1a
  • 47
  • 1
  • 7
2
votes
2 answers

How do I prove this algorithm's correctness?

My algorithm: Construct a new graph G' whereas for every vertex v in V, create two vertices v_0 and v_1 in G', and for every edge (u, v) in E, create two edges (u_0 , v_1) and (u_1,v_0) in G'. Run Dijkstra on G' starting at s_0. All paths in G'…
2
votes
2 answers

Find loop invariant of this function

I need to find the loop invariant of gcd (euclid algorithm) but i don't know from where to start or what to look int f(int x, int y) { while (true) { int m = x % y; if(m == 0) return y; x = y; y…
PHPCore
  • 121
  • 9
2
votes
1 answer

Dafny fails to prove max element in integer array

I'm trying to prove a simple program in Dafny that finds the maximum element of an integer array. Dafny succeeds in a few seconds proving the program below. When I remove the comments from the last two ensures specifications, Dafny fires error…
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
2
votes
1 answer

Loop Invariants with Breaks

I am trying to understand how loop invariants interact with breaks. CLRS 3e (pg19) describes a loop invariant as requiring that If it is true before an iteration of the loop, it remains true before the next iteration. So given the following…
frog
  • 165
  • 2
  • 9
2
votes
0 answers

Determining loop invariant for a recursive function

Assume you have a function that returns the difference in length between two arrays, however, it's not using the conventional methods to find length but rather a recursive function. Can you help me find a loop invariant? I understand the loop…
bobaxs31
  • 53
  • 4
2
votes
1 answer

Why does j = n + 1 in the termination loop invariant of the insertion sort algorithm?

I am currently reading chapter 2 of the TCRC Introduction to Algorithms 3rd edition textbook and I am reading the author's interpretation of the loop invariant of this algorithm. I understand the author's logic for both the initialization and the…
2
votes
1 answer

Exponential method in dafny: invariant might not be maintained

I started learning Dafny and I just learned invariants. I've got this code: function pot(m:int, n:nat): int { if n==0 then 1 else if n==1 then m else if m==0 then 0 else pot(m,n-1) * m } method Pot(m:int, n:nat) returns (x:int) ensures x ==…
Byakko
  • 33
  • 8
1
2
3
9 10