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
0
votes
4 answers

How to find the loop invariant and prove correctness?

int i, temp; a is an array of integers [1...100] i = 1; while i < 100 if a[i] > a[i+1] temp = a[i] a[i] = a[i+1] a[i+1] = temp i = i+1 I'm having trouble understanding how to find loop invariants and writing…
atkayla
  • 8,143
  • 17
  • 72
  • 132
0
votes
1 answer

What's the loop invariant for this code?

I need to come up with a loop invariant for a given piece of code: //pre: x & y >= 0 //post: z = x^y //computes pow(x, y), x^y int pow(int x, int y){ int z = 1; while(y > 0){ if(y%2==0){ y /= 2; x = x*x; …
ceptno
  • 687
  • 2
  • 6
  • 28
0
votes
1 answer

What is the possible loop invariant

Could someone provide a possible loop invariant for the following simple algorithm: Input: A[0,...,n-1] and B[0,...,m-1], each might contain repeated elements Output: the first pair of (i,j) such that A[i] == B[j]. Algorithm: for i <- 0 to n-1 …
gongzhitaao
  • 6,566
  • 3
  • 36
  • 44
0
votes
1 answer

Loop Invariant functioning

I want to know about loop invariant. I came to know that in algorithms(mainly sorting algorithms) have a loop invariant and loop invariant indicates the correctness of an algorithm. How does this work? Can someone help me in understanding this?
user1658435
  • 574
  • 1
  • 9
  • 28
-1
votes
2 answers

Finding Loop Invariant Proof, Help Please?

So I have this assignment: I need help with question 2. I thought I knew how to do it, when I realized the factorials are being computed backwards. The algorithm is correct intuitively, but I can't seem to find a loop invariant that holds true…
-1
votes
1 answer

How to find loop invariant?

I was doing exercise about program verification, and I had some difficulties in finding this loop invariant: y = 0; while (y != x) { y = y + 1; } the precondition is x>=0 and the postcondition is x=y In the loop there is just one variable so I…
-1
votes
2 answers

Loop Invariant for Cut Rod Implementation CLRS

I was wondering what the loop invariant would be for the loop present in lines 4 - 6 of this code and how to prove it during intialization, mantience, and termination. def cut_rod(p, n): if n == 0: return 0 q = -inf for i = 1 to…
-1
votes
1 answer

Find loop invariant of this simple algorithm

I'm sure this is a really simple question but I can't seem to crack it. Prove the correctness of your algorithm; i.e. state its loop invariant and prove it by induction. Below is my algorithm. I know how to do the second part (prove by induction)…
-2
votes
1 answer

Loop invariants to figure the values of n, low, and high

so, I'm supposed to use the information provided above the loop to figure what the values of n, low, and high are going to be without the need to know what is actually happening inside the loop. Can someone explain how can I use loop invariant to…
-2
votes
2 answers

Loop invariant with no return value, is it possible? And program correctness

So I'm writing this program that store user input on linked list and reverse it, but I'm a bit lost. Is it possible to come up a loop invariants for types of loop where it doesn't return any value? For example a loop that is located inside…
Stupid
  • 15
  • 4
-4
votes
1 answer

simplifying THIS fibonacci method

so my work was to convert this method: long fib(long n) { long i = -1, a = 0, b = 1; while (++i != n) b = a + (a = b); return a; } into another iterative fibonacci method that still does the EXACT same just with the difference that the new one is…
EgZoShift
  • 29
  • 5
-4
votes
1 answer

I need to find the loop invariant for this program

I am very new to Algorithms and although i understand its very simple code im finding it difficult to find the loop invariant in the code below. Can someone please explicitly tell me what a good loop invariant for the code below is. Any help is much…
Chris
  • 1
  • 1
1 2 3
9
10