Proof of induction on pseudocode

不问归期 提交于 2019-12-10 12:08:36

问题


Given the pseudocode

MUL(a,b) 
   x=a
   y=0
   WHILE x>=b DO
      x=x-b
      y=y+1
   IF x=0 THEN
      RETURN(true)
   ELSE
      RETURN(false)

Let x(n) and y(n) denote the value of x and y after the while loop has run n times.

I have to show by the proof of induction that

x(n) + b*y(n) = a

What I've done:

P(n): x(n) + by(n) = a

Let a and b be arbitrary numbers then the first loop will give x(1) = a - b and y(1) = 0 + 1 = 1

P(1): x(1) + by(1) = a <=> a = a

so P(1) is true.

Assume P(n) is true. We want to show that P(n+1) is also true.

For step n + 1 the loop will give x(n+1) = x(n) - b and y(n+1) = y(n) + 1

P(n+1): x(n+1) + by(n+1) = a <=> x(n) + by(n) = a

Using the assumption that P(n) is true, it follows that P(n+1) is also true, and the proof is complete.

My question: Since this is my first time using the proof of induction on a pseudocode, I'm not sure how to go about it. I just want to know if this is the right way to work around the problem, and if not what should the process be like?

Thanks


回答1:


As you got it (almost) right in your question, answering this feels like overkill, but I'll do it anyway:

Your approach is fine, although you should not ignore the case where no loop iterations are executed at all, which is the case when a < b and corresponds to P0. If you prove P1 is true, and Pn+1 is true when Pn is true, you don't say anything about P0.

So with that slight modification, the derivation of the proof by induction goes as follows:

Define Pn as the value of the expression x + b*y after n iterations of the loop have been executed:

Pn : xn + b.yn = a

It is to be proved that Pn is true for all n >= 0

1. Base Case: P0

Note that this is a possible case: when the condition of the while loop is first evaluated, no iterations have been performed yet, so n is 0, yet we do have the values for the two variables at play: x0 and y0.

The pre-condition for the loop is determined by the assignment statements that precede the loop:

x=a
y=0

So we have:

P0 : x0 + b.y0 = a + b.0 = a

2. Inductive Step: Pn+1

Here we assume that Pn is true for a given n:

Pn : xn + b.yn = a

This is the pre-condition when starting the next iteration of the loop, in which the following statements are executed:

x=x-b
y=y+1

By substitution we get the post-condition for that particular iteration which by definition is Pn+1:

Pn+1 : (xn - b) + b.(yn + 1) = xn + b.yn = Pn = a



来源:https://stackoverflow.com/questions/40117582/proof-of-induction-on-pseudocode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!