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

Module 1 - PART 2

The document defines key concepts related to formal languages and finite automata. It begins by defining what an alphabet, string, and language are. It then discusses binary relations on strings like prefix, suffix, and substring. It introduces the concept of powers of an alphabet and Kleene closure. The document then provides a formal definition of a finite automaton (FA) as a 5-tuple machine and discusses languages of FAs. It describes the two types of FAs - non-deterministic finite automaton (NFA) and deterministic finite automaton (DFA). It explains string processing in FAs using transition diagrams, tables, and functions. Examples of string processing in NFAs and DFAs are also given.

Uploaded by

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

Module 1 - PART 2

The document defines key concepts related to formal languages and finite automata. It begins by defining what an alphabet, string, and language are. It then discusses binary relations on strings like prefix, suffix, and substring. It introduces the concept of powers of an alphabet and Kleene closure. The document then provides a formal definition of a finite automaton (FA) as a 5-tuple machine and discusses languages of FAs. It describes the two types of FAs - non-deterministic finite automaton (NFA) and deterministic finite automaton (DFA). It explains string processing in FAs using transition diagrams, tables, and functions. Examples of string processing in NFAs and DFAs are also given.

Uploaded by

Hema Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 63

Introduction

What is language: A language is a collection of sentences of finite


length and are constructed from a finite alphabet of symbols
1. What is alphabet

An alphabet is a finite, non-empty set of symbols


Notation: We use the symbol ∑ (sigma) to denote an alphabet
• Examples:
• Binary: Binary alphabet {0,1}, ∑ = {0,1}
• All lower case letters: ∑ = {a,b,c,..z}
• Alphanumeric: ∑ = {a-z, A-Z, 0-9}
• DNA molecule letters: ∑ = {a,c,g,t}
•…
Example − ∑ = {a, b, c, d} is an alphabet set where ‘a’, ‘b’, ‘c’,
and ‘d’ are symbols.
2. What is String
A string or word is a finite sequence of symbols chosen from ∑
Notation: (a) Letters u, v, w, x, y, and z denote strings.
(b) Convention: concatenate the symbols. No parentheses or commas used.

Examples: 0000 is a string over the binary alphabet.


a!? is a string over the English alphabet.

Example − ‘cabcad’ is a valid string on the alphabet set ∑ = {a, b, c, d}

• Empty string is  (or “epsilon”)

• Length of a string w, denoted by “|w|”, is equal to the number of (non- ) characters in


the string
• E.g., x = 010100 |x| = 6
• x = 01  0  1  00  |x| = ?

• xy = concatenation of two strings x and y


Definition:

Examples:
1. Let L be the language of all strings consisting of n 0’s followed by n 1’s:
L = {, 01, 0011, 000111,…}
2. Let L be the language of all strings of with equal number of 0’s and 1’s:
L = {, 01, 10, 0011, 1100, 0101, 1010, 1001,…}
Binary relations on strings
1. Prefix - u is a prefix of v if there is a w such that v = uw.
– Examples:
•  is a prefix of 0 since 0 = 0
• apple is a prefix of appleton since appleton = apple.ton
2. Suffix - u is a suffix of v if there is a w such
that v = wu.
– Examples:
•  is a suffix of 0 since 0 = 0

• ton is a suffix of appleton since appleton = apple.ton


Binary relations (contd.)

3. Substring - u is a substring of v if there are x


and y such that v = xuy.
– Examples:
• let is a substring of appleton since appleton = app.let.on
• 0 is a substring of 0 since 0 = epsilon.0.epsilon

Observe that prefix and suffix are special cases


of substring.
Powers of an alphabet

Language over alphabet  - a set of strings over .


– Notation: L.
– Examples:
• {0, 00, 000, ...} is an "infinite" language over the
binary alphabet.
• {a, b, c} is a "finite" language over the English
alphabet.

Let ∑ be an alphabet.
– ∑k = the set of all strings of length k
– ∑* = ∑0 U ∑1 U ∑2 U … kleene closure
– ∑+ = ∑1 U ∑2 U ∑3 U … positive closure

6
Finite Automata

• Formal Definition ,
• Languages of FA,
• Two types (NFA,DFA) ,
• String Processing
Finite Automata
Finite Automata is an abstract model of a computer.

Components: Input Tape, Finite Control and Tape Head .

FINITE TAPE HEAD


STATE
CONTROL INPUT TAPE
0 1 0 1 1
Input: String
Operation: String Processing (scans the string from left to right,
one symbol at a time and moves from state to state ) using its
transition function.
Output: Yes/No (Accepted/Rejected)
Formal Definition of FA
A Finite Automaton (FA) is a 5-tuple machine
M = (Q, Σ, , q0, F) where

Q is the set of states

Σ is the alphabet
 : Q  Σ → (2Q or Q) is the transition function
q0 is in Q is the start state or initial state

F  Q is the set of accept states

2Q is the set of subsets of Q


Transition function
• It is a function which guides the automata in
string processing. It takes two inputs (a state ,
a symbol) and gives one output (state).

There are three representations:


• Diagrammatic representation
• Tabular representation
• Functional representation
Diagrammatic representation: Transition diagram

• Nodes and edges are used.


• Nodes represent the states and edges represent the moves.
• The labels of the edges represent the processing symbols.
• There are two types of nodes:
a) single circled node indicating non-final (non-accepting) state
b) double circled node indicating final state.

b a
a,b
a b
q0 q1 q2
Tabular representation:
Transition table
• It consists of Rows and columns.
• Rows indicate state and columns indicate symbol.
• The entries of the table indicate the output state.
• The arrow and star symbols are used to point out the starting
and final states respectively.

a
b a,b
δ a b
→q0 q1 q0 a b
q0 q1 q2
q1 q1 q2
* q2 q2 q2
Functional Representation - Transition functions
•The name of the function is δ.
• The input parameters are q,a where q is a state and a is a symbol. The function returns a state p.

δ(q0 , a) = q1

δ(q0 , b) = q0
a
b a,b
δ(q1 , a) = q1
a b
δ(q1 , b) = q2 q0 q1 q2

δ(q2 , a) = q2

δ(q2 , b) = q2
Language of an Automata

L(M) = The language of machine M


= Set of all strings machine M accepts
= {w | is in F} where is an extended
transition function that takes a state q0 and a string
w and returns a state p which is in F.
= Regular language.

b a
a,b
a b
q0 q1 q2

L(M) ={ ab, bab, aab, aaab, abab, baab,


bbab ,abaa,abbb ...... }
= strings that consist of substring ‘ab’
Two types of Finite Automata
Deterministic: if there is exactly one output state in every
transition function of an automata, then that automata is
called Deterministic finite Automata(DFA)
:QΣ→ Q

Non-Deterministic: if there is zero or more output states


in any of the transition functions of an automata then that
automata is called Non-Deterministic Finite Automata
(NFA).  : Q  Σ → 2Q
NFA is the preliminary form of a machine, which can be
easily constructed using the basic constraints of a
language. Then it can be converted into DFA using subset
construction method and finally minimization methods
are used to reduce the size of the machine.
Extended Transition Function
• Given an DFA M, we define the extended
transition function δ^:Q×Σ∗→Q inductively by
δ^(q,ε)=q, and δ^(q,xa)=δ(δ^(q,x),a).
String Processing in DFA
Let M = (Q, Σ, δ, q0, F) where Q={ q0 , q1 ,q2 }, Σ,={a,b} F={q2 }
δ a b
→q0 q1 q0 Show that the string w = bbabb is
q1 q1 q2 accepted by the given FA, M.
* q2 q2 q2

= q2 is in F.
b
q0 q0 b q0 a q1 b q2 b Q2 is
There is a path from starting state to final state. Therefore thein F
given string is accepted
String Processing in DFA

11
q1 1
0,1
0 1
0111 111
q0 q12
0 0

1
Read string left to right q3
The machine accepts a string if the process ends
in a double circle
String Processing in NFA
 0 1 2
Test whether the
→*q0 {q0, q1, q2 } {q1, q2 } {q2 } strings 01122, 1221
*q1 Ф {q1, q2 } {q2 } are accepted by M.
*q2 Ф Ф
{q2 }

= q2 F
0 q0 1 q1 1 q1 2 q2 2
q0 q2F
0 q1 1 q2 1 q2
0
q2
There is at least one path from starting state to final state. Therefore
the given string is accepted.
String Processing in NFA
 0 1 2
Test whether the
→*q0 {q0, q1, q2 } {q1, q2 } {q2 } strings 1221 is
*q1 Ф {q1, q2 } {q2 } accepted by M.
*q2 Ф Ф
{q2 }

q0 1 q 2 q 2 q 1
1 2 2
1
q2 2 q 2 q 1
2 2

There is no even a single path from starting state to final state.


Therefore the given string is rejected.
Example DFA
DFA that accepts exactly two a's. Σ = {a}
The DFA accepts the only string "aa".
2. DFA that accepts strings with at least one a. Σ = {a}

3. DFA that accepts strings with at least one a. Σ = {a,b}


Problem-1: Construct DFA, which accepts set of all strings over {0, 1} which
interpreted as a binary number is divisible by 2. 

L(M)={0,10,100,110........}
Problem-2: Construct DFA, which accepts set of all strings over {0, 1} which
interpreted as binary number is divisible by 3. 

Problem-3: Construct DFA, which accepts set of all strings over {0, 1} which
interpreted as binary number is divisible by 4. 

Problem-4: Construct DFA, which accepts set of all strings over {0, 1} which
interpreted as binary number is divisible by 5. 
NFA
Non-Deterministic: if there is zero or more output states in any of
the transition functions of an automata then that automata is called
Non-Deterministic Finite Automata (NFA).  : Q  Σ → 2Q .

1. The Finite Automata are called NFA when there exist many
paths for specific input from the current state to the next state.
2. It is easy to construct NFA than DFA for a given regular
Language.
3. Every NFA is not DFA, but each NFA can be translated into
DFA.
4. NFA is defined in the same way as DFA, but with two
Exceptions.
i) It contains multiple next states
ii) It contains Epsilon (ε) transitions
DFA is a special case of the NFA in which for each state there is
unique transition on each input symbol.
Example NFA
Design a NFA for the following languages over
the alphabet {a,b}
• The set of all strings ending in aa.
• The set of all strings with the substring aba.
• The set of all strings beginning with bb.
• The set of all strings with even number of a’s.
• The set of all strings with even number of b’s.
• The set of all strings with odd number of a’s.
• The set of all strings with odd number of b’s.
• The set of all strings whose third symbol from the right end is b
• The set of all strings whose third symbol from the left end is b.
The set of all strings ending in aa.

NFA M=(Q, Σ, δ, q0, {q2})


where Q={ q0, q1, q2 } Σ={a,b}

δ : Transition diagram:
a,b
a a
q0 q1 q2

Transition table

Transition function
The set of all strings with the substring aba

• NFA M=(Q, Σ, δ, q0, {q3})


where Q={ q0, q1, q2, q3 } Σ={a,b}
• δ : Transition diagram:

a,b a,b
q0 a q1 b a
q2 q3
The set of all strings beginning with bb

• NFA M=(Q, Σ, δ, q0, {q2})


where Q={ q0, q1, q2 } Σ={a,b}
• δ : Transition diagram:
a,b
b b
q0 q1 q2
The set of all strings with even number of
a’s.
• NFA M=(Q, Σ, δ, q0, {q0})
where Q={ q0, q1 } Σ={a,b}
• δ : Transition diagram:

b a b
q0 q1
a
The set of all strings with even number of b’s

NFA M=(Q, Σ, δ, q0, {q0})


where Q={ q0, q1 } Σ={a,b}
δ : Transition diagram:

a b a
q0 q1
b
The set of all strings with odd number of a’s

• NFA M=(Q, Σ, δ, q0, {q1})


where Q={ q0, q1 } Σ={a,b}
• δ : Transition diagram:

b b
a
q0 q1
a
The set of all strings whose third symbol
from the right end is b
• NFA M=(Q, Σ, δ, q0, {q3})
• where Q={ q0, q1 q2, q3 } Σ={a,b}
• δ : Transition diagram:

a,b

b a,b a,b
q0 q1 q2 q3
The set of all strings whose third symbol
from the left end is b
• NFA M=(Q, Σ, δ, q0, {q3})
where Q={ q0, q1 , q2, q3 } Σ={a,b}
• δ : Transition diagram:

a,b
a,b a,b b
q0 q1 q2 q3
Automata for
• If the automata is given for a language L , then the automata
for can be easily constructed by changing all the non-
final states to final states and final states to non-final states.
• Given: The FA of L= {w | w consists of 10 as substring}
• M(L) =(Q, Σ, δ, A, {C}) where Q={ A, B ,C } Σ={a,b}
• δ : Transition diagram:
1 0,1
0

A B C
1 0

M( ) = (Q, Σ, δ, A, {A, B}) where Q={ A, B ,C }


Σ={a,b}
δ : Transition diagram: 1 0,1
0
A C
A 1 B 0
Automata for
The intersection of two regular languages can be constructed
by taking Cartesian product of states.
• Let M (L1) = (Q1, Σ, δ1, q1, F1)
M (L2) = (Q2, Σ, δ2, q2, F2)
• Then M ( ) = (Q2 X Q2, Σ, δ,( q1 ,q2 ), F1 XF2)
Example:
• Let L1 = The set of all strings with even number of b’s.
• NFA M(L1 ) = (Q, Σ, δ1, p, {p}) where Q={p, q }
Σ={a,b}

• Let L2 = The set of all strings with odd number of a’s.


• NFA M(L2 ) = (Q, Σ, δ2, r, {s}) where Q={ r, s }
Σ={a,b}
δ1 : Transition diagram: δ2 : Transition diagram:
a b a b b
a
p q r s
b a
M( ) = ({(p,r), (p,s), (q,r),(q,s)}, {a,b}, δ,
(p,r), {(p,s)})
= (p, s), = (q, r)
= (p, r), = (q, s)
= (q, s), = (p, r)
= (q, r), = (p, s)

a
(p,r ps
a
b b b b
a
qr qs
Class Tutorial
1. Construct the FA for the language {0n| n mod 3 =2,
n≥0}
2. Construct the FA for the language {0n| n mod 3 =1,
n≥0}
3. Construct the FA for the language {0n| n mod 3 =0,
n≥0}
4. Construct the FA for the language {0n| n mod 2 =1,
n≥0}
5. Construct the FA for the language {0n| n mod 2 =0,
n≥0}
Class Tutorial
Design a NFA for the following languages over the alphabet {0,1}
i. The set of all strings ending in 01.
ii. The set of all strings with the substring 11.
iii. The set of all strings beginning with 010.
iv. The set of all strings with even number of 1’s.
v. The set of all strings with odd number of 0’s.
vi. The set of all strings whose second symbol from the right end is
0
vii. The set of all strings with even number of 0’s and odd number of
1’s
viii. The set of all strings with even number of zero’s and even
number of 1’s
ix. The set of all strings with odd number of zero’s and odd number
of 1’s
x. The set of all strings with odd number of zero’s and even
number of 1’s
Converting NFA to DFA
(Construct DFA from NFA):

• Subset construction method :It involves


constructing all subsets of states of the NFA.

‘Lazy Evaluation’-
• In this method the transition functions are
generated only for reachable states.
Subset construction method(Lazy)
INPUT : NFA
OUTPUT : DFA
Step 1 :
Include the starting state of NFA (q 0) in DFA as starting state of DFA.
Step 2 :
Find the transition for all the symbols from q 0
Step 3 :
If the output state is new state, include it in DFA and find the
transition for all the symbols from that state.
Step 4 : Repeat step3 until there are no more new states
Step 5 :The state which includes final state of NFA is the final state of
DFA.
NFA: Convert NFA to DFA
0,1
δN 0 1
0 1
q0 q1 q2 →q0 {q0, q1 } {q0}
q1 - {q2}
Step 1 : Include q0 * q2 - -
Step 2 : Find transitions for 0,1 from q0

New state

Step 3 : Find transitions for 0,1 from newstate

New state
Step 4 : Repeat step 3 for newstate

Step 5 :Final state is { q0,q2}

DFA 1 0

δD 0 1
0 1
→q0 {q0, q1 } {q0}
{q0, q1 }
q0 {q0, q2 }
{q0, q1 } {q0, q1 } { q0,q2}

1 0
* { q0,q2} {q0, q1 } {q0}
Convert NFA to DFA
1
N a b 3 N 0 1
→q0 { q0 ,q1 } { q0} →p { q ,s } { q}
q1 q2 q1 q {r} {q,r}
r {s} {p}
q2 q3 q3
*s {p}
* q3 - q2
2
N 0 1 4
N 0 1
→p { p ,q } { p} →p { p,q } { p}
q {r} {r} q {r,s} {t}
r {s} r {p,r} {t}
*s {s} {s} *s
*t
ε-NFA or NFA with ε- Transitions.
• A Non-Deterministic finite automaton with ε-
Transitions (NFA) is represented by 5-tuples.
i.e. M = (Q, Σ, δ, q0, F) :
• Q is a finite non-empty set of states
• Σ is a finite non-empty set of symbols ( an alphabet)
• δ : QX Σ{ε} → 2Q (subset of Q) is the transition
function
• q0  Q is the start state
• F Q is a set of final states
Example ε-NFA- L={0l1m2n|l,m,n>=0}

 ε 0 1 2
→q0 q1 q0 Ф Ф

q1 q2 Ф q1 Ф

*q2 Ф Ф Ф q2

0 1
2
q0 ε q1 ε q2
ε-closure
• Epsilon closure of a state is the set of all states
that are reachable by following the transition
function from the given state through ε edge.
• ε-Closure (q0) = = { q0 , q1, q2}
• ε-Closure (q1) = = { q1, q2}
• ε-Closure (q2 ) = = {q2}
String processing in ε-NFA
Step 1: Compute ε-Closure [states
 ε a b c that can be reached by traveling
along zero or more ε arrows] for
→p {q} {p} Ф Ф
all states .
q {r} Ф {q} Ф
ε-Closure (p) = {p,q,r}
ε-Closure (q) = {q,r}
*r Ф Ф Ф {r} ε-Closure (r ) = {r}
Step2: Start with ε-closure (p)= {p,q,r}, where p is the starting state of given ε –NFA

= r F . Therefore the given string is accepted.


Converting ε-NFA to DFA /
(Construct DFA from ε-NFA)
Step1 :Compute the ε-Closure for each state.
Step 2: Start state of DFA is ε-Closure(q0) where
q0 is the start state of ε-NFA .
Step3:Find the transition function for ε-
Closure(q0)
Step4:If there is any new state, find the transition
function for that new state using the
following formula:

Step5:Continue step 4. until no more new states.


Consider the following ε-NFA
Transition Table Transition Diagram
 ε a b c a ε
a
→p {p} {q} {r} b
Ф p q

c
q {p} {q} {r} c ε
Ф b
r

*r {q} {r} {p}


Ф a

SKCET-CSE FLAT : UNIT - I (PART 3) 51


Compute the ε-Closure of each state

• ε-Closure (p)= = {p}


• ε-Closure (q)={p,q}
• ε-Closure (r )={p,q,r}  ε a b c

→p {p} {q} {r}


Ф

q {p} {q} {r}


Ф

*r {q} {r} {p}


Ф
SKCET-CSE FLAT : UNIT - I (PART 3) 52
Conversion-Direct Method
Start with ε-closure (p)= {p}
where p is the starting state of given ε -NFA

Existing
state

Newstate
SKCET-CSE FLAT : UNIT - I (PART 3) 53
Conversion Contd.

 a b c

→{p} {p} {p,q} {p,q,r}

{p,q}

*{p,q,r}
SKCET-CSE FLAT : UNIT - I (PART 3) 54
Conversion Contd.

Existing
state

Newstate
SKCET-CSE FLAT : UNIT - I (PART 3) 55
Conversion Contd.

 a b c

→{p} {p} {p,q} {p,q,r}

{p,q} {p,q} {p,q,r} {p,q,r}

*{p,q,r}
SKCET-CSE FLAT : UNIT - I (PART 3) 56
Conversion Contd.

Existing
state

Existing
state
SKCET-CSE FLAT : UNIT - I (PART 3) 57
Conversion Contd.

 a b c

→{p} {p} {p,q} {p,q,r}

{p,q} {p,q} {p,q,r} {p,q,r}

*{p,q,r} {p,q,r} {p,q,r} {p,q,r}


SKCET-CSE FLAT : UNIT - I (PART 3) 58
Consider the following ε-NFA.
i. Convert it into DFA
ii. Compute
iii. Give the set of all strings of length 3 or less
accepted by the automaton

 ε a b c
→p {q,r} Ф {q} {r}
q Ф {p} {r} {p,q}
*r Ф Ф Ф Ф
String Processing
 ε +/- . 0-9
→q {q1} {q1 } Ф Ф
0

q1 Ф Ф {q2} {q1, q4}


*q2 Ф Ф Ф {q3}
q3 {q5} Ф Ф {q3}
q4 Ф Ф {q3} Ф

*q5 Ф Ф Ф Ф

SKCET-CSE 60
Minimizing DFA (Partitioning method)
• partition the set of states into two groups:
– G1 : set of accepting states
– G2 : set of non-accepting states

• For each new group G


– partition G into subgroups such that states s1 and s2 are in the same group iff
for all input symbols a, states s1 and s2 have transitions to states in the same
group.

• Start state of the minimized DFA is the group


containing the start state of the original DFA.
• Accepting states of the minimized DFA are the
groups containing the accepting states of the
original DFA.
Minimizing DFA - Example
a

2
G1 = {2}
a
G2 = {1,3}
1 b a
b G2 cannot be partitioned because
3
δ(1,a)=2(G1) δ (1,b)=3 (G2)
b
δ(3,a)=2(G1) δ(3,b)=3 (G2)
So, the minimized DFA (with minimum states)
b
a a
{1,3} {2}

b
Construct a minimized DFA for the given DFA
1 2
δ a b
δ 0 1
-> A B E
-> A B A
B C F B A C
*C D H C D B
D E H *D D A
E F I E D F
*F G B F G E
G H B G F G
H I C H G D
*I A E

You might also like