CPSC 121: Models of Computation Module 9: Proof Techniques (part 2) - - PowerPoint PPT Presentation

cpsc 121 models of computation
SMART_READER_LITE
LIVE PREVIEW

CPSC 121: Models of Computation Module 9: Proof Techniques (part 2) - - PowerPoint PPT Presentation

CPSC 121: Models of Computation Module 9: Proof Techniques (part 2) Mathematical Induction Module 9: Announcements Assignment #5 is due Wednesday November 28 th at 19:00. Pre-class quiz #10 is tentatively due on Wednesday November 21 st at


slide-1
SLIDE 1

CPSC 121: Models of Computation

Module 9: Proof Techniques (part 2) Mathematical Induction

slide-2
SLIDE 2

CPSC 121 – 2018W T1 2

Module 9: Announcements

Assignment #5 is due Wednesday November 28th at 19:00. Pre-class quiz #10 is tentatively due on Wednesday November 21st at 19:00.

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.

slide-3
SLIDE 3

CPSC 121 – 2018W T1 3

Module 9: Mathematical Induction

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.

slide-4
SLIDE 4

CPSC 121 – 2018W T1 4

Module 9: Mathematical Induction

Pre-class quiz #9

Well done overall. You had a bit more trouble (77% avg) on the questions about the coin-changing induction proof. We will discuss induction proofs at length and do a lot of examples. Being comfortable with summations is important not

  • nly for CPSC 121, but for following courses too!

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

slide-5
SLIDE 5

CPSC 121 – 2018W T1 5

Module 9: Mathematical Induction

If you haven’t already done so,

read the rest of the chapters on mathematical induction (Epp4, 5.2 to 5.4, etc).

After this reading, you will be able to:

Given a theorem to prove stated in terms of its induction variable (i.e., usually, in terms of n), write

  • ut the skeleton of an inductive proof including:

the base case(s) that need to be proven, the induction hypothesis, and the inductive step that needs to be proven.

slide-6
SLIDE 6

CPSC 121 – 2018W T1 6

Module 9: Mathematical Induction

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

Establish 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. Prove properties of the non-negative integers (or a subset) with appropriate self-referential structure using weak or strong induction as needed.

slide-7
SLIDE 7

CPSC 121 – 2018W T1 7

Module 9: Mathematical Induction

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

CPSC 121: the BIG questions:

  • 1. How can we convince ourselves that an algorithm

does what it's supposed to do?

  • 2. How do we determine whether or not one algorithm

is better than another one?

Mathematical induction is a very very useful tool when proving the correctness or efficiency of an algorithm. We will see several examples of this.

slide-8
SLIDE 8

CPSC 121 – 2018W T1 8

Module 9: Mathematical Induction

Module Summary

Example: single-elimination tournaments. Defining and validating mathematical induction. More examples where we can use induction. A slightly different type of induction.

slide-9
SLIDE 9

CPSC 121 – 2018W T1 9

Module 9.1: Single-elimination tournaments

Problem: single-elimination tournament

Teams play one another in pairs The winner of each pair advances to the next round

Los Angeles Montréal Toronto New York Isl. Vancouver

  • St. Louis

Pittsburgh Buffalo Round 2

slide-10
SLIDE 10

CPSC 121 – 2018W T1 10

Module 9.1: Single-elimination tournaments

Question: if we have n rounds of playoffs, how many teams can participate?

a) n b) 2n c) n2 d) 2n e) None of the above.

slide-11
SLIDE 11

CPSC 121 – 2018W T1 12

Module 9.1: Single-elimination tournaments

How can we prove this result formally?

We will use a technique called mathematical induction. We will convince ourselves that it 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.

slide-12
SLIDE 12

CPSC 121 – 2018W T1 13

Module 9.1: Single-elimination tournaments

First we look at the following question instead:

If t teams can participate in a playoff with n rounds, how many teams can participate in a playoff with n+1 rounds? a) t+1 b) 2t c) t2 d) 2t e) None of the above.

slide-13
SLIDE 13

CPSC 121 – 2018W T1 15

Module 9.1: Single-elimination tournaments

Proof (with holes):

Consider an unspecified playoff with n rounds. Assume that We can think of a playoff with n+1 rounds as follows:

Two playoffs with n rounds proceed in parallel. The two winners then

Since each playoff with n rounds has

slide-14
SLIDE 14

CPSC 121 – 2018W T1 16

Module 9.1: Single-elimination tournaments

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(t, n): Up to t teams can participate in a playoff with n rounds

then we have proved

∀t Z ∈

+ n

Z ∀ ∈

+ , P(t, n)→P(2t, n+1)

slide-15
SLIDE 15

CPSC 121 – 2018W T1 17

Module 9.1: Single-elimination tournaments

So

We know that only 2 teams can play with 1 round. This is P(2, 1).

Given

P(2, 1) ∀t Z ∈

+ n

Z ∀ ∈

+, P(t, n) → P(2t, n+1)

We can deduce:

P(4, 2)

slide-16
SLIDE 16

CPSC 121 – 2018W T1 18

Module 9.1: Single-elimination tournaments

Given

P(4, 2) ∀t Z ∈

+ n

Z ∀ ∈

+, P(t, n) → P(2t, n+1)

We can deduce:

P(8, 3)

Given

P(8, 3) ∀t Z ∈

+ n

Z ∀ ∈

+, P(t, n) → P(2t, n+1)

We can deduce:

P(16, 4)

slide-17
SLIDE 17

CPSC 121 – 2018W T1 19

Module 9.1: Single-elimination tournaments

Given

P(16, 4) ∀t Z ∈

+ n

Z ∀ ∈

+, P(t, n) → P(2t, n+1)

We can deduce:

P(32, 5)

Etc... Therefore we can show that

∀n Z ∈

+, P(2n, n)

slide-18
SLIDE 18

CPSC 121 – 2018W T1 20

Module 9.1: Single-elimination tournaments

Another way to think about this:

Given an unspecified positive integer n. To prove P(2n+1, n+1):

First we prove P(2n, n). Then we use the fact that

∀t Z ∈

+ n

Z ∀ ∈

+, P(t, n) → P(2t, n+1)

Hence induction is more or less the same as recursion!

A fun example involving induction:

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

slide-19
SLIDE 19

CPSC 121 – 2018W T1 21

Module 9: Mathematical Induction

Module Summary

Example: single-elimination tournaments. Defining and validating mathematical induction. More examples where we can use induction. A slightly different type of induction.

slide-20
SLIDE 20

CPSC 121 – 2018W T1 22

Module 9.2: Defining and validating M.I.

Define Q(n) by:

Q(n): P(2n,n)

In the previous example, we proved

Q(1) ∀n Z ∈

+, Q(n) → Q(n+1)

and deduced that

∀n Z ∈

+, Q(n)

Why is this a valid proof technique?

slide-21
SLIDE 21

CPSC 121 – 2018W T1 23

Module 9.2: Defining and validating M.I.

Theorem:

Q(1) ^ ( n Z ∀ ∈

+, Q(n) → Q(n+1)) → n

Z ∀ ∈

+, Q(n)

Proof:

We use a proof by contradiction. Suppose that the premises hold, but that n Z ∀ ∈

+,

Q(n) is false. There is at least one value of n for which Q(n) does not hold. Let y be the smallest such value. Clearly y > 1, so y – 1 Z ∈

+.

slide-22
SLIDE 22

CPSC 121 – 2018W T1 24

Module 9.2: Defining and validating M.I.

Proof (continued):

Because y is the smallest value for which Q(n) is false, and y – 1 Z ∈

+, 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 ∀ ∈ Z+, Q(n) holds.

slide-23
SLIDE 23

CPSC 121 – 2018W T1 25

Module 9.2: Defining and validating M.I.

So a proof by mathematical induction has

One (or more) base case(s): Q(1)

Usually very straightforward to prove.

An induction step:

∀n Z ∈

+, Q(n) → Q(n+1) OR n

Z ∀ ∈

+, Q(n-1) → Q(n)

We can use any proof technique we know for it. Usually a direct proof will work well.

slide-24
SLIDE 24

CPSC 121 – 2018W T1 26

Module 9.2: Defining and validating M.I.

How do we decide which base cases we need?

Look at the induction step. Decide for which values

  • f n it can be used to prove Q(n).

The induction step might not be usable for some n:

because it needs Q(some x < n) and x would be smaller than smallest n for which Q holds. because some mathematical steps only work for large enough values of n.

If the induction step is not usable for n, then n needs to be a base case!

slide-25
SLIDE 25

CPSC 121 – 2018W T1 27

Module 9: Mathematical Induction

Module Summary

Example: single-elimination tournaments. Defining and validating mathematical induction. More examples where we can use induction. A slightly different type of induction.

slide-26
SLIDE 26

CPSC 121 – 2018W T1 28

Module 9.3: More induction examples

Example 1: recall the exercises from our first class:

Make group of 5 students. Then order each group from in order of day of birth by swapping pairs of adjacent students.

We claimed the maximum number of swaps for n students is n(n-1)/2.

slide-27
SLIDE 27

CPSC 121 – 2018W T1 29

Module 9.3: More induction examples

How many swaps do we need?

Suppose we place students from left to right.

The students already placed are ordered by day of birth. We swap each new student with his/her neighbour until he/she is at the right place.

The ith student may be swapped with all previous i-1 students. So the total number of swaps is at most Hence we need to prove that

i=0 n−1

i

i=0 n−1

i=n(n−1) 2

slide-28
SLIDE 28

CPSC 121 – 2018W T1 30

Module 9.3: More induction examples

Which fact(s) do we need to prove?

a) b) c) d) Both (a) and (b) e) Both (a) and (c)

i=0

i=0 ∀ n∈ℤ

+(∑ i=0 n−1

i= n(n−1) 2

)→(∑

i=0 n

i=(n+1) n 2

) (∀n∈ℤ

+ ,∑ i=0 n−1

i= n(n−1) 2

)→(∀ n∈ℤ

+ ,∑ i=0 n

i=(n+1)n 2

)

slide-29
SLIDE 29

CPSC 121 – 2018W T1 32

Module 9.3: More induction examples

Proof:

Base case: n = 1

Clearly, with only 1 student, no swap is needed: which is equal to 1 (1-1)/2

Induction step:

Pick an unspecified n ≥ 1. Assume that when we have n students, we need at most n(n-1)/2 swaps. Equivalently we assume that This is called the Induction Hypothesis.

i=0

i=0

i=0 n−1

i=n(n−1) 2

slide-30
SLIDE 30

CPSC 121 – 2018W T1 33

Module 9.3: More induction examples

Proof (continued)

Induction step (continued)

For n+1 students, we need at most swaps. By the induction hypothesis, this is equal to swaps, as required for the induction step.

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

i=0 n

i=(∑

i=0 n−1

i)+ n n(n−1) 2 + n=(n+ 1)n 2

slide-31
SLIDE 31

CPSC 121 – 2018W T1 34

Module 9.3: More induction examples

Example 2: 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.

i=0 t

a

i=a t+ 1−1

a−1

slide-32
SLIDE 32

CPSC 121 – 2018W T1 35

Module 9.3: More induction examples

Proof:

Base case: t = 0

In this case the summation is a0 = 1, and

Induction step:

Pick an unspecified t ≥ 0. Assume that Now

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

a

1−1

a−1 =1

slide-33
SLIDE 33

CPSC 121 – 2018W T1 36

Module 9.3: More induction examples

Example 3: 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 on the RHS larger, but never smaller.

Example: if I am smaller than you, then I am still smaller than you when you stand on a bench.

slide-34
SLIDE 34

CPSC 121 – 2018W T1 37

Module 9.3: More induction examples

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!

slide-35
SLIDE 35

CPSC 121 – 2018W T1 38

Module 9.3: More induction examples

Example 4: prove that for every n ≥ 1,

Note (this doesn’t help with the proof; it’s simply an interesting mathematical fact):

i=1 n 1

i

2≤2−1

n

i=1 ∞ 1

i

2=π 2

6

slide-36
SLIDE 36

CPSC 121 – 2018W T1 39

Module 9.3: More induction examples

Example 5: consider the following DFA: 3 2 4 1

slide-37
SLIDE 37

CPSC 121 – 2018W T1 40

Module 9.3: More induction examples

This DFA accepts the empty string, and which

  • ther strings?

a) Strings whose length is divisible by 5. b) Unsigned binary integers that are prime numbers. c) Strings where every 1 is followed by 01. d) Unsigned binary integers that are divisible by 5. e) None of the above.

slide-38
SLIDE 38

CPSC 121 – 2018W T1 42

Module 9.3: More induction examples

What should we prove (ignoring empty strings)?

a) The DFA is in state 0 if and only if it has seen one of the strings 1010, 1111 or 11001. b) The DFA is in state 0 if and only if s is divisible by 5. c) The DFA is in state 0 if and only if s is terminated by the string 101. d) The DFA is in state 0 if and only if s contains the substring 101 followed by zero or more 0's. e) None of the above.

slide-39
SLIDE 39

CPSC 121 – 2018W T1 44

Module 9.3: More induction examples

Theorem: The DFA ends up in state r after reading a string s if and only if the integer xs that s represents can be written as xs = 5q+r where q is an integer. Proof: by induction on the length n of s.

Base case: n = 1

This can be verified easily by looking at the DFA.

Induction step: consider an unspecified n > 1.

Induction hypothesis: the theorem holds for strings with n – 1 characters. Consider an unspecified string s with n bits.

slide-40
SLIDE 40

CPSC 121 – 2018W T1 45

Module 9.3: More induction examples

Proof (continued):

Suppose that s = bn-1bn-2...b2b1b0

Let xs be the unsigned integer represented by the string s.

and that t = bn-1bn-2...b2b1.

Let xt be the unsigned integer represented by the string t.

After reading the bits of t, the DFA is in some state r. By the induction hypothesis, the binary integer xt = 5q + r for some integer q.

slide-41
SLIDE 41

CPSC 121 – 2018W T1 46

Module 9.3: More induction examples

Proof (continued):

Now, xs = 2xt + b0. This means xs = 2(5q+r) + b0

= 10q + 2r + b0 = 5 (2q) + (2r + b0).

So the DFA should have the following transitions:

r b0 2r+b0 1 2 2 4 3 5 + 1 4 5 + 3 r b0 2r+b0 1 1 1 1 3 2 1 5 + 0 3 1 5 + 2 4 1 5 + 4

slide-42
SLIDE 42

CPSC 121 – 2018W T1 47

Module 9.3: More induction examples

Proof (continued):

It does! Hence after reading all n bits of s, the DFA ends up in state r if and only if the integer xs that s represents can be written as xs = 5q+r where q is an integer.

Hence by the principle of mathematical induction, the DFA ends up in state r after reading a string s if and

  • nly if $q

Z, s = 5q+r ∈ . QED

Corollary: the DFA is in state 0 if and only if s is divisible by 5.

slide-43
SLIDE 43

CPSC 121 – 2018W T1 48

Module 9.3: More induction examples

Mathematical induction is how we analyze recursive algorithms: Example:

(define (sum n) (if (= n 0) (+ n (sum (- n 1)))))

The type of induction we discussed so far works when the recursive call is with (- n 1)

i=0 n

i=n+(∑

i=0 n−1

i)

slide-44
SLIDE 44

CPSC 121 – 2018W T1 49

Module 9: Mathematical Induction

Module Summary

Example: single-elimination tournaments. Defining and validating mathematical induction. More examples where we can use induction. A slightly different type of induction.

slide-45
SLIDE 45

CPSC 121 – 2018W T1 50

Module 9.4: A slightly different type of induction

How do we handle more general recursions?

A recursive function/method has

One or more base cases

When it can compute the answer directly

An induction step

It computes recursively the solutions to one or more “smaller” sub-problems, and combines them to obtain its answer.

Our induction proofs mimic this structure.

It is similar to the templates you used in CPSC 110.

slide-46
SLIDE 46

CPSC 121 – 2018W T1 51

Module 9.4: A slightly different type of induction

We use a slightly different induction hypothesis.

Instead of proving that

∀x Z ∈

+, Q(x) → Q(x+1)

We prove that

∀x Z ∈

+, (Q(1) ^ Q(2) ^ ... ^ Q(x)) → Q(x+1)

We can also show that this type of induction is a valid proof technique.

The proof is the same proof by contradiction that we looked at earlier.

slide-47
SLIDE 47

CPSC 121 – 2018W T1 52

Module 9.4: A slightly different type of induction

CPSC 110 review: A binary tree is a data structure that is defined recursively: it is either

Empty, or A node with some data, and two children that are themselves trees.

20 9 2 15 5 10 17 7

slide-48
SLIDE 48

CPSC 121 – 2018W T1 53

Module 9.4: A slightly different type of induction

Example 6: 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?

slide-49
SLIDE 49

CPSC 121 – 2018W T1 54

Module 9.4: A slightly different type of induction

We prove this using mathematical induction on the size of the tree.

Base case: t is null

In this case t contains exactly 0 nodes.

Induction step:

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 correctly returns the size of the left sub- tree of t.

slide-50
SLIDE 50

CPSC 121 – 2018W T1 55

Module 9.4: A slightly different type of induction

Proof (continued)

Induction step (continued)

Similarly the right sub-tree of t is smaller than t, and so the 2nd recursive call correctly returns the size of the right sub-tree of t. But we return 1 + the sum of the values returned by the recursive calls. This is exactly the size of t (1 for the root, and the sum

  • f the sizes of the two sub-trees).

Hence by the principle of M.I., our algorithm computes correctly the size of every tree. QED

slide-51
SLIDE 51

CPSC 121 – 2018W T1 56

Module 9.4: A slightly different type of induction

Example 7: every positive integer n greater than 1 can be written as a product of primes. For which case(s) is the proof obvious?

a) n = 1 b) n = 2 c) n = 2, 3 or 5. d) n is prime. e) None of the above.

slide-52
SLIDE 52

CPSC 121 – 2018W T1 58

Module 9.4: A slightly different type of induction

Proof: we prove the result by induction on n.

Base case: n

We can write n = n and n is a product of primes (with

  • nly 1 prime).

Induction step: n

Suppose that every value from 2 to n – 1 is a product of primes. Since n

We can write n =

slide-53
SLIDE 53

CPSC 121 – 2018W T1 59

Module 9.4: A slightly different type of induction

Proof (continued)

Since 2 ≤ a < n, Since 2 ≤ b < n, Then

Hence by the principle of M.I., every positive integer larger than 1 can be written as a product of primes. QED

slide-54
SLIDE 54

CPSC 121 – 2018W T1 60

Module 9.4: A slightly different type of induction

Example 8: we can find the ith smallest element in an unsorted list as follows:

Pick a random element x of the list. Divide the list into three sublists:

list-smaller: elements smaller than x list-equal: elements equal to x list-larger: elements larger than x Then search

list-smaller if i ≤ length of list-smaller list-larger if i > length of list smaller + length of list-equal

  • therwise return x
slide-55
SLIDE 55

CPSC 121 – 2018W T1 61

Module 9.4: A slightly different type of induction

Example 8: continued

This algorithm is called randomized-quick-select. Racket code will be posted on the course web site. A student shows the expected number of steps S(n)

  • f the algorithm on a list with n elements is:

S(1) = 4c S(2) = 12c S(3) = 20c S(n) ≤ 2cn + S(ë3n/4û) when n ≥ 4

slide-56
SLIDE 56

CPSC 121 – 2018W T1 62

Module 9.4: A slightly different type of induction

Example 8: continued

Prove that for every n ≥ 1, S(n) ≤ 8cn.

slide-57
SLIDE 57

CPSC 121 – 2018W T1 63

Module 9.4: A slightly different type of induction

Example 9: binary search

Suppose we have something like a list, but whose ith 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 order.

Examples: (“Anh”, “Charles”, “Dora”, “Gregor”, “Wei”).

We want to find the position of a given element (for instance, “Dora”).

slide-58
SLIDE 58

CPSC 121 – 2018W T1 64

Module 9.4: A slightly different type of induction

The following algorithm (formerly known as B) works:

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

slide-59
SLIDE 59

CPSC 121 – 2018W T1 66

Module 9.4: A slightly different type of induction

Prove that binary search makes at most ⌈log2 (size+1)⌉ comparisons if size ≥ 1.

The proof is once again by induction on size. Base case: size =

The algorithm uses comparison, which is

Induction step:

Consider an unspecified size . Assume that