Based on slides by Patrice Belleville and Steve Wolfman
CPSC 121: Models of Computation Unit 9 Mathematical Induction - - PowerPoint PPT Presentation
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,
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
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
Quiz 9 Feedback
Generally: Issues: Essay Question:
- As usual, we will revisit the open-ended question shortly.
4 Unit 9 : Induction
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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