You are on page 1of 47

D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-

BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;


website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

INES-RUHENGERI
Administering Faculty: FAFS
Department: Computer Science

Module Title:
FORMAL LANGUAGE AND FINITE AUTOMATA

Computer Science Level 2


Number of Credits:10
Module Code: CSC 2211
Academic year: 2022-2023

Lecturer: Nsengiyumva Emmanuel,


Phone: +250786356510
Email: ensengiyumva@ines.ac.rw

1
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

TABLE OF CONTENTS

Unit 1: Introduction –Basics of Automata Theory, Basic concepts: definitions,


Alphabets and Languages, Closure property.

Unit 2:Finite Automata (FA) –Definition and description, Deterministic, Non-


deterministic finite automata and their equivalence,.

Unit 3:Regular Set –Regular Expression and regular set, Closure properties of
regular sets,Pumping lemma,

Unit 4: Push Down Automata (PDA)–Definition and description, PDA and Turing
Machines.

Unit 5: Turing Machines.

2
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

UNIT 1: INTRODUCTION

1. Basics of Automata:

Definitions:

Automata theory is the basis for the theory of formal languages. A proper treatment of
formal language theory begins with some basic definitions:

 A symbol is simply a character, an abstraction that is meaningless by itself.


 An alphabet is a finite set of symbols.
 A word is a finite string of symbols from a given alphabet.
 Finally, a language is a set of words formed from a given alphabet.

The set of words that form a language is usually infinite, although it may
be finite or empty as well. Formal languages are treated like mathematical sets, so they
can undergo standard set theory operations such as union and intersection.
Additionally, operating on languages always produces a language. As sets, they are
defined and classified using techniques of automata theory.

Formal languages are normally defined in one of three ways, all of which can be
described by automata theory:

 regular expressions
 standard automata
 a formal grammar system

Regular Expressions Example

3
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

alphabet A1 = {a, b}
alphabet A2 = {1, 2}
language L1 = the set of all words over A1 = {a, aab, ...}
language L2 = the set of all words over A2 = {2, 11221, ...}
language L3 = L1 ∪ L2

Automata Theory is an exciting, theoretical branch of computer science. It established


its roots during the 20th Century, as mathematicians began developing - both
theoretically and literally - machines which imitated certain features of man, completing
calculations more quickly and reliably. The word automaton itself, closely related to the
word "automation", denotes automatic processes carrying out the production of specific
processes. Simply stated, automata theory deals with the logic of computation with
respect to simple machines, referred to as automata. Through automata, computer
scientists are able to understand how machines compute functions and solve problems
and more importantly, what it means for a function to be defined as computable or for a
question to be described as decidable.

Automatons are abstract models of machines that perform computations on an input


by moving through a series of states or configurations. At each state of the
computation, a transition function determines the next configuration on the basis of a
finite portion of the present configuration. As a result, once the computation reaches an
accepting configuration, it accepts that input. The most general and powerful automata
is the Turing machine.

The major objective of automata theory is to develop methods by which computer


scientists can describe and analyze the dynamic behavior of discrete systems, in which
signals are sampled periodically. The behavior of these discrete systems is determined
by the way that the system is constructed from storage and combinational elements.
Characteristics of such machines include:

 Inputs: assumed to be sequences of symbols selected from a finite set I of input


4
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

signals. Namely, set I is the set {x1, x,2, x3... xk} where k is the number of inputs.
 Outputs: sequences of symbols selected from a finite set Z. Namely, set Z is the
set {y1, y2, y3 ... ym} where m is the number of outputs.
 States: finite set Q, whose definition depends on the type of automaton.

There are four major families of automaton:

 Finite-state machine
 Pushdown automata
 Linear-bounded automata
 Turing machine

The families of automata above can be interpreted in a hierarchal form, where the finite-
state machine is the simplest automata and the Turing machine is the most complex.
The focus of this project is on the finite-state machine and the Turing machine. A Turing
machine is a finite-state machine yet the inverse is not true.

Finite State Machines

The exciting history of how finite automata became a branch of computer science
illustrates its wide range of applications. The first people to consider the concept of a
finite-state machine included a team of biologists, psychologists, mathematicians,
engineers and some of the first computer scientists. They all shared a common interest:
to model the human thought process, whether in the brain or in a computer. Warren
McCulloch and Walter Pitts, two neurophysiologists, were the first to present a
description of finite automata in 1943. Their paper, entitled, "A Logical Calculus
Immanent in Nervous Activity", made significant contributions to the study of neural
network theory, theory of automata, the theory of computation and cybernetics. Later,
two computer scientists, G.H. Mealy and E.F. Moore, generalized the theory to much

5
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

more powerful machines in separate papers, published in 1955-56. The finite-state


machines, the Mealy machine and the Moore machine, are named in recognition of their
work. While the Mealy machine determines its outputs through the current state and the
input, the Moore machine's output is based upon the current state alone.

An automaton in which the state set Q contains only a finite number of elements is
called a finite-state machine (FSM). FSMs are abstract machines, consisting of a set of
states (set Q), set of input events (set I), a set of output events (set Z) and a state
transition function. The state transition function takes the current state and an input
event and returns the new set of output events and the next state. Therefore, it can be
seen as a function which maps an ordered sequence of input events into a
corresponding sequence, or set, of output events.

State transition function: I → Z

Finite-state machines are ideal computation models for a small amount of memory, and
do not maintain memory. This mathematical model of a machine can only reach a finite
number of states and transitions between these states. Its main application is in
mathematical problem analysis. Finite-machines are also used for purposes aside from
general computations, such as to recognize regular languages.

In order to fully understand conceptually a finite-state machine, consider an analogy to


an elevator:

An elevator is a mechanism that does not remember all previous requests for service
but the current floor, the direction of motion (up or down) and the collection of not-yet
satisfied requests for services. Therefore, at any given moment in time, an elevator in
operated would be defined by the following mathematical terms:

 States: finite set of states to reflect the past history of the customers' requests.
 Inputs: finite set of input, depending on the number of floors the elevator is able

6
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

to access. We can use the set I, whose size is the number of floors in the building.
 Outputs: finite set of output, depending on the need for the elevator to go up or
down, according to customers' needs.

A finite-state machine is formally defined as a 5-tuple (Q, I, Z, ∂, W) such that:

 Q = finite set of states


 I = finite set of input symbols
 Z = finite set of output symbols
 ∂ = mapping of I x Q into Q called the state transition function, i.e. I x Q → Q
 W = mapping W of I x Q onto Z, called the output function
 A = set of accept states where F is a subset of Q

From the mathematical interpretation above, it can be said that a finite-state machine
contains a finite number of states. Each state accepts a finite number of inputs, and
each state has rules that describe the action of the machine for ever input, represented
in the state transition mapping function. At the same time, an input may cause the
machine to change states. For every input symbol, there is exactly one transition out of
each state. In addition, any 5-tuple set that is accepted by nondeterministic finite
automata is also accepted by deterministic finite automata.

When considering finite-state machines, it is important to keep in mind that the


mechanical process inside the automata that leads to the calculation of outputs and
change of states is not emphasized or delved into detail; it is instead considered a
"black box", as illustrated below:

Having finite, constant amounts of memory, the internal states of an FSM carry no
further structure. They can easily be represented using state diagrams, as seen below:

The state diagram illustrates the operation of an automaton. States are represented
by nodes of graphs, transitions by the arrows or branches, and the corresponding inputs

7
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

and outputs are denoted by symbols. The arrow entering from the left into q0 shows
that q0 is the initial state of the machine. Moves that do not involve changes of states
are indicated by arrows along the sides of individual nodes. These arrows are known
as self-loops.

UNIT 2: FINITE AUTOMATA (FA)

1. Definition and description,


Finite automaton is a machine used to recognize patterns. FA accepts or rejects an
input based on already defined set of strings known as the language of the automaton.

A finite automaton/machine has a finite number of states where a state can either be
an accepting state or rejecting state. It takes a sequence of input and changes its
current state pointer according to the rules of the machine known as the state function.
After the input sequence ends the current state of the machine determines whether the
input is accepted or rejected based on the type of the state. In other words, if the
current/active state of a machine after the input given ends, is an accepting state the
input is known to be accepted and vice versa.

Hence a finite automaton is designed to result in an accepting state if the input lies in
the language (set of strings) we want to recognize. Also vice versa.

A simple fa example is that of airplane safety control. It tracks the state of the airplane,

8
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

like fuel, doors open, fins operating etc. Different events change the state of the airplane
safety control, which might either result into an accepting state (allowing starting the
engine and takeoff) or a rejecting state if the airplane is not a condition for a safe flight.

Methamatical Representation

A finite automaton is mathematically represented by

Q,,,q0,F
Where,
Q: set of all states.
Σ(Alphabet): all possible symbols for an input.
δ (Transition Function): the rules which specify the next state.
q0: the initial state
F: set of accepting state

Example:

Let's create FA which accepts strings starting with 'aa'

Here,
Q: {a1,a2,okay,never}
Σ(Alphabet): {a,b,c, ....} (finite)
q0: 'no input yet'
F: {okay}
δ (Transition Function):

state For input a For anything else


a1 a2 never
a2 okay never
never never never

9
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

okay okay okay


Design & Symbols

Finite automaton can also be represented graphically to easily understand what is


going on. Trying to understand the language of a machine through its state transition
function is very difficult. The sate transition function is good for computers while
human find the state diagram more comfortable.

Non Accepting/Rejecting States

Non accepting states are the sates at which if the input string ends the input is
considered to be rejected.

Accepting States

Accepting states are the sates at which if the input string ends the input is considered
to be accepted.

Trap States

For some languages a machine can determine if a string is valid or invalid in early stage
10
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

and after which no symbol has any effect on the result such a state is known as a trap
state.

State Transition

where the transition symbol is the character in the input which results in the transition.

Types

Different types of finite automata are

1. Deterministic finite automata


2. Nondeterministic finite automata

DETERMINISTIC FINITE AUTOMATA

Formal Definition of DFA

A DFA is a collection of 5-tuples same as we described in the definition of FA.

11
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

1. Q: finite set of states


2. ∑: finite set of the input symbol
3. q0: initial state
4. F: final state
5. δ: Transition function

Transition function can be defined as: δ: Q x ∑→Q

Graphical Representation of DFA

A DFA can be represented by digraphs called state diagram. In which:

1. The state is represented by vertices.


2. The arc labeled with an input character show the transitions.
3. The initial state is marked with an arrow.
4. The final state is denoted by a double circle.

Example 1:
1. Q = {q0, q1, q2}
2. ∑ = {0, 1}
3. q0 = {q0}
4. F = {q2}

DFA (Deterministic finite automata)


o DFA refers to deterministic finite automata. Deterministic refers to the
uniqueness of the computation. The finite automata are called deterministic
finite automata if the machine is read an input string one symbol at a time.
o In DFA, there is only one path for specific input from the current state to the
next state.
o DFA does not accept the null move, i.e., the DFA cannot change state without
any input character.
12
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

o DFA can contain multiple final states. It is used in Lexical Analysis in Compiler.

In the following diagram, we can see that from state q0 for input a, there is only one
path which is going to q1. Similarly, from q0, there is only one path for input b going
to q2.

Example 1:
1. Q = {q0, q1, q2}
2. ∑ = {0, 1}
3. q0 = {q0}
4. F = {q2}

Solution:

Transition Diagram:

13
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

Transition Table:

Present State Next state for Input 0 Next State of Input


1

→q0 q0 q1

q1 q2 q1

*q2 q2 q2

Example 2:

DFA with ∑ = {0, 1} accepts all starting with 0.

Solution:

Explanation:

o In the above diagram, we can see that on given 0 as input to DFA in state q0 the DFA change
always go to final state q1 on starting input 0. It can accept 00, 01, 000, 001....etc. It can't acc
starts with 1, because it will never go to final state on a string starting with 1.

14
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

Example 3:

DFA with ∑ = {0, 1} accepts all ending with 0.

Solution:

Explanation:

In the above diagram, we can see that on given 0 as input to DFA in state q0, the DFA changes state to

It can accept any string which ends with 0 like 00, 10, 110, 100....etc. It can't accept any string which en

1, because it will never go to the final state q1 on 1 input, so the string ending with 1, will not be accep

Will be rejected.

DETERMINISTIC AND NON-DETERMINISTIC FINITE AUTOMATA AND THEIR


EQUIVALENCE

Formal Definition of an NDFA

An NDFA can be represented by a 5-tuple (Q, ∑, δ, q0, F) where −

 Q is a finite set of states.

 ∑ is a finite set of symbols called the alphabets.

 δ is the transition function where δ: Q × ∑ → 2Q

(Here the power set of Q (2Q) has been taken because in case of NDFA, from a

15
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

state, transition can occur to any combination of Q states)

 q0 is the initial state from where any input is processed (q0 ∈ Q).

 F is a set of final state/states of Q (F ⊆ Q).

Graphical Representation of an NDFA: (same as DFA)

An NDFA is represented by digraphs called state diagram.

 The vertices represent the states.

 The arcs labeled with an input alphabet show the transitions.

 The initial state is denoted by an empty single incoming arc.

 The final state is indicated by double circles.

Example

Let a non-deterministic finite automaton be →

 Q = {a, b, c}

 ∑ = {0, 1}

 q0 = {a}

 F = {c}

The transition function δ as shown below −

Present State Next State for Input 0 Next State for Input 1

A a, b b

16
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

B c a, c

C b, c c

Its graphical representation would be as follows −

DFA vs NDFA

The following table lists the differences between DFA and NDFA.

DFA NDFA

The transition from a state is to a single The transition from a state can be to
particular next state for each input symbol. multiple next states for each input
Hence it is called deterministic. symbol. Hence it is called non-
deterministic.

Empty string transitions are not seen in NDFA permits empty string transitions.
DFA.

17
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

Acceptability by DFA and NDFA

A string is accepted by a DFA/NDFA iff the DFA/NDFA starting at the initial state ends
in an accepting state (any of the final states) after reading the string wholly.

A string S is accepted by a DFA/NDFA (Q, ∑, δ, q0, F), iff

δ*(q0, S) ∈ F

The language L accepted by DFA/NDFA is

{S | S ∈ ∑* and δ*(q0, S) ∈ F}

A string S′ is not accepted by a DFA/NDFA (Q, ∑, δ, q0, F), iff

δ*(q0, S′) ∉ F

The language L′ not accepted by DFA/NDFA (Complement of accepted language L) is

{S | S ∈ ∑* and δ*(q0, S) ∉ F}

Example

Let us consider the DFA shown in Figure 1.3. From the DFA, the acceptable strings can
be derived.

18
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

Strings accepted by the above DFA: {0, 00, 11, 010, 101, ...........}

Strings not accepted by the above DFA: {1, 011, 111, ........}

Practice problems on finite automata

Que-1: Draw a deterministic and non-deterministic finite automate which accept 00 and
11 at the end of a string containing 0, 1 in it, e.g., 01010100 but not 000111010.
Explanation: Design a DFA and NFA of a same string if input value reaches the final
state then it is acceptable otherwise it is not acceptable.
NFA of the given string is as follows:

19
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

Here, q0 shows the initial state, q1 and q2 are the transition states, and q3 and q4 are
the final states.
Note – NFA and DFA both have same power that means if NFA can recognized an
language L then DFA can also be defined to do so and if DFA can recognized an
language L then NFA can also be defined to do so.

20
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

Que-2: Draw a deterministic and non-deterministic finite automata which accept a string
containing “the” anywhere in a string of {a-z}, e.g., “there” but not “those”.
Explanation – Design a DFA and NFA of a same string if input value reaches the final
state then it is acceptable otherwise it is not acceptable. It is applicable for all the DFA
and NFA. Since, NFA is quit easier then DFA, so first make its NFA and then go through
the DFA.
NFA of the given string is as follows:

Deterministic Finite Automata examples:

Example 1:

Design a FA with ∑ = {0, 1} accepts those string which starts with 1 and ends with 0.

Solution:

The FA will have a start state q0 from which only the edge with input 1 will go to the
next stat

In state q1, if we read 1, we will be in state q1, but if we read 0 at state q1, we will reach
to state q2 which is the final state. In state q2, if we read either 0 or 1, we will go to q2
state or q1 state respectively. Note that if the input ends with 0, it will be in the final
state.

21
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

Example 2:

Design a FA with ∑ = {0, 1} accepts the only input 101.

Solution:

In the given solution, we can see that only input 101 will be accepted. Hence, for input
101, there is no other path shown for other input.

Example 3:

Design FA with ∑ = {0, 1} accepts even number of 0's and even number of 1's.

Solution:

This FA will consider four different stages for input 0 and input 1. The stages could be:

22
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

Here q0 is a start state and the final state also. Note carefully that a symmetry of 0's
and 1's is maintained. We can associate meanings to each state as:

q0: state of even number of 0's and even number of 1's.


q1: state of odd number of 0's and even number of 1's.
q2: state of odd number of 0's and odd number of 1's.
q3: state of even number of 0's and odd number of 1's.

23
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

Example 4:

Design FA with ∑ = {0, 1} accepts the set of all strings with three consecutive 0's.

Solution:

The strings that will be generated for this particular languages are 000, 0001, 1000,
10001, .... in which 0 always appears in a clump of 3. The transition graph is as follows:

Example 5:

Design a DFA L(M) = {w | w ε {0, 1}*} and W is a string that does not contain consecutive
1's.

Solution:

When three consecutive 1's occur the DFA will be:

Here two consecutive 1's or single 1 is acceptable, hence

24
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

The stages q0, q1, q2 are the final states. The DFA will generate the strings that do not
contain consecutive 1's like 10, 110, 101,..... etc.

Example 6:

Design a FA with ∑ = {0, 1} accepts the strings with an even number of 0's followed by
single 1.

Solution:

The DFA can be shown by a transition diagram as:

Non-Deterministic Finite Automata examples:

Example 1:
1. Q = {q0, q1, q2}
2. ∑ = {0, 1}
3. q0 = {q0}
4. F = {q2}

25
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

Solution:

Transition diagram:

Transition Table:

Present State Next state for Input 0 Next State of Input 1

→q0 q0, q1 q1

q1 q2 q0

*q2 q2 q1, q2

In the above diagram, we can see that when the current state is q0, on input 0, the next
state will be q0 or q1, and on 1 input the next state will be q1. When the current state is
q1, on input 0 the next state will be q2 and on 1 input, the next state will be q0. When the
current state is q2, on 0 input the next state is q2, and on 1 input the next state will be
q1 or q2.

26
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

Example 2:

NFA with ∑ = {0, 1} accepts all strings with 01.

Solution:

Transition Table:

Present State Next state for Next State of Input 1


Input 0

→q0 q1 ε

q1 ε q2

*q2 q2 q2

Example 3:

NFA with ∑ = {0, 1} and accept all string of length atleast 2.

Solution:

27
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

Transition Table:

Present State Next state for Input 0 Next State of Input 1

→q0 q1 q1

q1 q2 q2

*q2 ε ε

28
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

UNIT 3: REGULAR SET

Regular Expression:
Finite automata are used to recognize patterns of strings, regular
expressions are used to generate patterns of strings. A regular expression
is an algebraic formula whose value is a pattern consisting of a set of
strings, called the language of the expression.

Operands in a regular expression can be:

 characters from the alphabet over which the regular expression is


defined.
 variables whose values are any pattern defined by a regular expression.
 epsilon which denotes the empty string containing no characters.
 null which denotes the empty set of strings.

Operators used in regular expressions include:

29
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

 Union: If R1 and R2 are regular expressions, then R1 | R2 (also written


as R1 U R2 or R1 + R2) is also a regular expression.

L(R1|R2) = L(R1) U L(R2).

 Concatenation: If R1 and R2 are regular expressions, then R1R2 (also


written as R1.R2) is also a regular expression.

L(R1R2) = L(R1) concatenated with L(R2).

 Kleene closure: If R1 is a regular expression, then R1* (the Kleene


closure of R1) is also a regular expression.

L(R1*) = epsilon U L(R1) U L(R1R1) U L(R1R1R1) U ...

Closure has the highest precedence, followed by concatenation, followed by


union.

Examples

The set of strings over {0,1} that end in 3 consecutive 1's.

(0 | 1)* 111

The set of strings over {0,1} that have at least one 1.

0* 1 (0 | 1)*

The set of strings over {0,1} that have at most one 1.

0* | 0* 1 0*

30
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

The set of strings over {A..Z,a..z} that contain the word "main".

Let <letter> = A | B | ... | Z | a | b | ... | z

<letter>* main <letter>*

The set of strings over {A..Z,a..z} that contain 3 x's.


<letter>* x <letter>* x <letter>* x <letter>*

Or

A Regular Expression can be recursively defined as follows −


 ε is a Regular Expression indicates the language containing an empty string. (L
(ε) = {ε})
 φ is a Regular Expression denoting an empty language. (L (φ) = { })
 x is a Regular Expression where L = {x}
 If X is a Regular Expression denoting the language L(X) and Y is a Regular
Expression denoting the language L(Y), then
o X + Y is a Regular Expression corresponding to the language L(X) ∪
L(Y) where L(X+Y) = L(X) ∪ L(Y).
o X . Y is a Regular Expression corresponding to the language L(X) .
L(Y) where L(X.Y) = L(X) . L(Y)
o R* is a Regular Expression corresponding to the
language L(R*)where L(R*) = (L(R))*
 If we apply any of the rules several times from 1 to 5, they are Regular
Expressions.

Some RE Examples

Regular Regular Set


Expressions

31
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

(0 + 10*) L = { 0, 1, 10, 100, 1000, 10000, … }

(0*10*) L = {1, 01, 10, 010, 0010, …}

(0 + ε)(1 + ε) L = {ε, 0, 1, 01}

(a+b)* Set of strings of a’s and b’s of any length including the null string.
So L = { ε, a, b, aa , ab , bb , ba, aaa…….}

(a+b)*abb Set of strings of a’s and b’s ending with the string abb. So L =
{abb, aabb, babb, aaabb, ababb, …………..}

(11)* Set consisting of even number of 1’s including empty string, So L=


{ε, 11, 1111, 111111, ……….}

(aa)*(bb)*b Set of strings consisting of even number of a’s followed by odd


number of b’s , so L = {b, aab, aabbb, aabbbbb, aaaab, aaaabbb,
…………..}

(aa + ab + ba + String of a’s and b’s of even length can be obtained by


bb)* concatenating any combination of the strings aa, ab, ba and bb
including null, so L = {aa, ab, ba, bb, aaab, aaba, …………..}

Any set that represents the value of the Regular Expression is called a Regular Set.

Properties of Regular Sets


Property 1. The union of two regular set is regular.
Proof −
Let us take two regular expressions
RE1 = a(aa)* and RE2 = (aa)*

32
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

So, L1 = {a, aaa, aaaaa,.....} (Strings of odd length excluding Null)


and L2 ={ ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)
L1 ∪ L2 = { ε, a, aa, aaa, aaaa, aaaaa, aaaaaa,.......}
(Strings of all possible lengths including Null)
RE (L1 ∪ L2) = a* (which is a regular expression itself)
Hence, proved.
Property 2. The intersection of two regular set is regular.
Proof −
Let us take two regular expressions
RE1 = a(a*) and RE2 = (aa)*
So, L1 = { a,aa, aaa, aaaa, ....} (Strings of all possible lengths excluding Null)
L2 = { ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)
L1 ∩ L2 = { aa, aaaa, aaaaaa,.......} (Strings of even length excluding Null)
RE (L1 ∩ L2) = aa(aa)* which is a regular expression itself.
Hence, proved.
Property 3. The complement of a regular set is regular.
Proof −
Let us take a regular expression −
RE = (aa)*
So, L = {ε, aa, aaaa, aaaaaa, .......} (Strings of even length including Null)
Complement of L is all the strings that is not in L.
So, L’ = {a, aaa, aaaaa, .....} (Strings of odd length excluding Null)
RE (L’) = a(aa)* which is a regular expression itself.
Hence, proved.
Property 4. The difference of two regular set is regular.
Proof −
Let us take two regular expressions −
RE1 = a (a*) and RE2 = (aa)*
So, L1 = {a, aa, aaa, aaaa, ....} (Strings of all possible lengths excluding Null)

33
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

L2 = { ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)


L1 – L2 = {a, aaa, aaaaa, aaaaaaa, ....}
(Strings of all odd lengths excluding Null)
RE (L1 – L2) = a (aa)* which is a regular expression.
Hence, proved.
Property 5. The reversal of a regular set is regular.
Proof −
We have to prove LR is also regular if L is a regular set.
Let, L = {01, 10, 11, 10}
RE (L) = 01 + 10 + 11 + 10
LR = {10, 01, 11, 01}
RE (LR) = 01 + 10 + 11 + 10 which is regular
Hence, proved.
Property 6. The closure of a regular set is regular.
Proof −
If L = {a, aaa, aaaaa, .......} (Strings of odd length excluding Null)
i.e., RE (L) = a (aa)*
L* = {a, aa, aaa, aaaa , aaaaa,……………} (Strings of all lengths excluding Null)
RE (L*) = a (a)*
Hence, proved.
Property 7. The concatenation of two regular sets is regular.
Proof −
Let RE1 = (0+1)*0 and RE2 = 01(0+1)*
Here, L1 = {0, 00, 10, 000, 010, ......} (Set of strings ending in 0)
and L2 = {01, 010,011,.....} (Set of strings beginning with 01)
Then, L1 L2 = {001,0010,0011,0001,00010,00011,1001,10010,.............}
Set of strings containing 001 as a substring which can be represented by an RE − (0 +
1)*001(0 + 1)*
Hence, proved.

34
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

Unit 4:

Push Down Automata

The formal notation for a pushdown automata (PDA) involves seven tuples. We write the
specification of a PDA ‘M’ as follows;

M = (Q, ∑, Γ, δ, q0 , z0, F)

These tuples have the following meaning;

1. Q: A finite set of states, like the states of a finite automation.


2. ∑: A finite set of input alphabet/ symbols, also analogous to the corresponding
component of a finite automaton.

1. Γ: A finite stack alphabet. This touple, which has no finite-automaton analog, it


the set of symbols that we are allowed to push onto the stack.
2. δ: The transaction function. As for a finite automation, δ governs the behavior of

35
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

the automaton. Formally, δ takes as argument a triple δ(q, a, X), where;


1. q is a state in Q.
2. a is either an input symbol in ∑ or a = ε, the empty string, which is
assumed not to be an input symbol.
3. X is a stack symbol, that is a member of Γ.

Basically the complete transaction function of pushdown automata is as;

δ: (Q x (Σ ∪{ε}) x X) → (p, γ)

Here the output of δ is a finite set of pair (p, γ), where p is the new state, and γ is the string
of stack symbols that replaces X at the top of the stack. For instance, if γ = ε, then the stack
is popped, if γ = X, then the stack is unchanged, and if γ = YZ, then X is replaced by Z and Y
is pushed onto the stack.

5. q0: The initial state. The PDA is in this state before making any transitions.
6. z0: The stack start symbol. Initially, the PDA’s stack consists of one instance of
this symbol, and nothing else.
7. F: The set of final states or accepting states.

A pushdown automaton is like a finite state automaton that has a pushdown stack as
additional memory. Thus, it is a finite state device equipped with a one-way input tape
and a ‘last-in first-out’ external memory that can hold unbounded amounts of
information; each individual stack element contains finite information.

"Finite state machine" + "a stack"

A pushdown automaton has three components −

 an input tape,

36
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

 a control unit, and

 a stack with infinite size.

The stack head scans the top symbol of the stack.

A stack does two operations −

 Push − a new symbol is added at the top.

 Pop − the top symbol is read and removed.

A PDA may or may not read an input symbol, but it has to read the top of the stack in
every transition.

or

A PDA can be formally described as a 7-tuple (Q, ∑, S, δ, q0, I, F) −


 Q is the finite number of states
 ∑ is input alphabet
 S is stack symbols
 δ is the transition function: Q × (∑ ∪ {ε}) × S × Q × S*
 q0 is the initial state (q0 ∈ Q)

37
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

 I is the initial stack top symbol (I ∈ S)


 F is a set of accepting states (F ∈ Q)
The following diagram shows a transition in a PDA from a state q1 to state q2, labeled
as a,b → c −

This means at state q1, if we encounter an input string ‘a’ and top symbol of the stack
is ‘b’, then we pop ‘b’, push ‘c’ on top of the stack and move to state q2.

Terminologies Related to PDA

Instantaneous Description
The instantaneous description (ID) of a PDA is represented by a triplet (q, w, s) where
 q is the state
 w is unconsumed input
 s is the stack contents

Turnstile Notation
The "turnstile" notation is used for connecting pairs of ID's that represent one or many
moves of a PDA. The process of transition is denoted by the turnstile symbol "⊢".

Consider a PDA (Q, ∑, S, δ, q0, I, F). A transition can be mathematically represented by


the following turnstile notation −

(p, aw, Tβ) ⊢ (q, w, αb)

This implies that while taking a transition from state p to state q, the input symbol ‘a’ is

38
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

consumed, and the top of the stack ‘T’ is replaced by a new string ‘α’.

39
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

Figure: A pushdown automata is essentially a finite automaton with a stack data


structure

The informal way of pushdown automata representation is as represented in the above


figure. Which consist a “Finite-state control” reads inputs, one symbol at a time. The
pushdown automata are allowed to observe the symbol at the top of the stack and to
base its transaction on its current state, the input symbol, and the symbol at the top of
the stack. Alternatively, it may make a “spontaneous” transition, using ε as its input
instead of an input symbol. In one transition, the pushdown automata:

 Consumes from the input the symbol that it uses in the transition. If ε is used
for the input, then no input symbol is consumed.
 Goes to a new state, which may or may not be the same as the previous state.

Replace the symbol at the top of the stack by any string. The string could be ε, which
corresponds to a pop of the stack. It could be the same symbol that appeared at the top
of the stack previously; i.e. no change to the stack is made. If could also replace the top
stack symbol does not push or pop it. Finally, the top stack symbol could be replaced by
two or more symbols, which has the effect of (possibly) changing the top stack symbol,
and then pushing one or more symbol onto the stack.

Unit 5: TURING MACHINE


A Turing machine is an idealised model of a central processing unit (CPU) that controls
all data manipulation done by a computer, with the canonical machine using sequential
memory to store data. Typically, the sequential memory is represented as a tape of
infinite length on which the machine can perform read and write operations.

In the context of formal language theory, a Turing machine (automaton) is capable


of enumerating some arbitrary subset of valid strings of an alphabet. A set of strings
which can be enumerated in this manner is called a recursively enumerable language.

40
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

The Turing machine can equivalently be defined as a model that recognises valid input
strings, rather than enumerating output strings.

A Turing machine is a finite automaton that can read, write, and erase symbols
on an infinitely long tape. The tape is divided into squares, and each square
contains a symbol. The Turing machine can only read one symbol at a time, and it
uses a set of rules (the transition function) to determine its next action based on
the current state and the symbol it is reading.

41
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

For example:

Applications of different Automata:


1. Finite Automata ' A finite automaton is a mathematical model consisting of
states, input symbols, and a transition function. It reads input symbols and
transitions between states based on the transition function, either accepting or
rejecting the input. It's used to model and analyze systems with a finite number
of states in various fields. Some applications of finite automata are as follows:
1. Recognizing patterns in text or other data: Finite automata can be used to
search for and recognize specific patterns in text or other data. For
example, a finite automaton can be used to recognize email addresses,
URLs, or phone numbers in text. This is commonly used in text processing
software, such as search engines or spam filters.

42
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

2. Implementing regular expressions in programming languages: Regular


expressions are a way to describe patterns in text using a set of rules.
These rules can be translated into a finite automaton, which is used to
match the pattern in the input text. Many programming languages, such as
Perl, Python, and Java, use finite automata to implement regular
expressions.
3. Validating input in form fields on websites: Finite automata can be used
to validate user input in form fields on websites. For example, a finite
automaton can be used to ensure that a user enters a valid email address
or password that meets certain criteria, such as length or character
requirements.
4. Modeling digital circuits and switching systems: Finite automata can be
used to model digital circuits and switching systems, such as those found
in computers and other electronic devices. This can help engineers design
and optimize these systems for maximum efficiency and performance.
5. Designing and analyzing computer algorithms and programs: Finite
automata can be used to design and analyze computer algorithms and
programs. They can help programmers understand the behavior of the
program and identify potential errors or inefficiencies.
6. Building compilers and interpreters: Finite automata are used in the
development of compilers and interpreters, which are programs that
translate high-level programming languages into machine code that can
be executed by a computer. Finite automata are used to recognize and
process the syntax and semantics of the programming language.
7. Analyzing the behavior of software and hardware systems: Finite
automata can be used to analyze the behavior of software and hardware
systems, such as operating systems or network protocols. They can help
identify bugs or vulnerabilities in the system and optimize its performance.

43
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

8. Verifying the correctness of software and hardware systems: Finite


automata can be used to verify the correctness of software and hardware
systems by checking whether they satisfy certain requirements or
specifications.
9. Developing language models and machine learning algorithms: Finite
automata are used in the development of language models and machine
learning algorithms, which are used in natural language processing and
other applications. They can help identify patterns in data and make
predictions based on those patterns.
10. Studying formal languages and their properties: Finite automata are used
in the study of formal languages and their properties. They can help
identify the structure and rules of different types of languages and the
limitations of those languages in terms of expressiveness and complexity.
2. Push Down Automata - A pushdown automaton (PDA) is a mathematical model
similar to a finite automaton, but with a stack for memory. It has states, input
symbols, a transition function that maps states, input symbols, and top stack
symbols to other states and stack operations, and a set of accepting states. The
PDA reads input symbols and manipulates the stack, either accepting or rejecting
the input. PDAs are used to model more complex systems in computer science
and linguistics. Some applications of push down automata are as follows:

1. Parsing context-free grammars: Pushdown automata are often used to


parse context-free grammars. Context-free grammars are a type of formal
grammar that is commonly used to describe the structure of programming
languages, markup languages, and other languages. Pushdown automata
can recognize whether a string of symbols in a language can be generated
by a given context-free grammar.
2. Implementing stack-based memory management: Pushdown automata
can be used to implement stack-based memory management in

44
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

programming languages. In this type of memory management, memory is


allocated and deallocated from a stack data structure. Pushdown
automata can be used to implement the stack and manage the allocation
and deallocation of memory.
3. Modeling natural language processing: Pushdown automata can be used
to model natural language processing (NLP) systems. NLP is a subfield of
artificial intelligence that focuses on the interaction between computers
and human languages. Pushdown automata can be used to recognize and
parse sentences in natural language, which can be used to build more
advanced NLP systems.
4. Recognizing context-sensitive languages: Pushdown automata can be
used to recognize context-sensitive languages, which are a type of formal
language that is more complex than context-free languages. Context-
sensitive languages are used in many applications, such as natural
language processing and database management.
5. Modeling network protocols: Pushdown automata can be used to model
network protocols, such as the Transmission Control Protocol (TCP) and
the User Datagram Protocol (UDP). These protocols are used to facilitate
communication between computers over a network. Pushdown automata
can be used to recognize and process the messages exchanged between
computers.

Overall, pushdown automata are useful in a wide range of applications where


more complex language or memory management is required, such as
programming languages, natural language processing, network protocols, and
database management. They provide a more powerful computational model than
finite automata, allowing for more complex computations and recognition of
more complex languages.

45
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

3. Turing Machine - A Turing machine is a mathematical model of a general-


purpose computer that can perform any computation that any other computer
can. Here are some applications of Turing machines:

1. Artificial intelligence: Turing machines can be used in artificial intelligence


applications, such as natural language processing and machine learning.
Turing machines can be used to process and analyze large amounts of
data, recognize patterns, and learn from examples.
2. Cryptography: Turing machines can be used in cryptography, which is the
study of techniques for secure communication. Turing machines can be
used to encrypt and decrypt messages, and to generate random numbers.
3. Algorithmic complexity theory: Turing machines are used in algorithmic
complexity theory, which is the study of the computational resources
required to solve problems. Turing machines can be used to analyze the
time and space complexity of algorithms, and to determine whether a
problem is solvable by a computer.
4. Compiler design: Turing machines can be used in compiler design, which
is the process of transforming source code into executable code. Turing
machines can be used to implement the parsing and translation phases of
the compiler, which involves recognizing the syntax of the source code
and generating the corresponding executable code.
5. Computation theory: Turing machines are used in computation theory,
which is the study of the nature and limits of computation. Turing
machines can be used to define the class of computable functions, which
are functions that can be computed by a Turing machine.

Overall, Turing machines are a fundamental tool in computer science and are
used in a wide range of applications, including artificial intelligence,

46
D’ENSEIGNEMENT SUPERIEUR DE RUHENGERI 2022-
BP:155 Ruhengeri-Rwanda, Tel:+250788903030, +250788903032;
website:www.ines.ac.rw; Email: infoi@ines.ac.rw; 2023

cryptography, algorithmic complexity theory, compiler design, and


computation theory. They provide a powerful computational model that can
simulate any other computer, allowing for the analysis and design of complex
systems.

===============All the Best==================================

47

You might also like