1 Dfa
1 Dfa
Deterministic
Finite Automata
(DFA)
From Wikipedia:
An automaton is a self-operating machine, or a
machine or control mechanism designed to
automatically follow a predetermined sequence of
operations, or respond to predetermined instructions.
Finite Automaton
Input tape
String
Output
Finite String
Automaton
Finite Accepter
Input
String
Output
“Accept”
Finite
or
Automaton
“Reject”
Transition Graph/Diagram for a DFA
Input string: abba a, b
q5
a a, b
b a b accept state: double
q0 a q1 b q2 b q3 a q4 circle
initial final/accept
state state
transition
state
Initial Configuration
Input String
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Reading the Input
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Input finished
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4 Final state
Input finished AND Final state
è
Output: “accept”
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Rejection
a b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Input finished
a b a
a, b
Non-final state
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Input finished AND Non-Final state
è Output: “reject”
a b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Another input here the input is lambda
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a, b
Output:
“reject” q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Does the following DFA accept the input
string aab? accepted
a a b
a a, b
q0 b q1 a, b q2
a a b
a a, b
q0 b q1 a, b q2
a a b
a a, b
q0 b q1 a, b q2
Input finished
a a b
a a, b
Output: “accept”
q0 b q1 a, b q2
Does M accept the string bab?
b a b
a a, b
q0 b q1 a, b q2
b a b
a a, b
q0 b q1 a, b q2
b a b
a a, b
q0 b q1 a, b q2
Input finished
b a b
a a, b
Output: “reject” trap state.
a state which is not a final
state and from which it can
q0 b q1 a, b q2 not escape
Formal definition of DFA 𝑀 = (𝑄, Σ, 𝛿, 𝑞) , 𝐹)
deterministic machine:
Deterministic Finite Accepter/Automaton 𝑀 : for each node there should be
an outgoing arrow for each
• 𝑄 : States (a finite set of states) alphabets. But should not be 2
arrows for single alphabet.
• Σ : Alphabet (a finite set of input symbols)
• 𝛿 : Transition function (𝛿: 𝑄×Σ → 𝑄)
• 𝑞) : Initial state (an element of 𝑄)
• 𝐹 : Final states (a subset of 𝑄)
Input Alphabet Σ
Σ = {𝑎, 𝑏}
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
Set of States 𝑄 infinity > |Q| >= 1
𝑄 = {𝑞) , 𝑞3 , 𝑞4 , 𝑞5 , 𝑞6 , 𝑞7 }
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Initial State 𝑞)
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Final State(s) 𝐹
𝐹 = {𝑞6 } a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Transition Function 𝛿
𝛿: 𝑄×Σ → 𝑄
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
COMP 335 33
𝛿 𝑞) , 𝑎 = 𝑞3
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
𝛿 𝑞) , 𝑏 = 𝑞7
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
d (q2 , b ) = q3
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
Transition Function 𝛿
d a b
q0 q1 q5
q1 q5 q2 a, b
q2 q5 q3
q3 q4 q5 q5
q4 q5 q5 b a a, b
a b
q5 q5 q5 q0 a q1 b q2 b q3 a q4
∗ ∗
Extended Transition Function 𝛿 : 𝑄×Σ → 𝑄
Informal definition: 𝛿 ∗ (q, w) is the state in which the DFA is, after
it processes string w, starting from state q.
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
d * (q0 , ab ) = q2
shai star (q1 , abba) = q4
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
d * (q0 , abba ) = q4
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
shai * (q2 , ba) = q4
d * (q0 , abbbaa ) = q5
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
Observation: there is a walk from 𝑞 to 𝑞′ with label 𝑤
if and only if understanding this is very
𝛿 ∗ 𝑞, 𝑤 = 𝑞′ enough, recursive defn is just
formality
q w q¢
𝑤 = 𝜎3 𝜎4 … 𝜎A
s1 s2 sk
q q¢
Example: there is a walk from 𝑞) to 𝑞7 with label 𝑎𝑏𝑏𝑏𝑎𝑎
𝛿 ∗ 𝑞) , 𝑎𝑏𝑏𝑏𝑎𝑎 = 𝑞7
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
Recursive Definition
𝛿 ∗ 𝑞, 𝜆 = 𝑞 base case
𝛿 ∗ 𝑞, 𝑤𝜎 = 𝛿 𝛿 ∗ 𝑞, 𝑤 , 𝜎
q w q1 s q¢
𝛿 ∗ 𝑞, 𝑤 = 𝑞3 𝛿 𝑞3 , 𝜎 = 𝑞′
𝛿 ∗ 𝑞, 𝑤𝜎 = 𝑞′
𝛿 ∗ 𝑞) , 𝑎𝑏 =
𝛿 𝛿 ∗ 𝑞) , 𝑎 , 𝑏 =
𝛿 𝛿 𝛿 ∗ 𝑞) , 𝜆 , 𝑎 , 𝑏 =
𝛿 𝛿 𝑞) , 𝑎 , 𝑏 =
𝛿 𝑞3 , 𝑏 =
a, b 𝑞4
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
Languages Accepted by DFAs
The language accepted by a DFA 𝑀 is denoted L(M) and is the set of all
strings accepted by 𝑀. every string that is not here, is rejected
2 conditions: 1) accept what is here, 2) reject what is not
𝐿 𝑀 is the set of all strings that drive M to a Tinal state.
𝐿 𝑀 = 𝑤 ∈ Σ ∗ ∶ 𝛿 ∗ 𝑞) , 𝑤 ∈ 𝐹
a, b
q5 trap state
b a a , b
a b
q0 a q1 b q2 b q3 a q 4
accept
Another Example 𝐿 𝑀 = {𝜆, 𝑎𝑏, 𝑎𝑏𝑏𝑎}
a, b
q5
b a a a, b
b
q0 a q1 b q2 b q3 a q4
accept accept accept
Consider the DFA M given below.
What is L(M)? = { a^n b : n>=0}
Q: if L1 = {a^2n b : n>= 0}
does M accepts L1?
NO, because it does not reject L1(compliment)
a a, b
q0 b q1 a, b q2
q0 a q1 b q2
b a accept
dfa can not do infinity
q3 a, b memory, but can do finite
memory
Design a DFA M such that:
L(M) = 𝑤 ∶ 𝑤 contains the substring 001
to accept the compliment: accept the non accepted states and do not accept the accepted
states
Design a DFA M such that:
L(M) = 𝑤 ∶ 𝑤 does not contain the substring 001
1 0 0,1
1
l 0 1
0 00 001
0
Regular Languages
Definition: A language 𝐿 is regular if there is a DFA 𝑀 that accepts 𝐿:
𝐿 = 𝐿(𝑀)
For each of the above languages we can design a DFA that accepts it.
∗
Show that the language 𝐿 = 𝑎𝑤𝑎 ∶ 𝑤 ∈ 𝑎, 𝑏 is regular.
in this kind of question, we need to design a DFA.
a a
There are languages that are not regular.
Example: 𝐿 = {𝑎` 𝑏 ` ∶ 𝑛 ≥ 0}
As we will learn later, there is no DFA for this and (infinitely) many
other languages.
Review questions
• What is a Deterministic Finite Automaton (DFA)?