23
§3.1 Inductively Defined Sets & §3.2 Recursive Functions Take A = {3, 5, 7,... } . Although we can’t be entirely certain, presumably this means that A = { 2n +3 | n N } . Another way to describe A is to say: 3 A and, for all n N, if n A, then n +2 A . If we want to understand this last description of A as a definition, it has three (!) parts, as follows: There is an “initial” element of A, namely 3. You construct additional elements of A by adding 2 to any element of A. Nothing else belongs to A. We call this an “inductive definition” of A.

3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

Embed Size (px)

Citation preview

Page 1: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

§3.1 Inductively Defined Sets & §3.2 Recursive Functions

TakeA = {3, 5, 7, . . . } .

Although we can’t be entirely certain, presumably this means that

A = { 2n + 3 | n ∈ N } .

Another way to describe A is to say:

3 ∈ A and, for all n ∈ N, if n ∈ A, then n + 2 ∈ A .

If we want to understand this last description of A as a definition,it has three (!) parts, as follows:

◮ There is an “initial” element of A, namely 3.

◮ You construct additional elements of A by adding 2 to any element of A.

◮ Nothing else belongs to A.

We call this an “inductive definition” of A.

Page 2: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

General form of inductive definition of a set

An inductive definition of a set S has the following form:

◮ Basis: Specify one or more “initial” elements of S .

◮ Induction: Give one or more rules for constructing “new” elements of Sfrom “old” elements of S .

◮ Closure: Understand that S consists of exactly the elements that can beobtained by starting with the initial elements of S and applying the rulesfor constructing new elements of S .

Typically the closure condition is assumed (that is, left unstated), since it isstandard.

There is another, more mathematically elegant way to understand the closurecondition: S is the least set satisfying both the basis and induction

conditions.

Another way to understand this: S is the intersection of all sets that satisfy

both the basis and induction conditions.

Page 3: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

Example: Let S be defined as follows:

◮ Basis: 0 ∈ S .

◮ Induction: If n ∈ S , then n + 1 ∈ S .

Then S = N.

And we can check this. How? We can verify that N is the least set thatsatisfies the basis and induction conditions in the definition of the set S .[Actually, we’ll check that N is minimal, which for reasons we won’t fullyexplain (yet?), guarantees that it is least.]

1. N satisfies the basis condition.

2. N satisfies the induction condition.

3. No proper subset of N satisfies both conditions. Let’s check this. . .

Take any proper subset X of N. [We need to show that X doesn’t satisfy bothconditions. Why?] There is a least natural number n missing from X . (That’sa powerful claim.) Consider two cases. [Why are these cases exhaustive?]

Case 1: n = 0. Then X doesn’t satisfy the basis condition.

Case 2: n = k + 1 for some k ∈ X . Then X doesn’t satisfy the inductioncondition. (Why?)

Page 4: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

§3.1.1 Sets of Natural Numbers

An example: Let S be defined as follows:

◮ Basis: 0 ∈ S .

◮ Induction: If n ∈ S , then 2n + 1 ∈ S .

So what is S?

Notice: 20 − 1 = 0, and for all x ∈ N,

2x+1 − 1 = 2(2x − 1) + 1 .

Page 5: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

§3.1.2 An inductive definition of A∗ and languagesIf A is an alphbet (a finite set), the set of all strings over A, A∗, can be definedas follows:

◮ Basis: Λ ∈ A∗.

◮ Induction: If s ∈ A∗ and x ∈ A, then xs ∈ A∗.

[What happens if we replace xs above by sx?]

Let L be the language over {0, 1} defined as follows:

◮ Basis: Λ ∈ L.

◮ Induction: If s ∈ L, then 0s1 ∈ L.

Let L be the language over {0, 1} defined as follows:

◮ Basis:

1. Λ ∈ L.2. If x ∈ {0, 1}, then x ∈ L.

◮ Induction: If s ∈ L and x ∈ {0, 1}, then xsx ∈ L.

Page 6: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

Example: The set of decimal numbers, D :

◮ Basis: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ∈ D .

◮ Induction: If x ∈ D and d is a decimal digit, then dx ∈ D

Page 7: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

§3.1.3 Sets of lists over AIn Chapter 1 we introduced lists over a set A and the operations cons(h, L)where h ∈ A and L is a list over A, head(L), and tail(L).

We can define lists(A), the set of all lists over A as follows:

◮ Basis: 〈〉 ∈ lists(A).

◮ Induction: If x ∈ A and L ∈ lists(A), then cons(x , L) ∈ lists(A).

A Notational Convenience: instead of cons(x , L) we write:

x :: L

so L = head(L) :: tail(L) for example.

We assume :: is right-associative, a :: b :: L means a :: (b :: L), so we write:

a :: b :: c :: 〈〉 instead of a :: (b :: (c :: 〈〉))

Example the set S of lists of alternating elements from {0, 1}:

◮ Basis: 〈0〉, 〈1〉 ∈ S .

◮ Induction: If L ∈ S and head(L) = 0 then 1 :: L ∈ S .If L ∈ S and head(L) = 1 then 0 :: L ∈ S .

Page 8: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

§3.1.4 Sets of binary trees over A

Define the set B of binary trees over set A as follows:

◮ Basis: 〈〉 ∈ B.

◮ Induction: If L,R ∈ B and x ∈ A, then 〈L, x ,R〉 ∈ B.

Note: This is the list representation of binary trees, given as a shorthand forthe longer notation “tree(L, x ,R)” which is also presented in the text.

Define the set Twins over set A as follows:

◮ Basis: 〈〉 ∈ Twins.

◮ Induction: If x ∈ A and T ∈ Twins, then 〈T , x ,T 〉 ∈ Twins.

Page 9: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

For any nonempty binary tree T = 〈L, x ,R〉, let

left(T ) = L , root(T ) = x , right(T ) = R .

Define the set Opps over set {0, 1} as follows:

◮ Basis: If x ∈ {0, 1}, then 〈〈〉, x , 〈〉〉 ∈ Opps.

◮ Induction: If x , y ∈ {0, 1}, T ∈ Opps, and y 6= root(T ),then 〈T , x , 〈right(T ), y , left(T )〉〉 ∈ Opps.

Page 10: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

Other inductively defined setsSubsets of N× N, in §3.1.5 — Skip

Define a set F of subsets of N as follows:

◮ Basis: ∅ ∈ F .

◮ Induction: If n ∈ N and S ∈ F , then S ∪ {n} ∈ F .

What is F?

Can we prove this? (By showing that it is a minimal set satisfying the basisand induction conditions.)

Page 11: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

§3.2 Recursively Defined Functions

Recall: An inductive definition of a set S has the following form:

◮ Basis: Specify one or more “initial” elements of S .

◮ Induction: Give one or more rules for constructing “new” elements of Sfrom “old” elements of S .

A recursive definition of a function f : S → A sometimes follows the form of aninductive definition of its domain S :

◮ Basis: For each “initial” element x of S , define f (x).

◮ Induction: For each rule constructing a “new” element x of S from “old”elements x1, . . . , xk of S , define f (x) in terms of f (x1), . . . , f (xk).

For instance, given the inductive definition of N, it is not surprising thatrecursively defined functions with domain N are often specified as follows:

◮ Basis: Define f (0).

◮ Induction: Define f (n + 1) in terms of f (n), for all n ∈ N.

Page 12: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

§3.2.1 Example (numbers): Take f : N → N s.t.

f (0) = 0f (n + 1) = f (n) + n + 1

So

f (4) = f (3) + 4= f (2) + 3 + 4= f (1) + 2 + 3 + 4= f (0) + 1 + 2 + 3 + 4= 0 + 1 + 2 + 3 + 4

Other ways to write this recursive definition:

f (n) =

{

0 , if n = 0f (n − 1) + n , otherwise

f (n) = if n = 0 then 0 else f (n − 1) + n

Page 13: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

A rather different example of a recursively defined function with domain N:

f (0) = 0

f (1) = 1

f (n + 2) = f (n) + f (n + 1)

This one doesn’t correspond so nicely to the usual inductive definition of N.

Instead, this definition says directly what f does on 0 and on 1. And then forsuccessive numbers greater than 1, it (recursively) uses the value of f on theprior two numbers.

Roughly speaking, what’s crucial here is that we have a place to start — here,the base cases 0 and 1 — and the values for all other numbers are definedrecursively in terms of the values for “prior” numbers.

Technically, what’s necessary in such a definition is just that there be a unique

function that satisfies the conditions of the definition.

But such a definition also suggests a recursive algorithm for computing thevalue of the function for a given input. (In fact, such a definition can beunderstood as a recursive program.)

As you know, such recursive definitions of functions can be used withinductively defined domains other than N. . .

Page 14: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

§3.2.2 (strings) Recall: {0, 1}∗ can be defined inductively as follows:

◮ Basis: Λ ∈ {0, 1}∗.

◮ Induction: If s ∈ {0, 1}∗ and x ∈ {0, 1}, then xs ∈ {0, 1}∗.

Consider the “complement” function f : {0, 1}∗ → {0, 1}∗ s.t.

f (Λ) = Λf (0s) = 1f (s)f (1s) = 0f (s)

So, for example,

f (010) = 1f (10)= 10f (0)= 101f (Λ)= 101

How about defining a function g : {0, 1}∗ → {0, 1}∗ that reverses its input?

Page 15: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

◮ Basis: 〈〉 ∈ lists(A).

◮ Induction: If x ∈ A and L ∈ lists(A), then cons(x , L) ∈ lists(A).

We’ll also introduce convenient alternative notation: Instead of

cons(x , L)

we will sometimes writex :: L .

Here’s an inductive definition of the function from lists(A)× lists(A) tolists(A) that concatenates two lists (p. 163):

◮ Basis: cat(〈〉,M) = M

◮ Induction: cat(x :: L,M) = x ::cat(L,M)

Page 16: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

Here’s a recursive definition of a function from N× lists(N) to lists(N):

insert(x , 〈〉) = 〈x〉

insert(x , y :: L) =

{

x :: y :: L , if x ≤ y

y :: insert(x , L) , otherwise

Here’s a more interesting recursively-defined function, from lists(N) to lists(N):

sort(〈〉) = 〈〉

sort(x :: L) = insert(x , sort(L))

Page 17: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

Example 3.26 What would it be like to prove that these function definitions are“correct”?

Consider:

insert(x , 〈〉) = 〈x〉

insert(x , y :: L) =

{

x :: y :: L , if x ≤ y

y :: insert(x , L) , otherwise

Well, what is “correct” behavior here?

The argument would be “inductive”, following the shape of the definition ofthe function. . .

First we would argue that the base case is “correct”.

Then we would argue the recursive case is correct for any list y :: L over N,assuming that it is correct for the corresponding “prior” list L.

Page 18: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

Now consider the same questions for

sort(〈〉) = 〈〉

sort(x :: L) = insert(x , sort(L))

Such functions can be remarkably easy to write and prove correct.

Page 19: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

Example 3.27 (the map function, p. 164) Here’s the idea for an interestingfunction. It takes a list of elements from A and applies to each element afunction from A to B, returning the resulting list of elements from B.

map : (A → B)× lists(A) → lists(B)

So what is the base case like? And can we write it?

And what is the recursive case like? And can we write that?

And how hard would it be to “prove” that the function definition is “correct”?

Page 20: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

§3.2.5 Now let’s recursively define a function from lists(A) to lists(A) thatremoves duplicate elements from its input.

Here is a useful auxiliary function from A× lists(A) to lists(A):

removeAll(x , 〈〉) = 〈〉

removeAll(x , y :: L) =

{

y :: removeAll(x , L) , if x 6= y

removeAll(x , L) , otherwise

Now let’s write a definition for

removeDuplicates : lists(A) → lists(A)

removeDuplicates(〈〉) =

removeDuplicates(x :: L) =

Page 21: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

§3.2.4 (binary trees) Recall: We can define the set BT of binary trees over setA as follows:

◮ Basis: 〈〉 ∈ BT .

◮ Induction: If L,R ∈ BT and x ∈ A, then 〈L, x ,R〉 ∈ BT .

Problem 16 (a), p.176: Define a function from BT to N that counts the nodesin a binary tree:

nodes(〈〉) =

nodes(〈L, x ,R〉) =

Page 22: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

Problem 16 (b), p.176: Define a function from BT to Z that returns the“depth” of a tree.

Depth is typically understood to mean the length of the longest path from theroot to a leaf. By convention, if the tree is 〈〉 (so it has no root and no leaves),its depth is −1.

depth(〈〉) =

depth(〈L, x ,R〉) =

Page 23: 3.1 Inductively Defined Sets & 3.2 Recursive Functionsddunham/cs3512s15/notes/s3.… ·  · 2015-03-04You construct additional elements of Aby adding 2 to any element of . ... Examplethe

Now consider binary trees over Z.

Let’s write a function to compute the sum of the nodes of the tree.