Post on 04-Jul-2019
transcript
Finite Automata
Dr. Neil T. Dantam
CSCI-561, Colorado School of Mines
Fall 2018
Dantam (Mines CSCI-561) Finite Automata Fall 2018 1 / 43
Outline
Languages Review
Traffic Light Example
Deterministic Finite Automata
Nondeterministic Finite Automata
Dantam (Mines CSCI-561) Finite Automata Fall 2018 2 / 43
Languages Review
Outline
Languages Review
Traffic Light Example
Deterministic Finite Automata
Nondeterministic Finite Automata
Dantam (Mines CSCI-561) Finite Automata Fall 2018 3 / 43
Languages Review
Alphabets
Symbol: An abstract, primitive, atomic “thing”
I Examples: a, x, 4, θ, ℵ, !, ♥Set: An unordered collection, without repetition
Alphabet: A non-empty, finite set of symbols
I ΣB = {0, 1}I ΣE = {a, b, c , d , e, f , g , h, i , j , k, l ,m, n, o, p, q, r , s, t, u, v ,w , x , y , z}I ΣC = {♠,♥,♣,♦}
Dantam (Mines CSCI-561) Finite Automata Fall 2018 4 / 43
Languages Review
String
Sequence: An ordered list of objects
Example: (1, 2, 3, 5, 8, . . .)Abbreviated Notation: 12358 . . .Empty (zero-length) sequence: ε
String: A sequence over some alphabet
I ΓB = 101010I ΓE = helloI ΓC = ♣♥♦
Dantam (Mines CSCI-561) Finite Automata Fall 2018 5 / 43
Languages Review
Languages
Language: A set of strings
Examples: I Alternations of 0 and 1:{ε, 01, 10, 0101, 1010, 010101, 101010, . . .}
I 0s and 1s with no consecutive 1s:{ε, 0, 1, 00, 01, 10, 000, 001, 010, 100, 101, 0000, 0001, 0010, 0100, 0101, . . .}
Dantam (Mines CSCI-561) Finite Automata Fall 2018 6 / 43
Languages Review
Exercise: Languages
I Strings of 0s and 1s ending in 1:
{1, 01, 11, 001, 011, 101, 111, . . .}
I Strings 0s and 1s with an even number of 1s:
{ε, 0, 00, 11, 000, 011, 101, 011, . . .}
Dantam (Mines CSCI-561) Finite Automata Fall 2018 7 / 43
Traffic Light Example
Outline
Languages Review
Traffic Light Example
Deterministic Finite Automata
Nondeterministic Finite Automata
Dantam (Mines CSCI-561) Finite Automata Fall 2018 8 / 43
Traffic Light Example
Traffic Light Example
I State:I NS = {red, yellow, green}I EW = {red, yellow, green}I timeout = {0, 1}
I Sensors: Q = {timeout, ns-car, ew-car}
gr
grttimeout yrew-car
rgtimeout
rgttimeout
ry
ns-car
timeout
Dantam (Mines CSCI-561) Finite Automata Fall 2018 9 / 43
Traffic Light Example
Traffic Light w/ Pedestrian ExampleI State:
I NS = {red, yellow, green}, EW = {red, yellow, green}, timeout = {0, 1},I pedestrian = {0, 1}
I Sensors: Q = {timeout, ns-car, ew-car,pedestrian}
gr grttimeout
yrew-car
yrp
pedestrian
rgtimeout rgttimeout
ry
ns-car
ryppedestrian
timeout
rrptimeout
rrpttimeout
timeout
ns-car
ew-car
I More:I Left-turn arrowsI Railroad crossingsI Synchronize with other lights
Dantam (Mines CSCI-561) Finite Automata Fall 2018 10 / 43
Traffic Light Example
Transition Table
gr
grttimeout yrew-car
rgtimeout
rgttimeout
ry
ns-car
timeout
state timeout ns-car ew-car
gr grt ∅ ∅grt ∅ ∅ yryr rg ∅ ∅rg rgt ∅ ∅rgt ∅ ry ∅ry gr ∅ ∅
/∗∗ SWITCH CASE ∗∗/e v e n t = n e x t e v e n t ( ) ;switch ( s t a t e ) {case GR :
switch ( e v e n t ) {case TIMEOUT: s t a t e = GRT;
break ;d e f a u l t : s t a t e = DEAD;}/∗∗ . . . ∗∗/
}
/∗∗ LOOKUP TABLE ∗∗/e v e n t = n e x t e v e n t ( ) ;s t a t e = t a b l e [ s t a t e ] [ e v e n t ] ;
Dantam (Mines CSCI-561) Finite Automata Fall 2018 11 / 43
Deterministic Finite Automata
Outline
Languages Review
Traffic Light Example
Deterministic Finite Automata
Nondeterministic Finite Automata
Dantam (Mines CSCI-561) Finite Automata Fall 2018 12 / 43
Deterministic Finite Automata
Deterministic Finite Automata (DFA)
Definition: Deterministic Finite Automata (DFA)
A deterministic finite automaton is a 5-tuple: M = (Q,Σ, δ, q0,F ),where
I Q is a finite set call the states
I Σ is a finite set call the alphabet
I δ : Q × Σ 7→ Q is the transition function
I q0 ∈ Q is the start state
I F ⊆ Q is the set of accept states
Dantam (Mines CSCI-561) Finite Automata Fall 2018 13 / 43
Deterministic Finite Automata
Conceptual DFA Operation
σ = σ0 σ1 . . . σk−1︸ ︷︷ ︸history
σk σk+1 . . . σn︸ ︷︷ ︸future
tape
Finite ControlQ
{accept, reject}
M : Σ∗ 7→ {accept, reject}
accept Reach end of tape (string) withcontrol in accept state q ∈ F
reject Reach end of tape (string) withcontrol not in accept state q 6∈ F
language The language of M is the set ofstrings accepted by M,L (M) = {x | M(x) = accept}
Dantam (Mines CSCI-561) Finite Automata Fall 2018 14 / 43
Deterministic Finite Automata
Graphical DFA
states Q Graph Nodes
alphabet Σ Graph Edge Labels
transition function δ Graph Edges
start state q0 Designed graph node
accept states F Double-circled graph nodes
L (M) = {x ∈ {0, 1}∗ | x ends in 1}
q0 q1
start 0 11
0
state 0 1 Fq0 q0 q1 0q1 q0 q1 1
start
Dantam (Mines CSCI-561) Finite Automata Fall 2018 15 / 43
Deterministic Finite Automata
Example: FA Design
I L (M) ={w ∈ {0, 1}∗ | the number of 1s is odd}
I What state do we need to track?
I What are the state transitions?
I What are the start and accept states?
qeven qodd
qeven qodd
0 0
1
1
qeven qodd
start
0 0
1
1
Dantam (Mines CSCI-561) Finite Automata Fall 2018 16 / 43
Deterministic Finite Automata
Example: FA Design—continued
qeven qodd
qeven qodd
0 0
1
1
qeven qodd
start
0
state 0 1 Fqeven qeven qodd 0qodd qodd qeven 1
Dantam (Mines CSCI-561) Finite Automata Fall 2018 17 / 43
Deterministic Finite Automata
Exercise: FA Design
L (M) = {w ∈ {0, 1}∗ | w ends in a 1}
q0 q1start
1
0
0 1state 0 1 Fq0 q0 q1 0q1 q0 q1 1
Dantam (Mines CSCI-561) Finite Automata Fall 2018 18 / 43
Deterministic Finite Automata
Symbolic DFA Semantics
Transition Function δ : Q × Σ 7→ Q
Extended Transition Function δ̂ : Q × Σ∗ 7→ Q
base δ̂(q, ε) = qinductive For a ∈ Σ and ω ∈ Σ∗,
δ̂ (q, aω) = δ̂ (δ (q, a) , ω)
Accept δ̂(q0, ω) ∈ F
I M accepts ω
Reject δ̂(q0, ω) 6∈ F
I M rejects ω
Language L (M) ={ω ∈ Σ∗ | δ̂(q0, ω) ∈ F
}I M recognizes L (M)
Dantam (Mines CSCI-561) Finite Automata Fall 2018 19 / 43
Deterministic Finite Automata
Example: DFA Simulation
qeven qodd
start
0 0
1
1
state 0 1 Fqe qeven qodd 0qodd qodd qeven 1
I Simulate the DFA on the following strings:
1. ε
I δ̂ (qeven, ε) qeven
2. 01I δ̂ (qeven, 01) δ̂ (qeven, 1) δ̂ (qodd, ε) qodd
3. 010I δ̂ (qeven, 010) δ̂ (qeven, 10) δ̂ (qodd, 0) δ̂ (qodd, ε) qodd
Dantam (Mines CSCI-561) Finite Automata Fall 2018 20 / 43
Deterministic Finite Automata
Algorithm: DFA Simulation
Algorithm 1: DFA-Simulate(M)
Input: M = (Q,Σ, δ, q0,F ) ; // states,alphabet,transition,start,accept
Output: y ∈ {accept, reject}1 q ← q0;
/* Read next symbol on input tape */
2 σ ← next() ;3 while σ 6= EOF do
/* Follow state transition */
4 q ← δ(q, σ) ;/* Read next symbol on input tape */
5 σ ← next() ;
6 if q ∈ F then y ← accept;7 else y ← reject;
Dantam (Mines CSCI-561) Finite Automata Fall 2018 21 / 43
Deterministic Finite Automata
Exercise: DFA Simulation
q0 q1start
1
0
0 1state 0 1 Fq0 q0 q1 0q1 q0 q1 1
I Simulate the DFA on the following strings:
1. ε
I δ̂ (q0, ε) q0
2. 01
I δ̂ (q0, 01) δ̂ (q0, 1) δ̂ (q1, ε) q1
3. 010
I δ̂ (q0, 010) δ̂ (q0, 10) δ̂ (q1, 0) δ̂ (q0, ε) q0
Dantam (Mines CSCI-561) Finite Automata Fall 2018 22 / 43
Deterministic Finite Automata
Regular Languages
I The regular set is the set of languages that can be recognized by DFAsI R = {L (M) | M is a DFA}
I A language is a regular language if some DFA accepts itI (L ∈ R) ⇐⇒ ∃M, (L = L (M))
I What languages are not regular?
Dantam (Mines CSCI-561) Finite Automata Fall 2018 23 / 43
Nondeterministic Finite Automata
Outline
Languages Review
Traffic Light Example
Deterministic Finite Automata
Nondeterministic Finite Automata
Dantam (Mines CSCI-561) Finite Automata Fall 2018 24 / 43
Nondeterministic Finite Automata
Determinism vs. Nondeterminism
σ0 σ1 σ2 σ3 . . .
tape
q0 d1 d2 d3. . .
δ(q0, σ0) δ(d1, σ1) δ(d2, σ2) δ(d3, σ3)
q0 n10
...
n11
n20
...
...
...
Deterministic
Nondeterministic
Transition to single stateδdfa(qi , σ) = qj
Transition to a multiple statesδnfa(qi , σ) = {qj , qk , q`, . . .}
Dantam (Mines CSCI-561) Finite Automata Fall 2018 25 / 43
Nondeterministic Finite Automata
Example: NFA and Transition Table
q0 q1 q2startε ε
0 1 2
0’s, followed by 1’s, followed by 2’s
state 0 1 2 ε Fq0 {q0} ∅ ∅ {q1} 0q1 ∅ {q1} ∅ {q2} 0q2 ∅ ∅ {q2} ∅ 1
q0
q1 q2
start
b
aa, b
ε
a
state a b ε Fq0 ∅ {q1} {q2} 1q1 {q1, q2} {q2} ∅ 0q2 {q0} ∅ ∅ 0
Dantam (Mines CSCI-561) Finite Automata Fall 2018 26 / 43
Nondeterministic Finite Automata
Nondeterministic Finite Automata (NFA)
Definition: Nondeterministic Finite Automata (NFA)
A nondeterministic finite automaton is a 5-tuple: N = (Q,Σ, δ, q0,F ),where
I Q is a finite set call the states
I Σ is a finite set call the alphabet
I δ : Q × Σ 7→ P (Q) is the transition function
I q0 ∈ Q is the start state
I F ⊆ Q is the set of accept states
Dantam (Mines CSCI-561) Finite Automata Fall 2018 27 / 43
Nondeterministic Finite Automata
Exercise: NFA Transition Table
q0
q1
q2 q3
q4ε
ε
01
ε
0
1 ε
start
state 0 1 ε Fq0 ∅ ∅ {q1, q2} 0q1 {q4, q2} ∅ ∅ 0q2 ∅ {q1} {q3} 0q3 ∅ {q4} ∅ 0q4 ∅ ∅ {q3} 1
Dantam (Mines CSCI-561) Finite Automata Fall 2018 28 / 43
Nondeterministic Finite Automata
NFA vs. DFA
Given: NFA N = (QN ,Σ, δN , q0,N ,FN)
Find: DFA M = (QM ,Σ, δM , q0,N ,FM),such that L (M) = L (N)
Solution: DFA states are sets of NFA states
NFA DFAstates QN QM = P (QN)
alphabet Σ Σ
transition δN : QN × Σ 7→ P (QN) δM(Q, σ) , {q ∈ QN | σ-reachable from qi ∈ Q}start q0,N ∈ QN q0,M = {q ∈ QN | ε-reachable from q0,N}
accept FN ⊆ QN FM = {qm ∈ QM | qm ∩ FN 6= ∅}
Every NFA has an equivalent DFA
Dantam (Mines CSCI-561) Finite Automata Fall 2018 29 / 43
Nondeterministic Finite Automata
Practical usage of DFAs vs. NFAs
I Today’s physical computers are deterministicI Options:
1. Simulate NFA by tracking all possible current states2. Convert NFA to DFA by constructing the subsets of NFA state set
I Which option is better?
Dantam (Mines CSCI-561) Finite Automata Fall 2018 30 / 43
Nondeterministic Finite Automata
NFA Subset ConstructionSubroutines
ε–closure(q) States reachable from q on ε transitions
move-ε–closure(q, σ) States reachable from state q after reading symbol σ
Construct the reachable subsets of NFA states
Dantam (Mines CSCI-561) Finite Automata Fall 2018 31 / 43
Nondeterministic Finite Automata
Example: ε-closure
q0 q1 q2startε ε
0 1 2
I ε-closure(q0) = {q0, q1, q2}I ε-closure(q1) = {q1, q2}I ε-closure(q2) = {q2}
q0
q1 q2
start
b
aa, b
ε
a I ε-closure(q0) = {q0, q2}I ε-closure(q1) = {q1}I ε-closure(q2) = {q2}
States reachable from initial state on only ε transitionsDantam (Mines CSCI-561) Finite Automata Fall 2018 32 / 43
Nondeterministic Finite Automata
Algorithm: ε–closure
Algorithm 2: ε-closure(NFA,S,C)
Input: NFA, S, C ; // NFA, unvisited states, initial closure
Output: C’ ; // final closure
/* visit: P (Q)× Q 7→ P (Q) */
1 function visit(c,q) is
2 if q ∈ c then // base case, state q already in closure c
3 return c ;4 else // Recursive case, visit all ε-successors of state q
5 return ε−closure
NFA,
⋃
qε−→p∈NFA
p
︸ ︷︷ ︸ε-neighbors of q
,
{q} ∪ c︸ ︷︷ ︸add q to closure
;
6 C ′ ← fold-left(visit,C ,S) ;
Dantam (Mines CSCI-561) Finite Automata Fall 2018 33 / 43
Nondeterministic Finite Automata
Exercise: ε–closure
q0
q1
q2 q3
q4ε
ε
01
ε
0
1 ε
start
I ε-closure(q0) = {q0, q1, q2, q3}I ε-closure(q1) = {q1}I ε-closure(q2) = {q2, q3}I ε-closure(q3) = {q3}I ε-closure(q4) = {q3, q4}
Dantam (Mines CSCI-561) Finite Automata Fall 2018 34 / 43
Nondeterministic Finite Automata
Example: move-ε-closure
q0 q1 q2startε ε
0 1 2
I move-ε-closure(q0, 0) = {q0, q1, q2}I move-ε-closure(q0, 1) = {q1, q2}I move-ε-closure(q0, 2) = {q2}
q0
q1 q2
start
b
aa, b
ε
a I move-ε-closure(q0, a) = {q0, q2}I move-ε-closure(q1, a) = {q1, q2}I move-ε-closure(q2, a) = {q0, q2}
States reachable from initial state after reading one symbolDantam (Mines CSCI-561) Finite Automata Fall 2018 35 / 43
Nondeterministic Finite Automata
Algorithm: move-ε–closure
Algorithm 3: move-ε-closure(NFA,Q,σ)
Input: NFA, Q, σ ; // NFA, initial states, token
Output: C’ ; // reachable states
1 function visit(c,q) is
2 return ε−closure
NFA,
σ reachable from q︷ ︸︸ ︷⋃
qσ−→p∈NFA
p
, c
︸ ︷︷ ︸
ε-reachable after move
;
3 C ′ ← fold-left
visit, ∅, ε−closure(NFA,Q, ∅)︸ ︷︷ ︸ε-reachable from Q
;
Dantam (Mines CSCI-561) Finite Automata Fall 2018 36 / 43
Nondeterministic Finite Automata
Exercise: move–ε–closure
q0
q1
q2 q3
q4ε
ε
01
ε
0
1 ε
start
I move-ε-closure(q0, 0) = {q2, q3, q4}I move-ε-closure(q0, 1) = {q1, q3, q4}
Dantam (Mines CSCI-561) Finite Automata Fall 2018 37 / 43
Nondeterministic Finite Automata
Example 0: NFA to DFA
q0 q1 q2startε ε
0 1 2NFA
q0, q1, q2 q1, q2 q2start1 2
2
0
1
2DFA
Dantam (Mines CSCI-561) Finite Automata Fall 2018 38 / 43
Nondeterministic Finite Automata
Example 1: NFA to DFA
q0
q1 q2
start
b
aa, b
ε
a
NFA
q0, q2
q1 q2
q1, q2 q0, q1, q2
DFA
b
a
a
b
ab
a
a
b
start
Dantam (Mines CSCI-561) Finite Automata Fall 2018 39 / 43
Nondeterministic Finite Automata
Algorithm: NFA Simulation
Algorithm 4: NFA-Simulate(N)
Input: N = (Q,Σ, δ, q0,F ) ; // states,alphabet,transition,start,accept
Output: y ∈ {accept, reject}1 c ← ε-closure(q0);
/* Read next symbol on input tape */
2 σ ← next() ;3 while σ 6= EOF do
/* Follow state transition */
4 c ← move-ε-closure(N, c , σ) ;/* Read next symbol on input tape */
5 σ ← next() ;
6 if c ∩ F 6= ∅ then
7 y ← accept;8 else
9 y ← reject;
Dantam (Mines CSCI-561) Finite Automata Fall 2018 40 / 43
Nondeterministic Finite Automata
Algorithm: NFA to DFA Subset ConstructionFunctional Version
Algorithm 5: NFA-to-DFA: Functional
Input: N = (Q,Σ,E , q0,F ) ; // NFA states, alphabet, edges, start, accept
Output: M = (Q ′,Σ,E ′, q′0,F′) ; // DFA states, alphabet, edges, start, accept
1 Q ′ ← ∅; // Subsets
2 function visit-state(e,u) is
3 function visit-symbol(e,σ) is
4 u′ ← move-ε-closure(u, σ) ; // Find successor subset: uσ−→ u′
5 if u′ then return visit-state(e ∪
{u
σ−→ u′}, u′)
;
6 else return e ;
7 if u ∈ Q ′ then return e ; // Subset already constructed
8 else
9 Q ′ ← Q ′ ∪ {u};10 return fold-left(visit-symbol,Σ, e) ;
11 q′0 ← ε-closure(q0);12 E ′ ← visit-state(∅, q′0);13 F ′ = {q ∈ Q ′|q ∈ F};
Dantam (Mines CSCI-561) Finite Automata Fall 2018 41 / 43
Nondeterministic Finite Automata
Algorithm: NFA to DFA Subset ConstructionProcedural Version
Algorithm 6: NFA-to-DFA: Procedural
Input: N = (Q,Σ,E , q0,F ) ; // NFA states, alphabet, edges, start, accept
Output: M = (Q ′,Σ,E ′, q′0,F′) ; // DFA states, alphabet, edges, start, accept
1 q′0 ← ε-closure(q0) ;2 Q ′ ← {q′0};3 E ′ ← ∅ ;4 W ← {q′0}; // Work set
5 while W do // Construct Subsets
6 u ← pop(W );7 forall σ ∈ Σ do // Check for transition from Q ′
i on symbol σ
8 u′ = move-ε-closure(u, σ) ;9 if u′ then
10 if u′ 6∈ Q ′ then W ←W ∪ {u′} ; // Add to work list
11 Q ′ ← Q ′ ∪ {u′} ; // Add new subset
12 E ′ ← E ′ ∪{u
σ−→ u′}
; // Add new edge/transition
/* Accept States */
13 F ′ = {q ∈ Q ′|q ∈ F}
Dantam (Mines CSCI-561) Finite Automata Fall 2018 42 / 43