0% found this document useful (0 votes)
72 views

Recursive Function

Uploaded by

o8799621
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Recursive Function

Uploaded by

o8799621
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

18-11-2023

Recursive
Function
Dr. Madhusmita Sahu
Assistant Professor
Department of Computer Science and
Information Technology

Topics To Be Discussed
● Functions
● Sets
● Halting Problem

1
18-11-2023

Partial Function
● A partial function f from X to Y is a rule which assigns to
every element of X at most one element of Y.

Total Function
● A total function f from X to Y is a rule which assigns to
every element of X a unique element of Y.
● For example, if R denotes the set of all real numbers,
the rule f from R to itself given by f(r)=+√r is a partial
function since f(r) is not defined as a real number when
r is negative.
● But g(r)=2r is a total function from R to itself.

2
18-11-2023

Recursive Function
● The recursive functions are described over the natural
numbers I={0, 1, 2, 3, ...}.
● Recursive functions are built up from basic functions
by some operations.
● The problem of finding out whether a given problem is
‘solvable’ by automata reduces to the evaluation of
functions on the set of natural numbers or a given
alphabet by mechanical means.

Recursive Function
● A partial or total function f from Xk to X is also called a
function of k variables and denoted by f(x1, x2, …, xk).
● Xk is the set of all k-tuples of elements of X.
● For example,
○ f(x1,x2)=2x1+x2 is a function of two variables: f(1,2)=4
■ 1 and 2 are called arguments and 4 is called a
value.

3
18-11-2023

Recursive Function
○ g(w1,w2)=w1w2 is a function of two variables (w1w2
∈ 𝛴*)
○ g(ab,aa)=abaa
■ ab, aa are called arguments and abaa is a value.

Primitive Recursive Function


● A total function f over N is primitive recursive if
○ It is any one of the three initial functions [zero
function, successor function and projector function]
or
○ It can be obtained by applying composition and
recursion finite number of times to the set of initial
functions.

4
18-11-2023

Composition
● Let f : A → B and g : B → C
● Composition of f and g can then be expressed as:
g ͦ f:A→C
(g ͦ f)(x) = g(f(x))
h(x) = g(f(x))
● NB: In general composition is not commutative:
( g ͦ f )(x) ≠ ( f ͦ g )(x)

Composition
● Let g be a function containing k variables and f1 ... fk be
functions of n variables, so the composition of g and f
is defined as:
○ h(0) = k
■ //Base step
○ h( x1, ..., xn) = g(f1(x1, ..., xn), …, fk(x1 ,..., xn))
■ //Inductive step
● Example: h(x, y) = g(f1(x, y), f2(x, y), f3(x, y))

5
18-11-2023

Recursion
● Let g be a function containing k variables then h is
obtained through recursion as follows:
○ h(x1, …, xn) = g(…, h(x1, …, xn))
● Example: x + y
○ f(x, 0 ) = x (1)
○ f(x, y+1) = f(x, y) + 1 (2)
○ Input: f(3, 2) =f(3, 1) + 1 =(f(3, 0) + 1) + 1 =(3 + 1) + 1
=5

Recursion
● A function f of n+1 variables is defined by recursion if
there exists a function g of n variables and a function h
of n+2 variables and f is defined as follows:
○ f(x1, …, xn, 0)=g(x1, …, xn)
○ f(x1, …, xn, y+1)=h(x1, …, xn,y,f(x1, …, xn, y))
● It may be noted that f can be evaluated for all
arguments (x1, …, xn, y) by induction on y for fixed x1, …,
xn.

6
18-11-2023

Recursion
● The process is repeated for every x1, …, xn.
● As P11(x)=x for every x in N, P11 is the identity function.
● So, Pin is also termed as a generalised identity function.

Initial Function
● Zero function: Z(x)=0
● Successor function: S(x)=x+1
● Projection function: A projection function selects out
one of the arguments.
○ Pin(x1,x2,...,xn)=xi
○ Specifically, P1(x,y)=x and P2(x,y)=y

7
18-11-2023

Primitive Recursive Function


● Function is considered primitive recursive if it can be
obtained from initial functions and through finite number of
composition and recursion steps.
● To prove that a function is primitive recursive you need
show that it can be obtained from the initial functions
using only composition and recursion.

Primitive Recursive Function Example 1


● Show that the function f(x,y)=x+y is primitive recursive.
● f is a function of two variables.
● If we want f to be defined by recursion, we need a function
g of a single variable and a function h of three variables.
● f(x+0)=x+0=x
● Then g can be defined by
● g(x)=x=P11(x)

8
18-11-2023

Primitive Recursive Function Example 1


● Also, f(x,y+1)=x+(y+1)=(x+y)+1=f(x,y)+1
● h(x,y,f(x,y))=f(x,y)+1=S(f(x,y))=S(U33(x,y,f(x,y)))
● Define h(x,y,z)=S(U33(x,y,z))
● As g=P11, it is an initial function.
● h is obtained from the initial functions U33 and S by
composition and f by recursion using g and h.
● Thus, f is obtained by applying composition and recursion
finite number of times to initial functions P11, P33 and S.
● So, f is primitive recursive.

Primitive Recursive Function Example 2


● Show that the function f(x,y)=x*y is primitive recursive.
● As multiplication of two natural numbers is simply
repeated addition, f has to be primitive recursive.
● f(x,0)=x*0=0
● f(x,y+1)=x*(y+1)=x*y+x=f(x,y)+x
● Let f1(x,y)=x+y
● Then f(x,y+1)=f1(f(x,y),x)
● Thus, f(x,0)=Z(x) [initial function]

9
18-11-2023

Primitive Recursive Function Example 2


● f(x,y+1)=f1(P33(x,y,f(x,y)), P13(x,y,f(x,y)))
● By taking g=Z and defining h by
○ h(x,y,z)=f1(P33(x,y,z), P13(x,y,z))
■ We see that f is defined by recursion.
● As g and h are primitive recursive, f is primitive recursive.

Primitive Recursive Function Example 3


● Show that f(x,y)=xy is primitive recursive.
● We define f(x,0)=1
● f(x,y+1) =x*f(x,y) =U13(x,y,f(x,y))*U33(x,y,f(x,y))

10
18-11-2023

Primitive Recursive Function: Addition


● add(x,0)=x
● add(x,S(y))=S(add(x,y))
● add(2,2) =S(add(2,1)) =S(S(add(2,0))) =S(S(2)) =S(3) =4

Primitive Recursive Function: Multiplication


● mul(x, 0) = 0
● mul(x, S(y)) = add(x, mul(x, y))

11
18-11-2023

Primitive Recursive Function: Predecessor


● pred(0) = 0
● pred(S(y)) = y

● pred(x)=x-1 if x≠0 and x≥1


=0 if x=0

Primitive Recursive Function: Subtraction and Monus


● x∸y= x-y if x≥y
=0 if x<y
● monus(x,0)=x
● monus(x,S(y)=pred(monus(x,y))

12
18-11-2023

Primitive Recursive Function: Absolute


● |x|=x if x≥0
=-x if x<0

Primitive Recursive Function Example 4


● Prove that the predecessor function is primitive recursive.
● pred(0)=0
● pred(y+1)=P12(y,pred(y))
● Uses an initial function.
● Defined by recursion.

13
18-11-2023

Primitive Recursive Function Example 5


● Prove that the proper subtraction is primitive recursive.
● x∸0=x
● x∸(y+1)=pred(x∸y).
● Defined by recursion.
● Uses primitive recursive function pred(x).

Primitive Recursive Function Example 6


● Prove that the absolute function is primitive recursive.
● |x-y|=(x∸y)+(y∸x)

14
18-11-2023

Primitive Recursive Function Example 7


● Prove that the minimum function is primitive recursive.
● min(x,y)=x∸(x∸y)

Primitive Recursive Functions over {a,b}


● Let 𝛴={a, b}
● A function f(x) over 𝛴 is defined by recursion if there exists
a ‘constant’ string w∈𝛴* and functions h1(x,y) and h2(x,y)
such that
○ f(𝜀)=w
○ f(ax)=h1(x,f(x))
○ f(bx)=h2(x,f(x))
■ h1 and h2 may be functions in one variable.

15
18-11-2023

Primitive Recursive Functions over {a,b}


● A function f(x1, x2, …, xn) over 𝛴 is defined by recursion if
there exist functions g(x1, …, xn-1), h1(x1, …, xn+1), h2(x1,
…, xn+1) such that
○ f(𝜀, x2, …, xn)=g(x2, …, xn)
○ f(ax1, x2, …, xn)=h1(x1, x2, …, xn, f(x1, x2, …, xn))
○ f(bx1, x2, …, xn)=h2(x1, x2, …, xn, f(x1, x2, …, xn))
■ h1 and h2 may be functions of m variables,
where m<n+1.

Primitive Recursive Functions over {a,b}


● A total function f is primitive recursive if
○ It is any one of the three initial functions or
○ It can be obtained by applying composition and
recursion finite number of times to the initial
function.

16
18-11-2023

Primitive Recursive Functions over {a,b}


● If f1, f2, …, fk are partial functions of n variables and g is a
partial function of k variables, then the composition of g
with f1, f2, …, fk is a partial function of n variables defined
by
○ g(f1(x1, x2, …, xn), f2(x1, x2, …, xn), …, fk(x1, x2, …,
xn))

Initial Functions over {a,b}


● nil(x)=𝜀
● cons a(x)=ax
○ cons a(x) denotes the concatenation of the
‘constant’ string ‘a’ and x.
● cons b(x)=bx
○ cons b(x) denotes the concatenation of the
‘constant’ string ‘b’ and x.
● 𝜀∈𝛴 plays the role of 0 in ℕ.
● ax or bx plays the role of y+1 in ℕ.

17
18-11-2023

Regular Function
● Let g(x1, x2, …, xn, y) be a total function over ℕ.
● g is a regular function if there exists some natural number
y0 such that g(x1, x2, …, xn, y0)=0 for all values x1, x2, …,
xn in ℕ.
● For instance, g(x,y)=min(x,y) is a regular function since
g(x,0)=0 for all x in ℕ.
● But f(x,y)=|x-y| is not regular since=0 only when x=y and
so we cannot find a fixed y such that f(x,y)=0 for all x in ℕ.

Recursive Function
● A function is recursive if it can be obtained from the initial
functions by a finite number of applications of
compositions, recursion and minimisation over regular
function.
● A function f(x1, x2, …, xn) over ℕ is defined from a total
function g(x1, x2, …, xn, y) by minimisation if
○ f(x1, x2, …, xn) is the least value of all y’s such that
g(x1, x2, …, xn, y)=0 if it exists.
■ The least value is denoted by 𝜇y(g(x1, x2, …, xn,
y)=0)

18
18-11-2023

Recursive Function
○ f(x1, x2, …, xn) is undefined if there is no y such that
g(x1, x2, …, xn, y)=0.
● In general, f is partial.
● But if g is regular then f is total.

Example 1
● f(x)=x/2 is partial recursive function over ℕ.
● Let g(x,y)=|2y-x|.
● 2y-x=0 for some y only when x is even.
● Let f1(x)=𝜇y(|2y-x|=0).
● Then f1(x) is defined only for even values of x and is equal
to x/2.
● When x is odd, f1(x) is not defined.
● f1is partial recursive.
● As f(x)=x/2=f1(x), f is a partial recursive function.

19
18-11-2023

Example 2
● nil(abab)=𝜀
● cons a(abab)=aabab
● cons b(abab)=babab

Example 3
● Let f1(x,y)=x-y, f2(x,y)=y-x and g(x,y)=x+y be functions
over ℕ.
● f1 is defined only when x≥y and f2 is defined only when
y≥x.
● So, f1 and f2 are defined only when x=y.
● Hence, when x=y, g(f1(x,y), f2(x,y))=g(x-x,x-x)=g(0,0)=0
● Thus, composition of g with f1 and f2 is defined only for
(x,x), where x∈ℕ.

20
18-11-2023

Example 4
● Define n! by recursion
● f(0)=1
● f(n+1)=h(n,f(n)) where h(x,y)=S(x)*y

Example 5
● Let f1(x1,x2)=x1x2, f2(x1,x2)=𝜀, f3(x1,x2)=x1,
g(x1,x2,x3)=x2x3 be functions over 𝛴.
● Then g(f1(x1,x2), f2(x1,x2),
f3(x1,x2))=g(x1x2,𝜀,x1)=𝜀x1=x1
● So, the composition of g with f1, f2, f3 is given by a
function h, where h(x1,x2)=x1

21
18-11-2023

Example 6
● Let f1(x,y)=x+y, f2(x,y)=2x, f3(x,y)=xy and g(x,y,z)=x+y+z
be functions over ℕ.
● Then, g(f1(x,y),f2(x,y),f3(x,y))=g(x+y,2x,xy)=x+y+2x+xy
● Thus, the composition of g with f1, f2, f3 is given by a
function h: h(x,y)=x+y+2x+xy

Example 7
● Show that the following functions are primitive recursive.
○ Constant functions a and b (i.e. a(x)=a, b(x)=b)
○ Identity function
○ Concatenation
○ Transpose
○ Head function (i.e. head(a1a2...an)=a1)
○ Tail function (i.e. tail(a1a2...an)=an)
○ The conditional function “if x1≠𝜀 then x2 else x3”

22
18-11-2023

Example 7: Constant Functions a and b


● As a(x)=cons a (nil(x)), the function a(x) is the composition
of the initial function ‘cons a’ with the initial function ‘nil’
and is hence primitive recursive.

Example 7: Identity Function


● Let us denote the function by ‘id’.
● Then,
○ id(𝜀)=𝜀
○ id(ax)=cons a(x)
○ id(bx)=cons b(x)
● So, id is defined by recursion using ‘cons a’ and ‘cons b’.
● Therefore, the identity function is primitive recursive.

23
18-11-2023

Example 7: Concatenation
● The concatenation function can be defined by
○ concat(x1,x2)=x1x2
○ concat(𝜀,x2)=id(x2)
○ concat(ax1,x2)=cons a(concat(x1,x2))
○ concat(bx1,x2)=cons b(concat(x1,x2))
● So, concat is defined by recursion using id, cons a and
cons b.
● Therefore, concat is primitive recursive.

Example 7: Transpose
● The transpose function can be defined by trans(x)=xT.
● Then,
○ trans(𝜀)=𝜀
○ trans(ax)=concat(trans(x),a(x))
○ trans(bx)=concat(trans(x),b(x))
● Thus, trans(x) is primitive recursive.

24
18-11-2023

Example 7: Head Function


● The head function head(x) satisfies
○ head(𝜀)=𝜀
○ head(ax)=a(x)
○ head(bx)=b(x)
● Thus, head(x) is primitive recursive.

Example 7: Tail Function


● The tail function tail(x) satisfies
○ tail(𝜀)=𝜀
○ tail(ax)=id(x)
○ tail(bx)=id(x)
● Thus, tail(x) is primitive recursive.

25
18-11-2023

Example 7: The Conditional Function


● The conditional function can be defined by
○ cond(x1,x2,x3)=“if x1≠𝜀 then x2 else x3”.
● Then,
○ cond(𝜀,x2,x3)=id(x3)
○ cond(ax1,x2,x3)=id(x2)
○ cond(bx1,x2,x3)=id(x2)
● Thus, cond(x1,x2,x3) is primitive recursive.

Cardinality
● The size or cardinality of a finite set is defined by the
number of elements in the set.
● The size of a set A is denoted by |A|.
● The notation |A| < ∞ implies that A is a finite set.
● A finite set with n elements can be listed as {a1, a2, ...,
an},
○ where ai is the i-th element of A for i = 1, 2, . . . , n.
● The simplest example of an infinite set is the set ℕ = {1,
2, 3, ...} of natural numbers.

26
18-11-2023

Bijection
● A map f between sets S1 and S2 is called a bijection if f
is one-to-one and onto.
● In other words
○ If f(a) = f(b) then a = b.
■ This holds for all a, b ∈ S1.
○ For each b ∈ S2, there is some a in S1 such that f(a)
= b.

Bijection
● We write S1 ∼ S2 if there is a bijection f : S1 → S2.
● A function f is bijective if it is one-to-one and onto.

27
18-11-2023

Countable Set
● Let ℕ= {1, 2, 3, 4, …} be the set of natural numbers.
● We say an infinite set A have the same size as ℕ, if
there exists a one-to-one correspondence f: ℕ→A.
● In other words, for each a in A, there is a unique x in ℕ
such that f(x) = a.
● A set A is countable if |A| is finite, or A has the same
size as ℕ.

Countable Set
● A countable set is a set with the same cardinality as
some subset of the set of natural numbers.
● A countable set is either a finite set or a countably
infinite set.
● A set is countably infinite if it has one-to-one
correspondence with the natural number set ℕ.

28
18-11-2023

Countable Set
● A set S is said to be countably infinite or denumerable
if there exists a bijection f: ℕ→S.
● A set S is said to be countable if it is either a finite set
or a countably infinite set.
● A set S is said to be uncountable otherwise.
● A set is countable if there is a one-to-one mapping
between the elements of a set and natural number.

Countable Set Example


● ℕ is countable
○ Let f: ℕ→ℕ be f(x) = x
● ℤ is countable
○ Let f:ℕ→ℤ be
■ f(x) = (x-1)/2 when x is odd
■ f(x) = -x/2 when x is even
● Set of +ve odd numbers, ODD, is countable.
○ Let f: ℕ→ODD be f(x) = 2x - 1

29
18-11-2023

Countable Set Example


● The set of even natural numbers ℕeven ={2n|n𝜀ℕ} ={2, 4,
6, ....}
○ We can find a bijection f:ℕ→S with the formula
f(n)=2n.
○ So we can say that ℕeven is countably infinite and
thus also countable.
● Similarly, the set of odd natural numbers is also
countably infinite.

Uncountable Set
● A set S is uncountable if it is not countable.
● Since all finite sets are countable, uncountable sets are
all infinite.
● If there cannot be a one-to-one mapping between
elements of a set and natural numbers.
● The real numbers are uncountable.

30
18-11-2023

Finite Set
● Set contains specific or finite number of elements.

Infinite Set
● Set contains infinite number of elements.

31
18-11-2023

Summary of Sets
Set

Empty Non-empty

Countable Finite Infinite

Countable Countable Uncountable

Ackermann’s Function
● The Ackermann’s function is not primitive recursive but
recursive.
● The function is defined by
○ A(0,y)=y+1
○ A(x,0)=A(x-1,1)
○ A(x,y)=A(x-1, A(x,y-1))
● A(x,y) can be computed for every (x,y) and hence A(x,y)
is total.

32
18-11-2023

Ackermann’s Function
● The Ackermann function A(x,y) is defined for integer x
and y by

Ackermann’s Function
● Compute A(1,1), A(2,1), A(1,2), A(2,2).
● A(1,1) =A(0,A(1,0)) =A(0,A(0,1)) =A(0,2) =3
● A(1,2) =A(0,A(1,1)) =A(0,3) =4
● A(2,1) =A(1,A(2,0)) =A(1,A(1,1)) =A(1,3) =A(0,A(1,2) =A(0,4)
=5
● A(2,2) =A(1,A(2,1)) =A(1,5) =A(0,A(1,4)) =1+A(1,4)
=1+A(0,A(1,3)) =1+1+A(1,3) =1+1+1+A(1,2) =1+1+1+4 =7

33
18-11-2023

Ackermann’s Function
● It can be shown that

A(4,y) = 2^(2^(2^(...^2)))-3

y+3
times

Ackermann’s Function
● A(0,y) =y+1 from the definition.
● A(1,y) =A(0,A(1,y-1)) =A(1,y-1)+1 =A(0,A(1,y-2))+1 =A(1,y-
2)+2 =... =A(1,0)+y =A(0,1)+y =y+2
● A(2,y) =A(1,A(2,y-1)) =A(2,y-1)+2 =A(1,A(2,y-2))+2 =A(2,y-
2)+4 =... =A(2,0)+2y =A(1,1)+2y =2y+3
● A(4,0) =2^(2^2)-3 =2^4-3 =16-3 =13
● A(4,1) =2^(2^(2^2))-3 =2^(2^4))-3 =2^16-3
● A(4,2) =2^(2^(2^(2^2)))-3 =2^(2^2^(2^4)))-3 =2^(2^16)-3

34
18-11-2023

Halting Problem
● The halting problem is the problem of determining
whether the program will finish running or continue to
run forever from a description of an arbitrary
computer program and an input.
● The halting problem, commonly applied to Turing-
complete programs and models, is the problem of
finding out whether, with the given input, a program
will halt at some time or continue to run
independently.

Turing Machine Halting Problem


● Given a Turing machine TM and an input string w, the
problem is to determine whether the Turing machine
finish computing of the string w in a finite number of
steps.
● The answer must be either yes or no.

35

You might also like