patternMinor
Loop variant for a while loop that occasionally doesn't decrease?
Viewed 0 times
variantwhileloopoccasionallythatfordoesndecrease
Problem
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 negative since occasionally a becomes larger than b and vice versa. Any advice on attempting to find and prove the loop variant for this question?
EDIT: For anyone who is interested in this question, my best solution is as follows.
$$f_{1} = a + 2b + 1$$
def mystery(a,b):
# Precondition: a >= 0 and b >= 0
while a >= 0 and b >= 0:
if a < b:
a, b = b, a
else:
a = a - 1
return aEDIT: For anyone who is interested in this question, my best solution is as follows.
$$f_{1} = a + 2b + 1$$
Solution
Just a hint for now, since this is a practice problem: consider a lexicographic combination of orders.
In some more detail: Suppose you have two maps $f_1:S\to D_1$ and $f_2:S\to D_2$ from your program states $S$ into well-founded ordered domains $(D_1,\le_1)$ and $(D_2,\le_2)$. The lexicographic combination of $\le_1$ and $\le_2$ is the order $\le$ on $D_1\times D_2$ given by $(x_1,y_1)\le(x_2,y_2)$ iff either $x_1\le_1x_2$, or $x_1=x_2,y_1\le_2y_2$. It is also well-founded.
So if $f_1,f_2$ are such that
then the map $(f_1,f_2):S\to D_1\times D_2$ is a variant proving termination.
In some more detail: Suppose you have two maps $f_1:S\to D_1$ and $f_2:S\to D_2$ from your program states $S$ into well-founded ordered domains $(D_1,\le_1)$ and $(D_2,\le_2)$. The lexicographic combination of $\le_1$ and $\le_2$ is the order $\le$ on $D_1\times D_2$ given by $(x_1,y_1)\le(x_2,y_2)$ iff either $x_1\le_1x_2$, or $x_1=x_2,y_1\le_2y_2$. It is also well-founded.
So if $f_1,f_2$ are such that
- $f_1$ never increases, and
- whenever $f_1$ does not decrease, $f_2$ does,
then the map $(f_1,f_2):S\to D_1\times D_2$ is a variant proving termination.
Context
StackExchange Computer Science Q#49245, answer score: 7
Revisions (0)
No revisions yet.