35
Finite Automata Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Fall 2017 Dantam (Mines CSCI-561) Finite Automata Fall 2017 1 / 35

Finite Automata - Neil T. Dantam · Deterministic Finite Automata Graphical DFA states Q Graph Nodes alphabet Graph Edge Labels transition function Graph Edges start state q 0 Designed

Embed Size (px)

Citation preview

Finite Automata

Dr. Neil T. Dantam

CSCI-561, Colorado School of Mines

Fall 2017

Dantam (Mines CSCI-561) Finite Automata Fall 2017 1 / 35

Outline

Dantam (Mines CSCI-561) Finite Automata Fall 2017 2 / 35

Languages Review

Outline

Dantam (Mines CSCI-561) Finite Automata Fall 2017 3 / 35

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 2017 4 / 35

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 2017 5 / 35

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 2017 6 / 35

Traffic Light Example

Outline

Dantam (Mines CSCI-561) Finite Automata Fall 2017 7 / 35

Traffic Light Example

Traffic Light Example

I State:I NS = {red, yellow, green}I EW = {red, yellow, green}I timeout = {0, 1}

I Sensors:

I Q = {timeout,ns-car, ew-car}

gr

grttimeout yrew-car

rgtimeout

rgttimeout

ry

ns-car

timeout

Dantam (Mines CSCI-561) Finite Automata Fall 2017 8 / 35

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 2017 9 / 35

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 ;defau 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 2017 10 / 35

Deterministic Finite Automata

Outline

Dantam (Mines CSCI-561) Finite Automata Fall 2017 11 / 35

Deterministic Finite Automata

Deterministic Finite Automata (DFA)

I A deterministic finite automaton is a 5-tuple: M = (Q,Σ, δ, q0,F ), whereI Q is a finite set call the statesI Σ is a finite set call the alphabetI δ : Q × Σ 7→ Q is the transition functionI q0 ∈ Q is the start stateI F ⊆ Q is the set of accept states

Dantam (Mines CSCI-561) Finite Automata Fall 2017 12 / 35

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 2017 13 / 35

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 2017 14 / 35

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 2017 15 / 35

Deterministic Finite Automata

Example: FA Design—continued

qeven qodd

qeven qodd

0 0

1

1

qeven qodd

start

0 0

1

1

state 0 1 Fqeven qeven qodd 0qodd qodd qeven 1

Dantam (Mines CSCI-561) Finite Automata Fall 2017 16 / 35

Deterministic Finite Automata

Symbolic DFA Semantics

Transition Function δ : Q × Σ 7→ Q

Extended Transition Function δ̂ : Q × Σ∗ 7→ Q

base δ̂(q, ε) = qinductive For ω ∈ Σ∗ and a ∈ Σ, δ̂(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 2017 17 / 35

Deterministic Finite Automata

Example: DFA Simulation

qeven qodd

start

0 0

1

1

state 0 1 Fqeven qeven qodd 0qodd qodd qeven 1

I Simulate the DFA on the following strings:I εI 01I 010I 0100

Dantam (Mines CSCI-561) Finite Automata Fall 2017 18 / 35

Deterministic Finite Automata

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

7 y ← accept;8 else

9 y ← reject;

Dantam (Mines CSCI-561) Finite Automata Fall 2017 19 / 35

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 2017 20 / 35

Nondeterministic Finite Automata

Outline

Dantam (Mines CSCI-561) Finite Automata Fall 2017 21 / 35

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 2017 22 / 35

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 2017 23 / 35

Nondeterministic Finite Automata

Nondeterministic Finite Automata (NFA)

I A nondeterministic finite automaton is a 5-tuple: N = (Q,Σ, δ, q0,F ), whereI Q is a finite set call the statesI Σ is a finite set call the alphabetI δ : Q × Σ 7→ P (Q) is the transition functionI q0 ∈ Q is the start stateI F ⊆ Q is the set of accept states

Dantam (Mines CSCI-561) Finite Automata Fall 2017 24 / 35

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= ∅}

Dantam (Mines CSCI-561) Finite Automata Fall 2017 25 / 35

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 2017 26 / 35

Nondeterministic Finite Automata

NFA subset construction outines

ε–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 2017 27 / 35

Nondeterministic Finite Automata

ε-closure examples

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 2017 28 / 35

Nondeterministic Finite Automata

ε–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

3 return c ;4 else // Recursive case

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 2017 29 / 35

Nondeterministic Finite Automata

move-ε-closure examples

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 2017 30 / 35

Nondeterministic Finite Automata

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 2017 31 / 35

Nondeterministic Finite Automata

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 2017 32 / 35

Nondeterministic Finite Automata

NFA to DFA Subset ConstructionAlgorithm 5: NFA-to-DFA

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} , E ′ ← ∅ ;

/* Construct Subsets */

3 i ← 0 ;4 while i < |Q ′| do

/* Finding outgoing edges of i th DFA state */

5 forall σ ∈ Σ do

/* Check for transition from Q ′i on symbol σ */

6 u = move−ε−closure(N,Q ′i , σ) ;7 if u then

8 Q ′ ← Q ′ ∪ u ; // Add new subset

9 E ′ ← E ′ ∪{Q ′i

σ−→ u}

; // Add new edge/transition

10 i ← i + 1;

/* Accept States */

11 F ′ = {q ∈ Q ′|q ∈ F}

Dantam (Mines CSCI-561) Finite Automata Fall 2017 33 / 35

Nondeterministic Finite Automata

NFA to DFA Example

q0 q1 q2startε ε

0 1 2NFA

q0, q1, q2 q1, q2 q2start1 2

2

0

1

2DFA

Dantam (Mines CSCI-561) Finite Automata Fall 2017 34 / 35

Nondeterministic Finite Automata

NFA to DFA Examples (cont.)

q0

q1 q2

start

b

aa, b

ε

a

NFA

q0, q2

q1 q2

q1, q2 q0, q2, q2

DFA

b

a

a

b

a b

aa

b

start

Dantam (Mines CSCI-561) Finite Automata Fall 2017 35 / 35