You are on page 1of 61

AUTOMATA, COMPLEXITY AND

COMPUTABILITY
(BCS 214)

A SIMPLIFIED APPROACH WITH WORKED EXAMPLES, EXERCISES AND


SAMPLE EXAMINATION QUESTIONS

Mr. Okot Patrick (BSC Hons: Computer Science, Mathematics – MUK).


Teaching Assistant, Department of Computer Science
Gulu University
© All Rights Reserved

Revised Edition, July 2008


Dedication

This piece of work is dedicated, first and foremost, to God Almighty for keeping me
alive and to the ones I love; my mum Mary Lamunu, my brothers; Bonny, Erick,
Walter, Justine and Vincent, my Uncle Philip Otim and finally to my beloved fiancée
Molly Shiella.

ii
Preface

I believe that Automata, Complexity and Computability can be understood by


anyone who does computer science as a course. So I compiled and wrote this
book/tutorial that explains this very technical subject in a not-so-technical manner,
putting the concepts and theories of Automata into a format that the reader can
quickly grasp.
For readers new to the subject, this tutorial demonstrates what Automata are and
what they do. In the following pages I show the overall view and tell the readers
where to find more detailed information, if they so desire.
For those with more background, this text introduces the subject concisely,
describing the complexities of Automata without going into intricate detail. One may
say this tutorial leaves off where other Automata, Complexity and Computability
textbooks begin.
Of course, I’ve made some assumptions about my audiences. First, I assume readers
have some familiarity with computer systems; second, I assume they have a working
knowledge of mathematical models. I recommend (although I don’t require) that
readers be familiar with at least one mathematical model and one computer language.
I also found it necessary to include examples and exercises to illustrate the concepts
behind the topics covered in this course.

iii
Organization and Features

This tutorial is structured to explain the concepts of mathematical models by using


Automata, Complexity and Computability. The book covers topics like Finite state
machines and regular languages (Deterministic and Non-deterministic machines,
Regular expressions and regular grammars, Equivalence and minimisation), Push-
down Automata and context-free languages (Normal forms of grammars), and Top-
Down and bottom-up parsing (Markov algorithms, Turing machines; simple
examples of non-computable problems, NP-computable problems).
Although it is sometimes more difficult to understand the concepts of mathematical
models, it is a longer lasting achievement. It also pays off in the long run, because it
allows one to adapt as technology changes – as , inevitably, it does. Therefore, the
purpose of this tutorial is to give students a solid background in the structure of
Automata, their functions and how they interact and interrelate.
Chapter 1 talks about the basic definitions that must be grasped by the students,
which include amongst others the meanings of Automata, Alphabet and Algorithm.
It also covers the basic algebraic preliminaries and Mapping. Chapter 2 covers the
Deterministic Semi-Automata (DSA) and the Non-Deterministic Semi-Automata
(NDSA). Chapter 3 introduces another type of Atomaton called Deterministic Finite
Automata (DFA), and it also talks about the Non-Deterministic Finite Automata
(NDFA). Chapter 4 introduces the Regular Expressions (REs), while Chapter 5
explains the Transition Graphs (TGs) and talks about the Sets of words
corresponding to transition graphs. Chapter 6 introduces and explains into details, the
Grammars, while chapter 7 talks about the Push-Down Automata and context-free
grammar. Chapter 8 discusses the Markov Algorithms (MAs) and finally chapter 9
summarises the tutorial with Turing Machine
The tutorial concludes with reference aids and sample examination questions which I
think will be of great help and guidance to the students as far as passing this course is
concerned. The Bibliography can guide the reader to basic research on the subject.

iv
Methods of Presentation

This tutorial has a modular construction. Chapters 2 through 9 are the core of the
book, chapter 1 may be assigned as preliminary reading or may be covered in the
introductory lecture(s). The order of presentation in a classroom depends on and
needs to follow the table of contents in sequence since the proceeding chapters are
built on the preceding chapters.

Acknowledgements

Special thanks go to Dr. Okello Onen; Dean Faculty of Science – Gulu University
and Mr. Nimrod Mafabi; Head of Department- Computer Science for their support.
My gratitude to all my friends and colleagues, particularly those at the Department of
Computer Science; especially Mr. Tabo Geoffrey Olok, Miss Pauline Olango, Mr.
Billy Kalema and others; who were so generous with their encouragement, advice
and support.
Special thanks also go to my beloved fiancée Molly Shiella for her endless support
and encouragement; and for always being there whenever I need help both spiritually
and socially and to my uncle Mr. Philip Otim for seeing me through with my
education.

TABLE OF CONTENTS

v
Dedication...............................................................................................................ii
Preface....................................................................................................................iii
Organization and Features...........................................................................iv
Methods of Presentation.................................................................................v
Acknowledgements...........................................................................................v
CHAPTER ONE......................................................................1
1.1.0 BASIC DEFINITIONS.....................................................................................1
1.1.1 Automata:.....................................................................................................1
1.1.2 Alphabet:......................................................................................................1
1.1.3 Algorithm:....................................................................................................2
1.2.0 ALGEBRAIC PRELIMINARIES....................................................................2
1.2.1 Definitions:..................................................................................................2
1.3.0 MAPPING..........................................................................................................3
CHAPTER TWO.....................................................................5
2.1.0 DETERMINISTIC SEMI-AUTOMATA (DSA)..............................................5
2.1.1 Definition:.....................................................................................................5
2.2.0 NON-DETERMINISTIC SEMI-AUTOMATA (NDSA).................................7
2.2.1 Definition:.....................................................................................................7
CHAPTER THREE.................................................................8
3.1.0 DETERMINISTIC FINITE AUTOMATA (DFA)...........................................8
3.1.1 Definition:.....................................................................................................8
3.1.2 Examples:....................................................................................................8
3.2.0 NON-DETERMINISTIC FINITE AUTOMATA (NDFA)............................11
3.2.1 Definition:...................................................................................................11
3.2.2 Examples:..................................................................................................11
CHAPTER FOUR.................................................................13
4.0 REGULAR EXPRESSIONS............................................................................13
4.1 Definition one:..............................................................................................13
4.2 Definition two: ...........................................................................................13
4.3 Properties of Operations on REs:...........................................................14
CHAPTER FIVE...................................................................15
5.0 TRANSITION GRAPHS...................................................................................15
5.1.1 Definition:...................................................................................................15
5.1.2 More examples:........................................................................................16
5.2.0 SETS OF WORDS CORRESPONDING TO TRANSITION GRAPHS: 18
5.2.1 Examples:..................................................................................................18
5.2.2 Assignment:..............................................................................................24
CHAPTER SIX......................................................................28
6.0 GRAMMARS......................................................................................................28
6.1 Definition:......................................................................................................28
6.2 Classification of Grammars......................................................................28
6.2.1. Type 0 or Right Linear Grammar:...................................................28
6.2.2. Type 1 or Context Sensitive Grammar:.........................................29
6.2.3. Type 2 or Context Free Grammar:..................................................29
6.2.4. Type 3 or Regular Grammar:...........................................................29
6.3 Definitions.....................................................................................................30
6.4 Deriving Grammar from a Given Language:........................................31
CHAPTER SEVEN...............................................................33
7.0 PUSH-DOWN AUTOMATA AND CONTEXT-FREE GRAMMAR............33
7.1 Definition:......................................................................................................33

vi
7.2 Examples:......................................................................................................33
CHAPTER EIGHT................................................................38
8.0 MARKOV ALGORITHMS (MAs)....................................................................38
8.1 Introduction:.................................................................................................38
8.2 Definition:......................................................................................................38
8.3 Examples.......................................................................................................39
CHAPTER NINE...................................................................44
9.0 TURING MACHINE...........................................................................................44
9.1 Introduction:.................................................................................................44
9.2 Physical Interpretation:.............................................................................44
9.3 Description of the TM through Quintuples:.........................................44
9.4 Definition (other):........................................................................................45
BIBLIOGRAPHY..................................................................................................46
APPENDIX: SAMPLE QUESTIONS.............................................................47
NOTES.....................................................................................................................53

vii
CHAPTER ONE

1.1.0 BASIC DEFINITIONS

1.1.1 Automata:

These are mathematical models of devices which process information by


giving responses to inputs. The behaviour of discrete deterministic computing
devices processing a particular memory at a particular time instance depends
not only on the latest input but also on the entire sequence of past inputs.
They are capable of only a finite number of inputs, internal states and outputs
and are deterministic in the sense that an input sequence uniquely determines
the output behaviour.

1.1.2 Alphabet:

This is a finite non-empty set of distinct symbols. Each of these symbols is


called a letter and any sequence of zero or more letters of the alphabet is
called a word.

Example: Given {a, b};


We have λ, a, b, aa, ab, ba, bb, aaaa, etc as a sequence of letters.
Note: The same letter may occur several times in a word and a string
consisting of zero letters is called an empty word denoted by Λ or λ.

The set of all words over an alphabet I is denoted by I* which is an infinite set.
Subsets of I* are referred to as Languages over the alphabet I.
E.g. the following sets are languages over the alphabet I 2 = {x1, x2}:

L1 = {Λ, x1, x2, x1x2, x2x2};

L2={x1x2x2, x1x2x1, x1x2x1x1};

L3= {x1p / p is prime};

L4= {x1ix2j / i, j are natural numbers}.

1
Languages L1, L2 are finite while L3 & L4 are infinite.

1.1.3 Algorithm:

This is a list of instructions specifying a sequence of operations which will


give the answer to any problem at a given time. An algorithm written for
machine execution is called a program. Any process that can be performed
by a machine can be written as an algorithm. A program is usually written in
two main categories of languages; the High Level Language and the Machine
Language. High Level Languages are written and understood by humans e.g.
Java, C, C++, SQL, etc; while Machine language is understood by the
computer. Therefore, programs written in High Level Languages have to be
converted into Machine language by use of Compilers and Interpreters.

1.2.0 ALGEBRAIC PRELIMINARIES

1.2.1 Definitions:

The set of all ordered pairs (a, b) where and is called a


Cartesian product of A and B (AxB) i.e.

AxB= {(a, b): }.

Any subset of AxB is called a Binary Relation between A and B.

Let R be a relation between A and B i.e. R is a set of pairs (a, b). Then we
write R or aRb.
The inverse relation R-1 is defined by

bR-1a aRb or (b, a) R-1

Or R-1 is a relation between B and A. (Order sensitive)


Domain of R is {a / b and aRb}
Range of R is {b / a and aRb}

Consider a binary relation on a set S. A binary relation R over the set S is said
to be:

2
1. Reflexive: If for each s S, sRs i.e. {(s, s): s S};
2. Symmetric: If for s and t in S, sRt => tRs (R = R-1);
3. Transitive: If for s, t and u in S, sRt and tRu => sRu
4. A binary relation which is reflexive, symmetric and transitive is called
an equivalence relation.

Example:
If S = {1, 2, 3, 4}
Then;
R1= {(1, 1), (2, 2), (3, 3), (4, 4)} is a Reflexive relation.
NB: If one of the members of R doesn’t exist, then the relation is NOT
reflexive.

R2 = {(1, 2), (2, 1), (2, 3), (3, 2)} is a Symmetric relation.
And
R3 = {(1, 2), (2, 3), (1, 3)} is a Transitive relation

1.3.0 MAPPING

U is a mapping from set A to set B if it assigns every element of some


subset of A to one and only one element of B. A one-to-one mapping of a set
A onto itself is called a Permutation.

The identity relation IA over a set A is the set of all pairs (a, a); a A.

For any relation R over A, RIA = IAR = R.

If U is a permutation of A, then UU-1 = U-1U = IA.

E.g.
If A= {a, b, c} and B= {d, e, f},
Then
U = {(a, d), (b, e), (c, d)} is a mapping.
Note: Elements of the second set can be repeated but those of the first one
cannot.

3
And
U = {(a, b), (b, c), (c, a)} is a permutation; i.e. elements are formed from one
set.
IA = {(a, a), (b, b), (c, c)} is an identity relation.

Exercises:

1. What do you understand by the following terms?


i) A mapping
ii) A permutation
iii) An identity relation.

2. Let R be a binary relation over a set S. what is meant by R being:


i) Reflexive;
ii) Symmetric;
iii) Transitive;
iv) An equivalence relation?

3. Define the following terms:


i) Binary relation
ii) Algorithm
iii) Alphabet
iv) Automaton
v) Language

4
CHAPTER TWO

2.1.0 DETERMINISTIC SEMI-AUTOMATA (DSA)

2.1.1 Definition:

A deterministic semi-automaton is a triple or 3-tuple;


A = (S, Σ, M) where
S = {s0, s1, s2… sn-1} is a finite set of states of A;
Σ = {δ0, δ1,…, δm-1} is a finite set of inputs of A and;
M = {mδ0, mδ1, …, mδm-1} is a set of mappings of s→s (s onto s)

Example;
A = ({1, 2, 3, 4, 5, 6}, {δ 0, δ1}, {mδ0 = (1, 3), (2, 1), (3, 2), (4, 1), (5, 3), (6, 5);
mδ1 = (1, 4), (2, 5), (3, 3), (4, 3), (5, 3), (6,3)}) is a semi-automaton with 6
states and 2 inputs.
A semi-automaton can also be described by means of a table called a Next
State Table (NST) or Transition Table (TT). For the above example, the TT
will have the following form:

States
mδi 1 2 3 4 5 6

Inputs δ0 3 1 2 1 3 5

δ1 4 5 3 3 3 3

Another representation is by using a State graph or a Directed graph.


The nodes or vertices of the graph represent the states of A and for every
ordered pair (si, sj) mδk, an arrow-abled δk leads from si to sj.

5
A directed graph for the above example:

δ0 δ1

1 δ0 2 δ0 3
δ0, δ1
δ0 δ1 δ1 δ1

4 5 δ0 6

δ1

Exercise:

i) (a) Define a deterministic semi-automaton, explaining fully the


parameters used in your definition.

(b) Differentiate between deterministic and non-deterministic


semi-automaton.

ii) Given the following finite-state automaton:


M = ({p, q, r, s}, {0, 1},, p, {p}) where

 0 1
p r q
q s p
1. Represent the above automaton as a directed
r p s
graph.
s q r
2. Show the mappings of the above automaton in
the form.
(k, x) = k where k {p, q, r, s} and x {0, 1}

2.2.0 NON-DETERMINISTIC SEMI-AUTOMATA (NDSA)

6
2.2.1 Definition:

A non-deterministic semi-automaton is a 3-tuple A = (S, Σ, M)


where S and Σ are defined as before and M is a relation over S and not a
mapping.
A non-deterministic semi-automaton can also be described by directed graphs
and in this case there may be vertices or nodes from which no arrows
emanate and there may be vertices from which more than one arrow labelled
by the same input emanates.

Example:

A = ({1, 2, 3, 4, 5}, {0, 1}, {A0 = {(1, 2), (2, 1), (3, 2), (3, 3), (4, 3)}; A1 = {(1, 4),
(3, 4), (4, 2), (4, 3), (4, 5)}})
Transition table:
Ai 1 2 3 4 5
0 2 1 2,3 3
1 4 4 3,5

Directed graph:
0 0

0
1 0 2 3
1 1
1
0, 1
5 4
1

Exercise:

1. Define a Non-deterministic semi-automaton, explaining fully the


parameters used in your definition.

2. Differentiate between deterministic and non-deterministic semi-


automaton.

CHAPTER THREE

7
3.1.0 DETERMINISTIC FINITE AUTOMATA (DFA)

3.1.1 Definition:

A finite automaton is a 5-tuple or quintuple


 = (S, Σ, m, s0, F) where;
 S is a set of finite states
 Σ is a set of finite inputs
 m is a mapping of the initial states
 s0 S is the initial state (a distinguished element of S)
 F S is a set of final states (a distinguished subset of S)

Let A be an automaton set in the state S0 and let x Σ*.


If S0mx F, then A accepts x.
If S0mx F, then A rejects x.
Hence A is used to classify or organise words in Σ* so that if A is in state S 0,
and a word x Σ* is applied; if after reading x, A is in a state belonging to F,
then it’s said to accept x, otherwise x is rejected by A.

3.1.2 Examples:

A = (S, Σ, m, S0, F)
Where Σ = {0, 1}, S = {S0, S1, S2, S3}, F= {S0} and m is defined as follows:
δ(S0, 0)=S2
δ(S1,0)=S3
δ(S2,0)=S0
δ(S3,0)=S1
δ(S0,1)=S1
δ(S1,1)=S0
δ(S2,1)=S3
δ(S3,1)=S2

Directed graph:

8
NB: By convention, a double ring is used to denote or indicate a final state in
FA.

S0 S1
1

0 0 0 0

1
S2
S3
1

Let x = 110101 be input into A;

1 1 0 1 0 1
S0 → S1 → S 0 → S2 → S 3 → S1 → S 0
Therefore x will be accepted by automaton A.
NB: A string is accepted if it ends in a state which is a member of the final
state F.
A set of words accepted by an automaton A is called a regular set.
A partition Σ* into two disjoint sets i.e.
U = {set of words accepted by A}
Σ*-U = {set of words rejected by A}.

Exercise:

i) An automaton A has the following mappings:

(s0, 0) = s2
(s1, 0) = s3
(s2, 0) = s0
(s3, 0) = s1
(s0, 1) = s1
(s1, 1) = s0
(s2, 1) = s3
(s3, 1) = s2

9
Where 0, 1 are elements of the automaton alphabet and s 0, s1, s2 and
s3 are the states of the automaton, s0 being the initial and final state.

Which of the following strings are regular with regard to automaton A.

i) 110101
ii) 111010
iii) 101010
iv) 011011

10
3.2.0 NON-DETERMINISTIC FINITE AUTOMATA (NDFA)

3.2.1 Definition:

An NDFAn is a 5-tuple A = (S, Σ, m, S0, F) where m is a relation and not a


mapping on S and the rest are defined as before. A is said to accept a word x
Σ* if and only if S 0m F Ø i.e. if at least one path from an initial state
to a final state along vertices or arrows labelled in an appropriate order by the
letters of x.

3.2.2 Examples:

A = ({S0, S1, S2, S3, S4}, {0, 1}, m, S0, {S2, S4}) and m is defined as:
δ(S0, 0) = {S0, S3}
δ(S1, 0) = { Ø } i.e. Undefined
δ(S2, 0) = S2
δ(S3, 0) = S4
δ(S4, 0) = S4
δ(S0, 1) = {S0, S1}
δ(S1, 1) = S2
δ(S2, 1) = S2
δ(S3, 1) = Ø
δ(S4, 1)= S4

Solution:

Directed Graph:

0, 1
0, 1
S0 S3 S4
0 0
1

S1

S2

0, 1

11
Let x = 01101 be input into A;

Lower route:
0 1 1 0 1
S0 → S0 → S1 → S2 → S2→ S2; Hence x is accepted along this route

0 1 1 0 1
S0 → S0 → S1 → S0 → S1 →!!! Not accepted!

Upper route:
0 1
S0 → S3 →!!!! Rejected!

Exercise:

i) An automaton A has the following mappings:

(s0, 0) = {Ø}
(s1, 0) = s3
(s2, 0) = {s0, s3}
(s3, 0) = s1
(s0, 1) = s1
(s1, 1) = {Ø}
(s2, 1) = s3
(s3, 1) = {s2, s1}

Where 0, 1 are elements of the automaton alphabet and s 0, s1, s2 and


s3 are the states of the automaton, s0 being the initial and final state.

Which of the following strings are regular with regard to automaton A.

i) 110101
ii) 111010
iii) 101010
iv) 011011

12
CHAPTER FOUR

4.0 REGULAR EXPRESSIONS

4.1 Definition one:

Given two sets of words R and S from Σ*, we denote:

A SET UNION by
R+S = {x: x R or x S}

A SET CONCATENATION by
R.S = RS = {xy: x R, y S}

A CLOSURE OPERATOR by
R* = Λ + {x: x is obtained by concatenating a finite number of words of R}
= Λ + R + R2 + ……. + Ri + …… (i.e. R0 = Λ)
Where Ri = (……((RR)R….)R i times, i.e.

R* =

Example

Let Σ = {a, b}
R = {aa, ab} = {a2, ab}
S = {aba, ab, ba}
Then
R+S = {a2, ab, aba, ba}
RS = {a2aba, a2ab, a2ba, ababa, abab,abba}
R* = Λ + {a2, ab, a2a2, a2ab, abab, …….}
= (a2)m.(ab)n; m, n 0
Order of precedence in decreasing is *, ., +.
RS and SR give different results because the operation is order sensitive.

4.2 Definition two:

Let Σ be a finite alphabet; we define a regular set over Σ as follows: -

1. The empty set (Ø) is a regular set of Σ;


2. The empty word (Λ) is a regular set of Σ;
3. {a}is a regular set of Σ a Σ;

13
4. If P and Q are regular sets over Σ, then so are P+Q, PQ and P*;
5. Nothing else is a regular set.

Thus, a set of Σ* is regular if and only if it falls in any of the above, or can be
obtained from them by a finite number of applications of Union, Concatenation
and Closure.

4.3 Properties of Operations on REs:

1. R+R = R, R+Ø = R
2. R+S = S+R
3. (R+S)+T = R+(S+T)
4. (RS)T = R(ST) = RST
5. R.Λ = Λ.R = R
6. (R+S)T = RT+ST
7. T(R+S) = TR+TS
8. R*.R* = R*
9. (R*)* = R*
10. RR* = R*R = R*
11. R* = Λ+RR*
For any function F(R1, R2, ….., Rn),
12. F(R1, R2, ….., Rn)+( R1, R2, ….., Rn)* = ( R1, R2, ….., Rn)* i.e. F(R1, R2,
….., Rn) ( R1, R2, ….., Rn)*
13. (R*+S*)* = (R*S*)*
14. (RS)*R = R(SR)*
15. (R*S)*R* = (R+S)*
16. (R*S)* = (R+S)S*+ Λ
17. (SR*)* = S(R+S)*+ Λ

Exercise:

1. What is a regular set?

2. What is a regular expression and how may it be obtained?

3. What is the regular expression recognised by the following machine?

0
0

0 0
1 2 3 4
+
+
1
1

14
CHAPTER FIVE

5.0 TRANSITION GRAPHS

5.1.1 Definition:

Let Σ = {0, 1} be a two-letter alphabet.


A transition graph over Σ consists of: -
A finite set of vertices/nodes; at least one vertex is labelled with for the

initial state and some vertices are labelled or ;


Oriented branches (ordered pairs of vertices/arrows/loops);
Every branch is labelled with a 0, 1 or γ.
A word x is accepted by a transition graph G if a path from an
initial vertex to a final vertex such that the labels of the branches of the path
form the word x after the γ s are deleted. The empty word Λ is accepted if one
node is labelled or if a path from an initial to a final node whose
branches are all labelled with γ s. A set of words accepted by the transition
graph G is denoted by T (G).

Examples:
1.

An empty word cannot be accepted;


T (G) = 1

2.

An empty word is accepted;


T (G) = 1*

3.

Can’t accept any word, string, etc


T (G) = Ø

4.

Can accept 1, 11, 111, but not an empty word; γ creates a loop.
T (G) = 11*

5.

Can only accept an empty word,

15
T (G) = Λ

6.

Accepts the empty word Λ, 1, 11, 111, 100 …


T (G) = (10*)*

7.

Accepts 1, 10, 100, 1000, …


T (G) = 10*

8.

Accepts a 1, 01, and Λ.


T (G) = Λ + 01 + 1.

5.1.2 More examples:

1.

Accepts 1000, 1001, 0000, 0001, 1*000, 1*001, 0*000, 0*001, 0*000*, 0*001*,
1*000*, 1*001*, etc

T (G) = 0*000* + 0*001* + 1*000* + 1*001*

2.
T (G) = 0 + 11*
0 can be accepted and a series of 1s.

Therefore;
T (G1) = 0

T (G2) = 11*

16
We have to minimise on the number of gammas (γ) on the transition graph; so
we have:

If there was concatenation;


I.e. T (G) = 011*

i.e.
Create an arrow from final state of one node to initial node of the second one.
Therefore;

Which is the right transition graph for T (G) = 011*


Alt:

Exercise:

1. Draw Transition graphs to represent the following strings/expressions:

i) (a + b)* bab
ii) [10 + (0 + 11)0*1]*1
iii) (0 + 1)* 00 (0 + 1)*

17
5.2.0 SETS OF WORDS CORRESPONDING TO TRANSITION GRAPHS:

Tx: - represents all words which correspond to paths leaving from the vertices
in Ax to the final vertex in the transition graph G.
Ax: - represents the set of all vertices in G which can be reached from the
initial vertex following a path corresponding to the word x Σ* and including
any γ arrows.
To obtain a regular expression corresponding to a given transition graph, we
construct a table of derivatives which consists of four columns; i.e.
i) Inputs: - These are words x Σ* ordered by length and for words of
the same length ordered by their numerical magnitude.
ii) Vertices of G: - The corresponding subsets Ax are marked.
iii) Equal to: - If Axy = Ax, then we mark x in the row of xy.
iv) Includes Λ: - A ‘yes’ appears in this column if and only if the
corresponding Ax includes a final vertex.

The regular expression is got from

Tx = 0Tx0 + 1Tx1 + αx
Where

αx = Λ, x Tx
, x Tx

In the expression Tx, any terminal derivative on the R.H.S of the equation is
replaced by the “Equal to” non-terminal appearing in the 3rd column of the
table and in this way a system of equations in which the non-terminals appear
as unknowns is obtained.

5.2.1 Examples:

Obtain a regular expression for the strings recognised by the transition graphs
below:

18
Example 5.2.1.1
2
0
1

1
1 5 +
-
0
1
0
3 4 1
1

Using Tx = 0Tx0 + 1Tx1 + αx

T0 = 0T00 + 1T10 + α0

Table of derivatives:
Input Vertices of G Equal to Includes Λ
1 2 3 4 5
Non-terminals
Λ √ - - - - - -
0 - - √ - - - -
1 - √ - √ √ - Yes
Terminals
00 - - √ - - 0 -
01 √ - - - - Λ -
10 √ - - - - Λ -
11 - - √ - - 0 -

Now:
Tx = 0Tx0 + 1Tx1 + αx
From table;
T00 = T0, T01 = T Λ = T, T10 = T, T11 = T0

When x = Λ;
T Λ = 0T Λ0 + 1T Λ1 + α Λ

 T = 0T0 + 1T1 (1)

When x = 0;
T0 = 0T00 + 1T01 + α0
 T0 = 0T0 + 1T
(2)

When x = 1;
T1 = 0T10 + 1T11 + α1

19
=> T1 = 0T + 1T0 + Λ
(3)

Using the inference rule, i.e.


R = SR + T  R = S*T;
Equation (2) becomes
T0 = 0*1T
(4)
Substitute for T0 in equation (3);
T1 = 0T + 1(0*1T) + Λ
= (0 + 10*1)T + Λ
(5)

Substitute in equation (1);


T = 00*1T + 1[(0 + 10*1)T + Λ]
= (00*1 + 10 + 110*1)T + 1 Λ
= [10 + (0 + 11)0*1]T + 1
T = [10 + (0 + 11)0*1]*1. ……….Using Inference rules.

Possible strings:
1, (10)*1, (00*1)*1, (110*1)*1

Transition graph:

1 0

0
1
1
- +
1
1
0

Example 5.2.1.2

20
1

3
γ
0

γ 2
0

1 γ 4

Table of derivatives:
Input Vertices of G Equal to Includes Λ
1 2 3 4
Λ √ √ √ - - Yes
0 √ √ √ √ - Yes
1 - - √ - - -
00 √ √ √ √ 0 Yes
01 - - √ - 1 -
10 - √ √ - - -
11 - - √ - 1 -
Add more inputs; since 10 is not equivalent to any non-terminal
100 √ √ √ √ 0 Yes
101 - - √ - 1 -

Tx = 0Tx0 + 1Tx1 + αx
T00 = T100 = T0, T01 = T11 = T101 = T1

Using Tx = 0Tx0 + 1Tx1 + αx


WHEN x = Λ,
TΛ = 0T Λ0 + 1T Λ1 + α Λ
T = 0T0 + 1T1 + Λ (1)
WHEN x = 0,
T0 = 0T00 + 1T01 + α0
T0 = 0T0 + 1T1 + Λ (2)
WHEN x = 1,
T1 = 0T10 + 1T11 + α1
T1 = 0T10 + 1T1 (3)
WHEN x = 10,
T10 = 0T100 + 1T101 + α10
T10 = 0T0 + 1T1 (4)

(4) in (3);
T1 = 0(0T0 + 1T1) + 1T1
= 00T0 + (01 + 1)T1
T1 = (01 + 1)*00T0 (5)

21
(5) in (2);
T0 = 0T0 + 1[(01 + 1)*00T0] + Λ
= 0T0 + 1(01 + 1)*00T0 + Λ
= [0 + 1(01 + 1)*00]T0 + Λ
= [0 + 1(01 + 1)*00]* Λ
= [0 + 1(01 + 1)*00]*
T0 = [0 + 1(01 + 1)*00]* (6)

ANS: T = [0 + 1(01 + 1)*00][0 + 1(01 + 1)*00] + Λ

Example 5.2.1.3

3
γ
0

γ 2
0

1 γ 4
1

Table of derivatives:
Input Vertices of G Equal to Includes Λ
1 2 3 4
Λ √ √ √ - - Yes
0 √ √ √ √ - Yes
1 - - √ - - -
00 √ √ √ √ 0 Yes
01 √ √ √ √ 0 Yes
10 - √ √ - - -
11 - - √ - 1 -
100 √ √ √ √ 0 Yes
101 - - √ - 1 -

Using Tx = 0Tx0 + 1Tx1 + αx

22
From table: T101 = T11 = T1, T100 = T01 = T00 = T0
When x = Λ,
T Λ = 0T Λ0 + 1T Λ1 + α Λ
T = 0T0 + 1T1 + Λ
(1)
When x = 0,
T0 = 0T00 + 1T01 + α0
T0 = 0T0 + 1T0 + Λ
= (0 + 1)T0 + Λ
= (0 + 1)* Λ
T0 = (0 + 1)* (2)
When x = 1,
T1 = 0T10 + 1T11 + α1
T1 = 0T10 + 1T1 (3)
When x = 10,
T10 = 0T100 + 1T101 + α10
T10 = 0T0 + 1T1 (4)
Substituting (4) into (3);
T1 = 0(0T0 + 1T1) + 1T1
= 00T0 + (01 + 1) T1
= (01 + 1)*00T0 (5)
(2) into (5);
T1 = (01 + 1)*00(0 + 1)*
(6)
(2) and (6) into (1);
T = 0(0 + 1)* + 1[(01 + 1)*00(0 + 1)*] + Λ

Example 5.21..4
0 0

γ 0
0
1 2 3 4

1 1

Table of Derivatives:
Inputs Vertices Equal to Includes Λ
1 2 3 4
Λ √ √ √ - - Yes
0 - √ √ √ - Yes
1 - √ - - - -
00 - √ √ √ 0 Yes
01 - √ - √ - Yes
10 - √ √ - - -
11 - √ - - 1 -
010 - √ √ √ 0 Yes
011 - √ - √ 01 Yes

23
100 - √ √ √ 0 Yes
101 - √ - - 1 -

NB: Add more inputs since 01 and 10 are any of the Non-terminals. Hence
they also become non-terminals themselves.

Tx = 0Tx0 + 1Tx1 + αx
When x = Λ; T = 0T0 + 1T1 + Λ
(1)

When x = 0; T0 = 0T00 + 1T01 + α0


=> T0 = 0T0 + 1T01 + Λ
(2)
When x = 1; T1 = 0T10 + 1T11 + α1
=> T1 = 0T10 + 1T1
(3)

When x = 01; T01 = 0T010 + 1 T011 + α01


=> T01 = 0T0 + 1T01 + Λ
(4)

When x = 10; T10 = 0T100 + 1T101 + α10


=> T10 = 0T0 + 1T1

(5)

From (4), T01 = 0T0 + 1T01 + Λ


Using the inference rule, this becomes;
T01 = 1*(0T0 + Λ)
= 1*0T0 + 1*

Substitute in (2), T0 = 0T0 + 1(1*0T0 + 1*) + Λ


= (0 + 11*0)T0 + 11* + Λ
=> T0 = (0 + 11*0)*(11* + Λ)

Substitute (5) in (3); T1 = 0(0T0 + 1T1) + 1T1


= (01 + 1)T1 +00T0
=> T1 = (01 + 1)*00T0

From (1), T = 0T0 +1T1 + Λ


= 0T0 + 1(01 + 1)*00T0 + Λ
= [0 + 1(01 + 1)*00]T0 + Λ
T = [0 + 1(01 + 1)*00](0 + 11*0)*(11* + Λ) + Λ.

5.2.2 Assignment:

1. Given the following finite state automaton:


m = ({p, q, r, s}, {0, 1}, δ, p, {q}) where

24
Δ 0 1

P r q

q s p

r p s

s q r

i) Represent the above automaton as a directed graph.


ii) Show the mappings of the above automaton in the form δ(k, x) where k
{p, q, r, s} and x {0, 1}.
iii) Show if the word 110101 is regular.

2. Draw transition graphs to represent the following strings/expressions:


i) 1*0
ii) (01*)*
iii) (10 + 0*1)*1
iv) (0 + 11)0*1
3. Obtain regular expressions of the strings recognised by the following
transition graphs:

i)
0

1 0 2

1 1
3

0, 1

ii)

25
1

1
0

1 2 1 3 1 4
0

0 0

Exercise:

1. Given the transition graph (G) below:

 
1 2 3
+ 1

0 0

a).Generate a table of derivatives to indicate the vertices of G that


can be reached from the initial vertex by following a path
corresponding to strings x , where = {0, 1}. Your
table should also indicate for all terminal strings, the equivalent
lower order values of x and whether or not the string leads to a
final state.

26
b). Using the table of derivatives obtained in (a) above,
obtain the regular expression generated by the transition graph
G. simplify your expression to the smallest possible form.

2. Given the transition graph (G) below:

2 0
-
1
1
1 - 5 +
- -
1

0 3 1
- 1 -
-
4

a).Generate the Table of derivatives to indicate the vertices of G


that can be reached from the initial vertex by following a path
corresponding to strings x , where = {0, 1}. Your
table should also indicate for all terminal strings, the equivalent
lower order values of x and whether or not the string leads to a
final state.

b). Using the table of derivatives obtained in (a) above,


obtain the regular expression generated by the transition graph
G. simplify your expression to the smallest possible form.

27
CHAPTER SIX

6.0 GRAMMARS

These are mathematical systems of defining a language as well as a device


for giving the sentences in the languages a useful structure.

6.1 Definition:

A grammar is a 4-tuple (quadruple) G = (N, Σ, P, S) where


N = a finite set of non terminal symbols;
Σ = a finite set of terminal symbols such that N Σ= ;
P = a set of productions or rules for producing sentences from the grammar;
S = a distinguished symbol in N called the sentence symbol or start symbol.

A grammar for a language L uses two distinct sets of symbols, that is, the set
of Non terminals N and the set of terminal symbols Σ.

28
Σ is the alphabet over which the language is defined, N are symbols used in
the generation of words in L.

Example:
G = ({A, S}, {0, 1}, P, S) where P consists of:
S 0A1
0A 00A1
A Λ
Some derivatives: S 0A1 00A11 0011 (00Λ11)
Or S 0A1 01 (0Λ1)
S 00A11 000A111
000111

6.2 Classification of Grammars

6.2.1. Type 0 or Right Linear Grammar:

Each production (P) is of the form A xB or A x where A and B are in N,


and x Σ*.

6.2.2. Type 1 or Context Sensitive Grammar:

If each production (P) is of the form α β where . A true context


sensitive grammar is a grammar (N, Σ, P, S) in which each production in P is
of the form αAβ α β.
Where α, β (N Σ)*
γ (N Σ)+.
NB: Σ+ = Σ*- Λ.
Such a production can be interpreted to mean that A can be replaced by γ
only in the context - .

6.2.3. Type 2 or Context Free Grammar:

Each production P is of the form A α where A N and α (N Σ).

6.2.4. Type 3 or Regular Grammar:

Each production P is of the form A x or A Bx where A, B N and x Σ.

Example:

Z U0/V0 (Z can be replaced by U0 or V0)


U Z1/1

29
V Z0/0
Where Σ = {0, 1} and N = {U, V, Z}.

A set of strings generated by a regular grammar is also accepted by a finite


state automaton and vice versa. We can draw a state diagram to help us
recognize the sentences of a regular grammar G. In the diagram each non
terminal of G is represented by a node or state and in addition we have a start
state S. For each rule Q x there’s a directed arc labelled x from S to Q.
E.g.
Construct an automaton for the following grammar G whose production are
defined as follows: -
Z A0
A A0/Z1/0

1
0
Z A

0
0
S
Z A0
A A0
A Z1
A 0

Z A0 00
Z A0 A00 000
Z A0 Z10 A010 A0010 00010

6.3 Definitions

A derivation of some sentence w L (G) is a sequence


S w1 w2 ….. wn w and the strings S, w1, w2, ….., wn are
referred to as sentential forms of the derivation of sentence w.

The language generated by a grammar G, denoted by L (G), is the set of


sentences generated by G. A sentential form containing only terminal symbols
is called a Sentence generated by a grammar G.

30
Syntax refers to the set of rules for determining whether or not a sentence
generated by G is in L (G).
Example:

1) Consider a grammar given by

G = ({S}, {0, 1}, S, P) where P is:


S aSb
S Λ

Consider S aSb aaSbb aabb; aabb is a sentence of the language.


The other strings in the derivation are sentential forms.
Similarly, S * aabb is the same as above.
W Z
W * Z means W forms some sets to derive Z.

Now; for G = ({S}, {0, 1}, S, P)


Where P is
S aSb
S Λ
Λ, ab, aabb, aaabbb, ………………..
L (G) = {anbn; n 0}.

2) Consider
(i) S aSb
S b
(ii) S Ab
A aAb
A Λ
Possible strings:
b, abb, aabbb, aaabbbb, ………………
S aSb aaSbb aabbb

L (G) = {anbn+1; n 0}

For S Ab
A aAb
A Λ

31
S Ab b
S Ab aAbb abb
S Ab aAbb aaAbbb aabbb are the possible strings.
Here we have two grammars which generate the same language. Hence the
two grammars are equivalent.

6.4 Deriving Grammar from a Given Language:

1) Find a grammar corresponding to the language L (G) = {(a 2b2) n, n>0}.

Solution:

Possible strings:
n = 1, a2b2 = aabb
n = 2, (a2b2)2 = aabbaabb
aabb, aabbaabb, aabbaabbaabb, ……………
Hence;
S aabbA
A aabbA
A Λ

2) Find a grammar corresponding to the language L (G) = {a nbn+1, n 0}

Solution:

n = 0, anbn+1 = b
n = 1, anbn+1 = abb
n = 2, anbn+1 = aabbb
n = 3, anbn+1 = aaabbbb
b, abb, aabbb, aaabbbb, ……
Hence,
S Ab
A aAb
A

Exercise:

1. Define the following in relation to grammars:


i) Language

32
ii) Syntax
iii) Derivation
iv) Sentence
v) Sentential form

2. Find grammars for the following languages:

i) L(G) = {an bm; m,n>0}

ii) L(G) = {an bn+1; n>=0}

3. Give and briefly explain the four classifications of grammars.

4. construct an automaton for the following grammar G where = {0,


1} and N = {A, B, C} and P is defined as follows:

A  A0 | 1 | B1
B  C1 | 1
C  0 | A1 | B0

CHAPTER SEVEN

7.0 PUSH-DOWN AUTOMATA AND CONTEXT-FREE GRAMMAR

7.1 Definition:

A Push-Down automaton (PDA), m is a system


m = (K, Σ, , δ, q0, z0, F) where
K = finite set of states
Σ = finite alphabet of input symbols
(Ghe) = finite push-down alphabet
q0 K is the initial state.
z0 , is a particular push-down symbol called the Stack symbol. z0 initially
appears on top of the push-down stack.

33
F K is the set of final states.
δ is a mapping from K {Σ Λ} Г to finite subsets of K Г*, i.e.
δ (q0, a, z) = {(p1, γ1), (p2, γ2), ….., (pm, γm)} where q0 and pi K, a Σ, and γi
Г*

Acceptance of a word occurs if:


i) A sequence of moves empties the store, and
ii) It ends in the final state.

7.2 Examples:

L (G) = {anbn, n>0} is generated by the grammar S asb/ab. A PDA to


recognise the language would be:

START

a
INPUT=?
Λ
Λ
b
PUSH DOWN
S Yes
STACK EMPTY? REJECT

No
b POP UP

a
INPUT=?

Λ
No
STACK EMPTY?

Yes

ACCEPT

PDA = ({q0, q1, q2}, {a, b}, {z, a}, δ, q0, z, {q0}) where

δ(q0, a, z) = (q1, a, z)

34
δ(q1, a, a) = (q1, aa)
δ(q1, b, a) = (q2, a)
δ(q2, b, a) = (q2, Λ)
δ(q2, Λ, z) = (q0, Λ) Acceptance.

If we are to use aabb, as inputs;


δ(q0, aabb, z) δ(q1, abb, az) δ(q1, bb, aaz) δ(q2, b, az) δ(q2, Λ,
z) (q0, Λ) Accept the string.

2. L(G) = {anbm; m = n+1, n 0}


Or {anbn+1; n 0}
b
abb
aabbb
aaabbbb are the possible strings.
An empty word can’t be accepted

START

a
INPUT=?
Λ

PUSH DOWN b
S Yes
STACK EMPTY? REJECT

No
b POP UP

INPUT=?
a
INPUT=?
Λ
Λ
a, b No
STACK EMPTY?

Yes

ACCEPT

35
Example:

Consider the language given by


L(G) = {0i1j; j=i+1, i,j 0}

PDA = ({q0, q1, q2, q3}, {0, 1}, {0, z}, δ, q0, z, {q3})
Where
1. δ(q0, 0, z) = (q0, 0z)
2. δ(q0, 1, z) = (q2, Λ)
3. δ(q0, 0, 0) = (q0, 00)
4. δ(q0, 1, 0) = (q1, Λ)
5. δ(q1, 1, 0) = (q1, Λ)
6. δ(q1, 1, z) = (q2, Λ)
7. δ(q2, Λ, z) = (q3, Λ) Acceptance state.

Testing the string 00011111 for acceptance:


δ(q0, 00011111, z) δ(q0, 0011111, 0z) δ(q0, 011111, 00z) δ(q0,
11111, 000z) δ(q1, 1111, 00z) δ(q1, 111, 0z) δ(q1, 11, z) δ(q2, 1,
Λ) Invalid i.e. no production is applicable.

Exercise:

1. Give the formal definition of a Push-Down Automaton (PDA).


2. Construct an automaton for the following grammar G:

A  A0 | B1 | 1
B  C1 | 1
C  A0 | B0 | 0
Where = {0, 1} and N = {A, B, C}
3. Design a Push-Down Automaton to recognize the strings of the
language generated by the grammar G with the following set of
productions:

S  aAb
A  aAb

36
Ab

4. Consider the language L(G) generated by the grammar G with the


following set of productions:
S  Ab
A  aAb
A
i) Design a PDA to recognize the words of the language.
ii) Show if the string aaabbbbb is recognized as a valid sentence of
the language.

5. Consider the language given by L(G) = {ai, bj | j = i+1; j>=0}.

(i) Design a PDA to recognize the words of this language, carefully


explaining all the symbols used.
(ii) Show if the string aaabbbbb is recognized as a valid sentence of
the language.

6. (i) Construct a PDA for the language {0n1n2m | m, n>+0}.


(ii) Show if the string 000111222 is recognized as a valid sentence of
the language.

7. Design a Push Down Automaton to recognize the strings of the


language generated by the grammar G with the following set of
productions: -

S  aAb
A  aAb
Ab

37
CHAPTER EIGHT

8.0 MARKOV ALGORITHMS (MAs)

8.1 Introduction:

MAs deal with transformation of strings of symbols i.e. given a string A, to


transform it into a string B in a mechanical way.
The transformations of which Markov Algorithms are composed of are those
that replace the first left-most occurrence of the specified string A in the given
string by another specified string B.
MAs then consist of a sequence of such transformations and there are
essentially two kinds of transformations.
Consider a given alphabet and strings of symbols from this alphabet. Suppose
that the alphabet does not contain the symbols → and then;
i) The Simple Markov production is a string A B where A and B are
strings of the alphabet.
ii) The Conclusive or terminal Markov production is a string A B,
A,B *
A is called the antecedent and B is called the consequent.
A consequent is the second half of a hypothetical proposition. In the standard form of such a proposition, it is the
part that follows "then". In an implication, if P implies Q, then P is called the antecedent and Q is called
the consequent. In some contexts the consequent is called the apodosis.

38
8.2 Definition:

The MA is a finite sequence p1, p2, ………., pn of Markov productions applied


to the string according to the following procedure:
Let S be a given string; the sequence of productions P is searched to
find the first production Pi whose antecedent occurs in S.
If no such Pi exists, the operation of the algorithm halts without a change in S.
If there’s a Pi in the algorithm whose antecedent occurs in S, the first search,
Pi is applied to S. If this is a Conclusive production, the operation of the
algorithm halts with no further changes in S. If it’s a Simple production, a new
search is conducted using S′ into which S has been transformed.
NB: The execution begins with the first production and if a sub-string to be
replaced is found and replaced, execution resumes with the first statement of
the algorithm. If the statement to be replaced is not found, execution
continues to the next production.
The M.A terminates in either of the following ways: -
i) The last statement is executed and fails (algorithm is dropped)
ii) A statement terminated with a period is executed successfully.
If the operation of the algorithm ceases with the string S*, we say that S* is
the result of applying the algorithm to S.
The scheme works as follows: -
Let V be an alphabet and δi V*; then
i) Set i = 0
ii) Set j = 1
iii) If Pj is applicable to δi GO TO (v)
iv) Set j = j+1; if j>n, the algorithm is BLOCKED, else GO TO (iii)
v) Apply Pj to δi. . Set i = i+1. If Pi was simple, Go TO (ii), else
Terminate.

NOTE:
A M.A is applicable to a string if and only if there’s at least one occurrence of
an antecedent in S.

8.3 Examples
1. Duplication of strings:
P1: x x x (x V)

39
P2: xy y x (x, y V)
P3: Λ
P4: Λ
P5: Λ

NOTE:
, are referred to as markers from the auxiliary alphabet (V’)
The last production P5 introduces a marker (symbol from an auxiliary
alphabet) which is used to mark the sub-string to be moved; production P 4
moves the marker to leave the final string. The first production duplicates a
marked character but introduces a second marker to mark the duplicate
character for subsequent movement to the end of the original string.

Let S = abc
P5 (S’) P1 P1 P1 P2
abc Λabc αabc aβaαbc aβabβbαc aβabβbcβcα
βaβbcβcα

P2 P2 P3 P3 P3 P4
abβacβbβcα abcβaβbβcα abcaβbβcα abcabβcα abcabcα
abcabc.

The productions represent the M.A for duplicating the strings.

2. Reversal of characters of strings


P1:    (,  V’)
P2: x  x (x V)
P3: xy  yx (x, y V)
P4: x  x
P5:   
Let S = abc;
P5 P3 P3 P4 P5 P3 P2

40
abc abc bac bca bca bca cba
cba
P5 P2 P5 P1
cba cba cba cba.
NOTE: (i) Stop normally
(ii) No production which can be applied; are the 2 ways of stopping.

3. Count the number of occurrences of a substring in the string.


P1:     (,  v’); substring,
 =  where  = 1st character and  = rest of characters.
P2: x  x (x V)
P3: x  x
P4:   
P5:   

Let s = ABRACADABRA;  = ABR  = A,  = BR


P5 P1 P2
ABRACADABRA ABRACADABRA ABRACADABRA
P2 P2
ABRACADABRA ABRACADABRA ABRACADABRA
P2 P1 P2
…… ABRACADABRA ABRACADABRA
P2 P2 P3
ABRACADABRA ABRACADABRA ABRACADABRA
P3 P3 P3
ABRACADABRA ABRACADABRA… ABRACADABRA

P3 P3 P3
ABRACADABRA ABRACADABRA ABRACADABRA
P3 P4

41
ABRACADABRA …. ABRACADABRA ABRACADABRA.

The substring occurs twice in the string.

4. Add two binary numbers


P1: 001  1; (,  V’)
P2: 011  10; (0, 1  V)
P3: 101  10
P4: 111  11
P5: xy  yx; (x, y  V) i.e. x = 0 or 1, y = 0 or 1.
P6: 00  0
P7: 01  1
P8: 10  1
P9: 11  10
P10: x  x; ( = arithmetic operator)
P11: x  x
P12:   
P13:   
Note: P12 and p13 can be merged to   
E.g. Evaluate 101011 where  is the addition operator.
Solution:
101011 101011 100111 100111 100111
100110 100110 100110 10100 10100
1000 1000 1000 1000 1000 1000.

5. Write a Markov algorithm to remove all occurrences of a specified


substring from a string e.g. “ara” from “ararat”.

Solution:
Algorithm:
i) Introduce a marker:  
ii) Search for the substring: x  x
iii) Remove the substring: ara  
iv) Remove marker:   

42
v) Terminate
Hence, we combine the above and write in order of priority:
P1: ara  
P2: x  x
P3:   
P4:  
NOTE:
ara can be changed with A where A is any substring.
E.g.
P4 P1 P2 P2 P1 P3
araratara  araratara  ratara  ratara …  ratara  rat rat.

Exercise:

1: Design a Markov algorithm to replace the occurrences of the string “BRA”


in a given string with the substring “ME”.
2: Show the sequence of applications of the above algorithm starting from
the string “ABRACADABRA”.
3: What is meant by a Markov algorithm production being:
i) Simple
ii) Conclusive?

4: Describe the procedure according to which a finite sequence P 1, P2… Pn of


Markov productions is applied to a string.
5: What are the two ways in which a Markov algorithm can be terminated?
6: Apply the Markov algorithm given below to the string ‘READ’ and explain
the purpose of the algorithm.

R1:    ,  V’ (V’ = auxiliary alphabet)


R2: x  x x V (V = alphabet)
R3: xy  yx y V
R4: x  x
R5:   

43
CHAPTER NINE

9.0 TURING MACHINE

9.1 Introduction:

A Turing Machine is a mathematical model of a computing device. It’s a


machine comprising of an infinite tape marked of equal sections (squares of
the tape) and a tape head which can move, read and point a symbol in the
square of the tape over which it’s positioned.

The elements of a Turing machine are; - a Tape, a Read/Write head, and a


Program. Its actions each consisting of three steps i.e. write a symbol, then
move the read/write head and then change state.

A Turing Machine operates at discrete intervals of time and at each interval,


it’s in an internal state.

44
At a particular time t, what symbol is to be printed and whether the tape head
is to move to the right or left depends on the combination of the internal state
of the Turing Machine and the symbol in the square being read at that time.
This combination also determines the next internal state.

9.2 Physical Interpretation:

Internal States, Q = {q0, q1, ……., qj, ………..}


Tape Symbols, S = {Λ, S1, S2, ……, Sj, …………}
Output Function, f: Q S S; f (qi, Sj) = Sij
Next State Function, g: Q S Q; g (qi, Sj) = qij
There are 3 TM descriptions: - Quadruples, Quintuples and program.
9.3 Description of the TM through Quintuples:
TM = qi Sj Sij qij M where
Qi = Current state
Sj = Scanning symbol in current R/W head position
Sij = New symbol
Qij = Next state
M = Move right (R) or left (L)
9.4 Definition (other):

A Turing Machine is defined to be a finite set of quintuples such that each


quintuple is either of the form q i Sj St qk R or qi Sj St qk L; qi must not be qH, the
halt state or no two quintuples must begin with the same pair q i Sj i.e. the TM
set up must be deterministic.

Example 1:
A TM to ‘empty’ a string i.e. convert a string to null:

1: q0 S1 Λ q0 R
2: q0 Λ Λ qH R

Example 2:
A TM to convert n to n+1, where n is written in decimal notation:

1: q0 S1 S1 q0 R
2: q0 Λ Λ q1 L
3: q1 0 1 qH R
4: q1 1 2 qH R
5: q1 2 3 qH R

45
6: q1 3 4 qH R
7: q1 4 5 qH R
8: q1 5 6 qH R
9: q1 6 7 qH R
10: q1 7 8 qH R
11: q1 8 9 qH R
12: q1 9 0 q1 L
13: q1 Λ 1 qH R
Exercise:

1. Write a Markov Algorithm to replace all the occurrences of a given sub-


string A with a sub-string B in a given string.
2. Write a Markov Algorithm to subtract two binary numbers using
modulo-2.
3. Design a Turing Machine to convert n to n+2 where n is represented in
decimal notation.
4. Explain the physical interpretation of Turing Machines.

BIBLIOGRAPHY

1. Aho, Ullman and Hopcroft; Automata, Complexity & Computability

2. Arto Salomaa; Theory of Automata

3. Abraham Ginsberg; Algebraic theory of Automata

4. B.A Galler & A.J Perlis; A view to programming languages.

5. Compilers; Principles, techniques and tools by Aho, Sethi and Ullman.

46
APPENDIX: SAMPLE QUESTIONS

1. (a) Let R be a binary relation over a set S. What is meant by R being:


i) Symmetric;
ii) Reflexive;
iii) Transitive?

(b)
i) Define a deterministic semi-automaton, explaining fully the
parameters used in your definition and show how it differs from a
Non-deterministic semi-automaton.
ii) Using an appropriate example, show the possible
representations of a non-deterministic semi-automaton using a
transition table and a state graph.

2. a) What is a regular set?

b) An automaton A has the following mapping:

47
δ(θ0, 0) = θ2
δ(θ1, 0) = θ3
δ(θ2, 0) = θ0
δ(θ3, 0) = θ1
δ(θ0, 1) = θ1
δ(θ1, 1) = θ0
δ(θ2, 1) = θ3
δ(θ3, 1) = θ2

Where 0, 1 are elements of the automaton alphabet and θ 0, θ1, θ2 and


θ3 are the states of the automaton, θ0 being the initial and final state.

Show which of the following strings are regular with regard to


automaton A:

i) 110011
ii) 111000
iii) 101010
iv) 001110

3. (a) Define a semi-automaton, explaining fully the parameters used in


your definition.
(b) Differentiate between deterministic and non-deterministic semi-
automaton.

4. (a) What is a regular set?


(b) An automaton A has the following mappings:

δ(S0, 0) = S2
δ(S1, 0) = S3
δ(S2, 0) = S0
δ(S3, 0) = S1
δ(S0, 1) = S1
δ(S1, 1) = S0
δ(S2, 1) = S3
δ(S3, 1) = S2

Where 0, 1 are elements of the automaton alphabet and S 0, S1, S2 and


S3 are the states of the automaton, S0 being the initial and final state.

48
Which of the following strings are regular with regard to automaton A?

i) 110101
ii) 111010
iii) 101010
iv) 011011

5. (a) Construct the Finite-state Automaton for the language:


(a + b)*bab
(b) Show if the string bbabbabb is recognized by your finite automaton
as a valid sentence of the language.

6. What do you understand by the following terms: -

i) a mapping
ii) a permutation
iii) an identity relation

7. Draw Transition graphs to represent the following strings/expressions:

i) (a + b)*aa(a + b)*

ii) [10 + (0 + 11)0*1]*1

iii) (a +b)*bab

8. Define the following in relation to grammars: -


i) Language
ii) Syntax
iii) Derivation
iv) Sentence
v) Sentential form.
9. Write a Markov Algorithm (MA) to duplicate the characters in a given
string e.g. “xyz” should give “xyzxyz”. Apply your algorithm to the string
“SIM”.
10. Given the following finite-state automaton
M = ({a, b, c, d}, {0, 1}, δ, a, {a}) where

Inputs

δ 0 1
States
a c b
b d a
c a 49 d
d b c
a). Represent the above automaton as a directed graph.

b). Show the mappings of the above automaton in the form


δ (k, x) = k where k {a, b, c, d} and x {0, 1}

c). Show if the word 110101 is accepted by the finite-state


automaton above.

11. Find grammars for the following languages:


i) L(G) = {an bm; m, n>0}
ii) L(G) = {an bn+1; n>=0}

12. (a) Construct an automaton for the following grammar G:

X  X0 | Y1 | 1
Y  Z1 | 1
Z  X0 | Y0 | 0
Where Σ = {0, 1} and N = {X, Y, Z}.
(b) Design a push down automaton to recognize the strings of the
language generated by the grammar G with the following set of
productions:

S ----- aAb
A ---- aAb
A ---- b

13. Given the transition graph (G) below:

50
1

3
γ
0

γ 2
0

1 γ 4

i) Generate the table of derivatives to indicate the vertices of G


that can be reached from the initial vertex by following a path
corresponding to the strings x Σ, where Σ = {0, 1}. Your table
should also indicate for all terminal strings, the equivalent lower
order values of x and whether or not the string leads to a final
state.
ii) Using the table of derivatives obtained in (a) above, obtain the
regular expression generated by the transition graph G.

14. (a) What is meant by a Markov Algorithm production being


i) Simple
ii) Conclusive

(b) (i) Describe the procedure according to which a finite sequence P 1,


P2, ….., Pn of Markov productions is applied to a string.

(ii) What are the two ways in which a Markov Algorithm can be
terminated?

(c) (i) Design a Turing Machine to increment a number, represented in


decimal notation, by two (2).

(ii) Explain the physical interpretation of Turing Machines.

15. (a) Obtain a regular expression for the strings recognised by the
transition graphs below by generating a table of Derivatives to indicate
the vertices of G that can be reached from the initial vertex by following

51
a path corresponding to strings x , where = {0, 1}. Your
table should also indicate for all terminal strings, the equivalent lower
values of x and whether or not the string leads to a final state.
(b) Using the table of derivatives obtained in (a) above, obtain the
regular expression generated by the transition graph G.

1.

3
γ
0

γ 2
0

1 γ 4

2.

3
γ
0

γ 2
0

1 γ 4
1

52
16. Draw transition graphs to represent the following strings/expressions:
i) (a + b)*bab
ii) [10 + (11)0*1]*1
iii) (0 + 1)*00(0 + 1)*

17. (a) Define a Turing machine in form of quintuples clearly explaining the
symbols used in your definition.

(b) Apply the Turing machine quintuples given below to the decimal
number 9595 and explain the purpose of the TM.

1: q0 S1 S1 q0 R
2: q0 Λ Λ q1 L
3: q1 0 1 qH R
4: q1 1 2 qH R
5: q1 2 3 qH R
………………………

12: q1 9 0 q1 L
13: q1 Λ 1 qH R

NOTES

………………………………………………………………......................................
............................................................................................................................
............................................................................................................................
............................................................................................................................
………………………………………………………………......................................
............................................................................................................................
............................................................................................................................
............................................................................................................................
………………………………………………………………......................................
............................................................................................................................

53
............................................................................................................................
............................................................................................................................
………………………………………………………………......................................
............................................................................................................................
............................................................................................................................
............................................................................................................................
………………………………………………………………......................................
............................................................................................................................
............................................................................................................................
............................................................................................................................
………………………………………………………………......................................
............................................................................................................................
............................................................................................................................
............................................................................................................................
………………………………………………………………......................................
............................................................................................................................
............................................................................................................................
............................................................................................................................
………………………………………………………………......................................
............................................................................................................................
............................................................................................................................
............................................................................................................................
………………………………………………………………......................................
............................................................................................................................

54

You might also like