0% found this document useful (0 votes)
41 views19 pages

Deterministic Finite State Automata: Sipser Pages 31-46

The document defines deterministic finite automata (DFAs) and discusses their key components: states, alphabet, transition function, start state, and accepting states. It provides examples of DFAs and how they accept strings by tracing a path through the state diagram. DFAs can be represented as a data structure and algorithms can precisely implement the acceptance mechanism by checking if the final state of the input string's path is accepting. Exercises are provided to design DFAs for specific languages over given alphabets.

Uploaded by

ammar hussein
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)
41 views19 pages

Deterministic Finite State Automata: Sipser Pages 31-46

The document defines deterministic finite automata (DFAs) and discusses their key components: states, alphabet, transition function, start state, and accepting states. It provides examples of DFAs and how they accept strings by tracing a path through the state diagram. DFAs can be represented as a data structure and algorithms can precisely implement the acceptance mechanism by checking if the final state of the input string's path is accepting. Exercises are provided to design DFAs for specific languages over given alphabets.

Uploaded by

ammar hussein
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/ 19

Deterministic Finite State Automata

Sipser pages 31-46


Formal Definition

• A DFA is a quintuple A = (Q,Σ,δ,q0,F)


where

– Q is a set of states
– Σ is the alphabet (of input symbols)
– δ: Q × Σ → Q is the transition function
– q0 ∈ Q -- the start state
– F ⊆ Q -- final states

– Page 35 of Sipser
Example

• In our example,
• Q={q0,q1,q2},
Σ={0,1}, 1 1
q0=q0, q0 0 q1 0 q2 0,1
F={q2},
• and
δ is given by 6 equalities
• δ(q0,0)=q1,
• δ(q0,1)=q0,
• δ(q2,1)=q2
• …
Transition Table

• All the information presenting a DFA can be given by a single


thing -- its transition table:
0 1

Q0 Q1 Q0

Q1 Q2 Q1

*Q2 Q2 Q2

• The initial and final states are denoted by → and *


respectively.
Language of accepted Strings
• A DFA = (Q,Σ,δ,q0,F), accepts a string
• w = “w1w2…wn” iff

– There exists a sequence of states [r0, r1, … rn]


with 3 conditions
1. r0 = q0
2. δ(ri,wi+1) = ri+1 Acceptance is about
3. rn+1 ∈ F finding a sequence.

How do we find such


Page 40 in Sisper a sequence?
Example
• Show that “ABAB” is accepted.

• Here is a path [0,0,1,2,2]


– The first node, 0, is the start state.
– The last node, 2, is in the accepting states
– The path is consistent with the transition
• δ0A=0

• δ0B=1 Note that the path is one


longer than the string
• δ1A=2

• δ 2B=2
Definition of Regular Languages
• A language is called regular if some finite
automaton accepts (i.e. a DFA accepts it)

• Page 40 in Sipser
Extension of δ to Strings
• Given a state q and a string w, there is a unique path labeled w
that starts at q (why?). The endpoint of that path is denoted
δ(q,w)

• Formally, the function δ : Q × Σ* → Q


• is defined recursively:

– δ(q,ε)=q
– δ(q,x:xs)= δ(δ(q,x),xs)

• Note that δ(q,”a”)= δ(q,a) for every a∈Σ;


• so δ does extend δ.
Example trace

• Diagrams (when available) make it very easy


to compute δ(q,w) --- just trace the path
labeled w starting at q.

• E.g. trace 101 on the diagram below starting


at q1 1 1

q0 0 q1 0 q2 0,1
Implementation and precise arguments need
the formal definition.

δ(q1,101)= δ( δ(q1,1) ,01)


= δ(q1,01)
= δ( δ(q1,0) ,1)
= δ(q2,1)
= δ( δ(q2,0) ,ε)
= δ(q2,ε)
= q2 0 1
→q0 q1 q0
q1 q2 q1
δ(q,ε)=q
*q2 q2 q2
δ(q,x:xs)= δ(δ(q,x),xs)
Language of accepted strings - take 2

A DFA =(Q,Σ, δ,q0,F) accepts a string w iff δ(q0,w)∈ F

The language of the automaton A is


L(A)={w | A accepts w}.

More formally
L(A)={w | δ(Start(A),w) ∈ Final(A)}

Example:
Find a DFA whose language is the set of all strings over {a,b,c}
that contain aaa as a substring.
DFA’s as data structures
data DFA q s =
DFA [q] -- states
[s] -- symbols
(q -> s -> q) -- delta
q -- start state
[q] -- accept states

Note that the States and Symbols can be any type.


Programming for acceptance 1
path:: Eq q => DFA q s -> q -> [s] -> [q]
path d q [] = [q]
path d q (s:ss) = q : path d (trans d q s) ss

acceptDFA1 :: Eq a => DFA a t -> [t] -> Bool


acceptDFA1 dfa w = cond1 p && cond2 p && cond3 w p
where p = path dfa (start dfa) w
w =“w1w1…wn”
cond1 (r:rs) = (start dfa) == r Iff there exists a
cond1 [] = False sequence of states
[r0, r1, … rn]
cond2 [r] = elem r (accept dfa)
cond2 (r:rs) = cond2 rs 1. r0 = q0
cond2 _ = False 2. δ(ri,wi+1) = ri+1
3. rn ∈ F
cond3 [] [r] = True
cond3 (w:ws) (r1:(more@(r2:rs))) =
(trans dfa r1 w == r2) && (cond3 ws more)
cond3 _ _ = False
Programming for acceptance 2
-- δ = deltaBar
deltaBar :: Eq q => DFA q s -> q -> [s] -> q
deltaBar dfa q [] = q
deltaBar dfa q (s:ss) =
deltaBar dfa (trans dfa q s) ss

acceptDFA2 dfa w =
elem (deltaBar dfa (start dfa) w)
(accept dfa)
An Example

d1 :: DFA Integer Integer


d1 = DFA states symbol trans start final
where states = [0,1,2]
symbol = [0,1]
trans p a = (2*p+a) `mod` 3
start = 0
final = [2]
d1 = DFA states symbol trans start final
where states = [0,1,2]
symbol = [0,1]
trans p a = (2*p+a) `mod` 3
start = 0
final = [2]

DFA Q {0, 1, 2}
Sigma {0, 1}
Delta 0 0 -> 0
0 1 -> 1
1 0 -> 2
1 1 -> 0
2 0 -> 1
2 1 -> 2
q0 0
Final {2}
Missing alphabet
• I sometimes draw a state transition diagram
where some nodes do not have an edge
labeled with every letter of the alphabet, by
convention we add a new (dead) state where
all missing edges terminate.
1

A
B

2
Review
• DFAs are a computation mechanism
• They compute whether some string is in some language

• Several mechanisms can be defined that describe


how they compute. All essentially trace a path
through the state diagram.

• DFAs can be represented as a data structure

• Precise algorithms can be defined that implement


the computation mechanisms.
Exercises
• Define a DFA for the following languages

• {w | w has at least 3 a’s and at least 2 b’s}


• Σ = {a,b}

• {w | length w = 3 }
• Σ = {m,n,p}

• {w | w represents an integer }
• Σ = {a,b,c,0,2,1}

You might also like