CPSC 121: Models of Computation Unit 9 Mathematical Induction - - PowerPoint PPT Presentation

cpsc 121 models of computation
SMART_READER_LITE
LIVE PREVIEW

CPSC 121: Models of Computation Unit 9 Mathematical Induction - - PowerPoint PPT Presentation

CPSC 121: Models of Computation Unit 9 Mathematical Induction Based on slides by Patrice Belleville and Steve Wolfman Announcements Pre-class quiz #10 is tentatively due on Wednesday November 22nd at 9:00pm. Textbook sections: o Epp,


slide-1
SLIDE 1

Based on slides by Patrice Belleville and Steve Wolfman

CPSC 121: Models of Computation

Unit 9 Mathematical Induction

slide-2
SLIDE 2

Announcements

 Pre-class quiz #10 is tentatively due on Wednesday

November 22nd at 9:00pm.

  • Textbook sections:
  • Epp, 4th edition: 6.1, 7.1
  • Epp, 3rd edition: 5.1, 6.1
  • Rosen, 6th edition: 2.1, 2.3 up to the top of page 136.
  • Rosen, 7th edition: 2.1, 2.3 down to the bottom of page

141.

 Assignment #5 is due Monday November 27th at 4:00

pm.

Unit 9 : Induction 2

slide-3
SLIDE 3

Pre-Class Learning Goals

 By the start of class, you should be able to

  • Convert sequences to and from explicit formulas that

describe the sequence.

  • Convert sums to and from summation/Σ notation.
  • Convert products to and from product/Π notation.
  • Manipulate formulas in summation/product notation by

adjusting their bounds, merging or splitting summations/products, and factoring out values.

  • Given a theorem to prove and the insight into how to break

the problem down in terms of smaller problems, write out the skeleton of an inductive proof including:

  • the base case(s),
  • the induction hypothesis, and
  • the inductive step

3 Unit 9 : Induction

slide-4
SLIDE 4

Quiz 9 Feedback

 Generally:  Issues:  Essay Question:

  • As usual, we will revisit the open-ended question shortly.

4 Unit 9 : Induction

slide-5
SLIDE 5

In-Class Learning Goals

 By the end of this unit, you should be able to:

  • Prove properties of self-referential structures using inductive

proofs that naturally build on those self-references.

  • Critique formal inductive proofs to determine whether they

are valid and where the error(s) lie if they are invalid.

  • Formally prove properties of the non-negative integers (or a

subset like integers that have appropriate self-referential structure) —including both equalities and inequalities—using either weak or strong induction as needed.

5 Unit 9 : Induction

slide-6
SLIDE 6

Addressing the Course Big Questions

 CPSC 121: the BIG questions:

  • How can we convince ourselves that an algorithm

does what it's supposed to do?

  • How do we determine whether or not one algorithm is

better than another one?

 Mathematical induction is a very useful tool when

proving the correctness or efficiency of an algorithm.

 We will see several examples of this.

? ?

6 Unit 9 : Induction

slide-7
SLIDE 7

Outline

 Example: single-elimination tournaments.  Validating mathematical induction.  A Pattern for Induction  More examples using induction.  A slightly different type of induction.  Additional Examples

Unit 9 : Induction 7

slide-8
SLIDE 8

Example: Single-Elimination Tournament

 Problem: single-elimination tournament

  • Teams play one another in pairs
  • The winner of each pair advances to the next round
  • The tournament ends when only one team remains.

8

Montréal Montréal Los Angeles Montréal Toronto New York Isl. Vancouver

  • St. Louis

Pittsburgh Buffalo Los Angeles Los Angeles Toronto New York Isl. Round 1 Round 2 Round 3

Unit 9 : Induction

slide-9
SLIDE 9

Question

 What is the maximum number of teams which can

participate in a n-round single-elimination tournament ? (Try some examples first)

A. n B. 2n

  • C. n2
  • D. 2n

E. None of the above.

9 Unit 9 : Induction

slide-10
SLIDE 10

How can we prove it?

 How can we prove it for every n ?

We will use a technique called mathematical induction.

  • We show some basic cases first (for 0,1,2 )
  • Then we show that if the statement is true for case n

then it is true for case n+1 (inductive step) (OR if it is true for case n-1, then it is true for case n).

  • Then we conclude that the statement is true for every n.

 First we will convince ourselves that this is a valid proof

technique.

 Many theorems involve summations, so it is important to

be comfortable manipulating them.

 Hence the reading for pre-class quiz #9.

10 Unit 9 : Induction

slide-11
SLIDE 11

Base Case : n=0

 What is the max number of teams which can

participate in a tournament with 0 rounds?

  • A. 0

B. 1

  • C. 2
  • D. Any number

E. None of the above

 Is the statement correct for n = 0?

  • Yes, because ____________________

Unit 9 : Induction 11

slide-12
SLIDE 12

Inductive Step: Case for n  Case for n+1

 If at most 2n teams can participate in a tournament

with n rounds, then at most 2n+1 teams can participate in a tournament with n+1 rounds?

 If we want to prove this statement, which of the

following techniques might we use?

A. Antecedent assumption B. Witness proof

  • C. WLOG
  • D. Proof by cases

E. None of the above.

12 Unit 9 : Induction

slide-13
SLIDE 13

Working out the proof:

 Proof :

  • Consider an unspecified tournament with n rounds. Assume

that

  • How many teams we need to have a tournament with n+1

rounds?

  • We can think of a tournament with n+1 rounds as follows:
  • Two tournaments with n rounds proceed in parallel.
  • The two winners then play the n+1 round.
  • Since each tournament with n rounds has at most 2n teams,

the tournament with n+1 rounds has at most _______________ teams. QED

13 Unit 9 : Induction

slide-14
SLIDE 14

Completing the proof:

 Inductive Step: if at most 2n teams can play in an n-

round tournament, then at most 2n+1 teams can play in an (n+1)-round tournament.

 Proof:

  • Assume at most 2n teams can play in an n-round

tournament.

  • An (n+1)-round tournament is two n-round tournaments

where the winners play each other (since there must be a single champion).

  • By assumption, each of these may have at most 2n teams.

So, the overall tournament has at most 2*2n = 2n+1 teams. QED!

14 Unit 9 : Induction

slide-15
SLIDE 15

How Induction Works

 How do we use this to prove what we want?  Recall Universal Modus Ponens:

P(a) ∀x ∈ D, P(x) → Q(x) \ Q(a)

 If we define

  • P(n, 2n): Up to 2n teams can participate in a tournament with

n rounds

 then we have proved

  • ∀n ∈ N , P(n, 2n) → P(n+1, 2n+1)

Unit 9 : Induction 15

slide-16
SLIDE 16

How Induction Works(cont')

 So

  • We know that at most only 1 team can play with 0

rounds.

  • This is P(0, 1).

 Given

  • P(0, 1)
  • ∀n ∈ N , P(n, 2n) → P(n+1, 2n+1)

 We can deduce:

  • P(1, 2)

Unit 9 : Induction 16

slide-17
SLIDE 17

How Induction Works(cont')

 Given

  • P(1, 2)
  • ∀n ∈ N , P(n, 2n) → P(n+1, 2n+1)

 We can deduce:

  • P(2, 4)

 Given

  • P(2, 4)
  • ∀n ∈ N , P(n, 2n) → P(n+1, 2n+1)

 We can deduce:

  • P(3, 8)

Unit 9 : Induction 17

slide-18
SLIDE 18

How Induction Works(cont')

 Given

  • P(3, 8)
  • ∀n ∈ N , P(n, 2n) → P(n+1, 2n+1)

 We can deduce:

  • P(4, 16)

 Etc...  Therefore we can show that

  • ∀n ∈ N, P(n, 2n)

Unit 9 : Induction 18

slide-19
SLIDE 19

How Induction Works(cont')

 Another way to think about this:

  • Given an unspecified positive integer n.
  • To prove P(n+1, 2n+1):
  • First we prove P(n, 2n)
  • Then we use the fact that
  • ∀n ∈ N , P(n, 2n) → P(n+1, 2n+1)
  • Hence induction is more or less the same as recursion!

 A fun example involving induction:

http://www.youtube.com/watch?v=daRS98L9rHs&feat ure=related

Unit 9 : Induction 19

slide-20
SLIDE 20

Outline

 Example: single-elimination tournaments.  Validating mathematical induction.  A Pattern for Induction  More examples using induction.  A slightly different type of induction.  Additional Examples

Unit 9 : Induction 20

slide-21
SLIDE 21

Validating Mathematical Inducton

 Define

Q(n) ≡ P(n, 2n)

 In the previous example, we proved

  • Q(0)
  • ∀n ∈ N, Q(n) → Q(n+1)

 and deduced that

  • ∀n ∈ N, Q(n)

 Why is this a valid proof technique?

Unit 9 : Induction 21

slide-22
SLIDE 22

Validating M.I. (cont')

 Theorem:

  • Q(0) ^ (∀n ∈ N, Q(n) → Q(n+1)) → ∀n ∈ N, Q(n)

 Proof:

  • We use a proof by contradiction.
  • Suppose that the premises hold, but that

∀n ∈ N, Q(n) is false.

  • Then there is at least one value of n for which Q(n)

does not hold.

  • Let y be the smallest such value. Clearly y > 0, so

y–1 ∈ N.

Unit 9 : Induction 22

slide-23
SLIDE 23

Validating M.I. (cont')

 Proof (continued):

  • Because y is the smallest value for which Q(n) is

false, and y – 1 ∈ N, Q(y - 1) is true.

  • But then the 2nd premise implies that Q(y) is true, a

contradiction (since we assumed Q(y) was false).

  • Therefore the conclusion must be true, that is

∀n ∈ N, Q(n) holds.

Unit 9 : Induction 23

slide-24
SLIDE 24

Validating M.I. (cont')

 So a proof by mathematical induction has

  • One (or more) base case(s): Q(0)
  • Usually very straightforward to prove.
  • An induction step: ∀n ∈ N, Q(n) → Q(n+1)
  • We can use any proof technique we know for it.
  • Usually a WLOG + antecedent assumption proof

will work well.

Unit 9 : Induction 24

slide-25
SLIDE 25

Outline

 Example: single-elimination tournaments.  Validating mathematical induction.  A Pattern for Induction  More examples using induction.  A slightly different type of induction.  Additional Examples

Unit 9 : Induction 25

slide-26
SLIDE 26

Guidelines for a Simple Induction Proof

Type of Problem: Prove some property of a structure that is naturally defined in terms of itself. Part 1: Insight: First figure out how the problem “breaks down” in terms of smaller pieces? More specifically: How to define a problem of size n+1 using one or more pieces of size n. Part 2: Proof. Base case(s) : Figure out which problems cannot be broken down (usually small ones!). Those will end up as your base cases. Establish that the property is true for your base case(s). Inductive Step: prove that if the property is true for the piece(s) of size n, it is also true for the piece of size n+1.

26

Unit 9 : Induction

slide-27
SLIDE 27

A Pattern For Simple Induction

Theorem: P(n) is true for all n  _______. Proof: We prove it (or proceed) by induction on n. Base Case(s) (P(…) is true for _______): Prove each base case via your other techniques. Inductive Step: For any arbitrary n ≥ __x_____, Assume P(…) is true for _n __(inductive hypothesis) We’ll prove that P(n+1) is true.

Let n be any integer ≥_____x_______. Assume P(…) is true for __n________________.

Break P(n+1) down in terms of size n. The smaller cases are true, by the assumption (IH). Build back up to show that P(n+1) is true. This completes the induction proof. QED

27

Unit 9 : Induction

slide-28
SLIDE 28

A Pattern For Simple Induction

P(n) is true for _______ Which base cases? Almost certainly the smallest n for which P(.) has to be true. Otherwise, you don’t know yet. Do the rest of the proof first. Come back to the base case(s) when you know which one(s) you need! For an arbitrary n ≥ ___x____, What must x be? x must be such that

  • allows the antecedent of the induction step to reach the

largest of your base cases.

  • don't allow the antecedent of the induction step to go

beyond the smallest base case . Come back to this once you know your base case(s).

28

Unit 9 : Induction

slide-29
SLIDE 29

A Pattern For Simple Induction

assume P(.) is true for ____n_______. In this simple induction form, we assume that P() is true for a problem of some size k and we prove that it is also true for the problem of the next size (k+1). We usually use one of the following:

  • Assume P() is true for n and prove it true for n+1, or
  • Assume P() is true for n-1 and prove it true for n.

Break P(n) down in terms of the smaller case(s) How do we break the problem down in terms of smaller cases? THIS is the real core of every induction problem. Figure this

  • ut, and you’re ready to fill in the rest of the blanks!

29

Unit 9 : Induction

slide-30
SLIDE 30

Outline

 Example: single-elimination tournaments.  Validating mathematical induction.  A Pattern for Induction  More examples using induction.  A slightly different type of induction.  Additional Examples

Unit 9 : Induction 30

slide-31
SLIDE 31

Example 1: Sorting n items

 Recall the exercise from our first class for ordering 5 student

by their birthday.

 What is the max number of swaps that we need to sort n

items?

  • Suppose we place items from left to right.
  • The items already placed are ordered.
  • We swap each new item with its neighbour until it is at the right

place.

  • The i-th item may be swapped with all previous i-1 items.
  • So the total number of swaps is

σ1

𝑜 𝑗 − 1

= σ0

𝑜−1 𝑘 = n(n−1) 2

  • Hence we need to prove that σ0

𝑜 𝑗 = n(n+1) 2

31 Unit 9 : Induction

slide-32
SLIDE 32

Example 1: Sorting n items

 Which facts do we need to prove?

  • A. σ0

0 𝑗 = 0

  • B. For every n ≥ 0 if σ0

𝑜−1 𝑗 = 𝑜−1 𝑜 2

, then σ0

𝑜 𝑗 = 𝑜 𝑜+1 2

  • C. For every n > 0 if σ0

𝑜−1 𝑗 = 𝑜−1 𝑜 2

, then σ0

𝑜 𝑗 = 𝑜 𝑜+1 2

  • D. Both (a) and (c)

E. None of the above.

32 Unit 9 : Induction

slide-33
SLIDE 33

Example 1: Sorting n items

 Proof:

  • Base case: n = 0
  • Clearly : σ0

0 𝑗 = 0 = 𝑜 𝑜+1 2

  • Induction step:
  • Let n be any integer > 0. Assume that

(inductive hypothesis): σ0

𝑜−1 𝑗 = 𝑜−1 𝑜 2

  • Then
  • σ0

𝑜 𝑗 = ( σ0 𝑜−1 𝑗 ) + n

  • =

𝑜−1 𝑜 2

+ n (by the inductive hypothesis)

  • =

2𝑜+ 𝑜−1 𝑜 2

=

𝑜2+𝑜 2

=

𝑜(𝑜+1) 2

  • Hence by the principle of M.I., the theorem holds. QED

33 Unit 9 : Induction

slide-34
SLIDE 34

Example 2: Sum of Odd Numbers

Problem: What is the sum of the first n odd numbers? First, find the pattern. Then, prove it’s correct. The first 1 odd number? The first 2 odd numbers? The first 3 odd numbers? The first n odd numbers?

Historical note: Francesco Maurolico made the first recorded use

  • f induction in 1575 to prove this theorem!

34 Unit 9 : Induction

slide-35
SLIDE 35

Sum of Odd Numbers: Insight

Problem: Prove that the sum of the first n odd numbers is n2. How can we break the sum of the first, second, …, nth

  • dd number up in terms of a simpler sum of odd

numbers?

35 Unit 9 : Induction

slide-36
SLIDE 36

Sum of Odd Numbers: Insight

Problem: Prove that the sum of the first n odd numbers is n2. The sum of the first n odd numbers is the sum of the first n-1 odd numbers plus the nth odd number.

(See our recursive formulation of  from the last example!)

36 Unit 9 : Induction

slide-37
SLIDE 37

Sum of Odd Numbers

Theorem: For all positive integers n, the sum of the first n odd natural numbers is n2. Proof: We proceed by induction on n. Base Case(s) : Theorem is true for ?:

37 Unit 9 : Induction

slide-38
SLIDE 38

Sum of Odd Numbers

Theorem: For all positive integers n, the sum of the first n odd natural numbers is n2. Proof: We proceed by induction on n. Base Case : Theorem is true for n=1: The sum of the first 1 odd natural numbers is 1, which equals 12. 

38 Unit 9 : Induction

slide-39
SLIDE 39

Sum of Odd Numbers

Theorem: For all positive integers n, the sum of the first n odd natural numbers is n2. Proof: We proceed by induction on n. Base Case : Theorem is true for n=1: The sum of the first 1 odd natural numbers is 1, which equals 12.  Inductive Step For k > ?: assume P(?) is true and we’ll prove P(k) is true:

39 Unit 9 : Induction

slide-40
SLIDE 40

Sum of Odd Numbers

Theorem: For all positive integers n, the sum of the first n odd natural numbers is n2. Proof: We proceed by induction on n. Base Case : Theorem is true for n=1: The sum of the first 1 odd natural numbers is 1, which equals 12.  IInductive Step: For k > 1: assume the sum of first k-1 odds is (k-1)2 and we’ll prove that the sum of first k odds is k2 .

40 Unit 9 : Induction

slide-41
SLIDE 41

Sum of Odd Numbers

Theorem: For all positive integers n, the sum of the first n odd natural numbers is n2. Proof: We proceed by induction on n. Base Case : Theorem is true for n=1: The sum of the first 1 odd natural numbers is 1, which equals 12.  Inductive Step For k > 1: assume the sum of first k-1 odds is (k-1)2 and we’ll prove that the sum of first k odds is k2 . Let k be any integer greater than 1. Break P(k) down in terms of the smaller case(s). The smaller cases are true, by assumption. Build back up to show that P(k) is true.

41 Unit 9 : Induction

slide-42
SLIDE 42

Sum of Odd Numbers

Theorem: For all positive integers n, the sum of the first n odd natural numbers is n2. Proof: We proceed by induction on n. Base Case : Theorem is true for n=1: The sum of the first 1 odd natural numbers is 1, which equals 12.  Inductive Step For k > 1: assume the sum of first k-1 odds is (k-1)2 and we’ll prove that the sum of first k odds is k2 . Let k be any integer greater than 1. Then

42

 

k i

i

1

??? ) 1 2 (

Unit 9 : Induction

slide-43
SLIDE 43

Sum of Odd Numbers

Theorem: For all positive integers n, the sum of the first n odd natural numbers is n2. Proof: We proceed by induction on n. Base Case : Theorem is true for n=1: The sum of the first 1 odd natural numbers is 1, which equals 12.  Inductive Step For any k > 1: assume the sum of first k-1 odds is (k-1)2 and we’ll prove that the sum of first k odds is k2 . Let k be any integer greater than 1. Then

= ( k-1)2 + (2k-1) (by the assumption (IH)) = k2 – 2k + 1 + 2k -1 = k2 QED

43

 

  

    

1 1 1

) 1 2 ( )] 1 2 ( [ ) 1 2 (

k i k i

k i i

Unit 9 : Induction

slide-44
SLIDE 44

Example 3: Geometric Series

 We will prove that for every value of a ≠0, 1:  These summations occur frequently when we need to

determine the running time of divide-and-conquer algorithms.

CPSC 121 – 2016W T1 44

i= 0 t

a

i= a t+ 1− 1

a− 1

slide-45
SLIDE 45

Geometric Series

Proof:

 Base case: t = 0

  • In this case the summation is a0 = 1, and

 Induction step:

  • Let t be any unspecified integer ≥ 0.
  • Assume that
  • We will show that
  • Now

 Hence by the principle of M.I., the theorem holds. QED

CPSC 121 – 2016W T1 45

a

1− 1

a− 1 = 1

slide-46
SLIDE 46

Inequalities

 Example: prove that n ≥ 4, 2n < n!  Rules for inequalities:

  • Start from one side (say the left side)
  • Work step by step towards the other.
  • When dealing with <, you are allowed to make the

expression larger, but never smaller.

  • Example: if I am smaller than you, then I am still smaller

than you when you stand on a bench.

CPSC 121 – 2016W T1 46

slide-47
SLIDE 47

Example 4: Using Inequalities

 Prove that n ≥ 4, 2n < n!  Proof: by induction on n.

  • Base case: n =
  • Induction step: we want to prove that

n ≥ if 2n-1 < (n-1)! then 2n < n!

  • Consider an unspecified n ≥ .
  • Induction hypothesis: assume that 2n-1 < (n-1)!
  • Then 2n = 2(2n-1) < 2(n-1)! < n(n-1)! = n!
  • this is where we “approximate”
  • the induction hypothesis is used here
  • Hence by the principle of M.I., n ≥ 4, 2n < n!

CPSC 121 – 2016W T1 47

slide-48
SLIDE 48

Bad Example : What is Wrong ?

Theorem: All horses are the same colour. Proof: We proceed by induction on n, the size of the group of horses. Base Case : Theorem is true for n = 1. All horses in any group of one horse are obviously the same colour.  Inductive Step: For any k ≥ 2, assume that all horses in any group of size k-1 are the same colour, we’ll show that for groups of k horses.

  • Consider an arbitrary group of k horses with k ≥ 2 .
  • Remove any one horse from it. What remains is a group of k-1

horses, which are all the same colour by the IH. Only the set-aside horse may be a different colour.

  • Now, return the horse to the group and remove a different horse.

Again, the remaining horses are all the same colour, but from the previous step we already know that this time the set-aside horse is also the same colour. Therefore, all horses in any group of size k are the same colour.

QED

48 Unit 9 : Induction

slide-49
SLIDE 49

Outline

 Example: single-elimination tournaments.  Validating mathematical induction.  A Pattern for Induction  More examples using induction.  A slightly different type of induction.  Additional Examples

Unit 9 : Induction 49

slide-50
SLIDE 50

Strong Mathematical Induction

 The induction we have seen so far handles problems

which can be broken down to sub-problems of size 1 less that the original problem size.

 How do we handle more general problems which can

be defined in terms of one or more smaller similar problems with various but smaller sizes?

 We need to make our induction technique more

general.

Unit 9: Induction 50

slide-51
SLIDE 51

Strong Mathematical Induction

 When we want to prove

∀n ∈ Z+, Q(n) We use a slightly different induction step.

  • Instead of proving that
  • ∀n ∈ Z+, Q(n-1) → Q(n)
  • We prove that
  • ∀n ∈ Z+, (Q(1) ^ Q(2) ^ ... ^ Q(n-1)) → Q(n)

 That is, we now assume that the theorem is true for all

the numbers smaller than n and prove it for n

 We can also show that this type of induction is a valid

proof technique.

Unit 9: Induction 51

slide-52
SLIDE 52

Guidelines for Writing a General Induction Proof

How can we figure out an inductive proof?

 Start at the inductive step!  Look at a “big” problem (of size n).  Figure out how to break it down into smaller pieces (of

size less than n).

 Assume those smaller pieces work. That will end up

as your Induction Hypothesis.

 Figure out which problems cannot be broken down

(usually small ones!). Those will end up as your basis step(s).

 Prove the induction step.

52

slide-53
SLIDE 53

Example 5

 Every positive integer n greater than 1 can be written

as a product of primes.

 What base case(s) should we use?

A. n = 1 B. n = 2

  • C. n = 2, 3 or 5.
  • D. n is prime.

E. None of the above.

Unit 9: Induction 53

slide-54
SLIDE 54

Example 5

 Every positive integer n greater than 1 can be written

as a product of primes.

 What is the inductive step?

  • A. For every integer k >2, if k-1 is a product of primes, then k

is a product of primes

  • B. For every integer k ≥ 2, if k-1 is a product of primes, then k

is a product of primes

  • C. For every integer n >2, if every integer k, 2 ≤ k ≤ n-1, is a

product of primes, then n is a product of primes.

  • D. For every integer n ≥ 2, if every integer k, 2 < k ≤ n-1, is a

product of primes, then n is a product of primes . E. None of the above.

Unit 9: Induction 54

slide-55
SLIDE 55

Example 5

 Proof: we prove the result by induction on n.

  • Base case: n = 2
  • Since 2 is prime, the statement is true.
  • Induction step:
  • Let n be any integer greater than 2. Suppose that every

number from 2 to n-1 is a product of primes. We'll show that n is a product of primes

  • Case 1: ______________________

Unit 9: Induction 55

slide-56
SLIDE 56

Example 5

 Proof: we prove the result by induction on n.

  • Base case: n = 2 is prime.
  • Since 2 is prime, the statement is true.
  • Induction step:
  • Let n be any integer greater than 2. Suppose that every

number from 2 to n-1 is a product of primes. We'll show that n is a product of primes

  • Case 1: n is prime. Then the statement is true.

Unit 9: Induction 56

slide-57
SLIDE 57

Example 5

 Proof: we prove the result by induction on n.

  • Base case: n = 2 is prime.
  • Since 2 is prime, the statement is true.
  • Induction step:
  • Let n be any integer greater than 2. Suppose that every

number from 2 to n-1 is a product of primes. We'll show that n is a product of primes

  • Case 1: n is prime. Then the statement is true
  • Case 2: n is composite. Then

n =

Unit 9: Induction 57

slide-58
SLIDE 58

Example 5

 Proof: we prove the result by induction on n.

  • Base case: n = 2 is prime.
  • Since 2 is prime, the statement is true.
  • Induction step:
  • Let n be any integer greater than 2. Suppose that every

number from 2 to n-1 is a product of primes. We'll show that n is a product of primes

  • Case 1: n is prime. Then the statement is true
  • Case 2: n is composite. Then

n = a*b such that 1< a < n and 1< b < n

  • Unit 9: Induction

58

slide-59
SLIDE 59

Example 5

 Proof: we prove the result by induction on n.

  • Base case: n = 2 is prime.
  • Since 2 is prime, the statement is true.
  • Induction step:
  • Let n be any integer greater than 2. Suppose that every

number from 2 to n-1 is a product of primes. We'll show that n is a product of primes

  • Case 1: n is prime. Then the statement is true
  • Case 2: n is composite. Then

n = a*b such that 1< a < n and 1< b < n

  • By the induction hypothesis:

a = p1*p2*…*pm where pi is prime b = q1*q2*…*qr where qi is prime

  • and

n = p1*p2*…*pm*q1*q2*…*qr

QED

Unit 9: Induction 59

slide-60
SLIDE 60

Binary Trees

 CPSC 110 review: A binary

tree is a data structure that is defined recursively as following

 A binary tree is either

  • Empty, or
  • A node with some data, and two

children that are themselves binary trees.

Unit 9: Induction 60

20 9 2 15 5 10 17 7

slide-61
SLIDE 61

Proving Correctness : Binary trees

 Example 8: Consider the following function:

(define (tree-size t) (if (null? t) (+ 1 (tree-size (left-child t)) (tree-size (right-child t)))))

 How can we prove that it correctly computes the

number of (non-null) nodes of the tree?

Unit 9: Induction 61

slide-62
SLIDE 62

Example 8 : Binary trees

 We prove this using mathematical induction on the

size of the tree t.

  • Base case: t is null
  • In this case t contains exactly 0 nodes.
  • The algorithm returns 0. Therefore it is correct
  • Induction step:
  • Let t be any binary tree with size greater than 0.
  • Assume the algorithm works for trees that are smaller

than t.

  • Because the left sub-tree of t is smaller than t, the 1st

recursive calls returns the size of the left sub-tree of t.

Unit 9: Induction 62

slide-63
SLIDE 63

Example 8: Binary trees

  • Induction step (continued)
  • Similarly the right sub-tree of t is smaller than t, and so

the 2nd recursive call returns the size of the right sub- tree of t.

  • The algorithm then returns 1 + the sum of the values

returned by the recursive calls.

  • This is exactly the size of t (1 for the root + the sum of

the sizes of the two sub-trees).

  • Hence our algorithm computes correctly the size of every
  • tree. QED

Unit 9: Induction 63

slide-64
SLIDE 64

Example 9: Binary Search

 Example: binary search

  • Suppose we have something like a list, but whose i-th

element and length can be found in a single step.

  • This structure is called a vector in Racket.
  • It is similar to an ArrayList in Java.
  • We assume that we have such a vector, sorted in increasing
  • rder.
  • Example: (“Ann”, “Charles”, “Dora”, “Gregor”, “Wei”).
  • We want to find the position of a given element (for instance,

“Dora”).

Unit 9: Induction 64

slide-65
SLIDE 65

Example 9: Binary Search

 Claim: the following algorithm (formerly known as Binary

Search) works:

(define (binary-search avector first-pos last-pos x) (if (> first-pos last-pos) false (if (= first-pos last-pos) (if (= x (vector-ref avector first-pos)) first-pos false) (let ((mid-pos (quotient (+ first-pos last-pos) 2))) (if (= x (vector-ref avector mid-pos)) mid-pos (if (< x (vector-ref avector mid-pos)) (binary-search avector first-pos (- mid-pos 1) x) (binary-search avector (+ mid-pos 1) last-pos x)…)

Unit 9: Induction 65

slide-66
SLIDE 66

Example 9: Binary Search

 Proof: by induction on the size of the part of the vector

that we are searching.

  • Base cases: size ≤ 1
  • If size is 0, (i.e. first-pos > last-pos) then x can not be in

that part of the vector, so returning false is correct.

  • If size is 1, then there is only one possible location for x,

and the algorithm checks this position and returns the right value.

  • Induction step: let v be any vector of size ≥ 2
  • Suppose that the algorithm will find x (if it is in the vector)

for every vector with fewer than size elements.

  • We'll show that the algorithm will find x in any vector with

size elements.

Unit 9: Induction 66

slide-67
SLIDE 67

Binary Search

 Proof (continued)

  • If x is at position mid-pos of v, then the algorithm returns mid-

pos.

  • Otherwise, x is either smaller than the element at

position mid-pos, or larger.

  • If x is smaller then either x is in the first half of v or

not in v at all

  • Algorithm returns the result of searching the first half of v.

Since the size of the first half is less than v's size, by the IH the algorithm returns the correct result

  • If x is larger then either x is in the second half of v or

not in at all

  • Algorithm returns the result of searching the second half
  • f v. Since the size of the second half is less than v's size,

by the IH the algorithm returns the correct result

  • Hence by the principle of M.I., the algorithm returns the correct
  • value. QED

Unit 9: Induction 67

slide-68
SLIDE 68

Bad Example : What is Wrong ?

Theorem: All integers greater than or equal to 2 are even. Proof: We proceed by induction on n. Base Case : Theorem is true for the first case where n = 2. Since 2 = 2*1, 2 is even. Induction Step For any k >2, assume that k-2 is even and we’ll show that k is even.

  • Let k be any integer > 2 .
  • By the inductive hypothesis, k-2 is even.
  • Therefore k -2 = 2 m for some integer m
  • Then k = 2m + 2 = 2(m+1).
  • Since m+1 is an integer, k is even

QED

68

slide-69
SLIDE 69

Outline

 Example: single-elimination tournaments.  Validating mathematical induction.  A Pattern for Induction  More examples using induction.  A slightly different type of induction.  Additional Examples

Unit 9 : Induction 69

slide-70
SLIDE 70

Additional Examples

 Prove that for every non-negative integer n, the sum of

the first n powers of 2 is 2n+1 - 1.

 Prove that for every n ≥ 1, σ𝑗=1

𝑜 1 𝑗2 ≤ 2 − 1 𝑜

 (very challenging): Prove that binary search makes at

most ⌈log2 (size+1)⌉ comparisons if size ≥ 1. Give a proof by induction on the size of the vector. You can use the following theorem: Theorem: For any integer n ≥ 2, ⌈log2(2⌈n/2⌉)⌉ = ⌈log2 n⌉

Unit 9: Induction 70