






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The relationship between recursion and induction in programming. It discusses how induction can be used to establish the correctness and complexity of recursive programs. Defining functions, the sum of squares, finding closed-form solutions, and proving equality between recursive and closed-form functions using mathematical induction.
Typology: Slides
1 / 30
This page cannot be seen from the preview
Don't miss anything!























A well-known scientist (some say it was Bertrand Russell) once gave a public lecture on astronomy. He described how the earth orbits around the sun and how the sun, in turn, orbits around the center of a vast collection of stars called our galaxy. At the end of the lecture, a little old lady at the back of the room got up and said: "What you have told us is rubbish. The world is really a flat plate supported on the back of a giant tortoise." The scientist gave a superior smile before replying, "What is the tortoise standing on?" "You're very clever, young man, very clever", said the old lady. "But it's turtles all the way down! ”
It is often useful to describe a function in different ways
S(n) = 0+1+ … + n = Σ i
S(n) = n(n+1)/ n i=
A more complex example
SQ(0) = 0 SQ(3) = 0 2
Use n = 0, 1, 2, 3
a = 1/3 b = 1/2 c = 1/6 d = 0
2
2
2
3
2
This suggests SQ(n) = 0 2
SQ(n) = 0 2
SQ(0) = 0 SQ(n) = SQ(n-1) + n 2 , n > 0
SQ(4) = SQ(3) + 4 2 = SQ(2) + 3 2
4 2 = SQ(0) + 1 2
r
r
r
r
2
c
c
Domino 0 falls because we push it over Domino 0 hits domino 1, therefore domino 1 falls Domino 1 hits domino 2, therefore domino 2 falls Domino 2 hits domino 3, therefore domino 3 falls ...
Domino 0 falls because we push it over (Base Case or Basis) Assume that domino k falls over (Induction Hypothesis) Because domino k’s length is larger than inter-domino spacing, it will knock over domino k+1 (Inductive Step) Because we could have picked any domino to be the k th one, we conclude that all dominos will fall over (Conclusion)
There is also strong induction (later)
Recall: SQr(0) = 0 SQr(n) = SQr(n-1) + n 2 , n > 0 SQc(n) = n(n+1)(2n+1)/ Let P(n) be the proposition that SQr(n) = SQc(n) Basis: P(0) holds because SQr(0) = 0 and SQc(0) = 0 by definition Induction Hypothesis: Assume SQr(k) = SQc(k) Inductive Step: SQr(k+1) = SQr(k) + (k+1) 2 by definition of SQr(k+1) = SQc(k) + (k+1) 2 by the Induction Hypothesis = k(k+1)(2k+1)/6 + (k+1) 2 by definition of SQc(k) = (k+1)(k+2)(2k+3)/6 algebra = SQc(k+1) by definition of SQc(k+1) Conclusion: SQr(n) = SQc(n) for all n 0
Prove that 0+1+...+n = n(n+1)/ Basis: Obviously holds for n = 0 Induction Hypothesis: Assume 0+1+…+k = k(k+1)/ Inductive Step: 0+1+…+(k+1) = [0+1+…+k] + (k+1) by def = k(k+1)/2 + (k+1) by I.H. = (k+1)(k+2)/2 algebra Conclusion: 0+1+…+n = n(n+1)/2 for all n ≥ 0
If used a 5¢ stamp to make k, replace it by two 3¢ stamps. Get k+1. If did not use a 5¢ stamp to make k, must have used at least three 3¢ stamps. Replace three 3¢ stamps by two 5¢ stamps. Get k+1.