Induction and Recursion: Proving Statements and Solving Problems, Study notes of Computer Science

The concepts of induction and recursion, two closely related strategies used in mathematics and computer science. Induction is a mathematical method for proving statements about natural numbers, while recursion is a programming strategy for solving problems by reducing them to simpler instances. Defining functions, the sum of squares, finding closed-form expressions, and proving the equality of recursive and closed-form functions using mathematical induction.

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-emh
koofers-user-emh 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Induction
Lecture 5
CS2110 – Summer 2008
2
Overview
yRecursion
A programming strategy that solves a problem by reducing it
to simpler or smaller instance(s) of the same problem
yInduction
A mathematical strategyfor proving statements about natural
numbers 0,1,2,... (or more generally, about inductively
defined objects)
yInduction and recursion are very cl osely related
3
Defining Functions
yIt is often useful to describe a function in different ways
Let S : int int be the function where S(n) is the sum of the
integers from 0 to n. For example,
S(0) = 0 S(3) = 0+1+2+3 = 6
Definition: iterative form
S(n) = 0+1+ …+ n = Σi
Recursive definition
S(0) = 0
S(n) = n + S(n-1) for n > 0
Another characterization: closed form
S(n) = n(n+1)/2
n
i=0
4
Sum of Squares
yA more complex example
Let SQ : int int be the function that gives the sum of the
squares of integers from 0 to n:
SQ(0) = 0 SQ(3) = 02+ 12+ 22+ 32= 14
yDefinition (iterative form): SQ(n) = 02+ 12+ … + n2
yIs there an equivalent closed-form ex pression?
5
Closed-Form Expression for SQ(n)
ySum of integers between 0 through n was n(n+1)/2
which is a quadratic in n
yInspired guess: perhaps sum of squares of
integers between 0 through n is a cubic in n
yConjecture: SQ(n) = an3+bn2+cn+d
where a, b, c, d are unknown coefficients
yHow can we find the values of the fo ur unknowns?
Idea: Use any 4 values of n to generate 4 linear equations,
and then solve
6
Finding Coefficients
yUse n = 0, 1, 2, 3
SQ(0) = 0= 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/3 b = 1/2 c = 1/6 d = 0
SQ(n) = 02+12+…+n2 = an3+bn2+cn+d
pf3
pf4
pf5

Partial preview of the text

Download Induction and Recursion: Proving Statements and Solving Problems and more Study notes Computer Science in PDF only on Docsity!

1

Induction

Lecture 5 CS2110 – Summer 2008

2

Overview

y Recursion

ƒ A programming strategy that solves a problem by reducing it to simpler or smaller instance(s) of the same problem

y Induction

ƒ A mathematical strategy for proving statements about natural numbers 0,1,2,... (or more generally, about inductively defined objects)

y Induction and recursion are very closely related

3

Defining Functions

y It is often useful to describe a function in different ways

ƒ Let S : int → int be the function where S(n) is the sum of the integers from 0 to n. For example, S(0) = 0 S(3) = 0+1+2+3 = 6

ƒ Definition: iterative form Š S(n) = 0+1+ …+ n = Σ i

ƒ Recursive definition Š S(0) = 0 Š S(n) = n + S(n-1) for n > 0

ƒ Another characterization: closed form Š S(n) = n(n+1)/

n i=

4

Sum of Squares

y A more complex example

ƒ Let SQ : int → int be the function that gives the sum of the squares of integers from 0 to n:

SQ(0) = 0 SQ(3) = 0^2 + 1^2 + 2^2 + 3^2 = 14

y Definition (iterative form): SQ(n) = 0^2 + 1^2 + … + n^2

y Is there an equivalent closed-form expression?

Closed-Form Expression for SQ(n)

y Sum of integers between 0 through n was n(n+1)/

which is a quadratic in n

y Inspired guess: perhaps sum of squares of

integers between 0 through n is a cubic in n

y Conjecture: SQ(n) = an^3 +bn^2 +cn+d

where a, b, c, d are unknown coefficients

y How can we find the values of the four unknowns?

ƒ Idea: Use any 4 values of n to generate 4 linear equations, and then solve

Finding Coefficients

y Use n = 0, 1, 2, 3

SQ(0) = 0 = 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/3 b = 1/2 c = 1/6 d = 0

SQ(n) = 0^2 +1^2 +…+n^2 = an^3 +bn^2 +cn+d

7

Is the Formula Correct?

y This suggests

SQ(n) = 0^2 + 1^2 + … + n^2

= n^3 /3 + n^2 /2 + n/

= n(n+1)(2n+1)/

y Question: Is this closed-form solution true for all n?

ƒ Remember, we only used n = 0,1,2,3 to determine these coefficients ƒ We do not know that the closed-form expression is valid for other values of n

8

One Approach

y Try a few other values of n to see if they work.

ƒ Try n = 5: SQ(n) = 0+1+4+9+16+25 = 55 ƒ Closed-form expression: 5·6·11/6 = 55 ƒ Works!

y Try some more values…

y We can never prove validity of the closed-form

solution for all values of n this way, since there are an

infinite number of values of n

9

A Recursive Definition

y To solve this problem, let’s express SQ(n) in a

different way:

SQ(n) = 0^2 + 1^2 + … + (n-1)^2 + n^2 ƒ The part in the box is just SQ(n-1)

y This leads to the following recursive definition

SQ(0) = 0

SQ(n) = SQ(n-1) + n^2 , n > 0

y Thus,

SQ(4) = 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 Case

Recursive Case

10

Are These Two Functions Equal?

y SQ r (r = recursive)

SQ r(0) = 0

SQ r(n) = SQ r(n-1) + n^2 , n > 0

y SQ c (c = closed-form)

SQ c (n) = n(n+1)(2n+1)/

Induction over Integers

y To prove that some property P(n) holds for all

integers n ≥ 0,

ƒ Basis: Show that P(0) is true ƒ Induction Step: Assuming that P(k) is true for an unspecified integer k, show that P(k+1) is true

y Conclusion: Because we could have picked any k, we

conclude that P(n) holds for all integers n ≥ 0

Dominos

y Assume equally spaced dominos, and assume that spacing between dominos is less than domino length y How would you argue that all dominos would fall? y 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 ƒ ... y Is there a more compact argument we can make?

19

What are the “Dominos”?

y In some problems, it can be tricky to determine how to

set up the induction

y This is particularly true for geometric problems that

can be attacked using induction

20

A Tiling Problem

y A chessboard has one square cut out of it y Can the remaining board be tiled using tiles of the shape shown in the picture (rotation allowed)? y Not obvious that we can use induction!

21

Proof Outline

Consider boards of size 2n^ x 2n^ for n = 1,2,…

y Basis: Show that tiling is possible for 2 x 2 board

y Induction Hypothesis: Assume the 2k^ x 2 k^ board can

be tiled

y Inductive Step: Using I.H. show that the 2k+1^ x 2k+

board can be tiled

y Conclusion: Any 2 n^ x 2n^ board can be tiled, n = 1,2,…

ƒ Our chessboard (8 x 8) is a special case of this argument ƒ We will have proven the 8 x 8 special case by solving a more general problem!

22

Basis

y The 2 x 2 board can be tiled regardless of which one of the four pieces has been omitted

2 x 2 board

4 x 4 Case

y Divide the 4 x 4 board into four 2 x 2 sub-boards

y One of the four sub-boards has the missing piece ƒ By the I.H., that sub-board can be tiled since it is a 2 x 2 board with a missing piece

y Tile the center squares of the three remaining sub-boards as shown ƒ This leaves three 2 x 2 boards, each with a missing piece ƒ We know these can be tiled by the Induction Hypothesis

2 k+1^ x 2k+1^ case

y Divide board into four sub-boards and tile the center squares of the three complete sub-boards y The remaining portions of the sub-boards can be tiled by the I.H. (which assumes we can tile 2k^ x 2k^ boards)

25

When Induction Fails

y Sometimes an inductive proof strategy for some

proposition may fail

y This does not necessarily mean that the proposition is

wrong

ƒ It may just mean that the particular inductive strategy you are using is the wrong choice

y A different induction hypothesis (or a different proof

strategy altogether) may succeed

26

Tiling Example (Poor Strategy)

Let’s try a different induction strategy

y Proposition

ƒ Any n x n board with one missing square can be tiled

y Problem

ƒ A 3 x 3 board with one missing square has 8 remaining squares, but our tile has 3 squares; tiling is impossible

y Thus, any attempt to give an inductive proof of this

proposition must fail

y Note that this failed proof does not tell us anything

about the 8x8 case

27

A Seemingly Similar Tiling Problem

y A chessboard has opposite corners cut out of it. Can the remaining board be tiled using tiles of the shape shown in the picture (rotation allowed)?

y Induction fails here. Why? (Well…for one thing, this board can’t be tiled with dominos.)

28

Strong Induction

y We want to prove that some property P holds for all n

y Weak induction

ƒ P(0): Show that property P is true for 0 ƒ P(k) ⇒ P(k+1): Show that if property P is true for k, it is true for k+ ƒ Conclude that P(n) holds for all n

y Strong induction

ƒ P(0): Show that property P is true for 0 ƒ P(0) and P(1) and … and P(k) ⇒ P(k+1): show that if P is true for numbers less than or equal to k, it is true for k+ ƒ Conclude that P(n) holds for all n

y Both proof techniques are equally powerful

Conclusion

y Induction is a powerful proof technique

y Recursion is a powerful programming technique

y Induction and recursion are closely related

ƒ We can use induction to prove correctness and complexity results about recursive programs