0% found this document useful (0 votes)
9 views72 pages

2-Finite Automata

The document provides an overview of Deterministic Finite Acceptors (DFA) and Nondeterministic Finite Acceptors (NFA) in the context of theoretical computer science. It includes definitions, operations, transition functions, and examples of DFAs, as well as exercises for constructing DFAs for specific languages. Additionally, it discusses the concept of regular languages and provides coding exercises related to DFA validation.

Uploaded by

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

2-Finite Automata

The document provides an overview of Deterministic Finite Acceptors (DFA) and Nondeterministic Finite Acceptors (NFA) in the context of theoretical computer science. It includes definitions, operations, transition functions, and examples of DFAs, as well as exercises for constructing DFAs for specific languages. Additionally, it discusses the concept of regular languages and provides coding exercises related to DFA validation.

Uploaded by

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

Theoretical Computer Science

FINITE AUTOMATA
Vietnamese-German University
Ngoc Tran, Ph.D.
[email protected]

Binh Duong, SS 2025


Vietnamese-German University

Content
• Deterministic Finite Accepters (DFA).
• Nondeterministic Finite Accepters (NFA).
• Equivalence between DFA vs NFA.
• Reduction of the Number of States in Finite Automata.

2
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


• Definition 2.1: A deterministic finite accepter (dfa) is defined by the
quintuple:
M = (Q, , , q0, F)
Q is a finite set of internal states,
Σ is a finite set of symbols called the input alphabet,
δ: Q × Σ → Q is a transition function,
q0 ∈ Q is the initial state,
F ⊆ Q is a set of final states.

3
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


DFA Operation
• At the initial time, it is assumed to be in the initial state q0, with its
input mechanism on the leftmost symbol of the input string.
• The input mechanism can move only from left to right of the input
string and reads exactly one symbol on each step.
• During each move of the automaton, the input mechanism advances
one position to the right, so each move consumes one input symbol.

4
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


DFA Operation (cont.)
• When the end of the string is reached, the string is accepted if the
automaton is in one of its final states. Otherwise, the string is rejected.
• The transitions from one internal state to another are governed by the
transition function δ.
• For example, if δ (q0, a) = q1, then if the dfa is in state q0 and the current
input symbol is a, the dfa will go into state q1.

q0 = q-nought

5
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


Transition Graph: To visualize and represent finite automata.
• The vertices represent states. qi
• The edges represent transitions. a

• The labels on the vertices are the names of the states.


• The labels on the edges are the current values of the input symbol.
• The initial state is identified by an incoming unlabeled arrow not
originating at any vertex. q0

• The final states are drawn with a double circle. qn

6
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


Example: Given a DFA as: M = (Q, , , q0, F) with
Q = {q0, q1, q2},  = {0, 1}, F = {q1},  is given by
(q0, 0) = q0, (1) (q0, 1) = q1, (4)
(q1, 0) = q0, (2) (q1, 1) = q2, (5)
(q2, 0) = q2, (3) (q2, 1) = q1, (6)
• Transition Graph:

7
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


Example: Given a DFA as: M = (Q, , , q0, F) with
Q = {q0, q1, q2},  = {0, 1}, F = {q1},  is given by w=0001
- q0, 0: (q0, 0) = q0 (applied (1)
- q0, 0: (q0, 0) = q0, (applied (1)
(q0, 0) = q0, (1) (q0, 1) = q1, (4) - q0, 0: (q0, 0) = q0 (applied (1)
(q1, 0) = q0, (2) (q1, 1) = q2, (5) - q0, 1: (q0, 1) = q1 (applied (4)

(q2, 0) = q2, (3) (q2, 1) = q1, (6) As q1 is a final state, dfa accepts w.

• Transition Graph: 0 0
1 1
q0 q1 q2

0 1
9
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


Exercise
Which of the strings 100, 101, 0001, 0111, 1100, 01001, 11001, and
0000110 are accepted by the dfa in the figure?
0 0
1 1
q0 q1 q2

0 1

11
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


Extended Transition Function
δ* : Q × ∑* → Q
• The 2nd argument of δ* is a string, rather than a single symbol.
• δ*’s value gives the state the automaton will be in after reading that
string.
• Example
• If (q0, a) = q1, (q1, b) = q2, then *(q0, ab) = q2

13
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


δ* is defined recursively by:
• *(q, ) = q,
• *(q, wa) = (*(q, w), a) = (*(q, vb), a) = ((*(q, v), b), a) …… =
(…((*(q, ))…), b), a) , q  Q, w  *, a  .

14
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


Associated Language of DFA’s
Definition 2.2: The language L accepted by a dfa M = (Q, Σ, δ, q0, F) is
the set of all strings on Σ accepted by M. In formal notation:
L(M) = {w  *: *(q0, w)  F}.

Non-acceptance means that the dfa stops in a non-final state, so that


LM  = {w  * : *(q0 , w)  F}.

15
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


a a, b
Associated Language of DFA’s
b a, b q
Example: Consider the dfa (see Figure): q 0 q 1 2

• The language L accepted by the automaton is


L(M) = {anb : n  0}
• Trap state: when the dfa goes into trap state from which it can never
escape.
• E.g., q2 is the trap state.
• Trap state can be the final state or not.
• Can be defined for the trap state group which can be final or not.

16
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


Transition Table
• We can represent the function δ as a table:
• The row label is the current state,
• The column label represents the current input symbol.
• The entry in the table defines the next state.

a b a a, b
q0 q0 q1
b a, b
q1 q2 q2 q0 q1 q2
q2 q2 q2

17
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


Examples:
a) Find a dfa that recognizes the set of all strings on Σ = {a,b} starting
with the prefix ab. (e.g, abaaab, ab, aba, abb, …)
b) Find a dfa that accepts all the strings on {0,1}, except those
containing the substring 001.
a, b
1 0,1
0
a b
q0 q1 q2 0 0 1
 0 00 001
b a 1
q3 b)
a)
a, b 18
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


Exercise
For Σ = {a,b}, construct dfa's that accept the sets consisting of:
(a) all strings with exactly one a,
(b) all strings with at least one a,
(c) all strings with no more than three a's,
(d) all strings with at least one a and exactly two b’s.

19
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


Exercise
Give dfa's for the languages:
(a) L = {ab5wb2: w ∈ {a,b}*},
(b) L = {abnam: n ≥ 2, m ≥ 3},
(c) L = {w1abw2 : w1 ∈ {a,b}* ,w2 ∈ {a,b}*}, e.g., w1 = {lambda, a, b,
aa, bb, ab, abba, …}
(d) L = {ban : n ≥ 1},

23
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


Regular Languages
Definition 2.3: A language L is called regular if and only if there exists
some deterministic finite accepter (dfa) M such that L= L(M).
E.g.: Show that the language L is regular, given L= {awa: w ∈ {a,b}*}.
w = {lamda, a, b, aaa, bbbb, abba, … }
b a
a a
q0 q2 q3
b b
q1
a, b
25
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


Exercise
Show that the language L= {a2wb2: w ∈ {a,b}*} is regular.

26
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


Coding Exercise (Advanced)
• Input: Given a string s and a dfa.
• Output: Check if s is accepted by a DFA.

28
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


int m, n;

cout << "Enter numbers of rows and columns of Transition Table: " << endl;
cin >> m >> n;

int** arTrans = new int* [n];


for (int i = 0; i < n; i++)
arTrans[i] = new int[m];

// input array elements


for (int i = 0; i < n; ++i)
for(int j=0; j<m; j++)
{
cout << "ArTrans[" << i << "][" << j << "] = ";
cin >> arTrans[i][j];
}

29
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


// print array elements
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
cout << arTrans[i][j] << " ";
cout << endl;
}

string s;
cout << "Input string s: ";
cin >> s;

int *arEdgeVal;
int sizeE;
// Input sizeE and arEdgeVal
……

30
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Deterministic Finite Accepters (DFA)


Pseudocode of the function checking the validity of an input string s:

bool validS(int q0, string s, int int next_vertex = arTrans[qt][j];


arFinalState[], int sizeF, int **arTrans, qt = next_vertex;
int arEdgeVal[], int sizeE) }
{ int i;
bool res = false; //check if qt in arFinalState
for(i = 0; i < sizeF; i++)
int len = s.size(); if(qt == arFinalState[i]) break;
int qt = q0; if(i < sizeF) return true;
for (int i = 0; i < len; i++)//traverse S
{ return res;
for (int j = 0; j < sizeE; j++) //find s[i] }
if (arEdgeVal[j] == s.at(i)) break;

31
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


Definition 2.4: A nondeterministic finite accepter (nfa) is defined by the
quintuple:
M = (Q, Σ, δ, q0, F),
where Q, Σ, q0, F are defined as for deterministic finite accepters, but
 : Q  (  {})  2Q

32
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


• The range of δ is in the power set 2Q, so that its value is not a single
element of Q but a subset of Q. This subset defines the set of all possible
states that can be reached by the transition.
• E.g., if the current state is q1, the symbol a is read, and δ(q1, a) = {q0, q2}: then
either q0 or q2 could be the next state of the nfa.
• λ is allowed to be the second argument of δ.
• This means that the nfa can make a transition without consuming an input symbol.
• Although we still assume that the input mechanism can only travel to the right, it is
possible that it is stationary on some moves.
• The set δ(qi, a) may be empty , meaning that there is no transition
defined for this specific situation. Such a situation is called a dead
configuration, and the automaton simply stops without further action.
33
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


• NFA can be represented by a transition graph.
• Examples:

q1 a q2 a 1
a q3
q0 q1 0, 1 q
q0 0 2
a
a q4 
q5
a

(a) (b)

34
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


• Exercise
• Compare DFA vs NFA

35
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


Extended Transition Function
• For an nfa, the extended transition function is defined so that δ*(qi ,w)
contains qj if and only if there is a walk/path in the transition graph
from qi to qj labeled w. This holds for all qi, qj ∈ Q, and w ∈ Σ*.
• Example: • *(q1, ) = {q1, q2, q0}
• *(q2, ) = {q2, q0}

• *(q0, a) = {q1, q2, q0}
• *(q1, a) = {q1, q2, q0} a b, 
q0 q1 q2
• *(q1, b) = {q2, q0}
• *(q0, b) = 
• *(q0, ab) = {q1, q2, q0} 37
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


Case 1: How to compute δ*(q0 ,w) without -transitions.
- Compute the transition table
- Apply recursive method
• Basis: *(q, ) = q,
• Induction: *(q, wa) = (*(q, w), a), q  Q, w  *, a  .

39
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


Example: Compute δ*(q0 ,00101). 0, 1

0 1
• *(q0, ) = q0 q0 q1 q2
• *(q0, 0) = q0, 0) = {q0, q1}
•  *(q0, 00) = q0, 0)  q1, 0) = {q0, q1}
•  *(q0, 001) = q0, 1)  q1, 1) = {q0}  {q2} = {q0, q2}
•  *(q0, 0010) = q0, 0)  q2, 0) = {q0, q1}   = {q0, q1}
•  *(q0, 00101) = q0, 1)  q1, 1) = {q0}  {q2} = {q0, q2}

Is “00101” accepted by the above NFA?  slide 47


41
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


Case 2: How to compute δ*(q0 ,w) with -transitions.
- Compute the transition table
- Apply recursive method
• Basis: *(q, ) = {…},
• Induction: *(q, wa) = *(*(q, w), a), q  Q, w  *, a  .
• *(q, a) = ?

43
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


Let T be a subset of Q.
T , a  ⋃qT δq, a ; ∗T ,   ⋃qT q,  ;

Let move(q, a), move(T, a), -closure(q), -closure(T) be (q, a), (T, a),
*(q, ), *(T, ).
• *(q, a) = -closure(move(-closure(q), a))
• *(T, a) = -closure(move(-closure(T), a))

44
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


a a 
Example: Compute *(q0, a).
 q0 q4 q1
 q1 q2 q3
q1 q0, q3 q2
a
q0 q2
a
q4 
q5 q3
q4 q5
• *(q0, a) = -closure(move(-closure(q0), a)) q5
• -closure(q0) = {q0, q1, q2}
• move({q0, q1, q2}, a) = {q4, q0, q3}
• -closure({q4, q0, q3}) = {q4, q0, q3, q5, q1, q2}
So, *(q0, a) = {q0, q1, q2, q3, q4, q5}
46
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


Exercise:
1. Find a) δ*(q0, 1011) and b) δ*(q1, 01) in the below figure.
1
q0 q1 0, 1 q
0 2


2. Find a) δ*(q0, a) and b) δ*(q1, λ) in the below figure.

a 
q0 q1 q2

49
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


Definition 2.6: The language L accepted by an nfa M = (Q, Σ, δ, q0, F) is
defined as the set of all strings accepted in the above sense. Formally,
L(M) = {w  *: *(q0, w)  F  }.

In words, the language consists of all strings w for which there is a walk
labeled w from the initial vertex of the transition graph to some final
vertex.

Example: consider the example on slide 39


52
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


• Example: Language L is accepted by the below NFA:
L = {(10)n: n  0}

1 0, 1
q0 0 q1 q2

53
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


Exercise
1. Construct an nfa with three states that accepts the language {ab, abc}*
2. Find an nfa with three states that accepts the language
L= {an: n ≥ 1} ∪ {bmak: m ≥ 0, k ≥ 0}
3. Find an nfa with four states for L = {an: n ≥ 0} ∪ {bna: n ≥ 1}.

Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.


Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


Coding Exercise (Advanced)
• Input an NFA by a transition table.
• Input q, T and an edge label a.
• Compute and output the results of
• *(q, a) = -closure(move(-closure(q), a))
• *(T, a) = -closure(move(-closure(T), a))

58
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


PseudoCode // define the empty entry in the transition table arTrans is -1
void find_lamda_closure(int q, int N /*row size*/, int M /*col
size*/, int** arTrans, int& nLC, int* arLC)
{
arLC = new int[N];

nLC = 0; //size of arLC


//M-1 is the index of the last column
while(arTrans[q][M-1] >= 0)
{
q = arTrans[q][M - 1];
arLC[nLC] = q;
nLC++;
}

arLC = (int*)realloc(arLC, nLC * sizeof(int));


} 59
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


//int* arMoveA contains the states received from a transition a
from q
PseudoCode void move(int q, int a, int N /*row size*/, int M /*col size*/,
int** arTrans, int sizeE, int* arEdgeVal, int*arMoveA)
{
//find the column index associated with a
int j;
for (j = 0; j < sizeE; j++) //find j of a
if (arEdgeVal[j] == a) break;

//assume that the empty value is -1 in the transition table


int i = 0;
while (arTrans[q][j] != -1)
{ Solution of the case
arMoveA[i] = arTrans[q][j]; each entry is a
i++; combination of more
q = arTrans[q][j]; than one states?
}
} 60
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Nondeterministic Finite Accepters (NFA)


//define the T-closure {
void find_T_closure(int* T, int nT /*size of find_lamda_closure(T[i], N, M, arTrans, nLC,
T*/, int N /*row size*/, int M /*col size*/, arLC[i]);
int** arTrans, int& nTC, int* arTC) int count = 0;//count of new elements to be
{ added into arTC
nTC = 0; for (int j = 0; j < nTC; j++) // check if any
arTC = new int[N]; element in NLC exists in arTC? If not, add it
into arTC
int nLC; {
int** arLC; for (int k = 0; k < nLC; k++)
if (arLC[i][k] == arTC[j]) break;
//allocate memory to arLC if (k == nLC)
{
//find lamda-closure for each qi in T arTC[nTC + count] = arLC[i][k];
for (int i = 0; i < nTC; i++) //find count ++;
lamda-closure for each qi in T }
}
nTC += count;
} 61
Theoretical Computer Science } Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Equivalence between DFA vs NFA


Definition 2.7: Two finite accepters, M1 and M2, are said to be
equivalent if they both accept the same language.
L(M1) = L(M2)
Example: The dfa and nfa are equivalent since they both accept the
language L = {(10)n : n ≥ 0}.
0, 1
1 1 0, 1
q0 q1 1 q2 q0 q1 q2
0
0

dfa
0
nfa 

62
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Equivalence between DFA vs NFA


Discussion
• A dfa is a restricted kind of nfa, it is clear that any language that is
accepted by a dfa is also accepted by some nfa.
• There is a language accepted by some nfa for which, in principle, we
cannot find a dfa. But it turns out that this is not so.
• The classes of dfa's and nfa's are equally powerful: For every language
accepted by some nfa there is a dfa that accepts the same language.

63
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Equivalence between DFA vs NFA


• Theorem 2.2 Let L be the language accepted by a nondeterministic
finite accepter (NFA) MN = (QN, Σ, δN, q0, FN). Then there exists a
deterministic finite accepter (DFA) MD = (QD, Σ, δD, q0, FD) such that
L = L (MD)
• Procedure nfa-to-dfa
• Input: nfa MN = (QN, Σ, δN, q0, FN)
• Output: Transition graph GD of associated dfa MD

64
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Equivalence between DFA vs NFA


1. Create a graph GD with vertex δN*(q0, λ). Identify this vertex as the initial
vertex.
2. Repeat the following steps 3-6 until no more edges are missing.
3. Take any vertex {qi, qj, … , qk} of GD that has no outgoing edge for some
a∈Σ.
4. Compute δN*({qi, qj, … , qk}, a) = δN*(qi, a)  δN*(qj, a)  …  δN*(qk, a)
= {ql, qm, … , qn}.
5. Create a vertex for GD labeled {ql ,qm,…,qn} if it does not already exist.
6. Add to GD an edge from {qi ,qj ,…,qk} to {ql ,qm,…,qn} and label it with a.
7. Every state of GD whose label contains any qf ∈ FN is identified as a final
vertex.
65
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Equivalence between DFA vs NFA


Example
• Convert the nfa to an equivalent dfa.

*(q0, ) = {q0}
b a b 
*({q0}, a) = {q1, q2}
a  q0 q1
q0 q1 q2 *({q0}, b) = 
q1 q1 q2
*({q1, q2}, a) = {q1, q2}
a q2 q0
*({q1, q2}, b) = {q0}

67
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Equivalence between DFA vs NFA


Output dfa *(q0, ) = {q0}
a b 
a
a *({q0}, a) = {q1, q2} q0 q1
{q0} b {q1, q2} *({q0}, b) =  q1 q1 q2
b *({q1, q2}, a) = {q1, q2} q2 q0

*({q1, q2}, b) = {q0}
a, b

• A state of nfa is a set of states of dfa.


• A final state of dfa contains a final state of nfa.

68
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Equivalence between DFA vs NFA


• Find an associated dfa from the below nfa:
a
 a b 
q1 q2 q0 q1 q1 q3
a a
b q1 q0 q2
q0 a, 
 q2 q1, q2
q3 q4
b q3 q4 q3 q4
q4 q3

69
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Equivalence between DFA vs NFA
• N*(q0, ) = {q0, q3, q4}
• N*({q0, q3, q4}, a) = {q1, q2, q4}
a b 
• N*({q0, q3, q4}, b) = {q1, q2, q3, q4}
q0 q1 q1 q3
• N*({q1, q2, q4}, a) = {q0, q1, q2, q3, q4}
q1 q0 q2
• N*({q1, q2, q4}, b) = {q3, q4}
q2 q1, q2
• N*({q1, q2, q3, q4}, a) = {q0, q1, q2, q3, q4}
q3 q4 q3 q4
• N*({q1, q2, q3, q4}, b) = {q3, q4}
q4 q3
• N*({q0, q1, q2, q3, q4}, a) = {q0, q1, q2, q3, q4}
• N*({q0, q1, q2, q3, q4}, b) = {q1, q2, q3, q4}
• N*({q3, q4}, a) = {q4} N*({q3, q4}, b) = {q3, q4}
• N*({q4}, a) =  N*({q4}, b) = {q3, q4}
70
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Equivalence between DFA vs NFA a b 


q0 q1 q1 q3
q1 q0 q2
q2 q1, q2
{q1, q 2, q 4} q3 q4 q3 q4
a a q4 q3
b
a
{q0, q3, q4} b {q3, q4} {q4} {q0, q1, q2, q3, q4}
b
b a
b b a
{q1, q2, q3, q4}

71
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Equivalence between DFA vs NFA


• Find the associated dfa of the following nfa:
Nfa M1 Nfa M2 Nfa M3
a b  a b  a b 
q0 q1 q3 q1 q 0 q 1, q 3 q 3 q 3 q0 q1 q2 q1
q1 q2 q 2, q 0 q1 q2 q2 q0 q1 q1, q2 q3 q3
q2 q1 q2 q1 q2 q0, q2
q3 q0, q4 q3 q4 q3 q4 q4 q3 q2, q3
q4 q3, q4 q4 q4 q4 q3 F = {q0}
F = {q2} F = {q4}
72
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Equivalence between DFA vs NFA


Coding exercise (Advanced)
• Input an NFA.
• Output an equivalent DFA using the algorithm nfa2dfa on page 59.

73
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Reduction of the Number of States in Finite


Automata
• Given two equivalent dfas below:
0, 1
1 0, 1 1
q1 q3 q0 q1 q2
0
q0 0 0, 1
0
1 1
q2 1 q4 q5
(b)
0 0, 1 0
(a)

74
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Reduction of the Number of States in Finite


Automata
• Inaccessible state is the state that can never be reached from the initial
state.
• E.g., the state q5 plays absolutely no role in the automaton since it can never be
reached from the initial state q0 (see the previous slide).
• An inaccessible state can be removed (along with all transitions
relating to it) without affecting the language accepted by the
automaton.

75
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Reduction of the Number of States in Finite


Automata
• In figure a), the states reachable subsequent to the first move δ(q0, 0)
mirror those reachable from a first move δ(q0, 1). The second
automaton combines these two options.
• The second alternative (Figure b) is clearly preferable. Representation
of an automaton for the purpose of computation requires space
proportional to the number of states. For storage efficiency, it is
desirable to reduce the number of states as far as possible.

76
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Reduction of the Number of States in Finite


Automata
Definition 2.8:
Two states p and q of a dfa are called indistinguishable if
*(q, w)  F implies *(p, w)  F,
and *(q, w)  F implies *(p, w)  F,
for all w ∈ Σ*.
If, on the other hand, there exists some string w ∈ Σ* such that
*(q, w)  F and *(p, w)  F,
then the states p and q are said to be distinguishable by a string w.

77
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Reduction of the Number of States in Finite


Automata
• Two states are either indistinguishable or distinguishable.
• Indistinguishability has the properties of an equivalence relation: If p
and q are indistinguishable and if q and r are also indistinguishable,
then so are p and r, and all three states are indistinguishable.
• One method for reducing the states of a dfa is based on finding and
combining indistinguishable states.

78
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Reduction of the Number of States in Finite


Automata
• A method for finding pairs of distinguishable states.
procedure: mark()
Input: (|Q|  (|Q| -1)/2) pairs of (p, q) of a full dfa
Output: pairs of (p, q) are marks
1. Remove all inaccessible states.
• This can be done by enumerating all simple paths of the graph of the dfa
starting at the initial state.
• Any state not part of some path is inaccessible.

79
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Reduction of the Number of States in Finite


Automata
2. Consider all pairs of states (p, q). If p ∈ F and q ∉ F or vice versa,
mark the pair (p, q) as distinguishable.
3. Repeat the following step until no previously unmarked pairs are
marked.
i. For all pairs (p, q) and all a ∈ Σ, compute δ(p, a) = pa and δ (q, a) = qa.
ii. If the pair (pa ,qa) is marked as distinguishable, mark (p, q) as
distinguishable.

80
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Reduction of the Number of States in Finite


Automata
• Theorem 2.3: The procedure mark, applied to any dfa M = (Q, λ, δ, q0,
F), terminates and determines all pairs of distinguishable states.

• The states qi and qj are distinguishable with a string of length n if and


only if there are transitions:
(qi, a) = qk and (qj, a) = ql
for some a ∈ Σ, with qk and qi distinguishable by a string of length n-1.

81
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Reduction of the Number of States in Finite


Automata
• The procedure mark can be implemented by partitioning the states into
equivalence classes.
• Whenever two states are found to be distinguishable, they are
immediately put into separate equivalence classes.

82
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Reduction of the Number of States in Finite


Automata
• Once the indistinguishability classes are found, the construction of the
minimal dfa is straightforward.
• Given a dfa M = (Q, Σ, δ, q0, F), we construct a reduced dfa 𝑀 =
(𝑄,Σ, δ, q0, F).

83
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Reduction of the Number of States in Finite


Automata
procedure: reduce ()
1. Use procedure: mark() to generate the equivalence classes, say {qi, qj, …,
qk}.
2. For each set {qi, qj, …, qk} of such indistinguishable states, create a state
labeled ij …k for 𝑀.
3. For each transition rule of M of the form δ(qr, a) = qp, find the sets to
which qr and qp belong. If qr ∈ {qi ,qj ,…,qk} and qp ∈ {ql, qm, …, qn}, add
to δ a rule δ (ij … k, a) = lm … n.
4. The initial state q0 is that state of 𝑀 whose label includes the 0.
5. F is the set of all the states whose label contains i such that qi∈ F.

84
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Example
• Reduce the number of the states of the below dfa:

q1 0 1
0 0 1 0,1
0 q0 q1 q3
1 q1 q2 q4
q0 q2 q4
0 q2 q1 q4
1 1
q3 q3 q2 q4
q4 q4 q4

85
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

0 1
q0 q1 q3
(q0, q1)1 (q0, q3)1 (q1, q2) (q1, q4)0 (q2, q4)0 q1 q2 q4
(q0, q2)1 (q0, q4)0 (q1, q3) (q2, q3) (q3, q4)0 q2 q1 q4
q3 q2 q4
0 q4 q4 q4
0 0, 1
1,2,3 0,1 1
0 123 4
4

86
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Reduction of the Number of States in Finite


Automata
Theorem 2.4: Given any dfa M, application of the procedure reduce
yields another dfa 𝑀 such that
L(M) = L(𝑀)
Furthermore, 𝑀 is minimal in the sense that there is no other dfa with a
smaller number of states that also accepts L(M).

88
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Reduction of the Number of States in Finite


Automata
Reduce the number of the states of the below dfa.
0 1
q0
0 1 q0 q1 q2
q1 q2 q1 q3 q4
0 1 q2 q5 q5
0,1
1 0,1
q3 q4 q5 0 q6 q3 q3 q4
q4 q5 q5
0 1 0,1 q5 q6 q5
q6 q6 q6
89
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University
0 1
q0 q1 q2
q1 q3 q4
q2 q5 q5
q3 q3 q4
q4 q5 q5
q5 q6 q5
q6 q6 q6
0
0 1
1 0,1
1,3 2,4 5,6
0 0,1

90
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University
0 1
q0 q1 q2
(q0, q1)0 (q1, q2)1 (q2, q4) (q4, q5)0
q1 q3 q4
(q0, q2)0 (q1, q3) (q2, q5)0 (q4, q6)0
q2 q5 q5
(q0, q3)0 (q1, q4)1 (q2, q6)0 (q5, q6)
q3 q3 q4
(q0, q4)0 (q1, q5)0 (q3, q4)1
q4 q5 q5
(q0, q5)1 (q1, q6)0 (q3, q5)0
q5 q6 q5
(q0, q6)1 (q2, q3)1 (q3, q6)0
q6 q6 q6
1,3 0
0 1
2,4 1,3 1 2,4
0,1
5,6
0 0,1
5,6
92
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Reduction of the Number of States in Finite


Automata
Dfa M1 Dfa M2 Dfa M3 Dfa M4
a b a b a b a b
q0 q1 q4 q0 q1 q2 q0 q1 q2 q0 q1 q3
q1 q4 q2 q1 q2 q3 q1 q2 q3 q1 q2 q4
q2 q4 q3 q2 q2 q3 q2 q1 q4 q2 q0 q3
q 3 q 3 q3 q3 q5 q4 q3 q4 q0 q3 q1 q4
q4 q4 q5 q4 q5 q3 q4 q3 q0 q4 q2 q3
q5 q4 q6 q5 q5 q5
q6 q7 q6 q6 q1 q7 Exercise: Reduce the number
q 7 q7 q 7 q7 q6 q4 of the states of the below dfa.
94
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

Reduction of the Number of States in Finite


Automata
Coding exercise (Advanced)
• Input a dfa and its size n from the console or a file.
• Output all equivalence classes using the algorithm mark() on pages 63,
64.
• Output a new dfa after reducing the states as the algorithm reduce() on
page 68.

95
Theoretical Computer Science Lecturer: Ngoc Tran, Ph.D.
Vietnamese-German University

References
1. Peter Linz: An Introduction to Formal Languages and Automata,
Fifth edition, Jones & Bartelett learning, 2012.
2. Bài giảng môn Lý thuyết Automat và Ngôn ngữ Hình Thức, Hồ Văn
Quân, Đại học Kỹ Thuật TPHCM.

96
Module 10: Theoretical Computer Science

You might also like