0% found this document useful (0 votes)
12 views152 pages

Lecture 1 - Chapter 1-Introduction

The document introduces automata theory, focusing on abstract computing devices and the study of formal languages, grammars, and automata. It outlines the historical development of the theory, key concepts such as languages, strings, and grammars, and the Chomsky hierarchy of formal grammars. Additionally, it explains the types of grammars and their corresponding languages and automata.

Uploaded by

atinasianegash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views152 pages

Lecture 1 - Chapter 1-Introduction

The document introduces automata theory, focusing on abstract computing devices and the study of formal languages, grammars, and automata. It outlines the historical development of the theory, key concepts such as languages, strings, and grammars, and the Chomsky hierarchy of formal grammars. Additionally, it explains the types of grammars and their corresponding languages and automata.

Uploaded by

atinasianegash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 152

Chapter 1: Introduction

LANGUAGES, GRAMMARS AND AUTOMATA


Language
Symbol
Alphabet
String
Length of a string,
concatenation of
string, …
Grammars
Automata
Deterministic
Automata
Non-deterministic
WHAT IS AUTOMATA THEORY?

• Study of abstract computing devices, or


“machines”
• Automaton = an abstract computing device
• Note: A “device” need not even be a physical hardware!
• A fundamental question in computer science:
• Find out what different models of machines can do and
cannot do
• The theory of computation
• Computability vs. Complexity
3
(A pioneer of automata theory)

Alan turing (1912-1954)


• Father of modern computer science
• English mathematician
• Studied abstract machines called
turing machines even before
computers existed
• Heard of the turing test?.

4
Theory of computation: A historical
perspective

1930s • Alan turing studies turing machines


• Decidability
• Halting problem
1940-1950s • “Finite automata” machines studied
• Noam chomsky proposes the
“chomsky hierarchy” for formal
languages
1969 Cook introduces “intractable” problems
or “np-hard” problems
1970- Modern computer science: compilers, 5
computational & complexity theory evolve
Formal languages
• Formal language is a set of strings of symbols drawn from a
finite alphabet.
• Formal language can be specified either by a set of rules such
as
 Regular expressions or
 A context-free grammar that generates the language,
Or by a formal machine that accepts (recognizes) the language.
 Programming languages are examples of formal languages.
 Formal languages are defined by two sets of rules:
– Syntax: precise rules that tell you the symbols you
are allowed to use and how to put them together into legal
expressions.
– Semantics: precise rules that tell you the meanings of the
symbols and legal expressions.
Regular Expressions
 A regular expression can also be described as a sequence of
pattern that defines a string. ...
 Regular expressions are used to match character
combinations in strings/ pattern
 Examples
 a/b a or b
 (a/b)* zero or more occurrence of a or b
 (a/b)+ one or more occurrence of a or b

 (10)*
(0 + 10*) L = { 0, 1, 10, 100, 1000, 10000, … }
(0*10*) L = {1, 01, 10, 010, 0010, …}
(a+b)*abb L={abb, aabb, babb, aaabb, ababb,
…………..}
LANGUAGES & GRAMMARS

• Languages: “A language is a
Or “words” collection of sentences of finite
length all constructed from a finite
alphabet of symbols”
• Grammars: “A grammar can be
regarded as a device that
enumerates the sentences of a
language” - nothing more, nothing
less
• N. Chomsky, information and
control, vol 2, 1959
8
The central concepts of automata theory

Alphabet
An alphabet is a finite, non-empty set of symbols
• We use the symbol ∑ (sigma) to denote an alphabet
• Examples:
• Binary: ∑ = {0,1}
• All lower case letters: ∑ = {a,b,c,..z}
• Alphanumeric: ∑ = {a-z, A-Z, 0-9}
• DNA molecule letters: ∑ = {a,c,g,t}
• …
9
Cont….
Strings
A string or word is a finite sequence of symbols
chosen from ∑
• 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 = concatentation of two strings x and y


10
Cont…
Powers of an alphabet
Let ∑ be an alphabet.
• ∑K = the set of all strings of length k
• ∑* = ∑0 u ∑1 u ∑2 u …
• ∑+ = ∑ 1 u ∑2 u ∑3 u …

11
CONT….
LANGUAGES
L is a said to be a language over alphabet ∑, only if L  ∑*
 this is because ∑* is the set of all strings (of all possible
length including 0) over the given alphabet ∑
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,…}
DEFINITION:
• A language, L, is a collection of strings over a given alphabet
with some rules known as grammars. It may or may not be
finite.
• A language may be thought of as a subset of all possible strings
(L z Σ*)

• Example Let Σ = {a, b}


• The set {a, aa, abab, ba} is a finite language over Σ
• The set L = {anbn:n > 0} is an infinite language over
Σ
LANGUAGES
• Before we define a language lets come clear with
some important terms.
• Symbol - an arbitrary datum which has some meaning to
or effect on the machine.
• Letters and digits are examples of frequently
used symbols.
• Alphabet (â): is a finite set of symbols
Example:- Â = {a, b, c, d}, Â = {0, 1}
• String ( or word) – is a finite sequence of
symbols
Example:- W = abc,v = 123, Z = 01010110
LANGUAGES
• Length a string W, denoted by | W | , is the number of symbols
composing the string.
• Example: if W = abbab, then
• |W| = 5
• | W | b = 3 (length of a word w with respect to b )
• Empty string is denoted by ε or l , |l| = 0
• If Â= {a, b}, then ab, abb, a, b, are some of the strings over
Â
• Σ* =All strings of symbols from Σ (Kleene closure)
• Σ+ = Σ* - {ε} ( Positive closure)
• Σi = Set of Strings over Σ having length i.
LANGUAGES
• Example If Σ = {a, b, c} then
• Σ* = { l , a, b,c, aa, ab, bc, abc, , …}
• Σ + = { a, aa, abc, aabc, …}
• Σ 1= Σ
• Σ 3 = {aab, abc, ccc, abb, …}
• If w is a string then w0 = l
• Note that
• Σ* =

• Σ+ =
LANGUAGES

• Given a string w = abc


Prefix of w are l , a, ab,& abc

 Suffix of w are l , c, bc, & abc


 Proper prefix or suffix is a prefix or suffix other
than the string itself
 Proper prefix of w are l , a, & ab
 Proper suffix of w are l , c, & bc
LANGUAGES

• The concatenation of two strings is obtained by appending


the second string to the right of the first string
• Example: -
• Given w = a1a2..an & v = a1a2..am then concatenation of
w & v , denoted as wv, is given as wv = a1a2..ana1a2..am
• a3.a2.= aaabb
• Two strings are equal if they have the same number of
symbols and these symbols match, character for character.
• Concatenation operation is associative, and it
is not commutative
LANGUAGES

• The empty string serves as the identity element


for concatenation.
• That is, for all strings x, x.L =l.X =x
• String reversal – let w = a1a2…an be a string,
then the string reversal of w, reverse (w) or wr,
is
• Anan-1…a1
E.G. If w = abc, then reverse(w) = wr =cba
LANGUAGES
• Example: Let Σ = {0, 1}, then
• L = {x|x is in Σ* and x contains an even number of 0’s}
= {11, 1010, 00, 0000, 1001, etc}
• Language are sets: Hence, the union, intersection, &
differences of two languages are defined.
• Complement of a language L with respect to Σ* is L =
Σ* - L
• The reverse of a language is the set of all string
reversals, LR= {wR:weL}
LANGUAGES
• The concatenation of two languages L1 & L2 is the set of all
strings obtained by concatenating any element of L1 with
any element of L2:
i.e. L1L2 = {xy: x e L1, y e L2 }
• Example: L1 = {a, b} & L2 = {c, de}, then L1L2 = {ac,
ade, bc, bde}
• Lnis L concatenated with itself n times
• L0 = {l }, L1 = L, L2 = L1 L1 , L3 = L1 L1 L …
1

• L* = L0UL1UL2… (Star Closure)


• L+ = L1UL2UL3… (Positive closure)
LANGUAGES
• Example: If L = {anbn:n > 0} then
• L0= { l }
• L1= L = {anbn:n > 0}
• L2 = {anbnambm :n > 0, m > 0}. note that n
and m are unrelated – the string aaabbbaabb is
in L2
• L3 = {anbnambm akbk:n > 0, m > 0, k > 0},
here again n, m and k are unrelated – (that is
they be the same or different)
• LR= {bnan:n > 0}
GRAMMAR

• Standard way of representing the language is called grammar in Automata.


• Grammar contains set of production rules which makes the strings of
language.
• The set of all possible strings which can be derived from grammar is known
as Language of that grammar.

Real Life Example of Grammar


Grammar is just like the same as English grammar. If the sentence is correct
grammatically then that sentence will be the part of grammar otherwise not.
Example

“I am going to school”. It is a valid example of grammar.


I going am to school. It is not a valid example of grammar.
Elements of Grammar
Elements of a grammar depends on depends on types of grammar.
The basic elements of grammar are given below.
A grammar is defined as a quadruple G = (V, T, P, S) Where V, T, P, S
are the elements of Grammar (G).
1. V = Variables (Non-Terminal)
• Finite number of non-empty set Represented by capital
symbols. A, B, C
• Not a part of string which makes after production rule.
2. T = Terminal
• Finite set of Alphabets (Σ), Represented by small letters, i.e.
a,b,c.
All variables are replaced with non-terminals through production
rules.
• Terminals are part of string which makes after production rule.
GRAMMAR

• P = Production Rules
• (Finite set of non-empty rules to makes a string of
Language)
• i.e. P = { S → aSb , S → bSa , S → ∈ }
• 4. S = Start symbol
• Start symbol is used to start the production rule represented
by S.
GRAMMAR
EXAMPLE
Grammar G1: Consider the Grammar 1 (G1) with following
production Rules.

S → aSb
S→∈
Grammar G2: Consider the Grammar 2 (G2) with following
production Rules.

S → aAb / ∈
A → aAb / ∈
GRAMMAR

So, L(G) = {, ab, aabb, aaabbb, aaaabbbb, etc} L(G) =


{anbn : n≥0}
THE CHOMSKY HIERARCHY
• There are four categories of formal grammars in the chomsky
hierarchy,
• type 0 - the most general = difficult to parse but highly
expressive
• type 1
• type 2
• type 3 - the most restrictive
• easy to parse but less expressive
• every type 3 grammar is a type 2 grammar, and every type 2 is a
type 1 and so on.
• each category of grammars have corresponding category of
• languages and automata
THE CHOMSKY HIERACHY
• A containment hierarchy of classes of formal languages

Regular Context-
(DFA) free Context-
Recursively-
(PDA) sensitive
enumerable
(LBA)
(TM)
Types of Grammars
• Type 3 grammars - regular grammars - is a grammar which is
either left-linear or right-linear
• Left linear – all production have the form A -> bx, A -> x where A
& B are non-terminals and x is any string of terminals
• Right linear – all production have the form A -> xb, A -> x where
A & B are non-terminals and x is any string of terminals
• Type 2 grammars - context free grammars
• Productions are of the form X–> v where v is an
arbitrary string of symbols in V, and X is a single non-terminal.
Types of Grammars
• Type 1 grammars - context-sensitive grammars

• Productions are of the form uXw –> uvw where u, v and w are
arbitrary strings of symbols in V, with v non-null, and X a single
non-terminal.
• In other words, X may be replaced by v but only when it is
surrounded by u and w. (i.e. in a particular context).
• Type 0 Grammars - Free or unrestricted grammars
• These are the most general productions which have the form u –> v
where both u and v are arbitrary strings of symbols in V,
with u non-null.
• There are no restrictions on what appears on the left or
right- hand side other than the left-hand side must be non-empty.
Summary
Grammar Language Automata

Regular Grammars - Regular Languages Finite State Automata


Type 3

Context Free Context Free Push down Automata


Grammars - Type 2 Languages
Context-sensitive Context sensitive Bounded
grammars – Type 1 languages Turing
Machine
Free or unrestricted Recursively Turing Machine
grammars– Type 0 Enumerable
languages
FINITE AUTOMATA
LECT 2

33
WHAT IS AUTOMATA THEORY?
• Automata theory is the study of abstract computational devices
• Abstract devices are (simplified) models of real computations
• Computations happen everywhere: on your laptop, on your cell
phone, in nature, …
• An automata is an abstract model of a digital computer (= it is a self
operating machine).
• Every automaton includes some essential features.
• Mechanism for reading input – the input is a string over a given
alphabet written on an input file
• Produce an output in some form
• Have no temporary storage device – the storage cells can be
read and changed.
A SIMPLE COMPUTER
SWITCH

BATTERY

Input: switch
Output: light bulb
Actions (control unit): flip switch
States (storage): on, off
AUTOMATA

• A particular state of the control unit, input file, and


temporary storage is known as configuration.
• A move is the transition of the automaton from one
configuration to the next.
• One automaton differs from the other in the way in which
the output can be produced and the nature of the
temporary storage.
• automaton.
FINITE AUTOMATON (FA)
• Informally, a state diagram that comprehensively captures all
possible states and transitions that a machine can take while
responding to a stream or sequence of input symbols

• Recognizer for “regular languages”

• Finite state automata (finite accepters ) is a mathematical representation of a


system, with discrete input and output.

• A finite automaton is a finite-state machine where transitions are


defined by reading characters in a text string left-to-right.
• Consists of a finite sets of states and a set of transitions from state to
state.
Cont…
• They have no temporary storage.
• Hence, a finite automaton is limited in its capacity to remember things
during the computation, because its input file cannot be rewritten.
• A Finite Automata can be defined through its 5 tuples { Q, Σ, q, F, δ }.
Where
• Q : Finite set of states.
• Σ : set of Input Symbols.
• q : Initial state.
• F : set of Final States.
• δ : Transition Function.

• There are two types of finite accepters


• Deterministic finite state automata (DFA)
• Nondeterministic finite state automata (NFA
WORKING OF FINITE AUTOMATA
• Has a control unit
• The finite automata machine contains five tuple or elements.
• It has some finite states and rules for moving from one state
to another but it depends upon the given input symbol.
• Can be in any one of a finite number of internal states
• The internal state of the control unit is determined by the next-
state or transition function

^
PROCEDURES OF DEFINING A DFA/NFA
• Defining input alphabet and initial state
• Draw the transition diagram
• Check
• Do all states have out-going arcs labeled with all the input symbols
(DFA)
• Any missing final states?
• Any duplicate states?
• Can all strings in the language can be accepted?
• Are any strings not in the language accepted?

• Naming all the states


• Defining (S, , , q0, F)
Deterministic finite automata - definition
• A deterministic finite automaton (DFA) consists of:
• Q ==> a finite set of states
• ∑ ==> A finite set of input symbols (alphabet)
• q0 ==> a start state
• F ==> set of accepting states
• ==> A transition function, which is a mapping
between Q x ∑ ==> Q
• A DFA is defined by the 5-tuple:
• {Q, ∑ , q0,f,  }

41
WHAT DOES A DFA DO ON
READING AN INPUT STRING?
• Input: a word w in ∑*
• Question: is w acceptable by the DFA?
• Steps:
• Start at the “start state” q0
• For every input symbol in the sequence w do
• Compute the next state from the current state, given
the current input symbol in w and the transition
function
• If after all symbols in w are consumed, the current state is
one of the accepting states (F) then accept w;
• Otherwise, reject w. 42
TRANSITION GRAPH
• A labeled directed graph with designed node as a start node and one
or more final/accept nodes describes a finite automata.
• Notations

Nodes (States) are represented by

Initial node/state is represented by

Final node(s) is/are denoted by

Transitions from state q to p is a


q
denoted by p
The labels on the edges are input symbols
A SIMPLE AUTOMATON (0)
Transition States
rules
0 1

1 0

q1 q2 q3

Starting state 0,1

Accepting state
A SIMPLE AUTOMATON (1)
0 1

1 0

q1 q2 q3

start
0,1
accept

on input “0110”, the machine goes:


q1  q1  q2  q2  q3 = “reject”
A SIMPLE AUTOMATON (2)
0 1
1 0

q1 q2 q3

0,1

on input “101”, the machine goes:


q1  q2  q3  q2 = “accept”
A SIMPLE AUTOMATON (3)
0 1
1 0

q1 q2 q3

010: reject 0,1


11: accept
010100100100100: accept
010000010010: reject
: reject
TRANSITION GRAPH
Transition

graph
Example. Draw transition diagram for the following DFA
• M = ({q0, q1, q2}, {0, 1}, , q0, {q1}) where  is given as
(q0, 0) = q0, (q0, 1) = q1, (q1, 1) = q1, (q1, 0) = q2, (q2, 0) =
q1, (q2, 1) = q1 0 1

0
1
q0 q2
0, 1
q1
Exercise. Draw transition diagram for the following DFA
• M=({q0, q1, q2, q3 }, , {a, b}, q0, {q3}) where  is given as (q0,
a)=
q1, (q0, b) =q2, (q1, a) = q1, (q2, b) = q2, (q1, a) = q3, (q2,
b) = q3
TRANSITION TABLE
• A transition table is a good way to implement a FSA
• One row for each state, S
• One column for each symbol, A
• Entry in cell (S,A) gives the state or set of states can be reached from
state S on input A.
• A deterministic finite automaton (DFA) has a singe state in every cell

(a|b)*abb
INPUT
STATES a b

>Q0 {q0, q1} q0


Q1 q2
Q2 q3
*Q3
Tr a n s i t i o n F u n c t i o n

:QX Σ->Q
{A, B, C} X {a, b}->{A, B,
C}
For every input on a
state there is exactly
one transition.
REVISIT EXAMPLE #1:
1
Q = {Q0, Q1}

Σ = {0, 1} 0
q0 q1 1
START STATE IS Q0
0
F = {Q0}

δ: 0 1
q0 q1 q0

q1 q0 q1
REVISIT EXAMPLE #2:

Q = {Q0, Q1, Q2} a a/b/c


a
Σ = {A, B, C} c
q0 q1 c q2
START STATE IS Q0

F = {Q2} b b
δ: a b c
q0 q0 q0 q1

q1 q1 q1 q2

q2 q2 q2 q2

• SINCE δ IS A FUNCTION, AT EACH STEP M HAS EXACTLY ONE OPTION.

• IT FOLLOWS THAT FOR A GIVEN STRING, THERE IS EXACTLY ONE COMPUTATION.


EXTENSION OF δ TO STRINGS

δ^ : (Q x Σ*) –> Q

δ^(q,w) – The state entered after reading string w having


started in state q.

Formally:
1) δ^(q, Σ) = q, and
2) For all w in Σ* and a in Σ
δ^(q,wa) = δ (δ^(q,w), a)
1

0
q0 q1 1
0

Recall Example #1:


• What is δ^(q0, 011)? Informally, it is the state entered by M after processing 011
having started in state q0.
• Formally:
δ^(q0, 011) = δ (δ^(q0,01), 1) by rule #2
= δ (δ ( δ^(q0,0), 1), 1) by rule #2
= δ (δ (δ (δ^(q0, λ), 0), 1), 1) by rule #2
= δ (δ (δ(q0,0), 1), 1) by rule #1
= δ (δ (q1, 1), 1) by definition of δ
= δ (q1, 1) by definition of δ
= q1 by definition of δ

• Is 011 accepted? No, since δ^(q0, 011) = q1 is not a final state.


• Note that:

δ^ (q,a) = δ(δ^(q, ε), a) by definition of δ^, rule


#2
= δ(q, a) by definition of δ^, rule
#1
• Therefore:
δ^ (q, a1a2…an) = δ(δ(…δ(δ(q, a1), a2)…), an)

• However, we will abuse notations, and use δ in place of δ^:

δ^(q, a1a2…an) = δ(q, a1a2…an)


1 1 1
• Example #3:
0
0
q0 q1 q2
0
• What is δ(q0, 011)? Informally, it is the state entered by M after
processing 011 having started in state q0.
• Formally:
δ(q0, 011) = δ (δ(q0,01), 1) by rule #2
= δ (δ (δ(q0,0), 1), 1) by rule #2
= δ (δ (q1, 1), 1) by definition of
δ
= δ (q1, 1) by definition of
δ
= q1 by definition of
δ
• Is 011 accepted? No, since δ(q0, 011) = q1 is not a final state.
• Language?
1 1 1
• Recall Example #3:
0
0
q0 q1 q2

• What is δ(q1, 10)? 0

δ(q1, 10) = δ (δ(q1,1), 0) by rule #2


= δ (q1, 0) by definition of
δ
= q2 by definition of
δ

• Is 10 accepted? No, since δ(q0, 10) = q1 is not a final state. The fact
that δ(q1, 10) = q2 is irrelevant, q1 is not the start state!
DEFINITIONS RELATED TO DFAS

• Let M = (Q, Σ, δ,q0,F) be a DFA and let w be in Σ*. Then w is


accepted by M iff δ(q0,w) = p for some state p in F.

• Let M = (Q, Σ, δ,q0,F) be a DFA. Then the language accepted by M


is the set:
L(M) = {w | w is in Σ* and δ(q0,w) is in F}
• Another equivalent definition:
L(M) = {w | w is in Σ* and w is accepted by M}
Let L be a language. Then L is a regular language iff there exists a
DFA M such that L = L(M).
• Let M1 = (Q1, Σ1, δ1, q0, F1) and M2 = (Q2, Σ2, δ2, p0, F2) be DFAs.
Then M1 and M2 are equivalent iff L(M1) = L(M2).
• Notes:
– A DFA M = (Q, Σ, δ,q0,F) partitions the set Σ* into two sets: L(M) and
Σ* - L(M).
– If L = L(M) then L is a subset of L(M) and L(M) is a subset of L (def.
of set equality).
– Similarly, if L(M1) = L(M2) then L(M1) is a subset of L(M2) and L(M2)
is a subset of L(M1).
– Some languages are regular, others are not. For example, if

Regular: L1 = {x | x is a string of 0's and 1's containing


an even number of 1's} and
Not-regular: L2 = {x | x = 0n1n for some n >= 0}
• Can you write a program to “simulate” a given DFA, or any arbitrary
input DFA?
• Question we will address later:
– How do we determine whether or not a given language is regular?
Acceptance
Acceptance of a string by a FA:

- Scan the entire string and if we reach g final state from initial
state.
Acceptance of a language by a FA:

If all the strings in the language are accepted by the FA and


- all the strings those are not in the language must reject by
the FA.
AAcceptance
String c c e p t abyn c e
DFA
• A DFA accepts a string x if the sequence of transitions corresponding
to the symbols of x leads from the start state to an accepting/final
state.
• The transition function has to be extended to determine a string
acceptance by DFA.
•  * : Q X Â+ as defined as follows
•  *(q, x) =  *((q, a), y) if x = ay, a e Â, y e  +
• Note that:
• DFA must have a definite input symbol to move from one state to another
state
•  is total function. If  is not defined for a terminal a then  (q, a)
= D, if D is dummy state
• Example: Consider the following DFA
• M = ({q0, q1, q2}, {a, b}, , q0, {q1, q2} ) where (q0,
a) = q1,
(q0, b) = q2, (q2, a) = q2, (q1, b) = q1, (q1, a)
= q2
• Draw transition diagram of M
i•) x Check
= abbbif theii)
following strings are
x = abba iii)accepted
x = bab by M
a Answersq a q1
0 b
)
b a

q2
a
• b)
i)  *(q0, abbb) =  *(T(q0,a), bbb)
= *(q1, bbb)
= *(T(q1,b), bb)
= *(q1, bb)
= *(T(q1,b), b)
= (q1, b)
= q1
So, abbb is accepted by M because q1 e {q1, q2}
• Following the same step as above it can be proved that (ii) abba
is accepted and (iii) bab is rejected
• Construct a DFA that accept all strings over the
alphabets {a, b} where the string length is at
least 2.
- w {a,b} and |w|>2
- Σ = {a, b}
EXAMPLES
• Example: Find a deterministic finite accepter that
recognizes the set of all strings on  = {a, b} starting with the
prefix ab.
• Answer:

 Exercise: Find a DFA for the following language on


 = {a, b}, L = {w: | w | a mod 3 = 0}
 Answer
• GIVE A DFA M SUCH THAT:

L(M) = {X | X IS A STRING OF (ZERO OR MORE) A’S, B’S AND


C’S SUCH
THAT X DOES NOT CONTAIN THE
SUBSTRING AA} b/c a/b/c
a
a
q0 q1 q2
b/c

LOGIC:

IN START STATE (Q0): B’S AND C’S: IGNORE – STAY IN SAME STATE

Q0 IS ALSO “ACCEPT” STATE

FIRST ‘A’ APPEARS: GET READY (Q1) TO REJECT

BUT FOLLOWED BY A ‘B’ OR ‘C’: GO BACK TO START STATE Q0


66
WHEN SECOND ‘A’ APPEARS AFTER THE “READY” STATE: GO TO REJECT STATE Q2

IGNORE EVERYTHING AFTER GETTING TO THE “REJECT” STATE Q2


• Give a DFA M such that: L(M) = {x | x is a string of a’s, b’s and c’s
such that x contains the substring aba}

b/c a a/b/c
a a
b
q0 q1 q2 q3
c

b/c
Logic: acceptance is straight forward, progressing on each
expected symbol. However, rejection needs special care, in
each state (for DFA, we will see this becomes easier in NFA,
non-deterministic machine)
67
• Give a DFA M such that: L(M) = {x | x is a string of a’s and b’s such
that x contains both aa and bb}
first do, for a language where ‘aa’ comes before ‘bb’
then do its reverse; and then parallelize them. remember, you may have
multiple “final” states, but only one “start” state

a
a b
q1 q2 q3
a b a/b
a
q0 a b q7
b
b b a a
q4 q5 q6
b
EXAMPLE

• Construction of a DFA for the set of string over {a, b} such that length of
the string |w| is divisible by 2 i.e., |w| mod 2 = 0.

L = {?, aa, ab, ba, bb, aaaa, bbbb, ............}


• Design FA with ∑ = {0, 1} accepts even number of 0's and even
number of 1's.
This FA will consider four different stages for input 0 and input 1. The
stages could be:

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.
• Construct DFA for the language accepting strings starting with ‘101’
• All strings start with substring “101”.
• Then the length of the substring = 3.
• Therefore, minimum number of states in the DFA = 3 + 2 = 5.
• The minimized DFA has five states.
• The language l= {101,1011,10110,101101,.........}
• The transition diagram is as follows −
• Let ς = {0, 1}. Give DFAs for {}, {Σ}, Σ*, and Σ+.
0/1
0/1
FOR {}: FOR {Ε}: 0/1
q0 q0 q1

0/1
0/1

FOR Σ*: FOR Σ+: 0/1


q0 q0 q1
• Problem: third symbol from last is 1

0/1

1 0/1 0/1
q0 q1 q2 q3

Is this a DFA?

No, but it is a Non-deterministic Finite Automaton

73
• EXAMPLE #1:

0
q0 q1 1
0

1 0 0 1 1
q0 q0 q1 q0 q0 q0

• One state is final/accepting, all others are rejecting.


• The above DFA accepts those strings that contain an even number of
0’s, including the null string, over sigma = {0,1}
L = {all strings with zero or more 0’s}
• Note, the DFA must reject all other strings
DFA for strings containing 01
• What makes this DFA Regular expression: (0+1)*01(0+1)*
deterministic?
• Q = {q0,q1,q2}
• ∑ = {0,1}
1 0 0,1 • start state = q0

1 • F = {q2}
start 0
q0 q1 q2 • Transition table
Accepting
state symbols
0 1
• What if the language allows
q0 q1 q0
empty strings?
states

q1 q1 q2
*q2 q2 q2
• Example: empty string or start and end with 0 0/1
0
q0 q1
Q = {q0, q1} 0

Σ = {0, 1}
start state is q0

F = {q0}

δ:
{q1}0 {}1
q0
{q0, q1} {q1}
q1
76
Example
• To construct the DFA for the set of strings containing 011:

• First of all, we should know the smallest substring accepted by


this automata, i.e. 011 is the smallest string,

• Now it could contain any no of symbols before and after 011


• q0- Initial state
• qf- Final state
Exercise #2
• Build a dfa for the following language:
l = { w | w is a bit string which contains the
substring 11}

78
Non-deterministic finite automata (NFA)

• A non-deterministic finite automaton (NFA)


• is of course “non-deterministic”
• Implying that the machine can exist in more than one state at
the same time
• Transitions could be non-deterministic

1 qj
qi … • Each transition function therefore
1 maps to a set of states
qk
DEFINITIONS FOR NFAS

• Let M = (Q, Σ, δ,q0,f) be an NFA and let w be in Σ*. Then w is


accepted by M iff δ({q0}, w) contains at least one state in F.

• Let m = (q, σ, δ,q0,f) be an NFA. Then the language accepted by M is


the set:
L(M) = {w | w is in Σ* and δ({q0},w) contains at least one state in F}

• Another equivalent definition:


L(m) = {w | w is in σ* and w is accepted by m}
• The major difference b/n DFA and NFA
• The range of the transition function is 2Q
• Empty string, l , is allowed to be an argument of δ 80
Nondeterministic finite state automata (NFA)

• An NFA is a five-tuple:
M = (q, σ, δ, q0, f)
Q a finite set of states
Σ A finite input alphabet
Q0 the initial/starting state, q0 is in Q
F A set of final/accepting states, which is a subset of Q
Δ A transition function, which is a total function from Q x Σ to 2 Q
Δ : (q x σ) –> 2q :2q is the power set of q, the set of all subsets of q
δ(q,s) : The set of all states p such that there is a transition Labeled s from
q to p
Δ(q,s) is a function from q x s to 2q (but not only to q)
HOW TO USE AN NFA?
• Input: a word w in ∑*
• Question: is w acceptable by the NFA?
• Steps:
• Start at the “start state” q0
• For every input symbol in the sequence w do
• Determine all possible next states from all current states, given the
current input symbol in w and the transition function
• If after all symbols in w are consumed and if at least one of the
current states is a final state then accept w;
• Otherwise, reject w.
TRANSITION DIAGRAM
• FA can be represented using transition diagram.
• Corresponding to FA definition, a transition diagram has:
• States represented by circles;
• An alphabet (Σ) represented by labels on edges;
• Transitions represented by labeled directed edges between states. The
label is the input symbol;
• One start state shown as having an arrow head;
• One or more final state(s) represented by double circles.
• Example transition diagram to recognize (a|b)*abb

a b q2 b
q0 q1 q3

b
• Example #1: one or more 0’s followed by one or more 1’s

Q = {q0, q1, q2}

Σ = {0, 1} 1 0/1
0
START STATE IS Q0 0
q0 q1 1 q2
f = {q2}

δ:
{q0, q01} {}1
q0
{} {q1, q2}
q1
{q2} {q2}
q2
EXAMPLES

• Q = {q0,q1,q2}
• Σ = {0,1}
• start state = q0
• f = {q2}
Transition table
• Example #2: pair of 0’s or pair of 1’s as substring
Q = {q0, q1, q2 , q3 , q4} 0/1 0/1

Σ = {0, 1} 0 0
q0 q3 q4
START STATE IS q0
1 0/1
f = {q2, q4}
1 q2
δ: 0 1 q1

q0 {q0, q3} {q0, q1}

q1 {} {q2}
{q2} {q2}
q2
{q4} {}
q3
{q4} {q4}
q4
Example
Obtain an NFA to accept the following language L = {w | w abab n or
aban where n> 0} The machine to accept either ababn or aban where n
0 is shown below:

b a a
q0 a q1 q2 q3 a

q4
a a
q5 b a
q6 q7
• let Σ = {a, b, c}. give an NFA m that accepts:
L = {x | x is in Σ* and x contains ab}

a/b/c a/b/c

a b
q0 q1 q2

Is L a subset of L(M)? Or, does m accepts all string in L?


Is L(M) a subset of L? Or, does m rejects all strings not
in L?
• Is an NFA necessary? Can you draw a DFA for this l?
• Designing NFA is not as trivial as it seems: easy to create bug
accepting string outside language
Regular expression: (0+1)*01(0+1)*

NFA for strings containing 01


Why is this non-deterministic?
• Q = {q0,q1,q2}

0,1 0,1 •  = {0,1}


• start state = q0
start 0 1
q0 q1 q2 • F = {q2}
Final • Transition table
state symbols
0 1
What will happen if at state q1 q0 {q0,q1} {q0}
states

an input of 0 is received? q1 Φ {q2}


89
*q2 {q2} {q2}
STRING ACCEPTANCE BY NFA
Cont…

• Consider the following NFA M =(Q, Â, δ , q0 ,


F)
Check if the following strings are accepted by M
i. Baa
ii. abba
iii. aab
Cont…
WHAT IS AN “ERROR STATE”?
• A DFA for recognizing the key word “while”

w h i l e
q0 q1 q2 q3 q4 q5

Any other input symbol


qerr
Any symbol

• An NFA for the same purpose:

w h i l e
q0 q1 q2 q3 q4 q5 93

Transitions into a dead state are implicit


Differences: DFA vs. NFA
• Dfa • Nfa
1. Some transitions could be non-
1. All transitions are deterministic deterministic
• Each transition leads to exactly one • A transition could lead to a subset of
state states
2. For each state, transition on all 2. Not all symbol transitions need to be
possible symbols (alphabet) should be defined explicitly (if undefined will
defined go to an error state – this is just a
design convenience, not to be
3. Accepts input if the last state visited is confused with “non-determinism”)
in F
3. Accepts input if one of the last states
4. Sometimes harder to construct because is in F
of the number of states
4. Generally easier than a DFA to
5. Practical implementation is feasible construct
5. Practical implementations limited but
emerging (e.G., Micron automata
processor)

94
EQUIVALENCE OF DFAS AND NFAS
• Do DFAs and NFAs accept the same class of languages?
• Is there a language l that is accepted by a DFA, but not by
any NFA?
• Is there a language l that is accepted by an NFA, but not by
any DFA?
• Observation: every DFA is an NFA, DFA is only restricted NFA.
• Therefore, if l is a regular language then there exists an NFA m
such that l = l(m).
• It follows that nfas accept all regular languages.
• But do NFAs accept more? 95
• Lemma 2: let m be an NFA. Then there exists a DFA M’ such that
L(M) = L(M’).
• Proof: (sketch)
Let m = (q, σ, δ,q0,f).

Define a dfa m’ = (q’, σ, δ’,q’0,f’) as:

Q’ = 2Q each state in m’ corresponds to a


= {Q0, Q1,…,} subset of states from M

Where qu = [qi0, qi1,…qij]

F’ = {qu | qu contains at least one state in f}

Q’0 = [q0]
96

δ’(qu, a) = qv iff δ(qu, a) = qv


• Example of creating a DFA out of an NFA (as per the constructive
proof): 0/1
0
q0 q1
0

-->q0

δ for dfa: 0 1 q1
{q1} {}
write as write as
->q0 [q1] []

[q1]

[]

97
• Example of creating a DFA out of an NFA (as per the
constructive proof): 0/1
0
q0 q1
0

δ: 0 1
{q1} {}
->q0 write as
[q1]
{q0,q1} {q1}
[q1] write as
[q01]
[]

[q01] 98
• EXAMPLE OF CREATING A DFA OUT OF AN NFA (AS PER THE
CONSTRUCTIVE PROOF): 0/1
0
q0 q1
0

δ: 0 1
{q1} {}
->q0 write as
[q1]
{q0,q1} {q1}
[q1] write as
[q01]

[] []
[] 99

[q01]
• Example of creating a DFA out of an NFA:

0/1
0
q0 q1
δ: 0 1
0
->q0
{q1} {}
[q1]
{q0,q1 {q1}
}
[]
[] []
[q01] [q01] [q1]

100
• Construct DFA M’ as follows:

1
0/1
1 [q0]
0 [q1]
[]

δ({q0}, 0) = {q1}=> δ’([q0], 0) = [q1] 1


[q0q1]
δ({q0}, 1) = {}=> δ’([q0], 1) = [ ]
δ({q1}, 0) = {q0, q1} =>δ’([q1], 0) = [q0q1]
0
δ({q1}, 1) = {q1}=> δ’([q1], 1) = [q1]
δ({q0, q1}, 0) = {q0, q1}=> δ’([q0q1], 0) = [q0q1]
δ({q0, q1}, 1) = {q1} =>δ’([q0q1], 1) = [q1]
δ({}, 0) = {} =>δ’([ ], 0) = [ ]
δ({}, 1) = {} => δ’([ ], 1) = [ ]
101
NFA TO DFA BY SUBSET CONSTRUCTION

• Let N = {qn,∑,δn,q0,fn}

• Goal: build D={QD,∑, δ d,{q0},fd} s.T. L(d)=l(n)


• Construction:
1. QD= all subsets of QN (i.E., Power set)
2. Fd=set of subsets S of QN s.T. S∩fn≠φ
3. δ D: for each subset S of QN and for each input symbol a
in ∑:
• δ d(s,a) = U δ n(p,a)
p in s

102
EXAMPLE 1

Following are the various parameters


for NFA. Q = { q0, q1, q2 } ∑ = ( a, b )
F = { q2 } δ (Transition Function of
NFA)
Cont…
• Step 1: Q’ = ɸ step 2: Q’ = {q0} step 3: for
each state in Q’, find the states for each
input symbol. Currently, state in Q’ is q0,
find moves from q0 on input symbol a and
b using transition function of NFA and
update the transition table of DFA. δ’
(transition function of DFA)
• Now { q0, q1 } will be considered as a single δ’ (Transition Function
state. As its entry is not in Q’, add it to Q’. So Q’ of DFA)
= { q0, { q0, q1 } } Now, moves from state { q0,
q1 } on different input symbols are not present in
transition table of DFA, we will calculate it like:
δ’ ( { q0, q1 }, a ) = δ ( q0, a ) ∪ δ ( q1, a ) =
{ q0, q1 } δ’ ( { q0, q1 }, b ) = δ ( q0, b ) ∪ δ
( q1, b ) = { q0, q2 } Now we will update the
transition table of DFA.
Cont…
• Now { q0, q2 } will be considered as a single state. As its entry is not in
Q’, add it to Q’. So Q’ = { q0, { q0, q1 }, { q0, q2 } } now, moves from
state {q0, q2} on different input symbols are not present in transition table
of DFA, we will calculate it like: δ’ ( { q0, q2 }, a ) = δ ( q0, a ) ∪ δ ( q2, a
) = { q0, q1 } δ’ ( { q0, q2 }, b ) = δ ( q0, b ) ∪ δ ( q2, b ) = { q0 } now we
will update the transition table of DFA.

δ’ (transition function of DFA)


Cont.…

• As there is no new state generated, we are done with the


conversion. Final state of DFA will be state which has q2
as its component i.e., { q0, q2 }
• Following are the various parameters for DFA. Q’ = { q0, {
q0, q1 }, { q0, q2 } } ∑ = ( a, b ) F = { { q0, q2 } } and
transition function δ’ as shown above. The final DFA for
above NFA has been shown in below.
Example 3
• Convert the following non-deterministic finite automata (NFA) to
deterministic finite automata (DFA)-
Solution
• Transition table for the given non-deterministic finite automata
(NFA) is-
State / Alphabet 0 1

→q0 q0 q1, *q2


q1 q1, *q2 *q2
*q2 q0, q1 q1

Step-01: Step-02:
Let Q’ be a new set of states of Add transitions of start state q0
the Deterministic Finite Automata to the transition table T’.
(DFA). State /
0 1
Let T’ be a new transition table of Alphabet

the DFA. →q0 q0 {q1, q2}


Cont…
• STEP-03:

New state present in state Q’ is {q1, q2}.


Add transitions for set of states {q1, q2} to the transition table
T’. State / Alphabet 0 1

→q0 q0 {q1, q2}


{q1, q2} {q0, q1, q2} {q1, q2}

Step-04:
State / Alphabet 0 1
New state present in state Q’ is {q0, q1,
q2}. →q0 q0 {q1, q2}
Add transitions for set of states {q0, q1, {q1, q2} {q0, q1, q2} {q1, q2}
q2} to the transition table T’.
{q0, q1, q2} {q0, q1, q2} {q1, q2}
Cont…
• Step-05:
• Since no new states are left to be added in the transition table t’, so we stop.
• States containing q2 as its component are treated as final states of the DFA.
• Finally, transition table for deterministic finite automata (DFA) is-
State / Alphabet 0 1

→q0 q0 *{q1, q2}


*{q1, q2} *{q0, q1, q2} *{q1, q2}
*{q0, q1, q2} *{q0, q1, q2} *{q1, q2}

Now, Deterministic Finite Automata (DFA) may be drawn as-


Exercise
• Convert the following non-deterministic finite automata (NFA) to
deterministic finite automata (DFA)-
Idea: To avoid enumerating all of
power set, do
“lazy creation of states”

NFA to DFA construction: example

• L = {W | W ENDS IN 01} 1 0
NFA: DFA: 0 1
0,1 [q0] [q0,q1] [q0,q2]
0
0 1 1
q0 q1 q2

δD 0 1 δD 0 1
δN 0 1
Ø Ø Ø [q0] [q0,q1] [q0]
q0 {q0,q1} {q0}
[q0] {q0,q1} {q0} [q0,q1] [q0,q1] [q0,q2]
q1 Ø {q2}
[q1] Ø {q2} *[q0,q2] [q0,q1] [q0]
*q2 Ø Ø *[q2] Ø Ø
[q0,q1] {q0,q1} {q0,q2}
*[q0,q2] {q0,q1} {q0} 0. Enumerate all possible subsets
*[q1,q2] Ø {q2} 1. Determine transitions
112
*[q0,q1,q2] {q0,q1} {q0,q2}
2. Retain only those states
reachable from {q0}
Cont…

• L = {W | W ENDS IN 01} 1 0
NFA: DFA: 0 1
0,1 [q0] [q0,q1] [q0,q2]
0
0 1 1
q0 q1 q2

δN 0 1
δD 0 1
q0 {q0,q1} {q0} [q0] [q0,q1] [q0]
q1 Ø {q2} [q0,q1] [q0,q1] [q0,q2]
*q2 Ø Ø *[q0,q2] [q0,q1] [q0]

Main Idea:
Introduce states as you
113 go

(on a need basis)


FA WITH -TRANSITIONS

• We can allow explicit -transitions in finite automata


• i.e., A transition from one state to another state without consuming any
additional input symbol
• Explicit -transitions between different states introduce non-
determinism.
• Makes it easier sometimes to construct NFAs

Definition:  -NFAs are those NFAs with at least one explicit -


transition defined.
•  -NFAs have one more column in their transition table
114
EXAMPLE OF AN -NFA

L = {w | w is empty, or if non-empty will end in 01}

0,1

0 1 • -Closure of a state q,
q0 q1 q2
 eclose(q), is the set of all
start q’0 states (including itself) that
can be reached from q by
δE 0 1  repeatedly making an
arbitrary number of -
ECLOSE(q’0)
*q’0 Ø Ø {q’0,q0}
q0 {q0,q1} {q0} {q0} ECLOSE(q0)
ECLOSE(q1) transitions. 115
q1 Ø {q2} {q1}
*q2 Ø Ø {q2} ECLOSE(q2)
EXAMPLE OF AN -NFA
To simulate any transition:
Step 1) Go to all immediate destination states.
Step 2) From there go to all their -closure states as well.
L = {w | w is empty, or if non-empty will end in 01}
0,1

0 1
SIMULATE FOR W=101:
q0 q1 q2
 q 0’
 
start q’0
q 0’ q0
1 1
q0
δE 0 1  Ø
x 0
ECLOSE(q’0)
*q’0 Ø Ø {q’0,q0} q1
q0 {q0,q1} {q0} {q0} ECLOSE(q0) 1
q2 116
q1 Ø {q2} {q1}
*q2 Ø Ø {q2}
EXAMPLE OF ANOTHER -NFA
To simulate any transition:
Step 1) Go to all immediate destination states.
Step 2) From there go to all their -closure states as well.

0,1 SIMULATE FOR W=101:

0 1 ?
q0 q1 q2
  1

start q’0 q3

δE 0 1 
*q’0 Ø Ø {q’0,q0,q3}
q0 {q0,q1} {q0} {q0,q3}
q1 Ø {q2} {q1}
*q2 Ø Ø {q2}
q3 Ø {q2} {q3}

117
Converting ε-NFA to DFA

Step 1 − Consider M={Q, Σ, δ,q0,F)


is NFA with ε. We have to convert this Step 2 − We will obtain δ transition on
NFA with ε to equivalent DFA denoted [p1,p2,p3,…pn] for each input.
by δ0([p1,p2,p3,..pn],a)=ε-closure(δ(p1,a)
M0=(Q0,Σ, δ0,q0,F0) U δ(p2,a2)U……………… δ(pn,a))
Then obtain, = U (i=1 to n) ε-closure d(pi,a)
ε-closure(q0) ={p1,p2,p3,……pn} Where a is input ∈Σ
then [p1,p2,p2,….pn] becomes a start
state of DFA Step 3 − The state obtained
now[p1,p2,p3,….pn] ∈ Q0 [p1,p2,p3,…pn] ∈ Q0 .
The states containing final state in pi
is a final state in DFA
Example
Convert the following NFA with epsilon to
equivalent DFA
Consider the following NFA for conversion of NFA with epsilon to DFA −
To convert this NFA with epsilon, we will first
find the ε-closures, as given below −
• ε-closure(q0)={q0,q1,q2}
• ε-closure(q1)={q1,q2}
• ε-closure(q2)={q2}
Let us start from ε-closure of start state, as mentioned below −
When, ε-closure(q0)={q0,q1,q2}, we will call this state as A.
Now, let us find transition on A with every input symbol, as shown below −
δ'(A, a) = ε-closure(δ(A,a))
= ε-closure(δ(q0,q1,q2), a))
= ε-closure(δ(q0, a) ∪ δ(q1,a) U δ(q2,a) )
= ε-closure(ΦUq1 ∪q2)
= ε-closure(q1)
= {q1, q2} let us call it as state B
δ'(A, b) = ε-closure(δ(A,b))
= ε-closure(δ(q0,q1,q2), b))
= ε-closure(δ(q0, b) ∪ δ(q1,b) U δ(q2,b) )
= ε-closure(q0 U Φ∪q0)
= ε-closure(q0)
= {q0,q1, q2} its nothing but state A
δ'(B, a) = ε-closure(δ(B,a))
= ε-closure(δ(q1,q2), a)) Hence, the transition table for the
= ε-closure(δ(q1,a) U δ(q2,a) ) generated DFA is as follows −
= ε-closure(q1 ∪q2)
= ε-closure(q1) States\inputs a b
= {q1, q2} its nothing but A B A
state B
δ'(B, b) = ε-closure(δ(B,b)) B B A
= ε-closure(δ(q1,q2), b))
= ε-closure(δ(q1,b) U
δ(q2,b) )
= ε-closure(Φ∪q0)
= ε-closure(q0)
= {q0,q1, q2} its nothing but
state A
The DFA diagram is as follows −
Example 2
Convert the NFA with ε into its equivalent DFA.

Solution:
Let us obtain ε-closure of each state.
1.ε-closure {q0} = {q0, q1, q2}
2.ε-closure {q1} = {q1}
3.ε-closure {q2} = {q2}
4.ε-closure {q3} = {q3}
5.ε-closure {q4} = {q4}
Now, let ε-closure {q0} = {q0, q1, q2} be state A.
Hence

δ'(A, 0) = ε-closure {δ((q0, q1, q2), 0) }


= ε-closure {δ(q0, 0) ∪ δ(q1, 0) ∪ δ(q2, 0) }
= ε-closure {q3}
= {q3} call it as state B.

δ'(A, 1) = ε-closure {δ((q0, q1, q2), 1) }


= ε-closure {δ((q0, 1) ∪ δ(q1, 1) ∪ δ(q2, 1) }
= ε-closure {q3}
= {q3} = B.

δ'(B, 0) = ε-closure {δ(q3, 0) } = ϕ


δ'(B, 1) = ε-closure {δ(q3, 1) }
= ε-closure {q4}
= {q4} i.e. state C
δ'(C, 0) = ε-closure {δ(q4, 0) } = ϕ
δ'(C, 1) = ε-closure {δ(q4, 1) } = ϕ

The DFA will be,

Example
3
Convert the given NFA into its equivalent DFA.
Solution: Let us obtain the ε-closure of each state.

1.ε-closure(q0) = {q0, q1, q2}


2.ε-closure(q1) = {q1, q2}
3.ε-closure(q2) = {q2}
Now we will obtain δ' transition. Let ε-closure(q0) = {q0, q1, q2} call it
as state A.
δ'(A, 0) = ε-closure{δ((q0, q1, q2), 0)}
= ε-closure{δ(q0, 0) ∪ δ(q1, 0) ∪ δ(q2, 0)}
= ε-closure{q0}
= {q0, q1, q2}
δ'(A, 1) = ε-closure{δ((q0, q1, q2), 1)}
= ε-closure{δ(q0, 1) ∪ δ(q1, 1) ∪ δ(q2, 1)}
= ε-closure{q1}
= {q1, q2} call it as state B
δ'(A, 2) = ε-closure{δ((q0, q1, q2), 2)}
= ε-closure{δ(q0, 2) ∪ δ(q1, 2) ∪ δ(q2, 2)}
= ε-closure{q2}
= {q2} call it state C

Thus we have obtained 1.δ'(A, 0) = A


2.δ'(A, 1) = B
3.δ'(A, 2) = C

Now we will find the δ'(B, 0) = ε-closure{δ((q1, q2), 0)}


transitions on states B = ε-closure{δ(q1, 0) ∪ δ(q2, 0)}
and C for each input. = ε-closure{ϕ}
Hence =ϕ
δ'(B, 1) = ε-closure{δ((q1, q2), 1)}
= ε-closure{δ(q1, 1) ∪ δ(q2, 1)}
= ε-closure{q1}
= {q1, q2} i.e. state B itself
δ'(B, 2) = ε-closure{δ((q1, q2), 2)}
= ε-closure{δ(q1, 2) ∪ δ(q2, 2)}
= ε-closure{q2}
= {q2} i.e. state C itself.
Thus we have obtained 1.δ'(B, 0) = ϕ
2.δ'(B, 1) = B
3.δ'(B, 2) = C

Now we will obtain transitions for C:


δ'(C, 0) = ε-closure{δ(q2, 0)}
= ε-closure{ϕ}

δ'(C, 1) = ε-closure{δ(q2, 1)}
= ε-closure{ϕ}

δ'(C, 2) = ε-closure{δ(q2, 2)}
= {q2}
Hence the DFA is

As A = {q0, q1, q2} in which final state q2 lies hence A is


final state. B = {q1, q2} in which the state q2 lies hence B is
also final state. C = {q2}, the state q2 lies hence C is also a
final state.
Minimization of DFA
DFA minimization stands for converting a given DFA to its equivalent
DFA with minimum number of states. DFA minimization is also called
as Optimization of DFA and uses partitioning algorithm.

Minimization of DFA
Suppose there is a DFA D < Q, Σ, q0, δ, F > which recognizes a language L.
Then the minimized DFA D < Q’, Σ, q0, δ’, F’ > can be constructed for
language L as:

Step 1: We will divide Q (set of states) into two sets. One set will contain
all final states and other set will contain non-final states. This partition is
called P0.
Step 2: Initialize k = 1
Step 3: Find Pk by partitioning the different sets of Pk-1. In each set of Pk-1,
we will take all possible pair of states. If two states of a set are
distinguishable, we will split the sets into different sets in P k.
Applications of interest
• Comparing two DFAs:
• L(DFA1) == l(DFA2)?

• How to minimize a DFA?


1. Remove unreachable states
2. Identify & condense equivalent states into one

130
Cont…
Step 4: Stop when Pk=Pk-1 (No change in partition)
Step 5: All states of one set are merged into one. No. of states in
minimized DFA will be equal to no. of sets in Pk.
How to find whether two states in partition Pk are distinguishable ?
Two states ( qi, qj ) are distinguishable in partition Pk if for any input
symbol a, δ ( qi, a ) and δ ( qj, a ) are in different sets in partition P k-1.
Example
Consider the following DFA shown in figure.
Cont…
Step 1. P0 will have two sets of states. One set will contain q1, q2, q4 which
are final states of DFA and another set will contain remaining states. So P0
= { { q1, q2, q4 }, { q0, q3, q5 } }.
Step 2. To calculate P1, we will check whether sets of partition P0 can be
partitioned or not:
i) For set { q1, q2, q4 } :
δ ( q1, 0 ) = δ ( q2, 0 ) = q2 and δ ( q1, 1 ) = δ ( q2, 1 ) = q5, So q1 and q2
are not distinguishable.

Similarly, δ ( q1, 0 ) = δ ( q4, 0 ) = q2 and δ ( q1, 1 ) = δ ( q4, 1 ) = q5, So


q1 and q4 are not distinguishable.
Since, q1 and q2 are not distinguishable and q1 and q4 are also not
distinguishable, So q2 and q4 are not distinguishable. So, { q1, q2, q4 } set
will not be partitioned in P1.
ii) For set { q0, q3, q5 } :
δ ( q0, 0 ) = q3 and δ ( q3, 0 ) = q0
δ ( q0, 1) = q1 and δ( q3, 1 ) = q4
Cont…
Moves of q0 and q3 on input symbol 0 are q3 and q0 respectively which
are in same set in partition P0. Similarly, Moves of q0 and q3 on input
symbol 1 are q1 and q4 which are in same set in partition P0. So, q0 and
q3 are not distinguishable.

δ ( q0, 0 ) = q3 and δ ( q5, 0 ) = q5 and δ ( q0, 1 ) = q1 and δ ( q5, 1 ) =


q5
Moves of q0 and q5 on input symbol 1 are q1 and q5 respectively which
are in different set in partition P0. So, q0 and q5 are distinguishable. So,
set { q0, q3, q5 } will be partitioned into { q0, q3 } and { q5 }. So,
P1 = { { q1, q2, q4 }, { q0, q3}, { q5 } }
To calculate P2, we will check whether sets of partition P1 can be
partitioned or not:
Cont…
iii)For set { q1, q2, q4 } :
δ ( q1, 0 ) = δ ( q2, 0 ) = q2 and δ ( q1, 1 ) = δ ( q2, 1 ) = q5, So q1 and
q2 are not distinguishable.
Similarly, δ ( q1, 0 ) = δ ( q4, 0 ) = q2 and δ ( q1, 1 ) = δ ( q4, 1 ) = q5,
So q1 and q4 are not distinguishable.
Since, q1 and q2 are not distinguishable and q1 and q4 are also not
distinguishable, So q2 and q4 are not distinguishable. So, { q1, q2, q4 }
set will not be partitioned in P2.
iv)For set { q0, q3 } :
δ ( q0, 0 ) = q3 and δ ( q3, 0 ) = q0
δ ( q0, 1 ) = q1 and δ ( q3, 1 ) = q4
Moves of q0 and q3 on input symbol 0 are q3 and q0 respectively
which are in same set in partition P1. Similarly, Moves of q0 and q3
on input symbol 1 are q1 and q4 which are in same set in partition
P1. So, q0 and q3 are not distinguishable.
Cont…
v) For set { q5 }:
Since we have only one state in this set, it can’t be further partitioned. So,
P2 = { { q1, q2, q4 }, { q0, q3 }, { q5 } }
Since, P1=P2. So, this is the final partition. Partition P2 means that q1, q2
and q4 states are merged into one. Similarly, q0 and q3 are merged into one.
Minimized DFA corresponding to DFA of Figure 1 is shown in Figure 2 as:
When to call two states in a DFA “equivalent”?

Two states p and q are said to be equivalent iff:


Past doesn’t matter - only future does!

i) Any string w accepted by starting at p is also accepted by


starting at q;

P
W
And
Q
ii) Any string w rejected by starting at p is also rejected by starting
at q.
P
W
136
Q
 P≡q
Computing equivalent states in a DFA
Table filling algorithm
A =
0 1
B = =
0 1 0
A C E G C x x =
1 0 1
0 1 D x x x =
B 1
D 1
F 0
H E x x x x =
1 0 F x x x x x =
0
Pass #0 G x x x = x x =
1. Mark accepting states ≠ non-accepting states
H x x = x x x x =
Pass #1
2. Compare every pair of states A B C D E F G H
3. Distinguish by one symbol transition
4. Mark = or ≠ or blank(tbd)
Pass #2
5. Compare every pair of states 137
6. Distinguish by up to two symbol transitions (until different or same or tbd)
….
(keep repeating until table complete)
Cont…

A =
0 1
B =
0 1 0
A C E G C =
1 0 1
0 1 D =
B 1
D 1
F 0
H E =
1 0 F =
0
G =
H =
A B C D E F G H

138
Cont.…

A =
0 1
B =
0 1 0
A C E G C =
1 0 1
0 1 D =
1 1 0 E X X X X =
B D F H
1 0 F X =
0
G X =
1. Mark X between accepting vs. non-accepting state H X =
A B C D E F G H

139
Cont…

A =
0 1
B =
0 1 0
A C E G C X =
1 0 1
0 1 D X =
1 1 0 E X X X X =
B D F H
1 0 F X =
0
G X X =

1. Mark X between accepting vs. non-accepting state


H X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

140
Cont…

A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X =
1 1 0 E X X X X =
B D F H
1 0 F X =
0
G X X X =

1. Mark X between accepting vs. non-accepting state


H X X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

141
Cont…

A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
1 1 0 E X X X X =
B D F H
1 0 F X X =
0
G X X X X =

1. Mark X between accepting vs. non-accepting state


H X X = X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

142
TABLE FILLING ALGORITHM -
STEP BY STEP
A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
1 1 0 E X X X X =
B D F H
1 0 F X X X =
0
G X X X = X =

1. Mark X between accepting vs. non-accepting state


H X X = X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

143
Cont…

A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
B 1
D 1
F 0
H E X X X X =
1 0 F X X X =
0
G X X X = X X =

1. Mark X between accepting vs. non-accepting state


H X X = X X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

144
Cont…

A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
B 1
D 1
F 0
H E X X X X =
1 0 F X X X =
0
G X X X = X X =

1. Mark X between accepting vs. non-accepting state


H X X = X X X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

145
Cont…

A =
0 1
B = =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
B 1
D 1
F 0
H E X X X X =
1 0 F X X X X X =
0
G X X X = X X =
. Mark X between accepting vs. non-accepting state H X X = X X X X =
. Pass 1: A B C D E F G H
Look 1- hop away for distinguishing states or strings
. Pass 2:
Look 1-hop away again for distinguishing states or strings
continue…. 146
Cont…

A =
0 1
B = =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
B 1
D 1
F 0
H E X X X X =
1 0 F X X X X X =
0
G X X X = X X =

. Mark X between accepting vs. non-accepting state


H X X = X X X X =
. Pass 1: A B C D E F G H
Look 1- hop away for distinguishing states or strings
. Pass 2:
Look 1-hop away again for distinguishing states or strings Equivalences:
continue…. • A=B 147
• C=H
• D=G
Cont….

0 1 0 1

0 1 0 0 1
A C E G A C E
1 0 1 1 0
0 1 0
1 1 0 1
B D F H D F
1 0 1 0
0

Retrain only one copy for


each equivalence set of states

Equivalences:
• A=B 148
• C=H
• D=G
EXERCISE

Convert to an equivalent DFA


0 0

A 0 B 1 D
1 1
0 0
C E
1
1
APPLICATIONS

• Text indexing
• Inverted indexing
• For each unique word in the database, store all locations
that contain it using an NFA or a DFA
• Find pattern P in text example: google querying
• Extensions of this idea:
• PATRICIA tree, suffix tree

150
PROPERTIES OF DFAS AND NFAS

• The machine never really terminates.


• It is always waiting for the next input symbol or making
transitions.
• The machine decides when to consume the next symbol from the input
and when to ignore it.
• (But the machine can never skip a symbol)
• => A transition can happen even without really consuming an input
symbol (think of consuming  as a free token) – if this happens, then it
becomes an -NFA (see next few slides).
• A single transition cannot consume more than one (non-) symbol.

151
SUMMARY
• DFA
• Definition
• Transition diagrams & tables
• Regular language
• NFA
• Definition
• Transition diagrams & tables
• DFA vs. NFA
• NFA to DFA conversion using subset construction
• Equivalency of DFA & NFA
• Removal of redundant states and including dead states
• -Transitions in NFA
• Pigeon hole principles
• Text searching applications 152

You might also like