61
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)

CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)

Embed Size (px)

Citation preview

Induction and recursion

CS 103 Discrete Structures Lecture 13Induction and Recursion (1)

1Chapter 1 section 1.1 by Dr. Mosaad HassanInduction and recursionChapter 5With Question/Answer AnimationsChapter SummaryMathematical InductionStrong InductionWell-OrderingRecursive DefinitionsStructural Induction

Mathematical InductionSection 5.1Section SummaryMathematical InductionExamples of Proof by Mathematical InductionGuidelines for Proofs by Mathematical InductionMathematical InductionA technique for constructing proofs

Mathematical induction is based on the following rule of inference:(P(1) k [P(k) P(k + 1)]) n P(n), where nZ+

Proofs using induction require two steps: Basis step: Show that the statement P(n) holdsfor n = 1Inductive step: Show that if the statement P(n) holds for a positive integer n = k then it also holds for n = k + 1. P(k) is called the inductive hypothesis.

Mathematical Induction: ExampleConsider an infinite ladder, as shown in the figureTo know whether we can reach every step on this ladder, we know two things:We can reach the first rung of the ladderIf we can reach a particular rung of the ladder, then we can reach the next oneBy (1) and (2), weconclude that we canreach every rungProof by InductionMathematical induction can be used to prove that P(n) is true for all positive integers n

The proof by induction has two steps:Basis step: Verify that P(1) is trueInductive step: Show that for all +ive integers k, if P(k) is true, then P(k + 1) is true. That is, show thatP(k) P(k + 1) is true for all +ive integers kTo complete inductive step,Assume P(k) is true for an arbitrary +ive integer k Show that if P(k) is true then P(k + 1) must also be trueIf we verify those two steps, then P(n) is true for every +ive integer nThe above can be stated as a rule of inference as: (P(1) k [P(k) P(k + 1)]) n P(n), where n Z+Proof by Induction: Example 11 + 2 + 3 ++ n = n(n + 1)/2, where n Z+

Let P(n) be 1 + 2 + 3 ++ n = n(n + 1)/2Basis step: P(1) is true since 1 = 1(1 + 1)/2Inductive step: Let P(k) be true 1 + 2 + 3 + + k = k(k + 1)/2Based on that, we will show P(k + 1) to be trueFor that, add (k + 1) to both sides of P(k)1 + 2 + 3 ++ k + (k + 1)= k(k + 1)/2 + (k + 1) = [k(k + 1) + 2(k + 1)]/2= (k + 1)(k + 2)/2 P(k + 1) is true when P(k) is true(P(1) k [P(k) P(k + 1)]) n P(n), where n Z+Theorem is provedProof by Induction: Example 2Conjecture (guess) a formula for the sum of the first n positive odd integers and then prove your conjecture using mathematical induction.

The sum of the first n positive odd integers forn = 1, 2, 3, 4, 5 are:1 = 11 + 3 = 41 + 3 + 5 = 91 + 3 + 5 + 7 = 161 + 3 + 5 + 7 + 9 = 25The formula seems to be 1 + 3 + 5 + + (2n-1) = n2 Proof by Induction: Example 2Let P(n) be 1 + 3 + 5 + + (2n - 1) = n2

Basis step: P(1) is true since 1 = 12Inductive step: Let P(k) be true 1 + 3 + 5 + + (2k - 1) = k2 is trueBased on that, now we show P(k + 1) to be true. For that, we add the (k+1)st term 2(k + 1)-1 to both sides 1 + 3 + 5 ++ (2k - 1) + 2(k + 1) - 1= k2 + 2(k + 1) - 1= k2 + 2k + 1= (k + 1)2 P(k+1) is true when P(k) is true(P(1) k [P(k) P(k + 1)]) n P(n), where n Z+Theorem is provedProof by Induction: Example 31 + 2 + 22 +...+ 2n = 2n+1 1, where n Z and n 0

Let P(n) be 1 + 2 + 22 +... + 2n = 2n+1 - 1Basis step: P(0) is true since 1 = 20 = 21 - 1Inductive step: Let P(k) be true 1 + 2 + 22 + + 2k = 2k+1 - 1 is trueBased on that, now we show P(k + 1) to be true 1 + 2 + 22 + + 2k + 2k+1= 2k+1 - 1 + 2k+1= 2(2k+1) - 1= 2k+2 - 1 P(k + 1) is true when P(k) is true(P(1) k [P(k) P(k + 1)]) n P(n), n Z+, n 0Theorem is provedNote: Instead of starting with 1, we are starting with 0. As a rule, we start with the lowest allowed value, which generally is 1Proof by Induction: Example 4Sums of Geometric Progressions: Use mathematical induction to prove this formula for the sum of a finite number of terms of a geometric progression:

where n is a non-negative integer

Basis step: P(0) is true because

Proof by Induction: Example 4

Inductive step: Let P(k) be true

Based on that, we show P(k + 1) to be true. For that, add the (k + 1)st term to both sides of P(k)

P(k + 1) is true when P(k) is true(P(1) k [P(k) P(k + 1)]) n P(n), where n Z+

Proving Inequalities: Example 1n < 2n, where n Z+

Let P(n) be n < 2nBasis step: P(1) is true, because 1 < 21 = 2Inductive step: Let P(k) be truek < 2k is trueBased on that, now we show P(k + 1) to be truek + 1 < 2k + 1 (add 1 to both sides of P(k))2k + 1 < 2k + 2k (1 < 2k for all k)k + 1 < 2k + 2kk + 1 < 2(2k)k + 1 < 2k+1 P(k + 1) is true when P(k) is true(P(1) k [P(k) P(k + 1)]) n P(n), n Z+Proving Inequalities: Example 22n < n! where n Z+ and n 4

Let P(n) be 2n < n!Basis step: P(4) is true, because 24 = 16 < 4! = 24Inductive step: Let P(k) be true2k < k! is trueBased on that, now we show P(k + 1) to be true 2(2k) < 2(k!) (multiply both sides of P(k) by 2) 2k+1 < (k + 1)k! (2 < k + 1 for all k) 2k+1 < (k + 1)! P(k + 1) is true when P(k) is true(P(1) k [P(k) P(k + 1)]) n P(n), n Z+, n 4

Proving Divisibility: ExampleProve that n3 - n is divisible by 3 where n Z+

Let P(n) be the proposition that n3 - n is divisible by 3Basis step: P(1) is true since 13 - 1 = 0, which is divisible by 3Inductive step: Assume P(k) holds, i.e., k3 - k is divisible by 3, for an arbitrary positive integer k. To show that P(k + 1) follows, (k + 1)3 - (k + 1) = (k3 + 3k2 + 3k + 1) - (k + 1) = (k3 - k) + 3(k2 + k) By the inductive hypothesis, the first term (k3 - k) is divisible by 3 and the 2nd term is divisible by 3 since it is an integer multiplied by 3. Therefore, the sum of the two term is divisible by 3 P(k + 1) is true when P(k) is true(P(1) k [P(k) P(k + 1)]) n P(n), where n Z+Mathematical Induction: Template

Section 5.1: ExercisesFor both exercises, answer the following questions:

Exercises

Exercises

Exercises

CS 103 Discrete Structures Lecture 14Induction and Recursion (2)

23Chapter 1 section 1.1 by Dr. Mosaad HassanStrong Induction and Well-OrderingSection 5.2Section SummaryStrong InductionExample Proofs using Strong InductionWell-Ordering Property

Weak and Strong InductionWeak (or Mathematical) Induction Inductive Step: Assume P(k) to be true, and use that (and only that) to show P(k + 1) is trueP(k) P(k + 1)

Strong Induction Inductive Step: Assume P(1), P(2),, P(k) to be true, and use that to show that P(k + 1) is true[P(1) P(2) P(3) P(k)] P(k + 1)Strong InductionTo prove that P(n) is true for all positive integers n, where P(n) is a propositional function, complete two steps:

Basis Step: Verify that the proposition P(1) is true

Inductive Step: Show the conditional statement[P(1) P(2) P(k)] P(k + 1) holds for all positive integers kStrong Induction is sometimes called the second principle of mathematical induction or complete inductionStrong Induction: Infinite Ladder

Strong induction tells us that we can reach all rungs if:We can reach the first rung of the ladderFor every integer k, if we can reach the first k rungs, then we can reach the (k + 1)st rung

By doing so we will have then shown by strong induction that for every positive integer n, P(n) holds, i.e., we can reach the nth rung of the ladder

Strong Induction: Example 1For an infinite ladder, prove that we can reach every rung if:we can reach the 1st and 2nd rungs andif we can reach a rung, then we can reach 2 rungs higher(Try this with mathematical induction!) Solution: Proof uses strong inductionBASIS STEP: We can reach the 1st rungINDUCTIVE STEP: The inductive hypothesis is that we can reach the first k rungs, for any k 2. We can reach the (k + 1)st rung since we can reach the (k 1)st rung by the inductive hypothesis we can reach all rungs of the ladderWhich Form of Induction Should Be Used?We can always use strong induction instead of mathematical induction. But there is no reason to use it if it is simpler to use mathematical induction

In fact, the principles of mathematical induction and strong induction are equivalent

Strong Induction: Example 2If n Z and n > 1, then n can be written as the product of primesProof: Let P(n) be the proposition that n can be written as a product of primesBASIS STEP: P(2) is true since 2 can be written as a product one prime, itselfINDUCTIVE STEP: The inductive hypothesis is that P(j) is true for all integers j with 2 j k. To show that P(k + 1) must be true under this assumption, two cases need to be considered:If k + 1 is prime, then P(k + 1) is trueOtherwise, k + 1 is composite and can be written as the product of two positive integers a and b with 2 a b < (k + 1). By the inductive hypothesis a and b can be written as the product of primes and therefore k + 1 can also be written as the product of those primesIntegers greater than 1 can be written as the product of primesStrong Induction: Example 3Prove that every amount of postage of 12 cents or more can be formed using just 4-cent and 5-cent stamps Proof: Let P(n) be the proposition that postage of n cents can be formed using 4-cent and 5-cent stampsBASIS STEP: P(12), P(13), P(14), and P(15) holdP(12) uses three 4-cent stampsP(13) uses two 4-cent stamps and one 5-cent stampP(14) uses one 4-cent stamp and two 5-cent stampsP(15) uses three 5-cent stampsINDUCTIVE STEP: The inductive hypothesis states that P(j) holds for 12 j k, where k 15. Assuming the inductive hypothesis, it can be shown that P(k + 1) holdsUsing the inductive hypothesis, P(k 3) holds since k 3 12. To form postage of k + 1 cents, add a 4-cent stamp to the postage fork 3 centsP(n) holds for all n 12Same Example, but with Mathematical InductionProve that every amount of postage of 12 cents or more can be formed using just 4-cent and 5-cent stamps Proof: Let P(n) be the proposition that postage of n cents can be formed using 4-cent and 5-cent stampsBASIS STEP: Postage of 12 cents can be formed using three 4-cent stamps, therefore P(12) is trueINDUCTIVE STEP: The inductive hypothesis P(k) for any positive integer k is that postage of k cents can be formed using 4-cent and 5-cent stamps. To show P(k + 1) where k 12, consider two cases:If at least one 4-cent stamp has been used, then a 4-cent stamp can be replaced with a 5-cent stamp to yield a total of k + 1 centsOtherwise, no 4-cent stamp have been used and at least three 5-cent stamps were used. Three 5-cent stamps can be replaced by four 4-cent stamps to yield a total of k + 1 centsP(n) holds for all n 12Well-Ordering PropertyEvery nonempty set of nonnegative integers has a least elementExample 1: The set {2, 3, 9} has a least element, i.e. 1Example 2: The set of prime numbers has a least element, i.e. 2This property is one of the axioms of the positive integers and is equivalent to mathematical induction and strong inductionIt can be generalized to all sets instead of just integersDefinition: A set is well ordered if every subset has a least elementExample 1: N is well ordered under Example 2: The set of finite strings over an alphabet using lexicographic ordering is well ordered

Using the Well-Ordering Property in ProofsThe Division Algorithm: If n Z and d Z+, then there are unique integers q & r, 0 r < d, such that n = dq + r.n:dividend, d:divisor, q:quotient, r:remainder, e.g. 43 = 85+3Proof: Let S Z+ of the form n - dq. S is nonempty since -dq can be made as large as neededBy the well-ordering property, S has a least element r = n - dq0. Integer r N. It also must be the case that r < d. If it were not, then there would be a smaller nonnegative element in S, namely, n - d(q0 + 1). To see this, suppose that r d.Because n = d(q0 + 1), it follows that n - dq0 - d = r - d 0Consequently, there are integers q and r with 0 r < dSection 5.2: ExercisesLet a1 = 2, a2 = 9, and an = 2an1 + 3an2 for n 3. Use strong induction to show that an < 3n for all positive integers nRecursive Definitions and Structural InductionSection 5.3Section SummaryRecursively Defined FunctionsRecursively Defined Sets and StructuresStructural InductionGeneralized Induction

Recursively Defined FunctionsA recursive or inductive definition of a function consists of two steps

BASIS STEP: Specify the value of the function at zero

RECURSIVE STEP: Give a rule for finding its value at an integer from its values at smaller integersRecursive Functions: Examples Example 1: Suppose f is defined by: f(0) = 3, f(n + 1) = 2f(n) + 3 Find f(1), f(2), f(3), f(4) Solution:f(1) = 2f(0) + 3 = 23 + 3 = 9f(2) = 2f(1)+ 3 = 29 + 3 = 21f(3) = 2f(2) + 3 = 221 + 3 = 45f(4) = 2f(3) + 3 = 245 + 3 = 93

Example 2: Give a recursive definition of the factorial function n! Solution:f(0) = 1f(n + 1) = (n + 1) f(n)Recursive Functions: Example 3Give a recursive definition of:

Solution: The first part of the definition is

The second part is

Recursive Functions: Example 4The Fibonacci numbers are defined as follows:f0 = 0f1 = 1fn = fn1 + fn2 Find f2, f3 , f4 , f5

f2 = f1 + f0 = 1 + 0 = 1f3 = f2 + f1 = 1 + 1 = 2f4 = f3 + f2 = 2 + 1 = 3f5 = f4 + f3 = 3 + 2 = 5

In Chapter 8, we will use the Fibonacci numbers to model population growth of rabbits. This was an application described by Fibonacci himself42CS 103 Discrete Structures Lecture 15Induction and Recursion (3)

43Chapter 1 section 1.1 by Dr. Mosaad HassanRecursively Defined SetsRecursive definitions of sets have two parts:The basis step specifies an initial collection of elementsThe recursive step gives the rules for forming new elements in the set from those already known to be in the setSometimes the recursive definition has an exclusion rule, which specifies that the set contains nothing other than those elements specified in the basis step and generated by applications of the rules in the recursive stepWe will always assume that the exclusion rule holds, even if it is not explicitly mentionedWe will later develop a form of induction, called structural induction, to prove results about recursively defined setsRecursively Defined Sets: ExamplesExample 1: Subset of integers SBASIS STEP: 3 SRECURSIVE STEP: If x S and y S, then x + y is in SInitially 3 is in S, then 3 + 3 = 6, then 3 + 6 = 9, etc.

Example 2: The natural numbers NBASIS STEP: 0 NRECURSIVE STEP: If n is in N, then n + 1 is in NInitially 0 is in S, then 0 + 1 = 1, then 1 + 1 = 2, etc.

Recursively Defined StringsThe set * of strings over the alphabet :BASIS STEP: * ( is the empty string)RECURSIVE STEP: If w is in * and x is in , then wx* Example 1: If = {0, 1}, the strings in * are the set of all bit strings, , 0, 1, 00, 01, 10, 11, 000, 001, etc. Example 2: If = {a, b}, show that aab is in *Since * and a , therefore a *Since a * and a , therefore aa *Since aa * and b , therefore aab *

46String ConcatenationStrings can be combined via the operation of concatenation Let be the set of alphabet and * be the set of strings formed from the alphabet in . We can define the concatenation of two strings, denoted by , recursively as:BASIS STEP: If w *, then w = wRECURSIVE STEP: If w1 * and w2 * and x , then w1 (w2 x) = (w1 w2)xOften w1 w2 is written as w1w2If w1 = abra and w2 = cadabra, then w1w2 = abracadabraRecursive Definitions: String LengthThe length of a string can be recursively defined by:

BASIS STEP:l(w) = 0

RECURSIVE STEP:l(wx) = l(w) + 1 if w * and x Example: Balanced ParenthesesGive a recursive definition of the set of balanced parentheses P

Solution:BASIS STEP: () PRECURSIVE STEP: If w P, then ()w P, (w) P and w() P

Show that (() ()) is in P() P and w() P imply that ()() PFurthermore, (w) P implies that (()()) PWhy is ))(() not in P?Because P includes only matched pairs of parenthesesWell-Formed FormulaeThe set of well-formed formulae in propositional logic involving T, F, propositional variables, and operators from the set {, , , , }BASIS STEP: T, F, and x, where x is a propositional variable, are well-formed formulaeRECURSIVE STEP: If E and F are well formed formulae, then(E), (E F), (E F), (E F), (E F), are well-formed formulae

Example 1: [(p q) (q F)] is a well-formed formulaExample 2: pq is not a well formed formulaRooted TreesThe set of rooted trees, where a rooted tree consists of a set of vertices containing a distinguished vertex called the root, and edges connecting these vertices, can be defined recursively by these steps:BASIS STEP: A single vertex r is a rooted treeRECURSIVE STEP: Suppose that T1, T2, ,Tn are disjoint rooted trees with roots r1, r2,,rn, respectively. Then the graph formed by starting with a root r, which is not in any of the rooted trees T1, T2, ,Tn , and adding an edge from r to each of the vertices r1, r2,,rn, is also a rooted tree Building Up Rooted Trees

Trees are studied extensively in Chapter 11 Next we look at a special type of tree, the full binary tree Full Binary TreesAfull binary treeis atreein which every node has either zero or two childrenThe set of full binary trees can be defined recursively by these stepsBASIS STEP: There is a full binary tree consisting of only a single vertex rRECURSIVE STEP: If T1 and T2 are disjoint full binary trees, there is a full binary tree, denoted by T1T2, consisting of a root r together with edges connecting the root to each of the roots of the left subtree T1 and the right subtree T2Building Up Full Binary Trees

Induction & Recursively Defined Sets: ExampleProve that A = S if set S is defined by a recursive definition:Basis step: 3 S Recursive step: if x S and y S, then (x + y) Sand A = { 3, 6, 9, 12, 15, } = { x | x = 3n and n Z+ }

Proof: To prove that A = S, show that A S and S AA S: Let P(n) be 3n S, i.e. every element of A is in SBASIS STEP: 31 = 3 S, by the 1st part of recursive definitionINDUCTIVE STEP: Assume P(k) is true, i.e. 3k S. Since 3 S, it follows that 3k + 3 = 3(k + 1) S. P(k + 1) is trueS A:BASIS STEP: 3 S by the 1st part of recursive definition, and 3 = 31INDUCTIVE STEP: The 2nd part of the recursive definition adds x + y to S, if both x and y are in S. If x and y are both in A, then both x and y are divisible by 3. their sum x + y is also divisible by 3We used mathematical induction to prove a result about a recursively defined set. Next we study a more direct form of induction for proving results about recursively defined setsStructural InductionTo prove a property of the elements of a recursively defined set, we use structural inductionBASIS STEP: Show that the result holds for all elements specified in the basis step of the recursive definitionRECURSIVE STEP: Show that if the statement is true for each of the elements used to construct new elements in the recursive step of the definition, the result holds for these new elementsThe validity of structural induction can be shown to follow from the principle of mathematical inductionFull Binary Trees: Height, VerticesHeight h(T) of a full binary tree T is defined recursively as:BASIS STEP: The height of a full binary tree T consisting of only a root r is h(T) = 0RECURSIVE STEP: If T1 and T2 are full binary trees, then the full binary tree T = T1T2 has height h(T) = 1 + max[h(T1), h(T2)]

Number of vertices n(T) of a full binary tree T satisfies the following recursive formula:BASIS STEP: The number of vertices of a full binary tree T consisting of only a root r is n(T) = 1RECURSIVE STEP: If T1 and T2 are full binary trees, then the full binary tree T = T1T2 has the number of verticesn(T) = 1 + n(T1) + n(T2)Structural Induction & Binary TreesIf T is a full binary tree, then n(T) 2h(T)+1 1Proof: Use structural inductionBASIS STEP: The result holds for a full binary tree consisting only of a root, n(T) = 1 and h(T) = 0. Hence, n(T) = 1 20+1 1 = 1RECURSIVE STEP: Assume n(T1) 2h(T1)+1 1 and alson(T2) 2h(T2)+1 1 whenever T1 and T2 are full binary treesn(T)= 1 + n(T1) + n(T2)by recursive formula of n(T) 1 + (2h(T1)+1 1) + (2h(T2)+1 1)by inductive hypothesis 2max[2h(T1)+1 , 2h(T2)+1 ] 1because the sum of two terms is atmost two times the larger= 22max(h(T1), h(T2))+1 1 max(2x , 2y)= 2max(x,y) = 22h(t) 1 by recursive definition of h(T)= 2h(t)+1 1 2 . Generalized InductionGeneralized induction is used to prove results about sets (other than the integers) that have the well-ordering property

Example: Consider an ordering on NN, ordered pairs of nonnegative integers. Specify that (x1, y1) is less than or equal to (x2, y2) if either x1 < x2, or x1 = x2 and y1 < y2 . This is called the lexicographic orderingStrings are also commonly ordered by a lexicographic orderingThe next example uses generalized induction to prove a result about ordered pairs from NNGeneralized Induction: ExampleIf am,n is defined for (m, n) NN by a0,0 = 0 and

Show that am,n = m + n(n + 1)/2 is defined for all (m,n)NNSolution: Use generalized inductionBASIS STEP: a0,0 = 0 = 0 + 0(0 + 1)/2INDUCTIVE STEP: Assume that am,n = m+ n(n + 1)/2 whenever (m,n) is less than (m,n) in the lexicographic ordering of NNIf n = 0, by the inductive hypothesis we can conclude am,n = am1,n + 1 = m 1 + n(n + 1)/2 + 1 = m + n(n + 1)/2If n > 0, by the inductive hypothesis we can conclude am,n = am1,n + 1 = m + n(n 1)/2 + n = m + n(n + 1)/2

Section 5.3: ExercisesFind f(2) and f(3) if f(n) = 2f(n 1) + 6 and f(0) = 3Find f(2) and f(3) if f(n) = f(n 1) f(n 2) + 1 andf(0) = 1, f(1) = 4Give a recursive definition with initial condition(s) for the function f(n) = 2n, n = 1, 2, 3, . . . Give a recursive definition with initial condition(s) for the function f(n) = n!, n = 0, 1, 2, . . . Give a recursive definition with initial condition(s) of the set S = {3, 7, 11, 15, 19, 23, . . .}Give a recursive definition with initial condition(s) of the set S = {x | x is a multiple of 5, x Z+}Give a recursive definition with initial condition(s) of the strings 1, 111, 11111, 1111111, . . .