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

Using an invariant to determine the boundary conditions in a typical binary search problem

I am trying to solve the typical binary search question on LeetCode.com: Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index,…
user8305079
0
votes
1 answer

Using an invariant to determine boundary conditions in binary search

I am trying to solve Arranging Coins on LeetCode.com: You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed.…
user8305079
0
votes
2 answers

Algorithms. Add two n-bit binary numbers. What is a loop invariant of this problem?

I'm solving the exercise 2.1-4 from CLRS "introduction to algorithms". The problem is described as: Consider the problem of adding two n-bit binary integers, stored in two n-element arrays A and B. The sum of the two integers should be stored in…
0
votes
2 answers

What's an inductive invariant for this piece of code?

For this piece of code: // n is a user input that can be any integer s = 0 i = 0 while i < n: s = s + 1 i = i + 1 return s I would like to prove that the post condition is if n > 0 then s = sum(0, n) else s = 0 where sum(s,e)…
JRR
  • 6,014
  • 6
  • 39
  • 59
0
votes
1 answer

Dafny Loop Invariant might not Hold

This is a simple segregating 0s and 1s in the array problem. I can't fathom why the loop invariant does not hold. method rearrange(arr: array, N: int) returns (front: int) requires N == arr.Length requires forall i :: 0 <= i <…
tn2000
  • 23
  • 4
0
votes
1 answer

How can I figure out loop invariant in my binary search implementation?

bool binsearch(int x) { int i = 0, j = N; while(i < j) { int m = (i+j)/2; if(arr[m] <= x) { if(arr[m] == x) return true; i = m+1; } else { j = m; } …
0
votes
0 answers

Palindrome Validity Proof of Correctness

Leetcode Description Given a string of length n, you have to decide whether the string is a palindrome or not, but you may delete at most one character. For example: "aba" is a palindrome. "abca" is also valid, since we may remove either "b" or "c"…
0
votes
1 answer

Loop invariant for the sum of a binary tree

I want to find a loop invariant for the following piece of code, I cannot seem to figure out any sort of relationship presented in this piece of code. This algorithm's goal is to find the sum of all the elements in a binary tree. It stores the nodes…
0
votes
1 answer

Using methods inside loop invariants in Dafny

I'm trying to prove the simple gcd algorithm in Dafny, so I wrote the following, but it seems I can not use the method divides inside the loop invariants: method divides(d: int, x: int) returns (result: bool) requires d > 0 requires x > 0 …
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
0
votes
1 answer

Issues with Contract.Requires() and loop invariant

I am following the codecontracts tutorial (https://learn.microsoft.com/en-us/dotnet/framework/debug-trace-profile/code-contracts#usage-guidelines) and I seem to have trouble getting the simplest thing working. Given the method definition public int…
0
votes
1 answer

Loop Invariant of QuickSort Partition

I'm having trouble defining and proving a loop invariant for some implementation of Quicksort algorithm. This is neither Lomuto's nor Hoare's partition version. If you know about some known version of Quicksort that is implemented this way, let me…
Dennis
  • 2,271
  • 6
  • 26
  • 42
0
votes
1 answer

Invariant of an algorithm

Assume this algorithm which gives the maximal sum of a subarray back. And let a[] be an array of length n. randmax = 0 maximum = 0 for 0 <= i < n randmax = randmax + a_i if randmax > max max = randmax if randmax < 0 randmax =…
regsts
  • 1
0
votes
2 answers

Loop invariant proof on multiply algorithm

I'm currently stuck on a loop invariant proof in my home assignment. The algorithm that I need to prove correctness of, is: Multiply(a,b) x=a y=0 WHILE x>=b DO x=x-b y=y+1 IF x=0 THEN RETURN(y) ELSE …
0
votes
1 answer

Need help proving loop invariant (simple bubble sort, partial correctness)

The bubble-sort algorithm (pseudo-code): Input: Array A[1...n] for i <- n,...,2 do for j <- 2,...,i do if A[j - 1] >= A[j] then swap the values of A[j-1] and A[j]; I am not sure but my proof seems to work, but is overly…
Niki
  • 738
  • 8
  • 17
0
votes
1 answer

What is the loop invariant of the given program

I don't know how to find a loop invariant. I'm not sure where to start. Can anyone find the loop invariant of the given program and explain your method please. {n ≥ 0 ∧ i = 0} while i < n − 1 loop b[i] := a[i + 1]; i:=i + 1 end loop {∀j.(0 ≤ j < n −…
the_martian
  • 632
  • 1
  • 9
  • 21