Properties that hold before and after every execution of a loop. Used to prove correctness of algorithms.
Questions tagged [loop-invariants]
90 questions
11
votes
2 answers
How is the loop invariant obtained in this square root bound finding algorithm?
Originally on math.SE but unanswered there.
Consider the following algorithm.
u := 0
v := n+1;
while ( (u + 1) is not equal to v) do
x := (u + v) / 2;
if ( x * x <= n)
u := x;
else
v := x;
end_if
end_while
where u, v, and n…
Ken Li
- 3,106
- 3
- 24
- 38
7
votes
2 answers
Loop variant for a while loop that occasionally doesn't decrease?
I'm working on practice problems for a test I have, and every example of loop variant decreased with every iteration of the loop. On this one, the values remain the same when a < b. My attempts also got me a loop variant that has a chance of a…
Andrew Raleigh
- 129
- 8
7
votes
2 answers
Invariant For Nested Loop in Matrix Multiplication Program
I'm making a graduate thesis about proving correctness of program for multiplying 2 matrices using Hoare logic. For doing this, I need to generate the invariant for nested loop for this program:
for i = 1:n
for j = 1:n
for k = 1:n
…
asn32
- 171
- 1
- 4
7
votes
4 answers
Loop invariant for an algorithm
I have developed the following pseudocode for the sum of pairs problem:
Given an array $A$ of integers and an integer $b$, return YES if there are positions $i,j$ in $A$ with $A[i] + A[j] = b$, NO otherwise.
Now I should state a loop invariant…
forrestGump
- 787
- 1
- 10
- 14
5
votes
2 answers
Loop-invariant code motion with unsafe operations
There are well-known algorithms LICM, and they work well.
Unfortunately, there are certain cases when these optimizations can cause runtime failures with code that was initially correct.
Consider the following example:
read a
read_array bs
for (b in…
Alexey Andreev
- 151
- 4
5
votes
1 answer
Is there a model of ZF¬C where some program always terminates but has no loop variant?
Wikipedia has a proof that every loop that terminates has a loop variant—a well-founded relation on the state space such that each iteration of the loop results in a state that is less than the previous iteration's state under the relation. Here,…
Aaron Rotenberg
- 3,583
- 14
- 20
4
votes
1 answer
Developing invariants for comparing two strings
The following algorithm is supposed to compare two strings $S_1$ and $S_2$ ("/\" for empty string):
X = S1
Y = S2
E = true
// (1)
while X != /\ and Y != /\ and E == true
if head(X) == head(Y)
X = tail(X)
Y =…
hengxin
- 9,671
- 3
- 37
- 75
4
votes
0 answers
Expression of the weakest precondition of a while loop
I am interested in computing weakest preconditions (WP) of loops.
If I refer to Wikipedia "Predicate Transformer semantics", the WP for e.g. total correction of a loop annotated with an invariant I is (as discussed here):
WP(while E do S done, R) =…
Zooky
- 141
- 2
4
votes
2 answers
Loop invariant for a division algorithm
I'm having problems trying to understand the concept of loop invariants.
I have the following code, where M and N are predefined constants.
a = 0
b = M
c = 1
while M - c * N >= 0:
a = c
c = c + 1
b = M - a * N
print(a, b)
I have figured out…
gcarol
- 43
- 3
4
votes
1 answer
How to find loop invariant from weakest precondition?
Consider this code:
Precondition:
Postcondition: rv == i <==> ∃i, 0 ≤ i ≤ a.length-1, a[i] == key
function Find(Array a, int key) returns (int i) {
i = a.length -1;
while i ≥ 0 {
if a[i] == key {return;}
i = i - 1…
Teodorico Levoff
- 431
- 1
- 4
- 13
4
votes
3 answers
Finding a good loop invariant for a powering procedure
Consider the following algorithm for computing integer powers:
Procedure power(integer x, integer n)
power := 1
for i := 1 to n
power := power * x
return power
Can we say that the loop invariant is $power \leq x^n$…
Jack
- 66
- 1
- 5
4
votes
1 answer
Help Finding Loop Invariant From For Loop
I have created the algorithm below...
String A = v[0];
int val = 1;
for (int i = 1; i < v.length; i++) {
if (val == 0) {
A = v[i];
val++;
} else if (v[i].equals(A))
…
MCR
- 149
- 2
4
votes
1 answer
Invariance Textbook Problem: Clarification Needed
I am currently reading Michael Soltys' Analysis of Algorithms (2nd Edition), and Problem 1.13 of the subsection titled Invariance reads:
Let $n$ be an odd number, and suppose that we have the set $\{1,2,\dots,2n\}$. We pick any two numbers $a$, $b$…
Ziad Ismaili Alaoui
- 438
- 2
- 10
3
votes
1 answer
Is Loop Invariant Proof a form of Induction?
As far as I see, what computer scientists refer to as loop invariant proofs are exact replicas of induction proof. Is it true? Can I state that loop invariant proof implies an induction? Is there a theoretical difference?
Ninja Bug
- 259
- 3
- 5
3
votes
2 answers
Invariant on "Find K Closest Elements" problem
I run across this problem:
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always preferred.
For example,…
zack
- 206
- 1
- 6