Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lecture 5-FSMs-NFA-2-DFA

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 62

Theory of Computation

Lecture 5
NFA to DFA
2 methods

1
Syllabus and Terminologies
 Regular Languages .. Regular Sets
 REs (Regular Expressions)
 FSMs (or FSA/FA) … Finite State Machines/Automata
 DFA vs. NFA … Deterministic vs. Non-deterministic FSA
 Comparison and conversion
 Examples & Closure Operations
 Pumping Lemma
 Context Free Languages
 CFGs … Context Free Grammars
 PDA … Push Down Automata
 Parsing: CFG generating strings vs. PDA recognizing strings
 Turing Machine
05/11/2024 FCI-CU-EG 2
How to convert NFA to DFA??
 High-level sketch
NFA

Regular
expressions DFA

Lexical Table-driven
Specification Implementation of DFA

3
How to convert from NFA to DFA
 Create a Graph GD with vertex {q0}
 Repeat until no more edges are missing

 Take any vertex {qi, qj, .. qk} of GD that has no outgoing

edge
 Compute *(qi, a), *(qj, a).. *(qk, a)
 Form a union of all these * {ql, qm, .. qn}
 Create a vertex for GD labeled {ql, qm, .. qn}, if does not

exist
 Add an edge a from {qi, qj, .. qk} to {ql, qm, .. qn}
 Any vertex has q  F is identified as a final state
f N
 If M accepts , the vertex {q } in G is a final state
N 0 D

4
NFA to DFA conversion intuition
0, 1

1 0
NFA: q0 q1 q2

0 0

1 0
DFA: q0 q0 or q1 q0 or q2

1 1

5
NFA to DFA conversion intuition
0, 1

1 0
NFA: q0 q1 q2

0 0

1 0
DFA: q0 {q0, q1} {q0, q2}

1 1

6
NFA to DFA

 We are given an NFA M

 We want to convert it to an equivalent DFA M


LM   L(M )

7
Procedure NFA to DFA

1. Initial state of NFA: q0

 Initial state of DFA: q0 

8
Example
a

NFA
q0 a q1  q2
b

DFA
q0 
9
Procedure NFA to DFA

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


 Compute in the NFA
 * qi , a ,
 * q j , a ,
 {qi , qj ,..., qm
}
...
 Add transition
 {qi , q j ,..., qm }, a   {qi , qj ,..., qm
}

10
Example
a
 NFA
q0 a q1  q2
b
 * (q0 , a )  {q1, q2 }

q0  a
DFA q1, q2 
 q0 , a   q1, q2 

11
Procedure NFA to DFA

 Repeat Step 2 for all letters in alphabet, until no


more transitions can be added.

12
NFA to DFA
a

NFA
q0 a q1  q2
b
DFA
q0  a
q1, q2 
b 
13
NFA to DFA

a
NFA

q0 a q1  q2
b a
DFA
q0  a
q1, q2 
b


14
NFA to DFA
a

NFA
q0 a q1  q2
b
b a
DFA
q0  a
q1, q2 
b


15
NFA to DFA
a

NFA
q0 a q1  q2
b
b a
DFA
q0  a
q1, q2 
b

 a, b
16
NFA to DFA … where is the final state?
a

NFA
q0 a q1  q2
b
b a
DFA
q0  a
q1, q2 
b

 a, b
17
NFA to DFA
a

NFA
q0 a q1  q2
b a
DFA b

q0  a
q1, q2 
b

 a, b
18
Procedure NFA to DFA
{qi , q j ,..., qm }
3. For any DFA state

If some q j is a final state in the NFA

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


is a final state in the DFA

19
Example
a
q0 a q1  q2

q1  F
NFA b
a
b
DFA a
q0  q1, q2 
b q1, q2  F 
 a, b
20
Theorem

Take NFA M
 Apply procedure to obtain DFA M

Then M and M are equivalent :

LM   LM 

21
Finally

We have


Languages Languages
accepted accepted
by NFAs by DFAs

22
NFA to DFA
Formal Graphical Method

23
NFA to DFA

 To transfer from NFA to DFA we need some


method for eliminating both:

 or ε transitions, and
 Multiple transitions from a state on a single input character.
 Eliminating ε transitions involves the construction
of ε-closures .
 ε–closure of a single state s ( s ): is the set of
states reachable by a series of zero or more
ε transitions.

24
ε–closure

ε
In this NFA: 1
ε
2 a 3 ε 4
 1 = {1,2,4}
ε
 2 = {2}

 3 = {2,3,4}

 4 = {4}

25
NFA to DFA Method
 Input: An NFA S = States = { s0, s1, ..., sN} = SNFA
 δ = Transition function = TransitionNFA
δ: (Q x Σ) –> 2Q -2Q is the power set of Q, the set of all subsets of Q
δ(q,s) is a function from Q x S to 2Q (but not to Q)
 Output: A DFA S = States = {?, ?, ..., ?} = S
DFA

 f = Transition function = TransitionDFA


f: Q x Σ→Q
 Main Idea: Each state in SDFAwill be a set of states from the NFA
 SDFA = { {...}, {...} , ..., {...}}
q1
a
q0 q0
a a
q2
26
NFA DFA
NFA to DFA method
We’ll use...
 Transition
NFA(S,a) the transition function from NFA
 ε-Closure(I) where I is a single state from NFA
 ε-Closure(S) where S is a set of states from NFA

We’ll construct...
 S the set of states in the DFA
DFA

Initially, we’ll set SDFA to {}


 Add X to SDFA where X is some set of NFA states
Example: “Add {q0, q1, q2} to SDFA”
We’ll “mark” some of the states in the DFA.
Marked = “We’ve done this one”
Unmarked= “Still need to do this one”
27
example

 Start state:
ε-Closure (0) = {0, 1, 2, 4, 7}
 Call it A

A
 A is the first node in the equivalent DFA

28
Example CONT.

 Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
 Call it A

a ?
A
b ?

29
Example CONT.

 Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
 Transition
DFA(A,a)

=
a ?
A
 TransitionDFA(A,b)
b ?
=

30
Example CONT.

 Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
 Transition
DFA(A,a)

= ε-Closure (TransitionNFA(A,a))
= a ?
A
 TransitionDFA(A,b)
b ?
=

31
Example CONT.

 Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
 Transition
DFA(A,a)

= ε-Closure (TransitionNFA(A,a))
= ε-Closure ({3,8}) a ?
= A
 TransitionDFA(A,b)
b ?
=

32
Example CONT.

 Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
 Transition
DFA(A,a)

= ε-Closure (TransitionNFA(A,a))
= ε-Closure ({3,8}) a B
= {1,2,3,4,6,7,8} = B A
 TransitionDFA(A,b)
b ?
=

33
Example CONT.

 Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
 Transition
DFA(A,a)

= ε-Closure (TransitionNFA(A,a))
= ε-Closure ({3,8}) a B
= {1,2,3,4,6,7,8} = B A
 TransitionDFA(A,b)
b ?
= ε-Closure (TransitionNFA(A,b))
=

34
Example CONT.

 Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
 Transition
DFA(A,a)

= ε-Closure (TransitionNFA(A,a))
= ε-Closure ({3,8}) a B
= {1,2,3,4,6,7,8} = B A
 TransitionDFA(A,b)
b ?
= ε-Closure (TransitionNFA(A,b))
= ε-Closure ({5})
=

35
Example CONT.

 Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
 Transition
DFA(A,a)

= ε-Closure (TransitionNFA(A,a))
= ε-Closure ({3,8}) a B
= {1,2,3,4,6,7,8} = B A
 TransitionDFA(A,b)
b C
= ε-Closure (TransitionNFA(A,b))
= ε-Closure ({5})
= {1,2,4,5,6,7} = C

36
Example CONT.

 Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
 Transition
DFA(A,a)

= ε-Closure (TransitionNFA(A,a))
= ε-Closure ({3,8}) a B
= {1,2,3,4,6,7,8} = B A
 TransitionDFA(A,b)
b C
= ε-Closure (TransitionNFA(A,b))
= ε-Closure ({5})
= {1,2,4,5,6,7} = C

37
Example CONT.

 Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
 Transition
DFA(A,a)

= ε-Closure (TransitionNFA(A,a))
= ε-Closure ({3,8}) a B
= {1,2,3,4,6,7,8} = B A
 TransitionDFA(A,b) b C
= ε-Closure (TransitionNFA(A,b))
= ε-Closure ({5})
= {1,2,4,5,6,7} = C A is now done; mark it!
B and C are unmarked.
 So far: Let’s do B next...
38
Example CONT.

 Process B = {1,2,3,4,6,7,8}

a B
A
b C

39
Example CONT.

 Process B = {1,2,3,4,6,7,8}
 TransitionDFA(B,a)
=

a B
A
b C

40
Example CONT.

 Process B = {1,2,3,4,6,7,8}
 TransitionDFA(B,a)
= ε-Closure (TransitionNFA(B,a))
=
a B
A
b C

41
Example CONT.

 Process B = {1,2,3,4,6,7,8}
 TransitionDFA(B,a)
= ε-Closure (TransitionNFA(B,a))
= ε-Closure ({3,8})
= a B
A
b C

42
Example CONT.

 Process B = {1,2,3,4,6,7,8}
 TransitionDFA(B,a)
= ε-Closure (TransitionNFA(B,a))
= ε-Closure ({3,8})
= {1,2,3,4,6,7,8} = B

 TransitionDFA(B,b)
=

43
Example CONT.

 Process B = {1,2,3,4,6,7,8}
 TransitionDFA(B,a)
= ε-Closure (TransitionNFA(B,a))
= ε-Closure ({3,8})
= {1,2,3,4,6,7,8} = B

 TransitionDFA(B,b)
= ε-Closure (TransitionNFA(B,b))
=

44
Example CONT.

 Process B = {1,2,3,4,6,7,8}
 TransitionDFA(B,a)
= ε-Closure (TransitionNFA(B,a))
= ε-Closure ({3,8})
= {1,2,3,4,6,7,8} = B

 TransitionDFA(B,b)
= ε-Closure (TransitionNFA(B,b))
= ε-Closure ({5,9})
=

45
Example CONT.

 Process B = {1,2,3,4,6,7,8}
 TransitionDFA(B,a)
= ε-Closure (TransitionNFA(B,a))
= ε-Closure ({3,8})
= {1,2,3,4,6,7,8} = B

 TransitionDFA(B,b)
= ε-Closure (TransitionNFA(B,b))
= ε-Closure ({5,9})
= {1,2,4,5,6,7,9} = D

46
Example CONT.

 Process B = {1,2,3,4,6,7,8}
 TransitionDFA(B,a)
= ε-Closure (TransitionNFA(B,a))
= ε-Closure ({3,8})
= {1,2,3,4,6,7,8} = B

 TransitionDFA(B,b)
= ε-Closure (TransitionNFA(B,b))
= ε-Closure ({5,9})
= {1,2,4,5,6,7,9} = D

47
Example CONT.

 Process C = {1,2,4,5,6,7}

48
Example CONT.

 Process C = {1,2,4,5,6,7}
 TransitionDFA(C,a) =
 TransitionDFA(C,b) =

49
Example CONT.

 Process C = {1,2,4,5,6,7}
 TransitionDFA(C,a) = {1,2,3,4,6,7,8} = B
 TransitionDFA(C,b) =

50
Example CONT.

 Process C = {1,2,4,5,6,7}
 TransitionDFA(C,a) = {1,2,3,4,6,7,8} = B
 TransitionDFA(C,b) = {1,2,4,5,6,7} = C

51
Example CONT.

 Process C = {1,2,4,5,6,7}
 TransitionDFA(C,a) = {1,2,3,4,6,7,8} = B
 TransitionDFA(C,b) = {1,2,4,5,6,7} = C

52
Example CONT.

 Process C = {1,2,4,5,6,7}
 TransitionDFA(C,a) = {1,2,3,4,6,7,8} = B
 TransitionDFA(C,b) = {1,2,4,5,6,7} = C

 Process D = {1,2,4,5,6,7,9}
 TransitionDFA(D,a) =
 TransitionDFA(D,b) =

53
Example CONT.

 Process C = {1,2,4,5,6,7}
 TransitionDFA(C,a) = {1,2,3,4,6,7,8} = B
 TransitionDFA(C,b) = {1,2,4,5,6,7} = C

 Process D = {1,2,4,5,6,7,9}
 TransitionDFA(D,a) = {1,2,3,4,6,7,8} = B
 TransitionDFA(D,b) =

54
Example CONT.

 Process C = {1,2,4,5,6,7}
 TransitionDFA(C,a) = {1,2,3,4,6,7,8} = B
 TransitionDFA(C,b) = {1,2,4,5,6,7} = C

 Process D = {1,2,4,5,6,7,9}
 TransitionDFA(D,a) = {1,2,3,4,6,7,8} = B
 TransitionDFA(D,b) = {1,2,4,5,6,7,10} = E

55
Example CONT.

 Process C = {1,2,4,5,6,7}
 TransitionDFA(C,a) = {1,2,3,4,6,7,8} = B
 TransitionDFA(C,b) = {1,2,4,5,6,7} = C

 Process D = {1,2,4,5,6,7,9}
 TransitionDFA(D,a) = {1,2,3,4,6,7,8} = B
 TransitionDFA(D,b) = {1,2,4,5,6,7,10} = E

56
Example CONT.

 Process E = {1,2,4,5,6,7,10}
 TransitionDFA(E,a) =
 TransitionDFA(E,b) =

57
Example CONT.

 Process E = {1,2,4,5,6,7,10}
 TransitionDFA(E,a) = {1,2,3,4,6,7,8} = B
 TransitionDFA(E,b) =

58
Example CONT.

 Process E = {1,2,4,5,6,7,10}
 TransitionDFA(E,a) = {1,2,3,4,6,7,8} = B
 TransitionDFA(E,b) = {1,2,4,5,6,7} = C

59
Example CONT.

 Process E = {1,2,4,5,6,7,10}
 TransitionDFA(E,a) = {1,2,3,4,6,7,8} = B
 TransitionDFA(E,b) = {1,2,4,5,6,7} = C

Final States in DFA?


...which state(s) contain 10?

60
Final DFA

61
NFA vs. DFA !

62

You might also like