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
1 answer

Efficiency of member function returning std::vector.size() in object oriented framework

I have a class declared in prob.h thus: struct A_s{ int a, b; } class A_c{ private: std::vector vec_of_A_s; public: int vec_of_A_s_size() const{return static_cast(vec_of_A_s.size());} } With A_c A;//A is an…
Tryer
  • 3,580
  • 1
  • 26
  • 49
0
votes
1 answer

How to verify loop with z3?

I am a beginner user of Z3. How do I verify a loop (C code) with a loop invariant in Z3? For ex: int a[10],i; for(i = 0; i<10; i++) { a[i] = 0; }
0
votes
1 answer

How would I find the loop invariant for this small body of code?

Pre-condition: list of ints where len(list) >= 2 Post-condition: return the second smallest value. If there exists two smallest values in list, return smallest. def SecondSmallest(list): 1 smallest = min(list[0], list[1]) 2 second_smallest =…
AAAgradebeef
  • 55
  • 1
  • 2
  • 6
0
votes
1 answer

Dafny verifies insertion sort using swap

I'm working on how to use dafny to verify an insertion sort using "swap" adjacent elements but I can't find a reasonable invariant for the while loop, can anyone help me fix it? Here is the link: http://rise4fun.com/Dafny/wmYME
Lilac Liu
  • 49
  • 1
  • 4
0
votes
1 answer

The thing that doesnt change in the algorithm

k :=0 for i ←1 to n c←a[i] k←k+1 this is the algorithim to know the number of elements
0
votes
0 answers

What is the tips for construct invariant for simple program (like interview algorithm)?

I am always wondering how to prove the correctness of a simple program. For example, a interview problem like First Missing Positive. The program looks like this: public class Solution{ public int firstMissingPositive(int[] A){ int i =…
0
votes
1 answer

Translating Eiffel loops to languages that do not support loop invariants/variants

A loop in Eiffel follows this format: from Init invariant Invariant until Exit variant Variant loop Body end How would you translate the above Eiffel pseudo-code to a language that does not support loop invariants/variants? …
Eleno
  • 2,864
  • 3
  • 33
  • 39
0
votes
1 answer

Is this a correct invariant for this loop?

This is pseudocode for a linear search in an array, returning an index i if the desired element e in an array A is found, NIL otherwise (this is from the CLRS book, 3rd edition, exercise 2.1-3): LINEAR_SEARCH (A, e) for i = 1 to A.length …
pr0gma
  • 577
  • 3
  • 8
  • 18
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
  • 355
  • 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,403
  • 3
  • 21
  • 31
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
  • 10
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?
1 2 3
9
10