Quick viewing(Text Mode)

Chapter 3 Notes

Chapter 3 Notes

Section 3.0 3.0.1

Chapter 3

MATHEMATICAL REASONING

3.1 Proof Strategy 3.2 and Summations 3.3 3.4 Recursive Definitions 3.5 Recursive Algorithms 3.6∗ Program Correctness

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.1 Proof Strategy 3.1.1

3.1 PROOF STRATEGY

FORWARD AND BACKWARD REASONING

Example 3.1.1: Backward Reasoning (a + b) √ Let a, b ∈R, with a = b. Then > ab. 2 √ Proof: Conclusion is true if a + b>2 ab, which is true if (a + b)2 > 4ab, which is true if a2 +2ab + b2 > 4ab, which is true if a2 − 2ab + b2 > 0, which is true if (a − b)2 > 0, which is true. ♦

Example 3.1.2: Foreward Reasoning Let n ∈N, such that n is not divisible by 2 or 3. Then n2 − 1isdivisible by 24.

Proof: Since n is not divisible by 2 or 3, it follows that n =6k +1or6k +5. Case 1. (6k +1)2 − 1 ≡ 36k2 +12k mod 24 ≡ 12k2 +12k ≡ 0mod 24, since 2 divides k2 + k. Case 2. (6k +5)2 − 1 ≡ 36k2 +60k +24mod24 ≡ 12k2 +12k ≡ 0mod24, as above. ♦

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.1.2

TWO FAMOUS PROBLEMS Fermat’s Last Thm: For n>2, the equation ab + bn = cn has no solutions for non-zero a, b, c. Status: Proved by Andrew Wiles. Goldbach Conjecture: Every even number larger than 2 is the sum of two odd primes. Status: Open.

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.2 Sequences and Summations 3.2.1

3.2 SEQUENCES AND SUMMATIONS def: A in a A is a f from a subset of the integers (usually {0, 1, 2,...} or {1, 2, 3,...})toA. The values of a sequence are also called terms or entries. notation: The value f(n)isusually denoted an. A sequence is often written a0,a1,a2,.... Example 3.2.1: Two sequences.

1 1 1 1 a = 1, , , ,... n n 2 3 4 n bn =(−1) 1, −1, 1, −1,...

Example 3.2.2: Five ubiquitous sequences.

n2 0, 1, 4, 9, 16, 25, 36, 49,... n3 0, 1, 8, 27, 64, 125, 216, 343,... 2n 1, 2, 4, 8, 16, 32, 64, 128,... 3n 1, 3, 9, 27, 81, 243, 729, 2187,... n!1, 1, 2, 6, 24, 120, 720, 5040,...

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 FOUNDATIONS 3.2.2

STRINGS def: A set of characters is called an alphabet. Example 3.2.3: Some common alphabets: {0, 1} the binary alphabet {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} the decimal digits {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,A,B,C,D,E,F} the hexadecimal digits {A, B, C, D, . ..,X,Y,Z} English uppercase ASCII def: A string is a sequence in an alphabet. notation: Usually a string is written without commas, so that consecutive characters are jux- taposed. Example 3.2.4: If f(0) = M,f(1) = A, f(2) = T, and f(3) = H, then write “MATH”.

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.2 Sequences and Summations 3.2.3

SPECIFYING a RULE

Problem: Given some initial terms a0,a1, ..., ak of a sequence, try to construct a rule that is consistent with those initial terms. Approaches: There are two standard kinds of rule for calculating a generic term an. def: A for an is a function whose arguments are earlier terms in the sequence. def: A closed form for an is a formula whose argument is the subscript n.

Example 3.2.5: 1, 3, 5, 7, 9, 11,... recursion: a0 =1; an = an−1 +2for n ≥ 1 closed form: an =2n +1 The differences between consecutive terms often suggest a recursion. Finding a recursion is usually easier than finding a closed formula.

Example 3.2.6: 1, 3, 7, 13, 21, 31, 43,... recursion: b0 =1; bn = bn−1 +2n for n ≥ 1 2 closed form: bn = n + n +1

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 FOUNDATIONS 3.2.4

Sometimes, it is significantly harder to construct a closed formula. Example 3.2.7: 1, 1, 2, 3, 5, 8, 13, 21, 34,... recursion: c0 =1,c1 =1; cn = cn−1 + cn−2 for n ≥ 1 1 m+1 m+1 closed form: cn = √ G − g √ 5 √ 1+ 5 1 − 5 where G = and g = 2 2

INFERRING a RULE The ESSENCE of science is inferring rules from partial data. Example 3.2.8: Sit under apple . Infer gravity. Example 3.2.9: Watch starlight move 0.15 arc-seconds in total eclipse. Infer relativity. Example 3.2.10: Observe biological species. Infer DNA.

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.2 Sequences and Summations 3.2.5

Important life skill: Given a difficult general problem, start with special cases you can solve. Example 3.2.11: Find a recursion and a closed form for the arithmetic progression: c, c + d, c +2d, c +3d, ... recursion: a0 = c; an = an−1 + d closed form: an = c + nd. Q: How would you decide that a given sequence is an arithmetic progression? A: Calculate differences betw consec terms. def: The difference sequence for a sequence − ≥ an is the sequence an = an an−1 for n 1. an :1357911 Example 3.2.5 redux: an :22222 Analysis: Since an is constant, the sequence is specified by this recursion: a0 =1;an = an−1 +2for n ≥ 1. Moreover, it has this closed form: ··· an = a0 + a1 + a2 + + an

= a0 +2+2+···+2 =1+2n

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 FOUNDATIONS 3.2.6

If you don’t get a constant sequence on the first difference, then try reiterating. Revisit Example 1.7.6: 1, 3, 7, 13, 21, 31, 43,...

bn :13713213143 bn :246 8 1012 bn :222 2 2 Analysis: Since bn is constant, we have bn = 2+2n Therefore,

··· bn = b0 + b1 + b2 + + bn n 2 2 = b0 +2 j = 1+(n + n)=n + n +1 j=1

Consolation Prize: Without knowing about finite sums, you can still extend the sequence:

bn :1371321314357 bn :246 8 101214 bn :222 2 2 2

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.2 Sequences and Summations 3.2.7

SUMMATIONS def: Let an beasequence. Then the big-sigma notation n aj j=m means the sum

am + am+1 + am+2 + ···+ an−1 + an terminology: j is the index of summation terminology: m is the lower limit terminology: n is the upper limit terminology: aj is the summand Theorem 3.2.1. These formulas for summing falling powers are provable by induction (see §3.3): n 1 n 1 j1 = (n +1)2 j2 = (n +1)3 2 3 j=1 j=1 n 1 n 1 j3 = (n +1)4 jk = (n +1)k+1 4 k +1 j=1 j=1

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 FOUNDATIONS 3.2.8

Example 3.2.12: True Love and Thm 3.2.1 On the jth day ... True Love gave me (j +1)2 j +(j − 1) + ···+1= gifts. 2

1 13 1 = j2 = 22 + ···+132 2 2 j=2 1 = [2+6+···+ 78] = 364 slow 2 1 143 = · = 364 fast 2 3

Corollary 3.2.2. High-powered look-ahead to formulas for summing jk : j =0, 1, ..., n.

n n 1 1 j2 = (j2 + j1)= (n +1)3 + (n +1)2 3 2 j=1 j=1 n n j3 = (j3 +3j2 + j1)=··· j=1 j=1

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.2 Sequences and Summations 3.2.9

POTLATCH RULES for CARDINALITY def: nondominating cardinality: Let A and B be sets. Then |A|≤|B| means that ∃ one-to- one function f : A → B. def: Set A and B have equal cardinality (write |A| = |B|)if∃ bijection f : A → B, which obviously implies that |A|≤|B| and |B|≤|A|. def: strictly dominating cardinality: Let A and B be sets. Then |A| < |B| means that |A|≤|B| and |A| = |B|. def: The cardinality of a set A is n if |A| = |{1, 2,...,n}| and0ifA = ∅. Such cardi- nalities are called finite. notation: |A| = n. def: The cardinality of N is ω (“omega”), or alternatively, ℵ0 (“aleph null”). def: A set is countable if it is finite or ω.

Remark: ℵ0 is the smallest infinite cardinality. The real numbers have cardinality ℵ1 (“aleph one”), which is larger than ℵ0, for reasons to be given.

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 FOUNDATIONS 3.2.10

INFINITE CARDINALITIES

Proposition 3.2.3. There are as many even nonnegative numbers as non-negative numbers. Proof: f(2n)=n is a bijection. ♦

Theorem 3.2.4. There are as many positive integers as rational fractions.

1 1 1 1 1 1 … 1 2 3 4 5 6 2 2 2 2 2 2 … 1 2 3 4 5 6 3 3 3 3 3 3 … 1 2 3 4 5 6 4 4 4 4 4 4 … 1 2 3 4 5 6 5 5 5 5 5 5 … 1 2 3 4 5 6 MMMMMMO p (p + q − 1)(p + q − 2) Proof: f = + p ♦ q 2 2 (4)(3) Example 3.2.13: f = + 2=8 3 2

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.2 Sequences and Summations 3.2.11

Theorem 3.2.5. (G. Cantor) There are more positive real numbers than positive integers.

Semi-proof: A putative bijection f : Z+ →R+ would generate a sequence in which each real number appears somewhere as an infinite decimal fraction, like this:

f(1) = .8841752032669031 ... f(2) = .1415926531424450 ... f(3) = .3202313932614203 ... f(4) = .1679888138381728 ... f(5) = .0452998136712310 ...... f(?) = .73988 ...

Let f(n)k be the kth digit of f(n), and let π be the permutation 0 → 9, 1 → 0,...9 → 8. Then the infinite decimal fraction whose kth digit is π(f(n)k)isnot in the sequence. Therefore, the function f is not onto, and accordingly, not a bijection. ♦

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.3 Mathematical Induction 3.3.1

3.3 MATHEMATICAL INDUCTION

From modus ponens: p basis assertion p → q conditional assertion q conclusion we can easily derive “double modus ponens”:

p0 basis assertion p0 → p1 conditional assertion p1 → p2 conditional assertion

p2 conclusion

We might also derive triple modus ponens, quadruple modus ponens, and so on. Thus, we have no trouble proving assertions about arbi- trarily large integers. For instance, The initial domino falls. If any of the first 999 dominoes falls, then so does its successor. Therefore, the first 1000 dominoes all fall down.

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.3.2

The induction axiom for the integers may be characterized as THE GREAT LEAP TO INFINITY Given a countably infinite row of dominoes, suppose that: (1) The initial domino falls. (2) If domino n, then so domino n +1. Conclusion: All the dominoes all fall down.

basis induction hypothesis

and (∀n)

0nn+1 imply

• • • • •

01234

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.3 Mathematical Induction 3.3.3

Example 3.3.1: a proof by induction Calculate the sum of the first k oddnumbers: 1+3+5+···+(2k − 1) Practical Method for General Problem Solving. Special Case: Deriving a Formula Step 1. Calculate the result for some small cases. Step 2. Guess a formula to match all those cases. Step 3. Verify your guess in the general case. Step 1. examine small cases (empty sum) = 0 1=1 1+3=4 1+3+5=9 1+3+5+7=16 Step 2. It sure looks like 1+3+...+(2k−1) = k2. Step 3. Try to prove this assertion by induction.   k (∀k)  (2j − 1) = k2 j=1

(see next page for proof)

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.3.4   k Basis Step.  (2j − 1) = k2 when k =0 j=1   k Ind Hyp.  (2j − 1) = k2 when k = n j=1 Ind Step. Consider the case k = n +1.

n+1 n (2j − 1) = (2j − 1) + [2(n +1)− 1] j=1 j=1 n = (2j − 1)+2n +1 j=1 = n2 +2n +1by ind. hyp. =(n +1)2 by factoring ♦

Why is induction important to CS majors? It is the method used to prove that a loop or a recursively defined function correctly calculates the intended result. (just for a start)

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.3 Mathematical Induction 3.3.5

Example 3.3.2: another proof by induction Calculate the sum of the first k numbers: 1+2+3+···+ k Step 1. examine small cases 0 · 1 (empty sum)=0= 2 1 · 2 1=1= 2 2 · 3 1+2=3= 2 3 · 4 1+2+3=6= 2 4 · 5 1+2+3+4=10= 2 k k(k +1) Step 2. Infer pattern: j = . 2 j=1 Step 3. Use induction proof to verify pattern. See next page.

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.3.6

k k(k +1) Proposition 3.3.1. j = . 2 j=1

k k(k +1) 0 · 1 Basis Step. j = = when k =0. 2 2 j=1 k k(k +1) Ind Hyp. j = when k = n. 2 j=1 Ind. Step. n+1 n j = j +(n +1) j=1 j=1 n(n +1) = +(n +1) byind hyp 2 n(n +1) 2(n +1) = + by arithmetic 2 2 n(n +1)+2(n +1) = by arithmetic 2 (n + 2)(n +1) = distrib in numerator 2 (n + 1)(n +2) = commutativity ♦ 2

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.3 Mathematical Induction 3.3.7

NONALGEBRAIC APPLICATIONS of INDUCTION Consider tiling a 2k-by-2k chessboard. (k=3inthefigure below) with L-shaped tiles, so that one corner-square is left uncovered.

Basis Step. You can do this whenk=0. Ind Hyp. Assume you can do this fork=n. Ind. Step. Prove you can do it fork=n+1.

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.3.8

ALTERNATIVE FORMS of INDUCTION In a proof by induction, verifying the inductive premise means you show that the antecedent of the quantified statement implies the conclusion. def: In a proof by mathematical induction, the inductive hypothesis is the antecedent of the inductive premise. Standard 0-based inductive rule of inference: 0 ∈ S basis premise (∀n)[n ≥ 0 ∧ n ∈ S ⇒ n +1∈ S] ind prem (∀n)[n ≥ 0 ⇒ n ∈ S] conclusion

Alternative Form 1. Using an other than zero as a basis. b ∈ S basis premise (∀n)[n ≥ b ∧ n ∈ S ⇒ n +1∈ S] ind prem (∀n)[n ≥ b ⇒ n ∈ S] conclusion

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.3 Mathematical Induction 3.3.9

Example 3.3.3: using 5 as the basis n2 > 2n +1for all n ≥ 5 Basis Step. 52 > 2 · 5+1 Ind Hyp. Assume k2 > 2k +1for k ≥ 5. Ind. Step. (k +1)2 = k2 +2k +1 by arithmetic > (2k +1)+2k +1 byind hyp =4k +2 by arithmetic =2(k +1)+2k by arithmetic ≥ 2(k +1)+10 since k ≥ 5 ≥ 2(k +1)+1 since 10 ≥ 1 ♦

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.3.10

Example 3.3.4: 2n >n2 for all n ≥ 5. Basis Step. 25 > 52 Ind Hyp. Assume 2k >k2 for k ≥ 5 Ind. Step. 2k+1 =2· 2k arithmetic =2k +2k arithmetic >k2 + k2 ind. hyp. >k2 +(2k +1)byExample 3.3.3 =(k +1)2 arithmetic ♦

Example 3.3.5: Prove that any postage of 8 cents or more can be created from nothing but 3-cent and 5-cent stamps. Basis Step. 8 = 1 · 3/c+1· 5/c Ind Hyp. Assume n/cpossible from 3’s and 5’s. Ind. Step. Try to make (n + 1)/cpostage. Suppose that n = r · 3/c+s · 5/c Case 1: s ≥ 1. Then n +1=... Case 2: s =0. Then n +1=...

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.3 Mathematical Induction 3.3.11

Alternative Form 2. Inductive hyposthesis is that the first n dominoes all fall down. b ∈ S basis premise (∀n)[n ≥ b ∧ (∀k ≤ n)[k ∈ S] ⇒ n +1∈ S] ind p (∀n)[n ≥ b ⇒ n ∈ S] conclusion

Example 3.3.6: Prove that every integer n>0 is the product of finitely many primes. Basis Step. 1 is the empty product. Ind Hyp. Assume that 1,...,n are each a prod- uct of finitely many primes. Ind Step. (1) Either n +1is prime, or ∃b, c ∈Zsuch that n +1=bc. (law of excl middle, def of prime) (2) But b and c are the products of finitely many primes. (by Ind Hyp) (3) Thus, so is bc. ♦

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.3.12

Mind-Benders re Induction 1. 2/3 ancestry 2. All solid billiard balls are the same color. 3. Everyone is essentially bald.

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.4 Recursive Definitions 3.4.1

3.4 RECURSIVE

Functions can be defined recursively. The simplest form of recursive definition of a function f on the natural numbers specifies a basis rule (B) the value f(0) and a recursion rule (R) how to obtain f(n) from f(n − 1), ∀n ≥ 1 Example 3.4.1: n- n! (B) 0!=1 (R) (n + 1)!=(n +1)· n!

However, recursive definitions often take some- what more general forms. Example 3.4.2: mergesort (A[1 ...2n]: real) ifn=0 return(A) otherwise return(merge (m’sort(1st half), m’sort(2nd half)))

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.4.2

Since a sequence is defined to be a special kind of a function, some sequences can be specified recursively. Example 3.4.3: Hanoi sequence 0, 1, 3, 7, 15, 31,... h0 =0 hn =2hn−1 +1for n ≥ 1 Example 3.4.4: Fibonacci seq 1, 1, 2, 3, 5, 8, 13,... f0 =1 f1 =1 fn = fn−1 + fn−2 for n ≥ 2 Example 3.4.5: partial sums of sequences n a0 if n =0 aj = n−1 j=0 aj + an otherwise j=0

Example 3.4.6: Catalan sequence 1, 1, 2, 5, 14, 42,... c0 =1 cn = c0cn−1 + c1cn−2 + ···+ cn−1c0 for n ≥ 1

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.4 Recursive Definitions 3.4.3

RECURSIVE of SETS def: A recursive definition of a set S com- prises the following: (B) a basis clause that specifies a set of primitive elements; (R) a recursive clause that specifies how ele- ments of the set may be constructed from ele- ments already known to be in set S; there may be several recursive subclauses; (E) an implicit exclusion clause that anything not in the set as a result of the basis clause or the recursive clause is not in set S. Backus Normal Form (BNF) is an example of a context-free grammar that is useful for giving resursive definitions of sets. In W3261, you will learn that context-free languages are recognizable by pushdown automata.

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.4.4

Example 3.4.7: a rec. def. set of integers (B) 7, 10 ∈ S (R) if r ∈ S then r +7,r+10∈ S

This reminds us of the postage stamp problem. Claim (∀n ≥ 54)[n ∈ S] Basis: 54 = 2 · 7+4· 10 Ind Hyp: Assume n = r · 7+s · 10 with n ≥ 54. Ind Step: Two cases. Case 1: r ≥ 7. Then n+1=(r−7)·7+(s+5)·10. Case 2: r<7 ⇒ r · 7 ≤ 42 ⇒ s ≥ 2. Then n + 1=(r +3)· 7+(s − 2) · 10.

In computer science, we often use recursive definitions of sets of strings.

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.4 Recursive Definitions 3.4.5

RECURSIVE DEFINITION of STRINGS notation: The set of all strings in the alphabet Σisgenerally denoted Σ∗. Example 3.4.8: {0, 1}∗ denotes the set of all binary strings. def: string in an alphabet Σ (B) (empty string) λ is a string; (R) If s is a string and b ∈ Σ, then sb is a string.

Railroad Normal Form for strings

λ

character

Example 3.4.9: BNF for strings string ::= λ |stringcharacter

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.4.6

RECURSIVE DEFINITION of IDENTIFIERS def: An identifier is (for some programming languages) either (B) a letter, or (R) an identifier followed by a digit or a letter.

letter

letter

digit

Example 3.4.10: BNF for identifiers

lowercase letter ::= a | b |···|z uppercase letter ::= A | B |···|Z letter ::= lowercase letter|uppercase letter digit ::= 0 | 1 | ···|9 identifier ::= letter|identifierletter |identifierdigit

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.4 Recursive Definitions 3.4.7

ARITHMETIC EXPRESSIONS def: arithmetic expressions (B) A numeral is an arithmetic expression.

(R) If e1 and e2 are arithmetic expressions, then all of the following are arithmetic expressions:

e1 + e2,e1 − e2,e1 ∗ e2,e1/e2,e1 ∗∗e2, (e1)

Example 3.4.11: Backus Normal Form

expression ::= numeral |expression + expression |expression−expression |expression∗expression |expression/expression |expression∗∗expression | (expression)

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.4.8

SUBCLASSES of STRINGS Example 3.4.12: binary strings of even length (B) λ ∈ S (R) If b ∈ S, then b00,b01,b10,b11 ∈ S. Example 3.4.13: binary strings of even length that start with 1 (B) 10, 11 ∈ S (R) If b ∈ S, then b00,b01,b10,b11 ∈ S. def: A strict palindrome is a character string that is identical to its reverse. (In natural lan- guage, blanks and other punctuation are ignored, as is the distinction between upper and lower case letters.) Able was I ere I saw Elba. Madam, I’m Adam. Eve. Example 3.4.14: set of binary palindromes (B) λ, 0, 1 ∈ S (R) If x ∈ S then 0x0, 1x1 ∈ S.

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.4 Recursive Definitions 3.4.9

LOGICAL PROPOSITIONS def: propositional forms (B) p, q, r, s, t, u, v, w are propositional forms (R) If x and y are propositional forms, then so are ¬x, x ∧ y, x ∨ y, x → y, x ↔ y and (x). Propositional forms under basis clause (B) are called atomic. Remark: Recursive definition of a set facilitates proofs by induction about properties of its elements.

Proposition 3.4.1. Every proposition has an even number of parentheses. Proof: by induction on the length of the derivation of a proposition. Basis Step. All the atomic propositions have evenly many parentheses. Ind Step. Assume that propositions x and y have evenly many parentheses. Then so do proposi- tions ¬x, x ∧ y, x ∨ y, x → y, x ↔ y and (x). ♦

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.4.10

CIRCULAR DEFINITIONS def: Awould-be recursive definition is circular if the sequence of iterated applications it generates fails to terminate in applications to elements of the basis set. Example 3.4.15: a circular definition from Index and Glossary of Knuth, Vol 1. Circular Definition, 260 see Definition, circular Definition, circular, see Circular definition

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.5 Recursive Algorithms 3.5.1

3.5 RECURSIVE ALGORITHMS review : An algorithm is a computational representation of a function. Remark: Although it is often easier to write a correct recursive algorithm for a function, iterative implementations typically run faster, because they avoid calling the stack. RECURSIVELY DEFINED ARITHMETIC Example 3.5.1: recursive of natural numbers: succ = successor, pred = predecessor.

Algorithm 3.5.1: recursive addition

recursive function: sum(m, n) Input: integers m ≥ 0,n≥ 0 Output: m + n If n =0then return (m) else return sum(succ(m),pred(n))

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.5.2

Example 3.5.2: iterative addition of natural numbers

Algorithm 3.5.2: iterative addition

function: sum(m, n) Input: integers m ≥ 0,n≥ 0 Output: m + n While n>0 do m := succ(m); n := pred(n); endwhile Return (m)

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.5 Recursive Algorithms 3.5.3

Example 3.5.3: proper subtraction of natural numbers: succ = successor, pred = predecessor.

Algorithm 3.5.3: proper subtraction

recursive function: diff(m, n) Input: integers 0 ≤ n ≤ m Output: m − n If n =0then return (m) else return diff(pred(m),pred(n))

Example 3.5.4: natural :

Algorithm 3.5.4: natural multiplication

recursive function: prod(m, n) Input: integers 0 ≤ n ≤ m Output: m × n If n =0then return (0) else return prod(m, pred(n)) + m

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.5.4

Example 3.5.5: factorial function:

Algorithm 3.5.5: factorial

recursive function: factorial(n) Input: integer n ≥ 0 Output: n! If n =0then return (1) else return prod(n, factorial(n − 1)) notation: Hereafter, we mostly use the infix notations +, -, *, and ! to mean the functions sum, diff, prod, and factorial, respectively.

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.5 Recursive Algorithms 3.5.5

RECURSIVELY DEFINED RELATIONS def: The (Iverson) truth function true assigns to an assertion the boolean value TRUE if true and FALSE otherwise. Example 3.5.6: order relation:

Algorithm 3.5.6: order relation

recursive function: ge(m, n) Input: integers m, n ≥ 0 Output: true (m ≥ n) If n =0then return (TRUE) elseif m =0then return (FALSE) else return ge(m − 1,n− 1)

Time-Complexity: Θ(min(m, n)).

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.5.6

OTHER RECURSIVELY DEFINED FUNCTIONS review Euclidean algorithm:

Algorithm 3.5.7: Euclidean algorithm

recursive function: gcd(m, n) Input: integers m>0,n≥ 0 Output: gcd (m, n) If n =0then return (m) else return gcd(n, m mod n)

Time-Complexity: O(ln n). Example 3.5.7: Iterative calc of gcd (289, 255) m1 = 289 n1 = 255 r1 =34 m2 = 255 n2 =34 r2 =17 m3 =34 n3 =17 r3 =0

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.5 Recursive Algorithms 3.5.7 def: The execution of a function exhibits exponential recursive descent if a call at one level can generate multiple calls at the next level. Example 3.5.8: Fibonacci function f0 =1,f1 =1,fn = fn−1 + fn−2 for n ≥ 2 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,... √ 1+ 5 n Time-Complexity: Θ . 2

Algorithm 3.5.8: Fibonacci function

iterative speedup function: fibo(n) Input: integer n ≥ 0 Output: fibo (n) If n =0∨ n =1then return (1) else fn−2 := 1; fn−1 := 1; for j := 2 to n step 1 fn := fn−1 + fn−2; fn−2 := fn−1; f − := f ; endfor n 1 n return fn

Time-Complexity: Θ(n).

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.5.8

RECURSIVE STRING OPERATIONS Σ=set, c ∈ Σ (object), s ∈ Σ∗ (string) Σ0 =Λ={λ} strings of length 0 Σn =Σn−1 × Σ strings of length n Σ+ =Σ1 ∪ Σ2 ∪··· all finite non-empty strings Σ∗ =Σ0 ∪ Σ1 ∪ Σ2 ∪··· all finite strings

These three primitive string functions are all defined and implemented nonrecursively for arbi- trary sequences, not just strings of characters. def: appending a character to a string append : Σ∗ × Σ → Σ∗ non-recursive (a1a2 ···an,c) → a1a2 ···anc def: first character of a non-empty string first : Σ+ → Σ non-recursive a1a2 ···an → a1 def: trailer of a non-empty string trailer : Σn → Σn−1 a1a2 ···an → a2 ···an

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.5 Recursive Algorithms 3.5.9

These four secondary string functions are all defined and implemented recursively. def: length of a string length : Σ∗ →N length(s)=0 ifs = λ = 1+length(trailer(s)) if s = λ def: concatenation of two strings concat : Σ∗ × Σ∗ → Σ∗ (s ◦ t)=s if t = λ = append(s, first(t)) ◦ trailer(t)ifs = λ notation: It is customary to overload the concatenation operator ◦ so that it also appends. def: reversing a string reverse : Σ∗ → Σ∗ s−1 = s if s = λ = trailer(s)−1 ◦ first(s)ifs = λ def: last character of a non-empty string last: Σ+ → Σ last(s)=first(s−1)

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.5.10

RECURSIVE ARRAY OPERATIONS

Algorithm 3.5.9: location recursive function: location(x, A[]) Input: target value x, sorted array A[] Output: 0ifx ∈ A; min{j | x = A[j]} if x ∈ A If length (A)=1then return (true (x = A[1])) elseif x ≤ midval(A) return location(x, fronthalf(A)) else return location(x, backhalf(A)) function: midindex(A) Input: array A[] Output: middle location of array A midindex(A)= length(A)/2 function: midval(A) Input: array A[] Output: value at middle location of array A midval(A)=A[midindex(A)] continued on next page

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Section 3.5 Recursive Algorithms 3.5.11

Algorithm 3.5.9: location, continuation function: fronthalf(A) Input: array A[] Output: front half-array of array A fronthalf(A)=A[1 ...midindex(A)] function: backhalf(A) Input: array A[] Output: back half-array of array A fronthalf(A)=A[midindex(A)+1...length(A)]

Time-Complexity: Θ(log n).

Algorithm 3.5.10: verify ascending order recursive function: ascending(A[]) Input: array A[] Output: TRUE if ascending; FALSE if not if length (A[])≤ 1 then return (TRUE) else return (a1 ≤ a2 ∧ ascending(trailer(A[ ])))

Time-Complexity: Θ(n).

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed. Chapter 3 MATHEMATICAL REASONING 3.5.12

Algorithm 3.5.11: merge sequences recursive function: merge(s, t) Input: ascending sequences s, t Output: merged ascending sequence If length (s)=0then return t elseif s1 ≤t1 return first(s) ◦ merge(trailer(s),t) else return first(t) ◦ merge(s, trailer(t))

Algorithm 3.5.12: mergesort recursive function: msort(A) Input: ascending array A[] Output: merged ascending array If length (A[])≤ 1 then return (A[]) else return merge(msort(fronthalf(A), msort(backhalf(A))

Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.