Discrete Structure Li Tak Sing(ζεΎ·ζ) Lecture 13 1 More examples on inductively defined sets ο¬Find an inductive definition for each set S of strings. 1. 2. 3. 4. Even palindromes over the set {a,b} Odd palindromes over the set {a,b} All palindromes over the set {a,b} The binary numerals. 2 Solution 1. Basis:Λ ∈ π΄ induction:ππ π₯ ∈ π΄ π‘βππ ππ₯π, ππ₯π ∈ π΄ 2. Basis:π, π ∈ π΄ induction:ππ π₯ ∈ π΄ π‘βππ ππ₯π, ππ₯π ∈ π΄ 3. Basis:Λ, π, π ∈ π΄ induction:ππ π₯ ∈ π΄ π‘βππ ππ₯π, ππ₯π ∈ π΄ 4. Basis:0,1 ∈ π΄ induction:ππ π₯ ∈ π΄ πππ π₯ ≠ 0 π‘βππ π₯1, π₯0 ∈ π΄ 3 More examples on inductively defined sets ο¬Find an inductive definition for each set S of lists. 1. {<a>, <a,a>, <a,a,a>,..} 2. {<1>, <2,1>, <3,2,1>,..} 3. {<a,b>, <b,a>, <a,a,b>, <b,b,a>, <a,a,a,b>, <b,b,b,a>,...} 4. {L| L is a list with even length over {0,1,2}} 4 Solution 1. Basis:< π >∈ π΄ induction:ππ π₯ ∈ π΄ π‘βππ ππππ (π, π₯) ∈ π΄ 2. Basis:< 1 >∈ π΄ induction:ππ π₯ ∈ π΄ π‘βππ ππππ (βπππ π₯ + 1, π₯) ∈ π΄ 3. Basis:< π, π >, < π, π >∈ π΄ induction:ππ π₯ ∈ π΄ π‘βππ ππππ (βπππ π₯ , π₯) ∈ π΄ 4. Basis:<>∈ π΄ induction:ππ π₯ ∈ π΄ πππ π¦, π§ ∈ {0,1,2} π‘βππ π₯π¦π§ ∈ π΄ 5 More examples on inductively defined sets ο¬Find an inductive definition for the set B of binary trees that represent arithmetic expressions that are either numbers in N or expressions that use operations + or -. 6 Solution ο¬Basis:π‘πππ(<>, 0, <>) ∈ π΄ induction:ππ πΏ, π ∈ π΄ π‘βππ π‘πππ πΏ, +, π , π‘πππ πΏ, −, π ∈ π΄, ππ π‘πππ <>, π, <> ∈ π΄, π‘βππ π‘πππ(<>, π + 1, <>) ∈ π΄ 7 More examples on inductively defined sets ο¬Find an inductive definition for each subset S of Nο΄N. 1. S={(x,y)| y=x or y=x+1} 2. S={(x,y) | x is even and yο£x/2 8 Solution 1. Basis:(0,0) ∈ π΄ induction:ππ π, π ∈ π΄ π‘βππ π + 1, π + 1 , π, π + 1 ∈ π΄ 2. Basis:(0,0) ∈ π΄ induction:ππ π, 0 ∈ π΄ π‘βππ π + 2,0 ∈ π΄ π ππ (π, π) ∈ π΄ πππ π + 1 ≤ 2 π‘βππ (π, π + 1) ∈ π΄ 9 Recursive Functions and Procedures ο¬Procedure ο‘A program that performs one or more actions. ο‘A procedure may return one or more values through its argument list. For example, a statement like allocate(m,a,s) might perform the action of allocating a block of m memory cells and return the values a and s, where a is the beginning address of the block and the s tells whether the allocation was successful. 10 Definition of recursively defined ο¬ A function or a procedure is said to be recursively defined if it is defined in terms of itself. ο¬ If S is an inductively defined set, then we can construct a function f with domain S as follow: ο‘For each basis element xοS, specify a value for f(x). ο‘Give rules that, for any inductively defined element xοS, will define f(x) in terms of previously defined value of f. 11 Constructing a recursively defined procedure ο¬If S if an inductively defined set, we can construct a procedure P to process the elements of S as follows: ο‘For each basis element xοS, specify a set of actions for P(x). ο‘Give rules that, for any inductively defined element xοS, will define the actions of P(x) in terms of previously defined actions of P. 12 Numbers ο¬Sum of integers. ο¬f(n)=0+1+2+...+n ο¬Definition: ο‘f(n)= if n=0 then 0 else f(n-1)+n ο‘Alternatively, it can be written as ο¬f(0)=0 ο¬f(n)=f(n-1)+n ο¬This is known as the pattern matching method 13 Numbers ο¬Adding odd numbers ο¬f(n)=1+3+...+(2n+1) ο¬Definition: ο‘f(0)=1 ο‘f(n)=f(n-1)+(2n+1) 14 The rabbit program ο¬ The Fibonacci numbers are the numbers in the sequence 0,1,1,2,3,5,8,13 where each number after the first two is computed by adding the preceding two numbers. ο¬ Assume that at the beginning there is one pair of rabbits. They give birth to another pair of rabbit in one month. ο¬ Let f(n) represents the number of pairs of rabbits at the n-th month. At that time, there were only f(n-2) mature rabbits which give birth to f(n-2) new rabbits. So the total number of rabbits is the total number of rabbits at the (n-1)th month plus these newly born f(n-2) rabbits. ο¬ So f(n)=f(n-1)+f(n-2) ο¬ The sequence 0,1,1,2,3,... is called the Fibonacci numbers. 15 Sum and product notation ο¬Sum of sequence a1,a2,....,an n ο₯a ο½ a 1 ο« a 2 ο« ..... ο« a n i i ο½1 ο¬ Product of a sequence a1,a2,....,an n ο a i ο½ a 1 a 2 ..... a n i ο½1 16 Factorial ο¬n!=1ο2ο.....οn ο¬0!=1 ο¬n!=(n-1)!οn 17 Examples ο¬Construct a recursive definition for each of the following functions, where all variables are natural numbers. 1. 2. 3. 4. f(n)=0+2+4+...+2n. f(n)=floor(0/2)+floor(1/2)+....+floor(n/2). f(n,k)=k+(k+1)+(k+2)+...+(k+n). f(n,k)=0+k+2k+...+nk. 18 1. π 0 = 0 π π = π π − 1 + 2π 2. π 0 = πππππ 0 2 π π π = π π − 1 + πππππ 2 3. π 0,0 = 0 π 0, π = π 0, π − 1 + 1 π π, π = π π − 1, π + π + π 4. π 0, π = 0 π π, π = π π − 1, π + ππ 19 Lists ο¬f(n)=<n,n-1,..,1,0> ο¬f(n)= if n=0 then <0> else cons(n,f(n-1)) ο¬Using the pattern matching method ο‘f(0)=<0> ο‘f(n)=cons(n,<n-1,...,1,0>) =cons(n,f(n-1)) 20 Recursive procedures ο¬Let P(n) be the procedure that prints out the numbers in the list <n,n-1,...,0>. ο¬P(n): if n=0 then print(0) else print(n); P(n-1) fi 21 The distribute function ο¬dist(3,<1,2,3>)=<(3,1),(3,2),(3,3)> ο¬How to define this function recursively? ο¬dist(x,L)= if L=<> then <> else (x,head(L))::dist(x,tail(L)) ο¬Pattern matching method: ο‘dist(x,<>)=<> ο‘dist(x,a::L)=(x,a)::dist(x,L) 22 The pairs function ο¬pairs(<a,b,c>,<d,e,f>)=<(a,d),(b,e),(c,f)> ο¬pairs(A,B)=if A=B=<> then <> else (head(A),head(B))::pairs(tail(A),tail(B)) ο¬pairs(<>,<>)=<>, pairs(x::T, y::U)=(x,y)::pairs(T,U) 23 Concatenation of Lists ο¬cat(<a,b>,<c,d,e>)=<a,b,c,d,e> ο¬cat(L,M)=if L=<> then M else head(L)::cat(tail(L),M) ο¬Pattern matching method: ο‘cat(<>,A)=A ο‘cat(x::L,A)=x::cat(L,A) 24 Sorting a list by insertion ο¬sort(<>)=<> ο¬sort(x::L)=insert(x,sort(L)) ο¬insert(x,S)=if S=<> then <x> else if x<head(S) then x::s else head(S)::insert(x,tail(S)) 25 Example ο¬Write recursive definition for the following list functions. 1. The function "last" that returns the last elemnt of a nonempty list. For example last(<a,b,c>)=c 2. The function "front" that returns the list obtained by removing the last element of a nonempty list. For example front(<a,b,c>)=<a,b>. 26 Solution 1. πππ π‘ < π > = π πππ π‘ π β· π = πππ π‘ π ππ πππ π‘ π₯ = ππ π‘πππ π₯ =<> π‘βππ βπππ(π₯) πππ π πππ π‘ π‘πππ π₯ 2. πππππ‘ < π > =<> πππππ‘ π β· π = π β· πππππ‘ π ππ πππππ‘ π₯ = πππ‘πππ π₯ =<> π‘βππ <> πππ π βπππ π₯ β· πππππ‘(π‘πππ π₯ ) 27