Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

Lecture 02 – Non Deterministic Finite Automata

• Introduction to Non-Deterministic Finite Automata (NFA)

• Construction of NFA and it’s Problem

Dr. Nida Aslam


2/5/2024
FiniteAutomata
language theory

Theory of computation
finiteautomataandlanguagetheory
Computational models
computational complexity x

Three pillars

L language Symbols Alphabets E


Token string word
statement sentence
program paragraph

A Automata a simple thatcontains finitesetofstates


machine representation Descriptiveform

G
iffaithp
Grammar Transition stategraph diagraph

Transitiontable

Finiteautomata pushmachine TuringMachine morecomplexmorepowerful

simplemachine

FA DFA
dathe
Deterministic
languge JaÑ au ab aan aan aba
always onpathforeacheventinput a b
Finite automata definepathforall possibleinput
mayhavedead configuration Trap a a deadffiguration

NondeterministicFA NFA NDFA

Epsilon NDFA ENFA ENDFA


Finite Automata
 Finite Automata(FA) is the simplest machine to recognize patterns.

 There are two main kinds of Finite state Automata (FSA)

 Deterministic Finite Automata (DFA):


 At a particular state, a unique path determined for each alphabet.

 Non-Deterministic Finite Automata (NFA)


 At a particular state, a unique path may not be determined for each input
alphabet.

Dr. Irfan 2/5/2024 2


Deterministic Finite Automata (DFA)
 The finite automata are called DFA if the machine reads an input string one symbol at a time.

 Deterministic refers to the uniqueness of the computation.

 A Deterministic Finite Automata (DFA) is a type of finite automata with a unique transition from each
state to every input symbol.

 In DFA, there is only one path for specific input from current state to the next state.

 DFA doesn’t accept the null move i.e., DFA can’t change state without any input character.

 DFA can contains multiple final states. 0,1


0 1
 It is used in lexical analysis in compilers.
1 0
q0 q1 q2

Start State Final or


accepting State

Dr. Irfan 2/5/2024 3


Formal Definition of DFA
 A DFA is the collection of five tuples.

 Q: Finite set of States.


0,1
 ∑: Finite set of input symbols. 0 1

 q0: Initial state 1 0


q0 q1 q2
 F: Final state
Start State Final or
accepting State
 δ : Transition Function
EP
capita jitidstat.at
state g 0
 Transition Function can be defined as: δ: Q × ∑ → Q go I q
q I q
 δ: Current State (Q) × Input (∑) → Next state (Q) q 0 92
92 0 92
92 1 92
Dr. Irfan 2/5/2024 4
Graphical Representation of DFA
 DFA can be represented by diagraphs called state graphs or transition diagrams.
 The state is represented by vertices

 The arcs labeled with an input characters shows the transition.

 The initial stet is marked with an arrow.


 {0, 1} are inputs.
 The final state is denoted by a double circle.
 q0: Initial state
0,1
0 1  q1: Intermediate state

1 0  q2: Final state


q0 q1 q2

Start State Final or


accepting State

Dr. Irfan 2/5/2024 5


Transition Table
 It is basically a tabular representation of the transition function that takes two
arguments (a state and a symbol) and returns a value (the next state).
 Rows corresponds to states

 Column corresponds to input symbols

 Entries correspond to the next states.

 The start state is marked with arrow (→)

 The accept state are marked with star (*)

0 1
→ q0 q0 q1
Transition
Table
iii* q1 q1 q1
Dr. Irfan 2/5/2024 6
Transition Function
 The mapping function denoted by (δ).

 The transition function returns a state which can be called as next state.

 Two parameters are passed to this transition function.


 Current state
 Input symbol

 δ (Current state, current input symbol) = next state

 Example:

 δ(q0 , 0 ) = q0 0 1

 δ(q0 , 1 ) = q1 → q0 q0 q1
* q1 q1 q1

Dr. Irfan 2/5/2024 7


Deterministic Finite Automata
Example:
a b
 DFA can be defined as.
→1 2 3
 Set of alphabets Σ  {a, b}
*2 2 2
 Finite set of states Q={1,2,3} 3 3 3
 One initial I={1}

 One/more/none final states F={2} a, b


a
 Transition Table to show all possible transitions 1 2

b
a, b
Why Dead state in DFA?? 3

Dead state

Dr. Irfan 2/5/2024 8


Example of DFA
Example:

 Let DFA be Q={ a, b, c} , q0 = {a}, F= {c}, ∑ = {0,1}

 Construct the diagraph using the transition table.


Transition Table

0 1

→a a b
b c a
*c b c

Dr. Irfan 2/5/2024 9


Example

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

 The DFA accepts those strings that contain an even number of 0’s, including the null
1
string, over Sigma = {0,1}
not loop
0
L = {all strings with zero or more 0’s} 1
q0 q1
0
 Note, the DFA must reject all other strings
The firststring is empty

 This DFA also accept the empty string as well. 1 00

Dr. Irfan 2/5/2024 10


Example
Example:
a a a/b/c

c c
q0 q1 q2

b b

a c c c b accepted

q0 q0 q1 q2 q2 q2

a a c rejected

q0 q0 q0 q1

 Accepts those strings that contain at least two c’s

Dr. Irfan 2/5/2024 11


Example
Example:

 Exactly Two a’s

b
90 9 92 9 a
go 9 90
9 92 9
 At Least Two b’s 92 93 92
93 9 93

Dr. Irfan 2/5/2024 12


Acceptance of language
 A language acceptance is defined by "if a string w is accepted by the
machine M i.e., if it is reaching the final state F by taking the string w.

 Not accepted if not reaching the final state.

 Number of state = min_string + 1

Dr. Irfan 2/5/2024 13


Introduction – NFA

 A Non-deterministic Finite Automaton (NFA) or (NDFA)

 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

n deterministicFA
a qj
NFA Each transition function therefore maps to
NDFA qi …
Fromonestatetoanother a set of states
a
zero
path no
dead contiguration
qk
onepath a se
than
more one
path

Dr. Irfan 2/5/2024 14


Non Deterministic Finite Automata (NFA)
 The FA are called NFA, when there exist many paths for specific input from the current state
to the next state.

 It is easy to construct NFA than DFA for a given regular language.

 Every NFA is not DFA, but each NFA can be translated into DFA.

 DFA → Only one path for specific input.

 NFA → many paths for specific input.

 NFA is defined in the same way as DFA, but with two exceptions,
 It contains multiple next state
 It contains

Dr. Irfan 2/5/2024 15


Formal Definition of NFA
 NFA also have five states same as DFA but with
different transition function,

 Transition Function can be defined as: δ: Q × ∑ → 𝟐𝑸

 NFA is the collection of five tuples.

 Q: Finite set of States.

 ∑: Finite set of input symbols.

 q0: Initial state

 F: Final state

 δ : Transition Function

 A set of final states F ⊆ Q.

Dr. Irfan 2/5/2024 16


NFA(NDFA)
Example:

 Set of all strings with two consecutive a’s or two consecutive b’s:

a b
0 0,1 0,2
1 3

2 3

3 3 3

Note that some states have an empty transition on an a or b, and some have multiple
transitions on a or b.

Dr. Irfan 2/5/2024 17


Introduction – NFA
Example:
 Why is this non-deterministic?

a,b a,b
start a b
q0 q1 q2
Final a b
state go
go go 9
 Fill these
q 92
 Q = { go 9 }
g 9
  = { a b} 92 92
 start state = {
go }
 F={ } Function
92
 = Transition table É ExQ How to do it?

Dr. Irfan 2/5/2024 18


Introduction – NFA
Example:
a a, b
a
A B
 Q  Finite set of states  {A, B}

 Σ  Input alphabet  {a, b}

 q0  Starting / Initial state  {A}


(A, a) A
(A, b)
 F  set of final state  {B} (B, a) B
(B, b)
 δ  Transition function  Q * Σ  {A, B} * { a, b}

Dr. Irfan 2/5/2024 19


Introduction – NFA
Example:

 In NFA, we have no Dead Configuration, while in DFA we have.

 For example: NFA for a language start with “a”


a b
a a, b
a →A
a b
A B
*B
b

Where’s Dead Configuration and why?

Dr. Irfan 2/5/2024 20


Graphical and Tabular Representation of NFA
 An NFA can be represented by digraphs called state diagrams same like DFA.

 Example: Q= {q0, q1, q2}, ∑ ={0,1} initial state=q0, Final State=q2

Dr. Irfan 2/5/2024 21


Example: NFA
Example:

 Design NFA for the following Transition table.

NFA State Transition Table


0 1
→q0 {q0, q3} {q0, q1} 0
q1 q2 -
*q2 q2, q2
q3 {q3, q2} q2

Dr. Irfan 2/5/2024 22


Construct the Transition Table
 Example: In the below figure which of them is NFA and which one is DFA

BothNFA
andDFA

(a)

onlyNFA

(b)

Dr. Irfan 2/5/2024 23


Construct NFA for given Languages
∑={a, b}
 Example: L2 = Language containing “a”
9 aa ab ba aba

a 9am
g

 Example: L3 = {Ends with “a”}

90 a g

 Example: L4 = {Start with “ab”}

90 a q b g

Dr. Irfan 2/5/2024 24


Construct NFA for given Languages

 Example: L5 = {Containing “ab”}

90 a q b 92

 Example: L6 = {Ends with “ab”}

90 a q b g

Dr. Irfan 2/5/2024 25


DFA & NFA

Dr. Irfan 2/5/2024 26


DFA vs. NFA
 All transitions are deterministic.  Some transitions could be non-deterministic.
 Each transition leads to exactly one state  A transition could lead to a subset of states.
 For each state, transition on all possible
 Not all symbol transitions need to be defined
symbols (alphabet) should be defined
explicitly.
 Accepts input if the last state visited is in
 Accepts input if one of the last states is in Final
Final
 Sometimes harder to construct because  Generally easier than a DFA to construct
of the number of states.  Practical implementations limited.
 Practical implementation is feasible.
 Non-deterministic feature is not associated
 Digital computers are deterministic.
with the real computers.
 move is not allowed.
 move is allowed.

2/5/2024 27
Conversion of NFA  DFA

Conversion of DFA & NFA

 We have seen both NFA and DFA and also seen the construction of NFA is more easier.

Which one is more powerful?

Both of them equally powerful and both of


them are using for the same purpose.

2/5/2024 28
Conversion of NFA  DFA
 To check that both are equally powerful, If I could do this

 𝑁𝐹𝐴 ≅ 𝐷𝐹𝐴

 𝐷𝐹𝐴 ≅ 𝑁𝐹𝐴 (As every DFA by default is NFA, so there’s no need to proof)

𝑵𝑭𝑨 ≅ 𝑫𝑭𝑨
 Example – 01: 𝜮 = 𝒂, 𝒃 𝐿 = { 𝑆𝑡𝑎𝑟𝑡 𝑤𝑖𝑡ℎ "a" }

 NFA is a, b
a
A B

2/5/2024 29
Conversion from NFA to DFA
 DFA: Transition Function can be defined as: δ: Q × ∑ → Q

 NFA: Transition Function can be defined as: δ: Q × ∑ → 𝟐𝑸

 Steps for converting NFA to DFA


 Step 1: Convert the given NFA to its equivalent transition table.
 Step 2: Create the DFA’s start state.
 Step 3: Create the DFA’s transition table.
 Step 4: Create the DFA’s final states.
 Step 5: Simplify the DFA.
 Step 6: Repeat steps 3-5 until no further simplification is possible

Dr. Irfan 2/5/2024 30


Conversion of NFA  DFA
NFA a, b DFA
a
A B

a.CC
Trap
 There are many methods for converting, but I will use subset construction.

NFA State Transition Table


DFA State Transition Table
a b
a b
→A
B - →A B - c
trap

*B *B B B
B B
C C C
Introduce a new state instead of “Empty” Construct DFA from STT
state and then develop STT for the new State.
Dr. Irfan 2/5/2024 31
Conversion of NFA  DFA
Cantusecurlybra
a a, b A a s AP b its forDFAE3
a
A B
a.be
DFA State Transition Table

NFA State Transition Table a b


the
a b
Eating
→A [A,B] - C

*[A,B] [A,B] B
-addthis.fi a
→A
{A, B}pg
*B B B
*B
B B C C C

Construct DFA from STT

Dr. Irfan 2/5/2024 32


Conversion of NFA  DFA
Example:

A ñ
90 0 q.iq goin

NFA State Transition Table


DFA State Transition Table
δN 0 1
δD 0 1
→q0 {q0,q1cost
} {q0}
ir →q0 [q0,q1] q0
q1 Ø AddjI {q2} [q0,q1] [q0,q1] [q0,q2]
efIDF Addj.I
*q2 Ø fisea Ø Addin ba *[q0,q2] [q0,q1] q0

Construct DFA from STT

Dr. Irfan 2/5/2024 33


Example:
 Example: Convert the NFA to DFA
a,b
a b
b b s
q0 q1 q2
s 90 i tmg

DFA State Transition Table


NFA State Transition Table
a b
a b
→q0 q0 [q0, q1]
→q0 q0 {q0, q1}
[q0,q1] q0 [q0,q1, q2]
q1 - q2
*[q0,q1, q2] q0 [q0,q1, q2]
*q2 - -

Dr. Irfan 2/5/2024 34


Continue…
 Draw the diagraph for the DFA DFA State Transition Table
a b
→q0 q0 [q0, q1]
[q0,q1] q0 [q0,q1, q2]
*[q0,q1, q2] q0 [q0,q1, q2]

Dr. Irfan 2/5/2024 35


NFA to DFA
 Example: Convert the NFA to DFA  Solve????

DFA
a a its
a b
90 90,4 90 a b
9 9 9 go 90,9 90
g as 9s 90,4 9019192 90191
90191193
g 9 9014119 90191,92193

90,9 rs 9019192 9019192


nominins aominins aominins

Dr. Irfan 2/5/2024 36


NFA to DFA conversion
Example:

NFA DFA

State / Alphabet 0 1 State / Alphabet 0 1


→q0 q0 {q1, q2} →q0 q0 [q1, q2]
q1 q1, q2 q2 *[q1, q2] [q0, q1, q2] [q1, q2]
*q2 q0, q1 q1 *[q0, q1, q2] [q0, q1, q2] [q1, q2]

Dr. Irfan
[H/W-1] Construct & Convert NFA  DFA
 L = { String ends with “a”}

9
ab

a
s 9 s 90 e
E 4.91

NFA State Transition Table DFA State Transition Table

a b a b
9019 90 Go 90,9 9
Go
9019 90,9 9
q

Dr. Irfan 2/5/2024 38


[H/W-2] Construct & Convert NFA  DFA
 L = { second letter from Left side is “a”}
Jb
93
a

ab b 9
A

NFA State Transition Table

Go
a

9
b

9
DFA State Transition Table

90 9
a b
9
by
9 92 92 93

92 92 9 9 92 92

93 93 93
Dr. Irfan 2/5/2024 39
[H/W-3] Construct & Convert NFA  DFA
 L = { second letter from Right side is “a”}

a
g ais q go ask.it ffeein
NFA State Transition Table DFA State Transition Table

a b a b
90,9 10 90 190.9 go
Go
9019 90,9 9a 90,9
9 9 9
90,9 90,9
92
90,9 9a 90,9 9a 90,92
Dr. Irfan 2/5/2024 40
[H/W-4] Construct & Convert NFA  DFA
a
 L = { third letter from Right side is “a”} b
n
a sina.ws
b
in I
at
9 ab s 92 ab
Dodd fff
a b
a

go 9 s 9 q.iq go.gs I go.a.is

NFA State Transition Table DFA State Transition Table

9 b a b
90,9 10 90 190,9 90
Go
90191 90,9 9a 90,9
9 9 9
99 90,91193 90193
92 9 9
90,9 90,9 9
9 9 q q
Dr. Irfan
9019.19219 90 92193 2/5/2024 41
90,9 q 90,9 9a 90,9
9 92,9s 90,9 9 90,9s
90,9 am 9019.19219s
9 92,9s

End of Lecture - 02
Any questions?

2/5/2024 42

You might also like