Deterministic Finite Automata (DFA) : Md. Rafsan Jani Assistant Professor Department of CSE Jahangirnagar University
Deterministic Finite Automata (DFA) : Md. Rafsan Jani Assistant Professor Department of CSE Jahangirnagar University
Theory of Computation
Lecture 02
• Definition of DFA
• Components of DFA
• Converting a problem to DFA
• Example of DFA
• Regular Language
2
Finite Automata
3
DFA
In DFA, for each input symbol, one can determine the state
to which the machine will move. Hence, it is called
Deterministic Automaton.
4
Observations
5
Formal definition of DFA
A DFA can be represented by a 5-tuple (Q, Σ, δ, q0, F), where:
6
Graphical Representation of a DFA
A DFA is represented by digraphs called state diagram.
The dead state is a special state from where for all possible input
symbol the transition repeats to itself.
7
Example: DFA accepting only string “abba”
a, b
q5
a, b
b a a b
q0 a q1 b q2 b q3 a q4
8
Set of States Q
Example
Q q0 , q1, q2 , q3 , q4 , q5
a, b
q5
a, b
b a a b
q0 a q1 b q2 b q3 a q4
9
Input Alphabet Σ
Example
a, b a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
10
Initial State q0
Example
a, b
q5
a, b
b a a b
q0 a q1 b q2 b q3 a q4
11
Set of Accepting States F such that F ⊆ Q
Example
F q4 a, b
q5
a, b
b a a b
q0 a q1 b q2 b q3 a q4
12
Transition Function δ : Q x Σ → Q
(q , x ) q
x
q q
13
Example:
q0 , a q1
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
14
q0 , b q5
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
15
q2 , b q3
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
16
Transition Table for δ
symbols
a b
q0 q1 q5
q1 q5 q2
states
q2 q5 q3
a,b
q3 q4 q5
q4 q5 q5 q5
q5 q5 q5 a,b
b a a b
q0 a q1 b q2 b q3 a q4
17
: Q * Q
^
(q, w) q
18
Special case:
19
Example:
^
^ ^
q0 , ab q0 , a , b q0 , , a , b q0 , a , b q1 , b q2
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
20
^
^ ^
q1 , bba q1 , bb , a q1 , b , b , a
^
q1 , , b , b , a q1 , b , b , a
q2 , b , a q3 , a q4
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
21
^
q0 , abbbaa q5
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
22
Example: Recognizing Strings Ending in “ing”
Not i or g
Not i
Not i or n i
Start i i
Not i
23
Example: Strings With No 11
0 0,1
1 1
A B C
Start 0
24
Alternative Representation: Transition Table
Final states
starred Columns =
0 1 input symbols
* A A B
Arrow for B A C
*
start state C C C
0 0,1
Rows = states
1 1
A B C
Each entry is δ
of the row and Start 0
column.
25
Example : Tennis
• Probably, it was invented in the 12th century or 1879.
• Game = at least four points and at least two points more than
the opponent
26
Winning a Game
• One person serves throughout.
• To win, you must score at least 4 points.
• You also must win by at least 2 points.
• Inputs are s = “server wins point” & o = “opponent wins point.
• Scores from zero to three points are described as " love",
"fifteen", "thirty", and "forty", respectively
• Scores equal to forty for each, the score is not called out as
"forty-forty", but rather as "deuce“
• For server "advantage" is called “Ad-in“
• For opponent "advantage“ is called “Ad-out"
27
Server
s Wins
40-Love s
s s Ad-in
o s
30-Love
s 40-15 o
o s o
Start 15-Love s
30-15 40-30
s s s
o o o
Love 15-all 30-all o deuce
s s s
o o
15-30 30-40
Love-15 s o
s
o o s
Love-30 15-40
s o
o
o Ad-out
Love-40
o
o
Opp’nt
Wins
28
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s s s
o o o
Love 15-all 30-all deuce
o s
* o
Love-15
s
o
15-30
s
s
30-40
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins 29
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
Start
*15-Love s o s
40-15 o
s
o
30-15 40-30
s s s
o o o
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins 30
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s
o * s
o
s
o
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins 31
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
*
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s s s
o o o
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins 32
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
Love
s
o
15-all
s
o *
30-all
s
o
deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins 33
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
*
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s s s
o o o
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins 34
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s
o
s
o
s
o *
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins 35
Example: Processing a String
sosososososs
Server
Wins
*
s
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s s s
o o o
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins 36
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
Love
s
o
15-all
s
o
30-all
s
o *
deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins 37
Example: Processing a String
sosososososs
Server
Wins
*
s
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s s s
o o o
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins 38
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
Love
s
o
15-all
s
o
30-all
s
o *
deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins 39
Example: Processing a String
sosososososs
Server
Wins
*
s
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s s s
o o o
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins 40
Example: Processing a String
sosososososs *
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s s s
o o o
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins 41
{a , b }
a, b
q0 a q1 b q2
b a accept
q3 a, b
42
L M = { all binary strings containing substring “001” }
0,1
1 0
1
0 0 00 1 001
0
43
L M = { all binary strings without substring “001”}
1 0 0,1
1
0 1
0 00 001
0
44
L(M ) awa : w a , b
*
a
b
b
q0 a q2 q3
b a
q4
a, b
45
Language of a DFA
Automata of all kinds define languages.
46
Example: String in a Language
Start at A.
0 0,1
1 1
A B C
Start 0
47
Example: String in a Language
1 1
A B C
Start 0
48
Example: String in a Language
1 1
A B C
Start 0
49
Example: String in a Language
1 1
A B C
Start 0
50
Example – Concluded
The language of our example DFA is:
{w | w is in {0,1}* and w does not have two consecutive 1’s}
These conditions
Such that… about w are true.
51
For a DFA A Q , , , q0 , F
Language accepted by A :
^
L A w : q0 , w F
*
q0 w q q F
52
Language rejected by : A
^
L A w : q0 , w F
*
q0 w q q F
53
Regular Languages
Definition:
54
Example regular languages:
{ } { } {a , b } *
n n
L {a b : n 0}
ADDITION {x y z : x 1 , y 1 , z 1 ,
n m k
nm k}
There is no DFA that accepts these languages (we will prove this in a later class)
56