Deterministic Finite Automata And Regular Languages
1 Deterministic Finite Automaton (DFA)
Input Tape String Output “Accept” Finite or Automaton “Reject”
Costa Busch - LSU 2 Transition Graph a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4 initial accepting state state transition state
Costa Busch - LSU 3 Alphabet Σ = {a,b} a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
For every state, there is a transition for every symbol in the alphabet
Costa Busch - LSU 4 head Initial Configuration Input Tape a b b a Input String a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Initial state
Costa Busch - LSU 5 Scanning the Input a b b a
a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 6 a b b a
a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 7 a b b a
a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 8 Input finished
a b b a
a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4 accept
Last state determines the outcome Costa Busch - LSU 9 A Rejection Case a b a Input String a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 10 a b a
a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 11 a b a
a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 12 Input finished a b a
a,b
reject q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Last state determines the outcome
Costa Busch - LSU 13 Another Rejection Case
Tape is empty (λ)
Input Finished (no symbol read) a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
reject
Costa Busch - LSU 14 This automaton accepts only one string
Language Accepted: L = {abba}
a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 15 To accept a string: all the input string is scanned and the last state is accepting
To reject a string: all the input string is scanned and the last state is non-accepting
Costa Busch - LSU 16 Another Example
L = {λ,ab,abba} a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4 Accept Accept Accept state state state Costa Busch - LSU 17 Empty Tape (λ)
Input Finished a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
accept
Costa Busch - LSU 18 Another Example
a a,b
b a,b q0 q1 q2
Accept trap state state Costa Busch - LSU 19 a a b Input String a a,b
b a,b q0 q1 q2
Costa Busch - LSU 20 a a b
a a,b
b a,b q0 q1 q2
Costa Busch - LSU 21 a a b
a a,b
b a,b q0 q1 q2
Costa Busch - LSU 22 Input finished a a b
a a,b accept
b a,b q0 q1 q2
Costa Busch - LSU 23 A rejection case b a b Input String a a,b
b a,b q0 q1 q2
Costa Busch - LSU 24 b a b
a a,b
b a,b q0 q1 q2
Costa Busch - LSU 25 b a b
a a,b
b a,b q0 q1 q2
Costa Busch - LSU 26 Input finished b a b
a a,b
b a,b q0 q1 q2
reject
Costa Busch - LSU 27 Language Accepted: L = {a nb :n ≥ 0}
a a,b
b a,b q0 q1 q2
Costa Busch - LSU 28 Another Example Alphabet: Σ = {1} 1
q0 q1 1
Language Accepted: EVEN = {x : x ∈ Σ* and x is even} = {λ, 11, 1111, 111111, …} Costa Busch - LSU 29 Formal Definition Deterministic Finite Automaton (DFA)
M = (Q,Σ,δ ,q0,F)
Q : set of states Σ : input alphabet λ ∉ Σ δ : transition function q0 : initial state F : set of accepting states
Costa Busch - LSU 30 Set of States Q
Example
Q = {q0,q1,q2,q3,q4,q5} a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 31 Input Alphabet Σ
λ ∉ Σ :the input alphabet never contains λ
Example Σ = {a,b} a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 32 Initial State q0
Example a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 33 Set of Accepting States F ⊆ Q
Example F = {q4} a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 34 Transition Function δ :Q×Σ → Q
δ(q,x ) = qʹ
q x qʹ
Describes the result of a transition from state q with symbol x
Costa Busch - LSU 35
Example: δ (q0,a) = q1
a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 36
δ (q0,b) = q5
a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 37 δ (q2,b) = q3
a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 38 Transition Table for δ symbols δ a b q0 q1 q5 q1 q5 q2 q2 q5 q3 a,b q3 q4 q5 states q4 q5 q5 q5 q5 q5 q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 39 Extended Transition Function δ * :Q × Σ* →Q
δ * (q,w ) = qʹ
Describes the resulting state after scanning string w from state q
Costa Busch - LSU 40
* Example: δ (q0,ab ) = q2
a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 41 * δ (q0,abbbaa ) = q5
a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 42 * δ (q1,bba ) = q4
a,b
q5 a,b b a a b a a q0 q1 b q2 b q3 q4
Costa Busch - LSU 43 Special case:
for any state q
δ * (q,λ) = q
Costa Busch - LSU 44 In general: δ * (q,w ) = qʹ implies that there is a walk of transitions
w = σ1σ2 σk q σ1 σ 2 σ k qʹ states may be repeated
q w qʹ
Costa Busch - LSU 45 Language Accepted by DFA
Language accepted by DFA M : it is denoted as L ( M ) and contains all the strings accepted by M
We also say that M recognizes L(M )
Costa Busch - LSU 46
For a DFA M = (Q,Σ,δ ,q0,F)
Language accepted by M :
* * L(M ) = {w ∈ Σ : δ (q0,w )∈F }
w q0 qʹ qʹ∈F
Costa Busch - LSU 47 Language rejected by M :
* * L(M) = {w ∈ Σ : δ (q0,w )∉F }
w q0 qʹ qʹ∉F
Costa Busch - LSU 48 More DFA Examples Σ = {a,b} a,b a,b
q q0 0
L(M) = { } L(M) = Σ* Empty language All strings
Costa Busch - LSU 49 Σ = {a,b}
a,b
a,b q0 q0
L(M) = {λ} Language of the empty string
Costa Busch - LSU 50 Σ = {a,b}
L(M )= { all strings with prefix ab } a,b
q0 a q1 b q2
b a accept
q3 a,b
Costa Busch - LSU 51
L(M) = { all binary strings containing substring 001 }
0,1 1 0 1
λ 0 0 00 1 001 0
Costa Busch - LSU 52
L(M) = { all binary strings without substring 001 }
1 0 0,1 1 0 1 λ 0 00 001
0
Costa Busch - LSU 53
* L(M) = {awa :w ∈ {a,b} } a b b
q0 a q2 q3 b a
q4
a,b Costa Busch - LSU 54 Regular Languages Definition: A language L is regular if there is a DFA M that accepts it ( L ( M ) = L )
The languages accepted by all DFAs form the family of regular languages
Costa Busch - LSU 55 Example regular languages: {abba} {λ,ab,abba} n {a b : n ≥ 0} {awa :w ∈ {a,b}* } { all strings in {a,b}* with prefix ab } { all binary strings without substring 001 } {x : x ∈ {1}* and x is even} { } {λ} {a,b}* There exist DFAs that accept these languages (see previous slides). Costa Busch - LSU 56 There exist languages which are not Regular:
L={anbn : n ≥ 0}
ADDITION = {x + y = z : x = 1n, y = 1m,z = 1k , n + m = k }
There are no DFAs that accept these languages
(we will prove this in a later class)
Costa Busch - LSU 57 Properties of Regular Languages
For regular languages L 1 and L 2 we will prove that:
Union: L1 ∪ L2 Concatenation: L1L2 Are regular Star: L1 * Languages R Reversal: L1
Complement: L1 Intersection: L ∩ L Costa1 Busch - LSU2 59 We say: Regular languages are closed under
Union: L1 ∪ L2 Concatenation: L1L2
Star: L1 * R Reversal: L1
Complement: L1 Intersection: L ∩ L Costa1 Busch - LSU2 60 A useful transformation: use one accept state
NFA a 2 accept states a b b
Equivalent a NFA 1 accept state a b b λ λ
Costa Busch - LSU 61 In General NFA
Equivalent NFA
λ Single λ accepting λ state
Costa Busch - LSU 62 Extreme case
NFA without accepting state
Add an accepting state without transitions
Costa Busch - LSU 63 Take two languages
Regular language L1 Regular language L2
L(M1) = L1 L(M2 ) = L2
NFA M1 NFA M 2
Single accepting state Single accepting state
Costa Busch - LSU 64 Example
M1 n ≥ 0 a n L1 ={a b} b
M 2 b a L2 = {ba}
Costa Busch - LSU 65 Union NFA for L1 ∪ L2 M1
λ
λ M 2
Costa Busch - LSU 66 Example n NFA for L1 ∪ L2 ={a b}∪{ba} n L1 ={a b} a b λ
λ L2 ={ba} b a
Costa Busch - LSU 67 Concatenation
NFA for L1L2
M1 M 2 λ
Costa Busch - LSU 68 Example
n n NFA for L1L2 ={a b}{ba} ={a bba}
n L1 ={a b} a L2 ={ba} b λ b a
Costa Busch - LSU 69 Star Operation w w w w NFA for L1 * = 1 2 k λ wi ∈ L1
M1 λ ∈ L1 * λ λ
λ
Costa Busch - LSU 70 Example
n NFA for L1* ={a b}* λ n L1 ={a b} a λ b λ
λ Costa Busch - LSU 71 Reverse R NFA for L1 L ʹ 1 M1 M1
1. Reverse all transitions
2. Make initial state accepting state and vice versa Costa Busch - LSU 72 Example
M1 a n L1 ={a b} b
ʹ M1 a R n L1 ={ba } b
Costa Busch - LSU 73 Complement ʹ L1 M1 L1 M1
1. Take the DFA that accepts L1
2. Make accepting states non-final, and vice-versa
Costa Busch - LSU 74 Example
M1 a a,b n b a,b L1 ={a b}
ʹ M1 n a a,b L1 ={a,b}*−{a b} b a,b
Costa Busch - LSU 75 Intersection
L1 regular We show L1 ∩ L2 L2 regular regular
Costa Busch - LSU 76 DeMorgan’s Law: L1 ∩ L2 = L1 ∪ L2
L1 , L2 regular
L1 , L2 regular
L1 ∪ L2 regular
L1 ∪ L2 regular
L1 ∩ L2 regular
Costa Busch - LSU 77 Example
n L1 ={a b} regular L1 ∩ L2 ={ab} L2 ={ab,ba} regular regular
Costa Busch - LSU 78 Another Proof for Intersection Closure
Machine M1 Machine M 2
DFA for L1 DFA for L2
Construct a new DFA M that accepts L1 ∩ L2
M simulates in parallel M 1 and M 2
Costa Busch - LSU 79 States in M
qi, p j
State in M1 State in M 2
Costa Busch - LSU 80 DFA M1 DFA M 2
a a q1 q2 p1 p2 transition transition
DFA M
a q1, p1 q2, p2 New transition
Costa Busch - LSU 81 DFA M1 DFA M 2
q0 p0 initial state initial state
DFA M
q0, p0 New initial state
Costa Busch - LSU 82 DFA M1 DFA M 2
p qi j pk
accept state accept states
DFA M
qi, p j qi, pk
New accept states
Both constituents must be accepting states
Costa Busch - LSU 83 Example:
n ≥ 0 m ≥ 0 n m L1 ={a b} L2 ={ab }
M1 M 2 a b b a q0 q1 p0 p1 a,b b a q2 p2 a,b a,b
Costa Busch - LSU 84 Automaton for intersection
L = {a nb} ∩ {abm } = {ab} a,b
q0, p0 a q0, p1 b q1, p1 a q2, p2 b a b a
q1, p2 b q0, p2 q2, p1 a b a,b
Costa Busch - LSU 85 M simulates in parallel M 1 and M 2
M accepts string w if and only if:
M1 accepts string w
and M 2 accepts string w
L(M ) = L(M1) ∩ L(M2)
Costa Busch - LSU 86 Regular Expressions
Regular Expressions Regular expressions describe regular languages
Example: (a + b ⋅c)*
describes the language {a,bc}* = {λ,a,bc,aa,abc,bca,...}
Costa Busch - LSU 88 Recursive Definition Primitive regular expressions: ∅, λ, α
Given regular expressions r 1 and r2
r1 + r2 r1 ⋅ r2 Are regular expressions r1 * (r1)
Costa Busch - LSU 89 Examples
A regular expression: (a + b⋅c)*⋅(c + ∅)
Not a regular expression: (a + b +)
Costa Busch - LSU 90 Languages of Regular Expressions
L ( r ) : language of regular expression r
Example L((a + b⋅c)*) = {λ,a,bc,aa,abc,bca,...}
Costa Busch - LSU 91 Definition
For primitive regular expressions: L(∅) = ∅
L(λ) = {λ}
L(a) = {a}
Costa Busch - LSU 92 Definition (continued)
For regular expressions r 1 and r2
L(r1 + r2 ) = L(r1)∪ L(r2 )
L(r1 ⋅r2 ) = L(r1) L(r2 )
L(r1 *) = (L(r1))*
L((r1)) = L(r1)
Costa Busch - LSU 93 Example Regular expression: ( a + b)⋅a*
L((a + b)⋅a*) = L((a + b)) L(a*) = L(a + b) L(a*) = (L(a)∪ L(b))(L(a))* = ({a}∪{b})({a})* = {a,b}{λ,a,aa,aaa,...} = {a,aa,aaa,...,b,ba,baa,...} Costa Busch - LSU 94 Example
Regular expression r = (a + b)*(a + bb)
L(r) = {a,bb,aa,abb,ba,bbb,...}
Costa Busch - LSU 95 Example
Regular expression r = (aa)*(bb)*b
L(r) ={a2nb2mb : n,m ≥ 0}
Costa Busch - LSU 96 Example
Regular expression r = (0 +1)*00 (0 +1)*
L(r) = { all strings containing substring 00 }
Costa Busch - LSU 97 Example
Regular expression r = (1+ 01)*(0 + λ)
L(r) = { all strings without substring 00 }
Costa Busch - LSU 98 Equivalent Regular Expressions
Definition:
Regular expressions r 1 and r2
are equivalent if L (r1) = L(r2)
Costa Busch - LSU 99 Example L = { all strings without substring 00 }
r1 = (1+ 01)*(0 + λ)
r2 = (1*011*)*(0 + λ) +1*(0 + λ)
r and r L(r ) = L(r ) = L 1 2 1 2 are equivalent regular expressions Costa Busch - LSU 100 Regular Expressions and Regular Languages
Theorem
Languages Regular Generated by = Languages Regular Expressions
Costa Busch - LSU 102 Proof:
Languages Regular Generated by ⊆ Languages Regular Expressions
Languages Regular Generated by ⊇ Languages Regular Expressions
Costa Busch - LSU 103 Proof - Part 1
Languages Regular Generated by ⊆ Languages Regular Expressions
For any regular expression r the language L ( r ) is regular
Proof by induction on the size of r
Costa Busch - LSU 104 Induction Basis Primitive Regular Expressions: ∅, λ, α Corresponding NFAs L(M1) = ∅ = L(∅)
regular L(M2) ={λ} = L(λ) languages a L(M3) ={a} = L(a)
Costa Busch - LSU 105 Inductive Hypothesis
Suppose that for regular expressions r1 and r 2 , L ( r 1 ) and L ( r 2 ) are regular languages
Costa Busch - LSU 106 Inductive Step We will prove: L(r1 + r2 )
L(r1 ⋅ r2 ) Are regular Languages L(r1 *)
L((r1)) Costa Busch - LSU 107 By definition of regular expressions:
L(r1 + r2 ) = L(r1)∪ L(r2 )
L(r1 ⋅ r2 ) = L(r1) L(r2 )
L(r1 *) = (L(r1))*
L((r1)) = L(r1)
Costa Busch - LSU 108 By inductive hypothesis we know: L ( r 1 ) and L ( r 2 ) are regular languages
We also know: Regular languages are closed under: Union L(r1)∪ L(r2 ) Concatenation L(r1) L(r2 ) Star (L(r1))*
Costa Busch - LSU 109 Therefore:
L(r1 + r2 ) = L(r1)∪ L(r2 )
Are regular L(r1 ⋅ r2 ) = L(r1) L(r2 ) languages
L(r1 *) = (L(r1))*
is trivially a regular language L((r1 )) = L(r1 ) (by induction hypothesis)
Costa Busch - LSU End of Proof-Part 1 110 Using the regular closure of these operations, we can construct recursively the NFA M that accepts L(M) = L(r) Example: r = r + r 1 2 L(M) = L(r) L(M1 ) = L(r1 )
λ
L(M2) = L(r2 ) λ
Costa Busch - LSU 111 Proof - Part 2
Languages Regular Generated by ⊇ Languages Regular Expressions
For any regular language L there is a regular expression r with L(r) = L
We will convert an NFA that accepts L to a regular expression Costa Busch - LSU 112 Since L is regular, there is a NFA M that accepts it
L(M ) = L
Take it with a single accept state
Costa Busch - LSU 113 From M construct the equivalent Generalized Transition Graph in which transition labels are regular expressions
Example: Corresponding M Generalized transition graph a c a c a,b a + b
Costa Busch - LSU 114 b b Another Example: a a,b q0 q1 q2 b
Transition labels b b are regular a expressions q0 q1 a + b q2 b
Costa Busch - LSU 115 b b Reducing the states: a q0 q1 a + b q2 b
Transition labels are regular bb*a b expressions bb*(a + b) q0 q2
Costa Busch - LSU 116 Resulting Regular Expression:
bb*a b bb*(a + b) q0 q2
r = (bb *a) *⋅bb *(a + b) ⋅b *
L(r) = L(M ) = L Costa Busch - LSU 117 In General Removing a state: e d c
qi q q j a b
ae*d ce*b ce*d
qi q j ae*b Costa Busch - LSU 118 By repeating the process until two states are left, the resulting graph is Initial graph Resulting graph r1 r4 r3
q0 q f r2 The resulting regular expression: r = r1 *r2(r4 + r3r1 *r2)* L(r) = L(M ) = L
Costa Busch - LSU End of Proof-Part 2 119 Standard Representations of Regular Languages
Regular Languages
DFAs
Regular NFAs Expressions
Costa Busch - LSU 120 When we say: We are given a Regular Language L
We mean: Language L is in a standard representation
(DFA, NFA, or Regular Expression)
Costa Busch - LSU 121