0% found this document useful (0 votes)
10 views47 pages

Week3

This document provides an overview of Nondeterministic Finite Automata (NFA), including its definition, formal structure, and examples of its application. It explains the differences between NFAs and Deterministic Finite Automata (DFA), as well as the process of converting an NFA to a DFA. Additionally, it discusses the technology behind NFAs and their equivalence in language recognition capabilities.

Uploaded by

Suhail
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)
10 views47 pages

Week3

This document provides an overview of Nondeterministic Finite Automata (NFA), including its definition, formal structure, and examples of its application. It explains the differences between NFAs and Deterministic Finite Automata (DFA), as well as the process of converting an NFA to a DFA. Additionally, it discusses the technology behind NFAs and their equivalence in language recognition capabilities.

Uploaded by

Suhail
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/ 47

Nondeterministic Finite

Automata
(Part 2)

1
Definition
 A Nondeterministic Finite Automaton (NFA)
 implying that the machine can exist in more than one
state at the same time
 transitions could be non-deterministic

 When an NFA reads an input symbol and there


are multiple transitions labeled with that
symbol
 it splits into multiple copies of itself, and
 follows all possibilities in parallel

2
Deterministic and nondeterministic computation with an
accepting branch
3
Formal Definition of NFAs
An NFA is also defined by the 5-tuple:
M  Q, ,  , q0 , F 
Q: Set of states, i.e. q0 , q1, q2 
 : Input aplhabet, i.e. a, b
: Transition function, which is a mapping
between Q x ∑ -> subset of Q
q0 : Initial state
F : Final states
Example #1 (Formal Definition Example)

L(M ) = {λ, 10, 1010, 101010, ...}


= {10} *

0
q0 q1 0, 1 q2
1

5
Example #1
• Q = {q0,q1,q2}
L(M ) = {λ, 10, 1010, 101010, ...} •  = {0,1}
= {10} * • start state = q0
0 • F = {q0}

q0 q1 0, 1 q2 • Transition table
1 symbols
0 1

q0
* Φ {q1}
states

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

6
Example #2: NFA for strings containing 01

• Q = {q0,q1,q2}
0,1 0,1 •  = {0,1}
• start state = q0
0 1 • F = {q2}
q0 q1 q2
• Transition table
symbols
0 1

q0 {q0,q1} {q0}
states

What was the q1


Φ {q2}

DFA? *
q2 {q2} {q2}

7
Example #3 L = { w | w ends in 01}

• Q = {q0,q1,q2}
0,1 •  = {0,1}
• start state = q0
0 1
q0 q1 q2 • F = {q2}
• Transition table

symbols
0 1

q0 {q0,q1} {q0}
states

q1 {q2}
Φ
q2
* Φ Φ
Example #4:
L = {w|w is a binary string s.t. the 2nd symbol is a 1}

What was the


NFA?

What was the


DFA?

9
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

Transitions into a dead state are implicit


Moves on a Chessboard
 States = squares.
 Inputs (alphabet) = r (move to an adjacent
red square) and b (move to an adjacent
black square).
 Start state (1), final state (9) are in
opposite corners.
1 2 3
4 5 6
7 8 9
Moves on a Chessboard
symbols

1 2 3 r b
1 {2,4} {5}
4 5 6 2 {4,6} {1,3,5}
3 {2,6} {5}
7 8 9

states
4 {2,8} {1,5,7}
5 {2,4,6,8} {1,3,7,9}
6 {2,8} {3,5,9}
r b b 7 {4,8} {5}
1 2 1 5 8 {4,6} {5,7,9}
4 3 1 *9 {6,8} {5}
5 3
7 7
9 Accept, since final state reached
Extension of δ to NFA Paths
Extended Transition Function *
Basis:  * (q,  )  {q}

Induction:
• Let  * (q0, w)  {p1, p2 , pk}
•  * (pi, a)  Si for i=1,2...,k

• Then,  * (q0, wa)  S U S U … U S


1 2 k
Extended Transition Function  *

 * q0 , a   q1

q4 q5
a a
q0 a q1 b q2  q3

14

𝛿 𝑞0 , 𝑎𝑎 = {𝑞4 , 𝑞5 }

q4 q5
a a
q0 a q1 b q2  q3

15
 * q0 , ab   q2 , q3 , q0 

q4 q5
a a
q0 a q1 b q2  q3

16
Language of an NFA
 An NFA accepts w if there exists at least
one path from the start state to an
accepting (or final) state that is labeled by
w

L(N) = { w | δ*(q0,w) ∩ F ≠ Φ }
The Language of an NFA M
F  q0 ,q5 
q4 q5
a a
q0 a q1 b q2  q3

 * q0 , aa   q4 , q5 aa  L(M )


F
18
F  q0 ,q5 
q4 q5
a a
q0 a q1 b q2  q3

 * q0 , ab   q2 , q3 , q0  ab  LM 


F
19
F  q0 ,q5 
q4 q5
a a
q0 a q1 b q2  q3

 * q0 , abaa   q4 , q5 aaba  L(M )


F
20
F  q0 ,q5 
q4 q5
a a
q0 a q1 b q2  q3

 * q0 , aba   q1 aba  LM 


F
21
q4 q5
a a
q0 a q1 b q2  q3

LM     ab*  ab* {aa}

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

 Micron Automata Processor (introduced in 2013)


 2D array of MISD (multiple instruction single
data) fabric w/ thousands to millions of processing
elements.
 1 input symbol = fed to all states (i.e., cores)
 Non-determinism using circuits
Example of equivalent machines
NFA M1
LM1   {10} * 0
q0 q1
1

DFA M2 0,1
LM 2   {10} * 0
q0 q1 1 q2
1
0
25
Equivalence of Machines

Definition for Automata:

Machine M1 is equivalent to machine M2

if L M1   L M 2 

26
Equivalence of DFA & NFA
Theorem (2 Theorems):
Should be A language L is accepted by a DFA if and only if it is
true for accepted by an NFA.
any L

Proof:
1. If part: (1st Theorem)
 A language L is accepted by a DFA if it is accepted by an NFA
 Prove by showing every NFA can be converted to an equivalent
DFA (in the next few slides…)

2. Only-if part : (2nd Theorem, is trivial)


 A language L is accepted by a NFA if it is accepted by a DFA
 Every DFA is a special case of an NFA where each state has
exactly one transition for every input symbol. Therefore, if L
is accepted by a DFA, it is accepted by a corresponding NFA.
Proof for the if-part
 If-part: A language L is accepted by a DFA if it is
accepted by an NFA
 rephrasing…
 Given any NFA N, we can construct a DFA D such
that L(N) = L(D)

 How to convert an NFA into a DFA?


 Observation: In an NFA, each transition maps to a
subset of states
Idea: Represent each “subset of NFA states”  a single
“DFA state”
Subset construction
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:
Notice that the input alphabets of the two automata are the same,
and the start state of D is the set containing only the start state of N.

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
NFA to DFA construction: Example
L = {w | w ends in 01} Idea: To avoid enumerating all of
power set, do
“lazy creation of states”

1 0

NFA: DFA: 0 1
{q0} {q0,q1} {q0,q2}
0,1 0
1
0 1
q0 q1 q2
δD 0 1 δD 0 1
Ø Ø Ø {q0} {q0,q1} {q0}
δN 0 1
{q0} {q0,q1} {q0} {q0,q1} {q0,q1} {q0,q2}
q0 {q0,q1} {q0}
{q1} Ø {q2} *{q0,q2} {q0,q1} {q0}
q1 Ø {q2}
* {q2} Ø Ø
*q2 Ø Ø
{q0,q1} {q0,q1} {q0,q2}
0. Enumerate all possible subsets
* {q0,q2} {q0,q1} {q0}
1. Determine transitions
* {q1,q2} Ø {q2}
2. Retain only those states
* {q0,q1,q2} {q0,q1} {q0,q2} reachable from {q0}
NFA to DFA: Repeating the example using
LAZY CREATION
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}
{q0,q1} {q0,q1} {q0,q2}
q1 Ø {q2}
*q2 Ø Ø *{q0,q2} {q0,q1} {q0}

Main Idea:
Introduce states as you go
(on a need basis)
Example #2 Subset Construction

Fig1. NFA Fig2. Transition table

3 δ a b
b a
a 1 {1,2,3,4,5} {4,5}
a
a 2 b 2 {3} {5}
a,b
1 5 {2}
3 ∅
a,b a 4 {5} {4}
4

b
*5 ∅ ∅
δ a b δ a b
1 {1,2,3,4,5} {4,5} {1} {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} {5} {4}
3 ∅ {2} {2,4,5} {3,5} {4,5}
4 {5} {4} {5} ∅ ∅
{4} {5} {4}
*5 ∅ ∅
{3,5} ∅ {2}
∅ ∅ ∅
{2} {3} {5}
{3} ∅ {2}

33
a

δ a b
b a
12345 245
{1} {1,2,3,4,5} {4,5}
35
a
*{1,2,3,4,5} {1,2,3,4,5} {2,4,5} a,b a
b
a b
*{4,5} {5} {4} ∅
1 3
*{2,4,5} {3,5} {4,5} a,b b
b a
*{5} ∅ ∅
a 2
{4} {5} {4}
45 5 b
*{3,5} ∅ {2}
b a
4
∅ ∅ ∅
{2} {3} {5}
b
{3} ∅ {2}

34
A bad case
#states(DFA) >> #states(NFA)
 L = {w | w is a binary string s.t., the kth symbol
from its end is a 1}

 NFA has k+1 states

 But an equivalent DFA needs to have at least 2k states

(Pigeon hole principle)


 m holes and >m pigeons
at least one hole has to contain two or more pigeons
“pigeons” are the squence of n bits, the “pigeonholes” are the
states
A bad case
#states(DFA) >> #states(NFA)
 L = {w | w is a binary string s.t., the 3th symbol
from its end is a 1}

?
A few subtle 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.
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 the transition table
Example of an -NFA
L = {w | w is empty, or if non-empty will end in 01}

0,1  -closure of a state q,


ECLOSE(q), is the set
0 1
q0 q1 q2 of all states (including
 itself) that can be
start q’0 reached from q by
repeatedly making an
δE 0 1  arbitrary number of -
*q’0 Ø Ø {q’0,q0} ECLOSE(q’0) transitions.
q0 {q0,q1} {q0} {q0} ECLOSE(q0)
q1 Ø {q2} {q1} ECLOSE(q1)
* q2 Ø Ø {q2} ECLOSE(q2)

39
To simulate any transition:
Example of an -NFA
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
q0 q1 q2
 Simulate for w=101:
start q’0
q0’
 
q0’ q0
δE 0 1  1
1
ECLOSE(q’0)
*q’0 Ø Ø {q’0,q0} q0
Ø
q0 {q0,q1} {q0} {q0} ECLOSE(q0) x 0
q1 Ø {q2} {q1} q1 q0
1
* q2 Ø Ø {q2}
q2 q0
x
40
To simulate any transition:
Step 1) Go to all immediate destination states.
Step 2) From there go to all their -closure states as well.
Example of another -NFA
0,1 Simulate for w=101:
q0
0
q1
1
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}
41
Equivalency of DFA, NFA, -NFA
 Theorem: A language L is accepted by some
-NFA if and only if L is accepted by some DFA

 Implication:

DFA ≡ NFA ≡ -NFA


(all accept Regular Languages)

If: A language L is accepted by a


DFA if it is accepted by an - NFA
Eliminating -transitions
Let E = {QE,∑,δE,q0,FE} be an -NFA
Goal: To build DFA D={QD,∑,δD,{qD},FD} s.t. L(D)=L(E)
Construction:
1. QD = all reachable subsets of QE factoring in -closures
2. qD = ECLOSE(q0)
3. FD = subsets S in QD s.t. S∩FE ≠ Ø
4. δD: for each subset S of QE and for each input symbol a∑:
 Let R = U δE(p,a) // go to destination states
p in S

 δD(S,a) = U ECLOSE(r) // from there, take a union


r in R of all their -closures

Reading: Section 2.5.5 in book


Example: -NFA  DFA

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

0,1

0 1
q0 q1 q2

start q’0

δE 0 1
 δD 0 1

*q’0 Ø Ø {q’0,q0} *{q’0,q0}


q0 {q0,q1} {q0} {q0} …
q1 Ø {q2} {q1}
*q2 Ø Ø {q2}
44
Example: -NFA  DFA

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

0,1 0
0
0 1 {q0,q1}
q0 q1 q2 0 1 {q0,q2}
 0
1
start start {q’0, q0} 1
q’0 q0 1
ECLOSE union
δE 0 1
 δD 0 1

*q’0 Ø Ø {q’0,q0} *{q’0,q0} {q0,q1} {q0}


q0 {q0,q1} {q0} {q0} {q0,q1} {q0,q1} {q0,q2}
q1 Ø {q2} {q1} {q0} {q0,q1} {q0}
*q2 Ø Ø {q2} *{q0,q2} {q0,q1} {q0}
45
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
Examples (NFA)
L ={w| w accepts any binary string that
contains 00 or 11 as a substring. }

47

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy