-1

In the book Algorithm design manual (page 16), proof by induction for correctness of below increment algorithm is discussed.

Increment(y)
    if (y == 0) return 1;
    else if (y % 2 == 1) return 2 * Increment(floor(y/2));
    else return y + 1;

But I am confused at one point(or assumption) in the discussion. Requesting for a solution (proof of correctness by induction) to understand better.

jitendra
  • 195
  • 1
  • 2
  • 10
  • what are you confused about and what are they proving? correctnes? running time? your question is too vague – Makogan Jun 03 '17 at 06:15
  • 1
    Maybe you should tell us the point you're confused about. Producing the same explanation here is not going to help you and explaining every part in detail is just going to waste everyone's time (including yours). Also, questions should not be dependent on external resources - you should copy the relevant details here. – Bernhard Barker Jun 03 '17 at 06:18
  • correctness, edited the question. @Makogan – jitendra Jun 03 '17 at 06:19

1 Answers1

4

Base case y=0:

By the first statement, the output is 0+1 = 1 = y+1;

Induction Hypothesis: assume that for 0 < n < y increment returns n+1.

We seek to prove that increment(y) returns y+1.

Trivial case, y is even (y=2k for some k):

Then y%2 is 0, the if clause is false, the returned value is y+1;

Odd case:

Assume y = 2k+1 (odd) then floor(y/2) = k and as such:

increment(floor(y/2))=increment(k) = k+1

so,

2*increment(floor(y/2)) = 2k+2 = y+1

QED

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Makogan
  • 8,208
  • 7
  • 44
  • 112