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

What is the loop invariant for this code?

An array contains integers that first increase in value and then decrease in value. It is unknown at which point the numbers start to decrease. Write efficient code to copy the numbers in the first array to another array so that the second array is…
Abhiram
  • 305
  • 1
  • 7
0
votes
1 answer

Insertion Sort Algorithm In place and loop variant

Part 1 I know that QuickSort can be used 'in place' but could someone explain to me how Insertion sort Algorithm does this using 'in place'. From my understanding: Insertion Sort starts at the first value and compares it to the next value, if that…
TheRapture87
  • 1,205
  • 2
  • 14
  • 30
0
votes
2 answers

Loop invariant (java)

I have the following code to reverse the digits in an integer: public class integerReversal { public static int reverseNum(int number){ int reversed = 0; int remainder; //{I: ; B: number > 0} while (number >…
user2049004
  • 157
  • 1
  • 9
0
votes
1 answer

Working out an invariant for a binary search

I need hep working out what the invariant for this code would be. I think there can be multiple invariants but i don't really understand the topic and online resources i have found still dont really help. The code i made for the binary search…
0
votes
2 answers

Loop invariant for this code?

I've read about loop invariants, but I'm a little confused. Let's say I have this code, what would the invariant be? Something like A+B =X ? public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long A =…
carlos
  • 5
  • 2
0
votes
3 answers

Determining the loop invariant?

I don't quite understand how to determine the loop invariant. I understand that its something that is true before a loop, after a loop, and during each loops iteration, but thats about it. Here is an example problem I'm working on, how would you…
0
votes
2 answers

How to derive the loop invariant?

Given the following code fragment, where x is a number. { y >= 0 } z = 0 n = y while (n > 0) begin z = z + x n = n – 1 end What does it compute? Prove it, showing how you derive the loop invariant. How can I do that please?
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
  • 6,193
  • 11
  • 53
  • 99
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
  • 677
  • 2
  • 6
  • 25
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,073
  • 3
  • 32
  • 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
  • 544
  • 1
  • 9
  • 27
-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

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
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
  • 37
  • 5
1 2 3
8
9