You are on page 1of 28

CHAPTER TWO

COMPUTABILITY

1
Topics that we will be discuss:
Computability Theory
Recursive Functions
Types of Recursive functions
Enumerable Languages
Turing Computable

2
Computability Theory
 Certain basic problems cannot be solved by computers.
 Example, “the problem of determining whether a mathematical statement is true or false”.
 The theories of computability and complexity are closely related.
 In complexity theory, the objective is to classify problems as easy ones and hard ones; whereas:
 In computability theory, the classification of problems is by those that are solvable and those
that are not.
 What makes some problems computationally hard and others easy?
 Computer problems come in different varieties; some are easy, and some are hard.
For example:
The sorting problem is an easy one.
The scheduling problem seems to be much harder than the
sorting problem.
3
Introduction to Recursion
Recursive function: a function that calls itself

Recursive function must have a way to control the number of

times it repeats

Usually involves an if-else statement which defines when


the function should return a value and when it should call itself
Depth of recursion: the number of times a function calls itself
4
Introduction to Recursion (cont’d.)

5
Problem Solving with Recursion
Recursion is a powerful tool for solving repetitive problems

Recursion is never required to solve a problem

Any problem that can be solved recursively can be solved

with a loop
Recursive algorithms usually less efficient than iterative ones

Due to overhead of each function call

6
Problem Solving with Recursion (cont’d.)
Some repetitive problems are more easily solved with recursion

General outline of recursive function:

If the problem can be solved now without recursion, solve and return

Known as the base case

Otherwise, reduce problem to smaller problem of the same structure

and call the function again to solve the smaller problem


Known as the recursive case

7
Using Recursion to Calculate the Factorial of a Number
In mathematics, the n! notation represents the factorial of a
number n
For n = 0, n! = 1
For n > 0, n! = 1 x 2 x 3 x … x n
The above definition lends itself to recursive programming
n = 0 is the base case
n > 0 is the recursive case
factorial(n) = n x factorial(n-1)

8
Using Recursion (cont’d.)

9
Examples of Recursive Algorithms
Summing a range of list elements with recursion

Function receives a list containing range of elements to be summed,


index of starting item in the range, and index of ending item in the range
Base case:
if start index > end index return 0

Recursive case:
return current_number + sum(list, start+1, end)

11
Examples of Recursive Algorithms (cont’d.)

12
The Fibonacci Series
Fibonacci series: has two base cases
 if n = 0 then Fib(n) = 0
 if n = 1 then Fib(n) = 1
 if n > 1 then Fib(n) = Fib(n-1) + Fib(n-2)

Corresponding function code:

13
Finding the Greatest Common Divisor
 Calculation of the greatest common divisor (GCD) of two positive integers
If x can be evenly divided by y, then
gcd(x,y) = y
Otherwise, gcd(x,y) = gcd(y, remainder of x/y)
 Corresponding function code:

14
The Towers of Hanoi
Mathematical game commonly used to illustrate the
power of recursion
Uses three pegs and a set of discs in decreasing sizes
Goal of the game: move the discs from leftmost peg to
rightmost peg
Only one disc can be moved at a time
A disc cannot be placed on top of a smaller disc
All discs must be on a peg except while being moved

15
The Towers of Hanoi (cont’d)
Problem statement: move n discs from peg 1 to peg 3 using peg 2 as a
temporary peg
Recursive solution:

If n == 1: Move disc from peg 1 to peg 3


Otherwise:
Move n-1 discs from peg 1 to peg 2, using peg 3
Move remaining disc from peg 1 to peg 3
Move n-1 discs from peg 2 to peg 3, using peg 1

16
The Towers of Hanoi (cont’d.)

17
Recursion versus Looping
Reasons not to use recursion:

Less efficient: entails function calling overhead that is not necessary with a loop
Usually a solution using a loop is more evident than a recursive solution
Loop is when the compiter do instruction repeatedly when some condition is
fullfilled
Some problems are more easily solved with recursion than with a loop

Example: Fibonacci, where the mathematical definition lends itself to recursion


Recursion doesn’t need a condition to be satisfied before it can run.
18
Primitive and Partial Recursive Functions
 Total Recursive Functions:

A recursive function is called total recursive function if it is defined for its all arguments.
Let f(a1, a2, …an) be a function defined on function g(b1, b2, …bn).

Then f is a total function if every element of f is assigned to some unique element of

function g.
A total function is called recursive or primitive recursive if and only if it is an initial

function over n,
or it is obtained by applying composition or recursion with finite number of times to the

19 initial function over n.


Total or Primitive Recursive Function
Multiplication of two positive integers is total recursive function or

primitive recursive function.


Not all total recursive function are primitive recursive function.

All primitive recursive function are total function.

Function like n!, logn are total recursive function.

20
Partial Recursive Functions:
A function f(a1, a2, ….an)computed by a TM is known as partial

recursive function.
if f is defined for some but not all values of a1, a2, ….an.Let f(a1, a2,

…an) is a function and defined on function g(b1, b2, ….bn)


then f is partial function if some element of f is assigned to almost

one element of function g.

21
Partial Recursive Function
A partial function is recursive if it is an initial function over N, or it is

obtained by applying recursion or composition or minimization on initial


function N.
Subtraction of two positive integers is partial recursive function.

The composition of partial(total) functions yields a partial(total) function.

The minimization of a total recursive function is a partial recursive

function.
22
Primitive Recursive function ? Total function ? Partial recursive Functions
Recursively Enumerable Recursive Languages
A recursively enumerable language is a recursively
enumerable subset in the set of all possible words over
the alphabet of the language.
A recursively enumerable language is a formal
language for which there exists a Turing machine (or
other computable function) which will enumerate all
valid strings of the language.
A language is called Recursively Enumerable if there
is a Turing Machine that accepts on any input within the
23
language.
Recursively enumerable languages are the formal languages that can

be decide-able, ( fully or partially).


According to the Chomsky hierarchy of formal languages, we can

see the recursively enumerable languages as a type 0 languages.


Some examples of recursively enumerable languages are;

Recursive languages

Regular language is Context sensitive languages

Context-free languages and many more.


24
26
Use of Turing Machine
Turing machine as a language recognizer.

Turing machine as a language generator.

Turing machine as a language evaluator.

Turing machine as a language decider.

27
Turing Computable
A (real) number is Turing computable if there exists a Turing machine which

computes an arbitrarily precise approximation to that number.


All of the algebraic numbers (roots of polynomials with algebraic coefficients) and

many transcendental mathematical constants, such as π is Turing-computable.


Turing computability is an outer boundary, and as you show, any theory that requires

more power than that surely is irrelevant to any useful definition of human rationality.
A slightly stricter boundary is posed by computational complexity, especially in its

common “worst case” form.

28
Computable Functions
 Computable functions are used to discuss computability without referring to any concrete
model of computation such as Turing machines or register machines.
 Any definition, however, must make reference to some specific model of computation but
all valid definitions yield the same class of functions.
 According to the Ch-Turing thesis, computable functions are exactly functions that

calculated using a mechanical calculation device given unlimited amounts of time and
storage space.
 Equivalently, this thesis states that a function is computable if and only if it has an

algorithm.
 Note that an algorithm in this sense is understood to be a sequence of steps a person with
29
unlimited time and an unlimited supply of pen and paper could follow.
w o
r T
p te
h a
f C
d o
E n

30

You might also like