






















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
Major points from this lecture are: Induction, Reasoning About Programs, Functions, Sum of Squares, Closed-Form Expression, Coefficients, Approach, Recursive, Two Functions Equal, Induction Over Integers . Object-Oriented Programming and Data Structures course includes program structure and organization, object oriented programming, graphical user interfaces, algorithm analysis, recursion, data structures (lists, trees, stacks, queues, heaps, search trees, hash tables, graphs), simple graph algo
Typology: Lecture notes
1 / 30
This page cannot be seen from the preview
Don't miss anything!























Lecture 20 CS
Spring 2013 A well-known scientist (some say it was Bertrand Russell) once gave apublic lecture on astronomy. He described how the earth orbits around thesun and how the sun, in turn, orbits around the center of a vast collection ofstars called our galaxy.At the end of the lecture, a little old lady at the back of the room got up andsaid: "What you have told us is rubbish. The world is really a flat platesupported on the back of a giant tortoise." The scientist gave a superiorsmile before replying, "What is the tortoise standing on?" "You're veryclever, young man, very clever", said the old lady. "But it's turtles all theway down! ”
We need clear problem statements - And then a rigorous way to convince ourselves that what we wrote solves the problem
Especially with recursion, concurrency - Today focus on recursion
It is often useful to describe a function in different ways
Let S : int → int be the func Ɵ on where S(n) is the sum of the integers from 0 to n. For example, S(0) = 0 S(3) = 0+1+2+ = 6 - Definition: iterative form - S(n) = 0+1+ … + n = Σ i - Another characterization: closed form - S(n) = n(n+1)/ n i=
more complex example
Let SQ : int → int be the func Ɵ on that gives the sum of the squares of integers from 0 to n: SQ(0)
2
2
2
2
Definition (iterative form): SQ(n)
2
2
n 2
Is there an equivalent closed
form expression?
Use n
a ∙ 0
b ∙ 0
c ∙ 0
d - SQ(1) = 1 = a ∙ 1 + b ∙ 1 + c ∙ 1 + d - SQ(2) = 5 = a ∙ 8 + b ∙ 4 + c ∙ 2 + d - SQ(3) = 14 = a ∙ 27 + b ∙ 9 + c ∙ 3 + d - Solve these 4 equations to get - a = 1/ b = 1/ c = 1/ d = 0
2
2
2
3
2
This suggests SQ(n)
2
2
n 2 = n 3
n 2
n(n+1)(2n+1)/
Question: Is this closed
form solution true for all n?
Remember, we only used n = 0,1,2, to determine these coefficients - We do not know that the closed ‐ form expression is valid for other values of n
To solve this problem, let’s express SQ(n) in a different way: - SQ(n) = 0 2
1 2
…
(n ‐
2
n 2
The part in the box is just SQ(n ‐
This leads to the following recursive definition
SQ(n ‐
n 2 ,^ n > 0
SQ(3)
4 2 = SQ(2)
3 2
4 2 = SQ(1)
2 2
3 2
4 2 = SQ(0)
1 2
2 2
3 2
4 2 = 0
1 2
2 2
3 2
4 2 Base CaseRecursive Case
SQ r (r = recursive) SQ r (0) = 0 SQ r (n) = SQ r (n ‐ 1) + n 2 , n > 0
SQ c (c = closed ‐ form) SQ c (n) = n(n+1)(2n+1)/
Assume equally spaced dominos, and assume that spacing between dominos is less than domino length - How would you argue that all dominos would fall? - Dumb argument: - 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 - ... - Is there a more compact argument we can make? 0 1 2 3 5 4
Argument: - 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+ (Inductive Step) - Because we could have picked any domino to be the k th one, we conclude that all dominos will fall over (Conclusion)
This is an inductive argument
This version is called weak induction
There is also strong induction (later)
Not only is this argument more compact, it works for an arbitrary number of dominoes!
Recall: SQ (0)r = 0 SQ (n)r = SQ (nr ‐ 1) + n 2 ,^ n > 0 SQ (n)c^ = n(n+1)(2n+1)/
Let P(n) be the proposition that SQ (n)r = SQ c (n)
Basis: P(0) holds because SQ (0)r = 0 and SQ c (0) = 0 by definition
Induction Hypothesis: Assume SQ (k)r = SQ (k)c^
Inductive Step: SQ (k+1)r = SQ (k)r
(k+1) 2 by definition of SQ (k+1)r = SQ c (k)
(k+1) 2 by the Induction Hypothesis = k(k+1)(2k+1)/
(k+1) 2 by definition of SQ c (k) = (k+1)(k+2)(2k+3)/ algebra = SQ c (k+1) by definition of SQ c (k+1)
Conclusion: SQ (n)r = SQ (n)c^ for all n 0
Prove that 0+1+...+n
n(n+1)/
Basis: Obviously holds for n
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)/
(k+1) by
(k+1)(k+2)/ algebra
Conclusion: 0+1+…+n
n(n+1)/ for all n
Claim: You can make any amount of postage above 8¢ with some combination of 3¢ and 5¢ stamps - Basis: True for 8¢: 8 = 3 + 5 - Induction Hypothesis: Suppose true for some k ≥ 8 - Inductive Step: - 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. - Conclusion: Any amount of postage above 8¢ can be made with some combination of 3¢ and 5¢ stamps