CMPU145 Foundations of Computer Science Spring 2019 Chapter

  • Slides: 17
Download presentation
CMPU-145: Foundations of Computer Science Spring, 2019 Chapter 4: Using Induction and Recursion CMPU

CMPU-145: Foundations of Computer Science Spring, 2019 Chapter 4: Using Induction and Recursion CMPU 145 – Foundations of Computer Science Peter Lemieszewski

Natural Numbers • The English language borrows words from virtually every corner of the

Natural Numbers • The English language borrows words from virtually every corner of the globe (and history). • Etymology: the study of word origins. • Much of the language of mathematics comes from Greek and Latin words like: • Axiom: A statement that is accepted as true and without proof • • From the Greek: axioma, meaning worth or quality Aka Postulate, from Latin: postulatum, mean a thing demanded Accepted as a priori knowledge* Axioms form the basis of… • Hypotheses: A premise • From the Greek: hypó, meaning under and; thesis, meaning a thing laid down • Hypotheses, as it relates to maths, help to form one or more… • Theorems: A proposition that can be proven to be true • From the Greek: theórema, meaning a subject for contemplation • Sometimes, theorems need a supporting cast… • Lemma: An intermediate theorem used is a “helper function” in Racket-speak • From the Greek: lémma, meaning a thing taken 9/25/2020 * Who to blame: Immanual Kant 2

Natural Numbers Defined I • Peano Axioms: Another first! Mathemation, Giuseppe Peano (1858 -1932)

Natural Numbers Defined I • Peano Axioms: Another first! Mathemation, Giuseppe Peano (1858 -1932) created a bunch (!) of axioms that are used to define the set natural numbers. (1) The recursive definition of the elements that make up N: • There exists a number 0 such that 0 ∈ N • Every natural number n has a natural number successor function denoted by: S(n). (2) The properties of the set N: • There does not exist an n ∈ N such that S(n) = 0 • Or… 0 ≠ S(n) for any n ∈ N • ∀ m and n in N, if m≠n, then S(n) ≠ S(m) • In other words, distinct natural numbers have distinct successors. • This means that the successor function is injective! 9/25/2020 CMPU 145 – Foundations of Computer Science 3

Natural Numbers Defined II (2) The properties of the set N: • There does

Natural Numbers Defined II (2) The properties of the set N: • There does not exist an n ∈ N such that S(n) = 0 • Or… 0 ≠ S(n) for any n ∈ N • ∀ m and n in N, if m≠n, then S(n) ≠ S(m) • In other words, distinct natural numbers have distinct successors. • This means that the successor function is injective! • If the set A is any subset of N with the property that 0 ∈ A and when ever n ∈ A , then both S(n) ∈ A & A = N 9/25/2020 CMPU 145 – Foundations of Computer Science 4

Natural Numbers Defined III Let’s resequence that last postulate as an axiom of induction:

Natural Numbers Defined III Let’s resequence that last postulate as an axiom of induction: • If the set A is any subset of N with the property that 0 ∈ A and when ever n ∈ A , then both S(n) ∈ A & A = N • Let P(n) is any property of natural numbers, such that a) P(0) holds true and, b) For every n, P(n) -> P(S(n)) holds true too. • Then P(n) holds true for all n∈ N • 9/25/2020 i. e. every natural number! CMPU 145 – Foundations of Computer Science 5

Natural Numbers: Examples Now we can use recursive definition to define things like… Addition!

Natural Numbers: Examples Now we can use recursive definition to define things like… Addition! then both S(n) ∈ A & A = N • Let P(n) be the property of addition such that a) n + 0 = n b) For a distinct m, n: n + S(m) = S(n+m) • We can write 1 = S(0), 2 = S(1), etc. • We should be able to prove that n + 1 = S(n) too! 9/25/2020 CMPU 145 – Foundations of Computer Science 6

Recursive Definition of Addition • Let’s (bottom-up) calculate 1 + 2 … • 1

Recursive Definition of Addition • Let’s (bottom-up) calculate 1 + 2 … • 1 + 0 = 1 • 1 + 1 = 1 + S(0) = S(1 + 0) = S(1) = 2 • 1 + 2 = 1 + S(1) = S(1 + 1) = S(2) = 3 etc. 9/25/2020 CMPU 145 – Foundations of Computer Science 7

We can also perform: • Top-down addition of 1 + 2 and more… •

We can also perform: • Top-down addition of 1 + 2 and more… • • 9/25/2020 1 = = = = + 2 1 + S(1) S(1 + S(0)) S(S(1 + 0)) S(S(1)) S(2) 3 CMPU 145 – Foundations of Computer Science 8

The Commutative Property of Addition I • Theorem: Given the following rules of addition:

The Commutative Property of Addition I • Theorem: Given the following rules of addition: a) x + 0 = x for all natural numbers x b) x + S(y) = S(x+y) for all natural numbers x and y then 0 + x = x for all natural numbers x. • Proof: Let P(x) be the proposition: 0 + x = x. • We want to show that P(x) holds true for all natural numbers x. • Base case: P(0): 0 + 0 = 0. LHS = 0 + 0 = 0, = RHS 9/25/2020 by rule a) above CMPU 145 – Foundations of Computer Science 9

The Commutative Property of Addition II • Theorem: Given the following rules of addition:

The Commutative Property of Addition II • Theorem: Given the following rules of addition: a) x + 0 = x for all natural numbers x b) x + S(y) = S(x+y) for all natural numbers x and y then 0 + x = x for all natural numbers x. • Proof: Let P(x) be the proposition: 0 + x = x. • We want to show that P(x) holds true for all natural numbers x. • Recursive case: Assume P(k): 0 + k = k. Prove P(S(k)): 0 + S(k) = S(k). LHS = 0 + S(k) = S(0 + k) by rule b) above = S(k) by P(k) = RHS 9/25/2020 CMPU 145 – Foundations of Computer Science 10

Another Commutative Property of Addition • Theorem: Adding 1 to one term is commutative

Another Commutative Property of Addition • Theorem: Adding 1 to one term is commutative x + S(y) = S(x)+y for all natural numbers x and y 9/25/2020 CMPU 145 – Foundations of Computer Science 11

Another Commutative Property of Addition I • Theorem: Given the following rules of addition:

Another Commutative Property of Addition I • Theorem: Given the following rules of addition: a) x + 0 = x for all natural numbers x b) x + S(y) = S(x+y) for all natural numbers x and y then x + S(y) = S(x) + y for all natural numbers x. 9/25/2020 CMPU 145 – Foundations of Computer Science 12

Another Commutative Property of Addition II • Theorem: Given the following rules of addition:

Another Commutative Property of Addition II • Theorem: Given the following rules of addition: a) x + 0 = x for all natural numbers x b) x + S(y) = S(x+y) for all natural numbers x and y then x + S(y) = S(x) + y for all natural numbers x. • Proof: Let P(m) be the proposition: x + S(n)=S(x) + n ∀ x. • We want to show that P(x) holds true for all natural numbers x. • Base case: P(0): x + S(0) = S(x) LHS = x + = = = S(0) S(x + 0) S(x)+ 0 = RHS 9/25/2020 by rule b) above by rule a) above CMPU 145 – Foundations of Computer Science 13

Another Commutative Property of Addition III • Theorem: Given the following rules of addition:

Another Commutative Property of Addition III • Theorem: Given the following rules of addition: a) x + 0 = x for all natural numbers x b) x + S(y) = S(x+y) for all natural numbers x and y then x + S(y) = S(x) + y for all natural numbers x. • Proof: Let P(m) be the proposition: x + S(n)=S(x) + n ∀ x. • We want to show that P(x) holds true for all natural numbers x. • Recursive case: Assume P(k): x + S(k) = S(x) + k. Prove P(S(k)): x + S(S(k)) = S(x) + S(k). LHS = x + S(S(k)) = S(x + S(k)) by rule b) above = S(S(x) + k) by P(k) = S(x) + S(k) by rule b) above = RHS 9/25/2020 CMPU 145 – Foundations of Computer Science 14

More Recursively defined functions … • • • Multiplication defined recursively: a) Base case:

More Recursively defined functions … • • • Multiplication defined recursively: a) Base case: x * 0 = 0 for all natural numbers b) Recursive case: x * S(y) = (x*y) + x for all natural numbers x, y We can also define: predecessor, “pre” a) Base case: pre(0) = 0 b) Recursive case: pre(S(x)) = x for all natural numbers x, y We can also define: abbreviated subtraction a) Base case: x-0 = 0 b) Recursive case: x-(S(y)) = pre(x-y) for all natural numbers x, y 9/25/2020 CMPU 145 – Foundations of Computer Science 15

Revisiting Set Definitions • A recursive definition of a set S consists of: •

Revisiting Set Definitions • A recursive definition of a set S consists of: • Base: One or more foundational elements in S • Recursive: One or more rules to construct new elements in S from existing elements in X • Closure: The condition that S consists of all and only the elements derived from the base elements and the recursion rules • Pretty much our definition for the set of natural numbers. 9/25/2020 CMPU 145 – Foundations of Computer Science 16

Another Handout ? • Yes. There is another handout. . . • Please read

Another Handout ? • Yes. There is another handout. . . • Please read this handout before Tuesday! 9/25/2020 CMPU 145 – Foundations of Computer Science 17