0% found this document useful (0 votes)
17 views35 pages

Lecture 08

Uploaded by

Muhammad Faisal
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)
17 views35 pages

Lecture 08

Uploaded by

Muhammad Faisal
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/ 35

Language Theory and Finite Automata

Lecture 08

Non-deterministic Finite Automata


Today Topics
1. Non-deterministic Finite Automata (NFA)
2. Language Acceptance and Transition Function
3. Examples, NFA, FA and TG
4. NFA Applications
5. NFA with Null string (NFA-Λ), examples
6. Proof of equivalence of FA and NFA
7. Construction of FA from RE
8. Memory Management
9. Distinguishable and Indistinguishable Strings
1. NFA –Informal Definition
• NFA is a TG with unique start state and a property
of having single letter as label of transitions.
• An NFA is a collection of
– Finite many states
– Unique initial state
– Some final states
– Finite set of input letters, e.g., Σ = {a, b, c}
– Transition function showing where to move if a
letter is input. There may be more than one or
may not be transition(s) for a certain letter
1. NFA-Formal Definition
• An NFA is a quintuple A=(Q,,s,F,), where the first
four components are as in a DFA, and the transition
function takes values in P(Q) instead of Q. Thus

 Q   P(Q)

• The extension   Q   P(Q) is defined by

(q,)={q}
(q,ua) is the union of the sets
(p,a), where p varies over all states in
(q,u),
i.e.,
p (q,u) (p,a),
2. NFA Language Acceptance

• An NFA accepts a string w iff (s,w)


contains a final state. The language of an
NFA N is the set L(N) of accepted
strings:
• L(N) = {w | (s,w)  F  }
2. Computing Transition Function (q0,000)
(q,ua) = (q,u) (p,a)
p
0
w = 000; F = {q0} q0 0
q1 0,1
q2 0,1
q3

1
(q0,000) = (x,0)
x(q0,00)
(q0,00) = (y,0)
y(q0,0)
(q0,0) = (q0,0) = {q0,q1}
(q0,00) = (y,0)
y{q0,q1}
(q0,00) = {q0,q1}  {q2} = {q0,q1,q2}
(q0,000)= (x,0)
x{q0,q1,q2}
(q0,000)={q0,q1}  {q2}  {q3}
(q0,000)={q0,q1,q2,q3}
(qo,w)  F = {q0,q1,q2,q3}  {q3}  
3. NFA- Examples
Example 1 Note that below NFA accepts language
of strings, defined over Σ = {a, b}, containing ‘aa’.
a,b a,b

1- a 2 a 3+

• Example 2 It is to be noted that the below NFA


accepts language consisting of a and ab.
2+
a
a
1-

3
a b 5+
3. Examples: FA to NFA
Example 1
• Consider following FA corresponding to (a + b)*b

a b b

- +
a

• The above FA may be equivalent to following NFA


a, b

– b +
3. Examples: FA to NFA
Example 2
Consider the following FA

b a a,b
a
– 1 +
b

The above FA may be equivalent to the following NFA

a, b a, b

– a 1 a +
3. Relationship between NFA, FA and TG

• It is to be noted that every FA can be


considered to be an NFA as well , but the
converse may not true.
• It may also be noted that every NFA can be
considered to be a TG as well, but the
converse may not true.
4. NFA-Application
Application of an NFA
Application of an
NFA in artificial intelligence
- 1 2 3
a a a
- 1 2 3 4 L 5 O
a a a 6 M 7 P
a a a a
8 N 9 +
4 5

a a a a
a a a
6 8 7 9 +
a a a
5. NFA- Λ
• In NFA, if Λ is allowed to be a label of an edge
then the NFA is called NFA with Λ (NFA-Λ ).

• An NFA is a collection of
1. Finite many states
2. Unique initial state
3. Some final states
4. Finite set of input letters, e.g., Σ = {a, b, c}
5. Finite set of transitions as in NFA. In addition
null (Λ) transitions are allowed
5. NFA- Λ
Example
• NFA with Null string which accepts the language of
strings, defined over Σ = {a, b}, ending in b.

a, b
b Λ
– 1 +

• Every FA may be considered to be an NFA-Λ as


well, but the converse may not true.

• Similarly every NFA-Λ may be considered to be a


TG as well, but the converse may not true
6. Conversion of FA to NFA and Vice Versa
Theorem:
Every FA can be converted to NFA and Vice Versa

Part I: Every FA is NFA


Part II: NFA to FA
Two methods are there
Proof (Method 1)
• An NFA is TG
• By Kleene’s theorem,
TG produces RE
• Hence NFA gives RE
• RE produces FA
• Hence NFA produces
FA
6. NFA to FA
Method 2
• In NFA, there may be more than one or no
transition for a certain letter
• Hence starting from initial state corresponding
to the initial state of given NFA
• The transition diagram of the corresponding FA,
can be built introducing an empty state for a letter
having no transition at certain state and
• A state corresponding to the combination of states,
for a letter having more than one transitions.
6. NFA to FA
Example 1 (Method 2)

1- a
2
2
b
a b
b 4+ a
1- 4+ a a, b

b a
3 b
3  a, b
6. NFA to FA
Example 2 (Method 2)
b

b b
1- b 2 3 4+

b b
1- 2 b
(3,4)+
a 4+
a a a,b

a, b
6. Transition Table: NFA to FA
a, b a, b

b x2 b x3 +
x1 -

New States after reading


Old States
a b

z1 - = x 1 x1 = z 1 (x1 ,x2 ) = z2

z2 = (x1 ,x2 ) (x1 , ) = x1 = z1 (x1 ,x2 ,x3 ) = z3

z3+ = (x1 ,x2 ,x3 ) (x1 ,x3 ) = z4 (x1 ,x2 ,x3 ) = z3

z4+ = (x1 ,x3 ) (x1 ,x3 ) = z4 (x1 ,x2 ,x3 ) = z3

b a
a b a
z1 - z2 b z3 + z4 +
a b
7. Construction of FA from RE
Method:
•Method is discussed considering the example.
Example
• To construct the FAs for the languages L1 = {a},
L2 = {b} and L3 = {Λ}
Step 1: Build NFA1, NFA2 and NFA3
corresponding to L1, L2 and L3 , respectively as
shown below
a b +/-
- + - +

Dr. Nazir Ahmad Zafar


7. Construction of FA from RE
Step 2:
• As discussed earlier for every NFA there is FA
equivalent to it, hence there must be FAs for the
above mentioned NFAs. The corresponding FAs
can be considered as follows


a + – b +
a,b
a,b a,b
a a,b
b +/ - 1
1
1
a,b
a,b Dr. Nazir Ahmad Zafar
7. Construction of FA from RE
Union, concatenation and closure of FAs
• NFA can be built corresponding to
1. union,
2. concatenation and
3. closure
of FAs corresponding to REs

• Then convert NFA to an equivalent FA

• Procedure is next
7.1. NFA corresponding to Union of FAs

• We are given FAs


• Introduce a new start state and connect it
with the states originally connected with the
old start state with the same transitions as
old start state,
• Then remove the -ve sign of old start state.
• This creates non-determinism and hence
results is an NFA.
7.1. NFA corresponding to Union of FAs
• FA1 NFA = FA1  FA2
b

1
a x2
x- a
a b b a -
b
a b a b b a
x3 x1 a x2
x4 +
b a b x4 + a x3

• FA2 b
a a, b
a,b y1 b y2+
a
b y2+
y1-
7.2. NFA corresponding to Concatenation of FAs

Cases
1.FA1 accepts null
2.FA2 accepts null
3.Both FA1 and
FA2 accept null
7.2. NFA corresponding to Concatenation of FAs
Case 1: FA1 may accept null
• Introduce additional transitions for each letter
connecting each final state of the first FA with the
states of second FA that are connected with the initial
state of second FA corresponding to each letter of the
alphabet.
• Remove the +ve sign of each of final states of first FA
• Remove –ve sign of the initial state of second FA.
• It will create non-determinism at final states of first FA
• Hence NFA, thus obtained, will be the required NFA.
7.2. NFA corresponding to Concatenation of FAs
• NFA = FA1FA2 2 a 4
b a,b a a
a b a,b

a 1 a b 6+
p- q r+ –
b b a b

33 b 5

2 a 4
a a a
b a a,b b a,b

pp- b q a 1 a 6+
r b
- a b
b
b

3 b 5 Dr. Nazir Ahmad Zafar


7.2. NFA corresponding to Concatenation of FAs
Case 2: FA2 may accept null
• If FA1 accepts Null string then every string
accepted by FA2 must be accepted by
concatenation of FAs. This situation is
accommodated in above method.
• If second FA2 accepts Null string, then every
string accepted by FA1 must be accepted by
required FA
• This can be done introducing new transitions at
final states of FA1, the +ve sign of these states
will not be removed.
Dr. Nazir Ahmad Zafar
7.2. NFA corresponding to Concatenation of FAs
• NFA = FA1FA2 (FA2 accepts Null string)
b a,b a, b
a
p- q a x y
b r+

a, b
b a,b
a
p- q a
b r +

a, b
a, b

x+ y

a,Dbr. Nazir Ahmad


Zafar
7.2. NFA corresponding to Concatenation of FAs
• Case 3: Both FA1 and FA2 accepts Null strings
FA1 FA2 b
a, b 1 3
x y
b a a
a a
a, b
b
FA1FA2 2 4
b
b
a,
 y b 3
b 1+
a, b
b a a a a
a
b
2 4

b
7.3. NFA corresponding to the Closure of an FA
Method
• Introduce an initial state which should be final
as well
• Connect it with the states originally connected
with old start state with same transitions as old
start state and remove –ve sign of old start state
• Introduce new transitions, for each letter, at
each of final states, including new final state,
with those connected with the old start state.
• This creates non-determinism and hence results
in the required NFA.
7.3. NFA corresponding to the Closure of an FA
Example 1
Introducing new Initial state to find FA Closure
b a a, b

1- 2 a 3+
b

Closure of FA a
a a, b
b a
± b 2 a
1 3+
b

b
7.3. NFA corresponding to the Closure of an FA
Example 2
Introducing new Initial state to find FA Closure
b a a
Given FA
1 2+

b
NFA which is closure of FA
a
b a
a
± b 1 2+

b
7.3. NFA corresponding to the Closure of an FA
Example 3 a,

Given FA b 1± a, b a, b
2+ 3

Closure of
FA
• It can be observed that FA* not only accepts the
Null string but every other string as well.
• We don’t need separate initial and final state.
• Hence an NFA corresponding to FA* may be
a, b

a, b 2+a, b a, b
1± 3
7.3. NFA corresponding to the Closure of an FA
Example 4
Given FA Closure of FA
q q

0 0 0 0
0 0 0,1
0, 1

p- 0 1 s+ ± p 0 1 s
+
1 1 1
1 1 1
r r
8. Memory required to recognize a language
Example 1  = {a, b}
• L = {w  {a, b}* | Length (w) ≥ 2  w neither ends in ab
nor in ba}

• Seven states required


to recognize the L

You might also like