Recursive Function
Recursive Function
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.
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
8
18-11-2023
9
18-11-2023
10
18-11-2023
11
18-11-2023
12
18-11-2023
13
18-11-2023
14
18-11-2023
15
18-11-2023
16
18-11-2023
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
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
25
18-11-2023
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.
29
18-11-2023
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
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.
35