0% found this document useful (0 votes)
14 views103 pages

Lect 04

Uploaded by

1026884611shc
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)
14 views103 pages

Lect 04

Uploaded by

1026884611shc
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/ 103

XJCO2321 Formal Languages and

Finite Automata

04: Non-Deterministic Finite


Automata
Non-deterministic Finite Automaton (NFA)

Alphabet = {a}

q1 a q2
a
q0
a
q3

Based on slides by Costas Busch 1


Alphabet = {a}

Two choices a
q1 q2 No transition
a
q0
a
q3 No transition

Based on slides by Costas Busch 2


First Choice

a a

q1 a q2
a
q0
a
q3

Based on slides by Costas Busch 3


First Choice

a a

q1 a q2
a
q0
a
q3

Based on slides by Costas Busch 4


First Choice
All input is consumed
a a

q1 a q2 “accept”
a
q0
a
q3

Based on slides by Costas Busch 5


Second Choice

a a

q1 a q2
a
q0
a
q3

Based on slides by Costas Busch 6


Second Choice
Input cannot be consumed
a a

q1 a q2
a
q0 Automaton Halts
a
q3 “reject”

Based on slides by Costas Busch 7


A NFA accepts a string:
if there is a computation path of the NFA
that accepts the string

all the symbols of input string are processed


and
the last is an accepting state

Based on slides by Costas Busch 8


aa is accepted by the NFA:

“accept”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3 q3 “reject”
because this
this computation
computation
is ignored
accepts aa
Based on slides by Costas Busch 9
Rejection example

q1 a q2
a
q0
a
q3

Based on slides by Costas Busch 10


First Choice

a
“reject”
q1 a q2
a
q0
a
q3

Based on slides by Costas Busch 11


Second Choice

q1 a q2
a
q0
a
q3

Based on slides by Costas Busch 12


Second Choice

q1 a q2
a
q0
a
q3 “reject”

Based on slides by Costas Busch 13


Another Rejection example

a a a

q1 a q2
a
q0
a
q3

Based on slides by Costas Busch 14


First Choice

a a a

q1 a q2
a
q0
a
q3

Based on slides by Costas Busch 15


First Choice
Input cannot be consumed
a a a

q1 a q2 “reject”
a
q0
a
Automaton halts
q3

Based on slides by Costas Busch 16


Second Choice

a a a

q1 a q2
a
q0
a
q3

Based on slides by Costas Busch 17


Second Choice
Input cannot be consumed
a a a

q1 a q2
a
q0 Automaton halts
a
q3 “reject”

Based on slides by Costas Busch 18


A NFA rejects a string:
if there is no computation of the NFA
that accepts the string.

For each possible computation path:


• All the input is consumed and the
automaton is in a non accepting state

OR
• The input cannot be consumed
Based on slides by Costas Busch 19
a is rejected by the NFA:

“reject”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3 “reject” q3

All possible computations lead to rejection


Based on slides by Costas Busch 20
aaa is rejected by the NFA:

“reject”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3 q3 “reject”

All possible computations lead to rejection


Based on slides by Costas Busch 21
Language accepted: L = {aa}

q1 a q2
a
q0
a
q3

Based on slides by Costas Busch 22


Epsilon Transitions

q0 a q1  q2 a q3

Based on slides by Costas Busch 23


a a

q0 a q1  q2 a q3

Based on slides by Costas Busch 24


a a

q0 a q1  q2 a q3

Based on slides by Costas Busch 25


input tape head does not move

a a

q0 a q1  q2 a q3

Automaton changes state

Based on slides by Costas Busch 26


all input is consumed

a a

“accept”

q0 a q1  q2 a q3

String aa is accepted
Based on slides by Costas Busch 27
Rejection Example

a a a

q0 a q1  q2 a q3

Based on slides by Costas Busch 28


a a a

q0 a q1  q2 a q3

Based on slides by Costas Busch 29


(read head doesn’t move)

a a a

q0 a q1  q2 a q3

Based on slides by Costas Busch 30


Input cannot be consumed

a a a

Automaton halts
“reject”

q0 a q1  q2 a q3

String aaa is rejected


Based on slides by Costas Busch 31
Language accepted: L = {aa}

q0 a q1  q2 a q3

Based on slides by Costas Busch 32


Another NFA Example

q0 a q1 b q2  q3


Based on slides by Costas Busch 33
a b

q0 a q1 b q2  q3


Based on slides by Costas Busch 34
a b

q0 a q1 b q2  q3


Based on slides by Costas Busch 35
a b

“accept”

q0 a q1 b q2  q3


Based on slides by Costas Busch 36
Another String

a b a b

q0 a q1 b q2  q3


Based on slides by Costas Busch 37
a b a b

q0 a q1 b q2  q3


Based on slides by Costas Busch 38
a b a b

q0 a q1 b q2  q3


Based on slides by Costas Busch 39
a b a b

q0 a q1 b q2  q3


Based on slides by Costas Busch 40
a b a b

q0 a q1 b q2  q3


Based on slides by Costas Busch 41
a b a b

q0 a q1 b q2  q3


Based on slides by Costas Busch 42
a b a b

“accept”

q0 a q1 b q2  q3


Based on slides by Costas Busch 43
Language accepted

L = ab, abab, ababab, ...


= abab
*

q0 a q1 b q2  q3


Based on slides by Costas Busch 44
Another NFA Example

0
q0 q1 0, 1 q2
1

Based on slides by Costas Busch 45
Language accepted

L =  , 10, 1010, 101010, ...


= 10
*

0
q0 q1 0, 1 q2
1 (redundant
state)

Based on slides by Costas Busch 46
Simple Automata

M1 M2
q0 q0

L( M 1 ) = {} L( M 2 ) = { }

Based on slides by Costas Busch 47


NFAs are interesting because we can
express languages easier than DFAs

NFA M1 DFA M2 a
q2
q0 a q1
a
q0 a q1

L( M1 ) = {a} L( M 2 ) = {a}
Based on slides by Costas Busch 48
Formal Definition of NFAs

M = (Q, ,  , q0 , F )

Q: Set of states, i.e. q0 , q1, q2 


: Input aplhabet, i.e. a, b  
: Transition function

q0 : Initial state

F: Accepting states
Based on slides by Costas Busch 49
Transition Function 

 (q, x ) = q1 , q2 ,, qk 

q1
x resulting states reached
q x
q2 by following one transition
x
with input symbol x

qk

Based on slides by Costas Busch 50


States reachable from q0 scanning 1

 (q0 , 1) = q1

0
q0 q1 0, 1 q
2
1

Based on slides by Costas Busch 51
States reachable from q1 scanning 0

 (q1,0) = {q0 , q2 }

0
q0 q1 0, 1 q
2
1

Based on slides by Costas Busch 52
States reachable from q0 with one transition
scanning no input symbol

 (q0 ,  ) = {q2 }

0
q0 q1 0, 1 q
2
1

Based on slides by Costas Busch 53
States reachable from q2 scanning 1

 (q2 ,1) = 

0
q0 q1 0, 1 q
2
1

Based on slides by Costas Busch 54
*
Extended Transition Function 
Similar with  but applied on strings

 (q0 , a ) = q1
*

q4 q5
a a
q0 a q1 b q2  q3

Based on slides by Costas Busch 55
States reachable from q0 scanning aa

 (q0 , aa ) = q4 , q5 
*

q4 q5
a a
q0 a q1 b q2  q3

Based on slides by Costas Busch 56
States reachable from q0 scanning ab

 (q0 , ab ) = q2 , q3 , q0 
*

q4 q5
a a
q0 a q1 b q2  q3

Based on slides by Costas Busch 57
Special case:

for any state q

q   (q,  )
*

Based on slides by Costas Busch 58


In general
q j   (qi ,w ) : there is a walk from qi to q j
*

with label w

qi w qj

w =  1 2  k
1 2 k
qi qj

Based on slides by Costas Busch 59


The Language of an NFA M
The language accepted by M is:
L(M ) = w1 , w2 ,..., wn 

Where for each wm


 (q0 ,w m ) = {qi ,..., qk , , q j }
*

and there is some qk  F (accepting state)


Based on slides by Costas Busch 60
w m  L (M )
 (q0 ,w m )
*

qi
wm

q0 w
qk qk  F
m

wm qj

Based on slides by Costas Busch 61


F = q0 ,q5 
q4 q5
a a
q0 a q1 b q2  q3

 (q0 , aa ) = q4 , q5 
*
aa  L(M )
F
Based on slides by Costas Busch 62
F = q0 ,q5 
q4 q5
a a
q0 a q1 b q2  q3

 (q0 , ab ) = q2 , q3 , q0 
*
ab  L(M )
F
Based on slides by Costas Busch 63
F = q0 ,q5 
q4 q5
a a
q0 a q1 b q2  q3

 (q0 , abaa ) = q4 , q5 


*
abaa  L(M )
F
Based on slides by Costas Busch 64
F = q0 ,q5 
q4 q5
a a
q0 a q1 b q2  q3

 * (q0 , aba ) = q1 aba  L(M )


F
Based on slides by Costas Busch 65
q4 q5
a a
q0 a q1 b q2  q3

L(M ) = ab  ab {aa}


* *

Based on slides by Costas Busch 66


NFAs accept the Regular
Languages
Equivalence of Machines

Definition:

Machine M1 is equivalent to machine M2

if L( M1 ) = L( M 2 )

Based on slides by Costas Busch 68


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
Based on slides by Costas Busch 69
Theorem:

=
Languages
Regular
accepted
Languages
by NFAs
Languages
accepted
by DFAs

NFAs and DFAs have the same computation power,


namely, they accept the same set of languages
Based on slides by Costas Busch 70
Proof: we need to show

Languages
accepted  Regular
Languages
by NFAs
AND
Languages
accepted  Regular
Languages
by NFAs

Based on slides by Costas Busch 71


Proof-Step 1

Languages
accepted  Regular
Languages
by NFAs

Every DFA is trivially a NFA

Any language L accepted by a DFA


is also accepted by a NFA
Based on slides by Costas Busch 72
Proof-Step 2

Languages
accepted  Regular
Languages
by NFAs
Any NFA can be converted to an
equivalent DFA

Any language L accepted by a NFA


is also accepted by a DFA
Based on slides by Costas Busch 73
Conversion of NFA to DFA
NFA M
a
q0 a q1  q2
b

DFA M
q0 

Based on slides by Costas Busch 74


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

DFA M
q0  a
q1, q2 

Based on slides by Costas Busch 75


 * (q0 , b ) =  empty set

NFA M a
q0 a q1  q2
b

DFA M
q0  a
q1, q2 
b

 trap state
Based on slides by Costas Busch 76
 (q1 , a ) = {q1 , q2 }
*

NFA M a  * (q2 , a ) = 
q0 a q1  q2 union

b q1, q2 

a
DFA M
q0  a
q1, q2 
b


Based on slides by Costas Busch 77
 (q1 , b ) = {q0 }
*

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


q0 a q1  q2
union

b q0 

a
DFA M b

q0  a
q1, q2 
b


Based on slides by Costas Busch 78
NFA M a
q0 a q1  q2
b

a
DFA M b

q0  a
q1, q2 
b

 a, b trap state
Based on slides by Costas Busch 79
END OF CONSTRUCTION

NFA M a
q0 a q1  q2 q1  F
b
a
DFA M b

q0  a
q1, q2 
q1, q2  F 
b

 a, b
Based on slides by Costas Busch 80
General Conversion Procedure

Input: an NFA M

Output: an equivalent DFA M 


with L(M ) = L(M )

Based on slides by Costas Busch 81


The NFA has states q0 , q1, q2 ,...

The DFA has states from the power set


, q0 , q1 , q0 , q1 , q1 , q2 , q3 , ....

Based on slides by Costas Busch 82


Conversion Procedure Steps

step

1. Initial state of NFA: 0


q
 (q0 ,  ) = q0 ,
*

Initial state of DFA: q0 , 

Based on slides by Costas Busch 83


Example
a
 (q0 ,  ) = q0 
*

NFA M
q0 a q1  q2
b

DFA M
q0 

Based on slides by Costas Busch 84


step
2. For every DFA’s state {qi , q j ,..., qm }

compute in the NFA


 * (qi , a )
Union
  * (q j , a )
= {qk , ql,..., qn }
...
  * (qm , a )

add transition to DFA


 ({qi , q j ,..., qm }, a ) = {qk , ql,..., qn }
Based on slides by Costas Busch 85
Example  * (q0 , a) = {q1, q2 }
NFA M a
q0 a q1  q2
b

DFA M
q0  a
q1, q2 
 (q0 , a ) = q1, q2 
Based on slides by Costas Busch 86
step
3. Repeat Step 2 for every state in DFA and
symbols in alphabet until no more states
can be added in the DFA

Based on slides by Costas Busch 87


Example
NFA M a
q0 a q1  q2
b

a
DFA M b

q0  a
q1, q2 
b

 a, b
Based on slides by Costas Busch 88
step

4. For any DFA state {qi , q j ,..., qm }

if some qj is accepting state in NFA

Then, {qi , q j ,..., qm }


is accepting state in DFA

Based on slides by Costas Busch 89


Example
NFA M a
q0 a q1  q2 q1  F
b
a
DFA M b

q0  a
q1, q2 
q1, q2  F 
b

 a, b
Based on slides by Costas Busch 90
Lemma:
If we convert NFA M to DFA M 
then the two automata are equivalent:
L( M ) = L( M  )

Proof:
We need to show: L( M )  L( M  )
AND
L( M )  L( M  )
Based on slides by Costas Busch 91
First we show: L( M )  L( M  )

We only need to prove:

w L(M ) w  L(M )

Based on slides by Costas Busch 92


NFA
Consider w L(M )

q0 w qf

symbols
w = a1a2  ak
a1 a2 ak q
q0 f

Based on slides by Costas Busch 93


symbol
ai
qi qj

denotes a possible sub-path like

symbol

qi
  ……  ai  ……  q
j

Based on slides by Costas Busch 94


We will show that if w L(M )

w = a1a2  ar
a1 a2 ar q
NFA M: q0 f

then

a1 a2 ar
DFA M:
{q0 , } {q f ,}
state
w  L(M ) state
label label
Based on slides by Costas Busch 95
More generally, we will show that if in M:
(arbitrary prefix) v = a1a2  an nr
a1 a2 an
NFA M: q0 qi qj ql qm

then

M:
a1 a2 an
DFA
{q0 , } {qi ,} {q j ,} {ql ,} {qm ,}

Based on slides by Costas Busch 96


Proof by induction on |v|

Induction Basis: |v |= 1 v = a1

a1
NFA M: q0 qi

M:
a1
DFA
{q0 , } {qi ,}

is true by construction of M 

Based on slides by Costas Busch 97


Induction hypothesis: 1 | v | k
v = a1a2  ak

Suppose that the following hold


a1 a2 ak
NFA M: q0 qi qj qc qd

M:
a1 a2 ak
DFA
{q0 , } {qi ,} {q j ,} {qc ,} {qd ,}

Based on slides by Costas Busch 98


Induction Step: | v |= k + 1
v = a1a2  ak ak +1 = vak +1
 
v
Then this is true by construction of M 
a1 a2 ak ak +1
NFA M: q0 qi qj qc qd qe

v
M:
a1 a2 ak ak +1
DFA
{q0 , } {qi ,} {q j ,} {qc ,} {qd ,} {qe ,}

Based on slides by Costas Busch


v 99
Therefore if w L(M )
w = a1a2  ar
a1 a2 ar
q0 qf
NFA M:
then

a1 a2
M:
ar
DFA
{q0 , } {q f ,}
w  L(M )
Based on slides by Costas Busch 100
We have shown: L( M )  L( M  )

With a similar proof


we can show: L( M )  L( M  )

Therefore: L(M ) = L(M )

END OF PROOF
Based on slides by Costas Busch 101
Follow-Up / Further Reading
• Read Chapter 1.2 of Sipser
• Read Chapter 2.2, 2.3, 2.4, and 2.5 of
Wilson

Based on slides by Costas Busch 102

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