Deterministic Finite Automata (DFA) DFA can be defined by quintuples (𝑄, Σ,𝛿, 𝑞 0, 𝐹) 𝑄 is finite set of states Σ is finite set of alphabets 𝛿is transition function 𝑞 0 is initial state 𝐹finite set of final states
Example of DFA 𝑄= 𝐴, 𝐵, 𝐶 Σ= 𝑎,𝑏 𝛿:𝑄 Χ Σ→𝑄 𝑞 0 =𝐴 𝐹={𝐵}
Transition Function A B C A B C 𝛿:𝑄 Χ Σ→𝑄 {A, B, C} X {a, b} {A, B, C} (A, a) (A, b) (B, a) (B, b) (C, a) (C, b) A B C A B C For every input on a state there is exactly one transition.
Question Why this FA is called Deterministic Finite Automata? Every state on seeing any input it is always going to go to only one state.
DFA Construct a DFA, that accepts set of all strings over {a, b} of length 2. Ans: Σ= 𝑎,𝑏 𝐿={𝑎𝑎, 𝑎𝑏, 𝑏𝑎, 𝑏𝑏} A a B a C
DFA B B B D C A C A C A Is this a complete DFA? No Now is this a complete DFA? After reaching sate ‘D’ we can’t go back to the final state, that is why this state is called dead state/trap state. B C A a, b a, b a, b B C A a, b a, b D
Acceptance Acceptance of a string by a FA: Scan the entire string and if we reach a final state from initial state. Acceptance of a language by a FA: If all the strings in the language are accepted by the FA and all the strings those are not in the language must reject by the FA.
DFA Construct a DFA that accept all strings over the alphabets {a, b} where the string length is at least 2. 𝝎∈ 𝒂, 𝒃 and 𝝎 ≥𝟐 𝜮= 𝒂,𝒃 𝑳={𝒂𝒂, 𝒂𝒃, 𝒃𝒂, 𝒃𝒃,𝒂𝒂𝒂,……𝒃𝒃𝒃,………} Up to this is a DFA of string length 2. This is not the required DFA. Now this is required DFA. a, b C a,b A B a,b
DFA Construct a DFA that accept all strings over the alphabets {a, b} where the string length is at most two. 𝝎∈ 𝒂, 𝒃 and 𝝎 ≤𝟐 𝜮= 𝒂,𝒃 𝑳={𝝐,𝒂,𝒃,𝒂𝒂, 𝒂𝒃, 𝒃𝒂, 𝒃𝒃} To accept null string (𝝐) the only way to make initial state to both initial state and final state. D a, b C a,b A B a,b
Minimum number of states 𝝎 =𝟐 𝝎 ≥𝟐 𝝎 ≤𝟐 𝝎 =𝒏 n + 2 𝝎 ≥𝒏 n + 1 𝝎 ≤𝒏 n + 2
DFA Construct a DFA that accept all strings over the alphabets {a, b} where the string length is divisible by two. 𝑳={𝜖,𝑎𝑎,𝑎𝑏,𝑏𝑎,𝑏𝑏,𝑎𝑎𝑎𝑎…𝑏𝑏𝑏𝑏………} This is not a finite automata A a, b B a, b C a, b D a, b E Length 0 Length 1 Length 2 Length 3 Length 4
DFA Construct a DFA that accept all strings over the alphabets {a, b} where the string length is divisible by two. This is not minimal A B a, b C Length 0 Length 1 Length 2 a, b A B a, b Length Even Length Odd
DFA Construct a DFA that accept all strings over the alphabets {a, b} where the string length is not divisible by two. We will get 0 or 1 after diving the length of a string by two. That is why we need two state. If string length is 3 then we will get 0 or 1 or 2 as remainder after dividing any length by 3. So we will be requiring three state in that case. A a, b B Length Even Length Odd a, b
DFA Construct a DFA that accept all strings over the alphabets {a, b} where the string length is divisible by three. 𝜔 𝑚𝑜𝑑 3=0 𝐿={𝜖, 𝑎𝑎𝑎,………,𝑏𝑏𝑏,𝑎𝑎𝑎𝑎𝑎𝑎,………,𝑏𝑏𝑏𝑏𝑏𝑏,………………} Length 0,3,6,… Length 1,4,7,… Length 2,5,8,… A a, b B a, b C a, b
DFA B Construct a DFA where 𝜔 ≅1 𝑚𝑜𝑑 3 A C 𝜔 ≅1 𝑚𝑜𝑑 3 means 𝜔 𝑚𝑜𝑑 3=1 For 𝜔 𝑚𝑜𝑑 𝑛=0 𝑜𝑟 𝜔 ≅𝑚 𝑚𝑜𝑑 n (m is any number) in general for minimal DFA we need 𝑛 number of states. A a, b C Length 0,3,6,… Length 1,4,7,… Length 2,5,8,… B ≅ congruent to
DFA Construct a DFA that accept all strings over the alphabets {a, b} where number of ‘a’ in the string is two. 𝑛 𝑎 𝜔 =2 𝐿= 𝑎𝑎,𝑏𝑎𝑎,𝑎𝑏𝑎,𝑎𝑎𝑏,𝑏𝑏𝑎𝑎,……… b b b C a a,b A B a a D 0 ‘a’s 1 ‘a’s 2 ‘a’s 3 ‘a’s
DFA Construct a DFA that accept all strings over the alphabets {a, b} where number of ‘a’ in the string at least two. 𝑛 𝑎 𝜔 ≥2 B a C 0 ‘a’s 1 ‘a’s 2 ‘a’s A b a, b
DFA Construct a DFA that accept all strings over the alphabets {a, b} where number of ‘a’ in the string at most two. 𝑛 𝑎 𝜔 ≤2 b B a C 0 ‘a’s 1 ‘a’s 2 ‘a’s A a,b a D 3 ‘a’s
DFA Construct a DFA that accept all strings over the alphabets {a, b} where number of ‘a’ in the string is even. 𝑛 𝑎 𝜔 𝑚𝑜𝑑 2=0 𝑛 𝑎 𝜔 ≅0 𝑚𝑜𝑑 2 b b A B a a Length Odd Length Even
DFA Construct a DFA that accept all strings over the alphabets {a, b} where number of ‘a’ in the string is odd. 𝑛 𝑎 𝜔 𝑚𝑜𝑑 2=1 𝑛 𝑎 𝜔 ≅1 𝑚𝑜𝑑 2 b b A B a a Length Odd Length Even
DFA Construct a DFA that accept all strings over the alphabets {a, b} where number of ‘a’ in the string is divisible by 3. 𝑛 𝑎 𝜔 𝑚𝑜𝑑 3=0 𝑛 𝑎 𝜔 ≅0 𝑚𝑜𝑑 3 A B a b C
DFA 𝑖𝑓 𝑛 𝑎 𝜔 ≅𝑘 𝑚𝑜𝑑 n , how to draw DFA?? 𝑛 number of states will be there If k=0 𝑞 0 will be final state If k=1 𝑞 1 will be final state If k=2 𝑞 2 will be final state If k=3 𝑞 3 will be final state So on
Assignment Construct a DFA that accept all strings over the alphabets {a, b} where number of ‘b’ in the string is divisible by 5. Construct a DFA that accept all strings over the alphabets {a, b} such that 𝑛 𝑎 𝜔 ≅2 𝑚𝑜𝑑 5 Construct a DFA that accept all strings over the alphabets {a, b} such that 𝑛 𝑏 𝜔 ≅1 𝑚𝑜𝑑 3
DFA Construct a DFA that accept all strings over the alphabets {a, b} where number of ‘a’ and number of ‘b’ in the string is even. 𝑛 𝑎 𝜔 𝑚𝑜𝑑 2=0 & 𝑛 𝑏 𝜔 𝑚𝑜𝑑 2=0 𝑛 𝑎 𝜔 ≅0 𝑚𝑜𝑑 2 & 𝑛 𝑏 𝜔 ≅0 𝑚𝑜𝑑 2 𝑛 𝑎 𝜔 𝑛 𝑏 𝜔 E = Even, O = Odd Four possible states representing this situation E O
DFA 1. String 𝜖 state ee 2. String ‘a’ state oe 3. String ‘b’ state eo 4. String ‘aa’ state ee 5. String ‘ab’ state oo 6. String ‘ba’ state oo 7. String ‘bb’ state ee 8. String ‘aba’ or ‘baa’ state eo 9. String ‘bab’ or ‘abb’ state oe ee eo oe oo b b a a a a b b
Cross Product Method B D A C 𝑛 𝑎 𝜔 𝑚𝑜𝑑 2=0 & 𝑛 𝑏 𝜔 𝑚𝑜𝑑 2=0 𝑛 𝑎 𝜔 𝑚𝑜𝑑 2=0 & 𝑛 𝑏 𝜔 𝑚𝑜𝑑 2=0 𝐴,𝐵 × 𝐶,𝐷 = 𝐴𝐶,𝐴𝐷,𝐵𝐶,𝐵𝐷 A B a b C D b a ee AC BC AD BD oe a a b A B C C A A C D a b b b b Observation: ‘a’s are counting in horizontal way and ‘b’s are counting in vertical way. a eo oo a
Cross Product Method Construct a DFA that accept all strings over the alphabets {a, b} where number of ‘a’ is divisible by 3 and number of ‘b’ in the string is divisible by 2. a 00 a 10 a 20 b b b b b b 01 a 11 a 21 a
Cross Product Method Construct a DFA that accept all strings over the alphabets {a, b} where number of ‘a’ and number of ‘b’ in the string is divisible by 3. 𝑛 𝑎 𝜔 𝑚𝑜𝑑 3=0 & 𝑛 𝑏 𝜔 𝑚𝑜𝑑 3=0 𝑛 𝑎 𝜔 ≅0 𝑚𝑜𝑑 3 & 𝑛 𝑏 𝜔 ≅0 𝑚𝑜𝑑 3 b a A B D E C F G H I
DFA Construct a DFA that accept all strings over the alphabets {a, b} such that 𝑛 𝑎 𝜔 𝑚𝑜𝑑 3≥ 𝑛 𝑏 𝜔 𝑚𝑜𝑑 2 No. of ‘a’s mod 3 = no. of ‘b’s mod 2 So this will be a final state No. of ‘a’s mod 3 > no. of ‘b’s mod 2 this will be a final state No. of ‘a’s mod 3 > no. of ‘b’s mod 2 this will be a final state a 00 a 10 a 20 No. of ‘a’s mod 3 < no. of ‘b’s mod 2 this will not be a final state No. of ‘a’s mod 3 = no. of ‘b’s mod 2 this will be a final state b No. of ‘a’s mod 3 > no. of ‘b’s mod 2 this will be a final state b b b b b 01 a 11 a 21 a
DFA Construct a minimal DFA which accepts all strings over {0,1}, which when interpreted as binary number is divisible by 2. Two remainders are possible, so two states 1 𝒒 𝟎 𝒒 𝟏 1
DFA Construct a minimal DFA which accepts all strings over {0,1}, which when interpreted as binary number is divisible by 3. Three remainders are possible, so three states 1 𝒒 𝟎 1 𝒒 𝟏 𝒒 𝟐 1 If the question is divisible by ‘n’ then number of state is n.
Transition Table 1 𝑞 0 𝑞 1 𝑞 2 q0 q1 q2 q0 q1 q2
Transition Table Construct a minimal DFA which accepts all strings over {0,1}, which when interpreted as binary number is divisible by 4 1 𝑞 0 𝑞 1 𝑞 2 𝑞 3
DFA Construct a minimal DFA which accepts all strings over {0,1}, which when interpreted as binary number is ≅1 𝑚𝑜𝑑 3. 𝒒 𝟎 𝒒 𝟏 1
DFA Construct a minimal DFA which accepts all strings over {0,1}, which when interpreted as binary number is ≅2 𝑚𝑜𝑑 3. 𝒒 𝟎 𝒒 𝟏 1
Transition Table Construct a minimal DFA which accepts all strings over {0,1,2}, which when interpreted as ternary number is divisible by 4 1 2 𝑞 0 𝑞 1 𝑞 2 𝑞 3 Lecture 8: {0,1,2} ternary number where base is 3.
DFA Construct a minimal DFA which accepts set of all strings over {a, b} where each string starts with an ‘a’. Construct a minimal DFA which accepts set of all strings over {a, b} where each string contains an ‘a’. Construct a minimal DFA which accepts set of all strings over {a, b} where each string ends with an ‘a’.
Assignment Construct a DFA that accept all strings over the alphabets {a, b} where number of ‘a’ and number of ‘b’ is odd in the string. Construct a DFA that accept all strings over the alphabets {a, b} where number of ‘a’ is even and number of ‘b’ is odd in the string . Construct a DFA that accept all strings over the alphabets {a, b} where number of ‘a’ is odd and number of ‘b’ is even in the string . Construct a DFA that accept all strings over the alphabets {a, b} where number of ‘a’ is multiple of three and number of ‘b’ is multiple of two in the string. Construct a DFA that accept all strings over the alphabets {a, b} such that 𝑛 𝑎 𝜔 𝑚𝑜𝑑 3= 𝑛 𝑏 𝜔 𝑚𝑜𝑑 2 Construct a DFA that accept all strings over the alphabets {a, b} such that 𝑛 𝑎 𝜔 𝑚𝑜𝑑 3 ≤ 𝑛 𝑏 𝜔 𝑚𝑜𝑑 2 Construct a DFA that accept all strings over the alphabets {a, b} such that 𝑛 𝑎 𝜔 𝑚𝑜𝑑 3=1 and 𝑛 𝑏 𝜔 𝑚𝑜𝑑 3=2 Construct a DFA that accept all strings over the alphabets {a, b} such that 𝑛 𝑎 𝜔 𝑚𝑜𝑑 3> 𝑛 𝑏 𝜔 𝑚𝑜𝑑 3 Construct a minimal DFA which accepts all strings over Octal number system, which when interpreted as binary number ≅1 𝑚𝑜𝑑 5
DFA Construct a minimal DFA which accepts set of all strings over {a, b} where each string start with “ab” L= {ab, aba, abb, ………} a, b A a B b C b a D a, b
DFA Construct a minimal DFA which accepts set of all strings over {a, b} where each string contains “ab” as a sub string. L= {ab, aab, aba, bab, abb ………} a,b b a A a B b C
DFA Construct a minimal DFA which accepts set of all strings over {a, b} where each string end with “ab” L= {ab, aab, bab, abab ………} a b a A a B b C b
DFA Construct a minimal DFA which accepts set of all strings over {a, b} where each string starts with ‘a’ and ends with ‘b’ or vice versa. L ={ab, ba, aab, abb, baa, bba. ……} b a A a B b C b a D b a b a E
DFA Construct a minimal DFA which accepts set of all strings over {a, b} where each string starts and ends with same symbol. L={𝜖, a, b, aa, bb, aba, bab,…………} b a A a B b C b a D b This DFA is complement of previous DFA and language also. We will get complement of a language by removing one language from 𝚺 ∗ . 𝑳𝟐= 𝚺 ∗ −𝑳𝟏 L2 is complement of L1 b a E a
Complement of DFA B B A A A B C A B C L1 = Even number of ‘a’ L2 = Odd number of ‘a’ A B a Length Even Length Odd b A B a Length Even Length Odd b L3 = Starts with ‘a’ L4 = Not starts with ‘a’ This rule is only for DFA’s not for NFA’s A B C a b a,b A B C a b a,b
Complement of DFA DFA can be defined by quintuples (𝑄, Σ,𝛿, 𝑞 0, 𝐹) Complement of DFA can be defined by quintuples (𝑄, Σ,𝛿, 𝑞 0, 𝑄−𝐹)
DFA Construct a minimal DFA which accepts set of all strings over {a, b} which accepts strings in which every 'a' is followed by 'b'. L={𝜖, b, ab, bb, aba, bab, …………} b b A a B b C a a a,b D
DFA Construct a minimal DFA which accepts set of all strings over {a, b} which accepts strings in which every 'a' never followed by 'b'. This is not complement of previous L={𝜖, a, aa,…..,b, bb,……, ba, bba…………} b a,b a A a B b C
DFA Construct a minimal DFA which accepts set of all strings over {a, b} which accepts strings in which every 'a' is followed by ‘bb'. a b b A a B b C b D a a a,b E
DFA Construct a minimal DFA which accepts set of all strings over {a, b} which accepts strings in which every 'a' never followed by ‘bb'. a a, b b A a B b C b D a
DFA C 𝐿= 𝑎 𝑛 𝑏 𝑚 |𝑛,𝑚≥1 A B D 𝐿={𝑎𝑏, 𝑎𝑎𝑏𝑏, 𝑎𝑎𝑎𝑏, 𝑎𝑏𝑏𝑏,………} b a a b a b 𝐿= 𝑎 𝑛 𝑏 𝑚 |𝑛,𝑚≥1 𝐿={𝑎𝑏, 𝑎𝑎𝑏𝑏, 𝑎𝑎𝑎𝑏, 𝑎𝑏𝑏𝑏,………} b a A a B b C b a D a, b
DFA C 𝐿= 𝑎 𝑛 𝑏 𝑚 |𝑛,𝑚≥0 A B 𝐿={𝜖,𝑎, 𝑎𝑎, …,𝑎𝑎𝑏, 𝑎𝑏𝑏,…𝑏, 𝑏𝑏, 𝑏𝑏𝑏………} a b 𝐿= 𝑎 𝑛 𝑏 𝑚 |𝑛,𝑚≥0 𝐿={𝜖,𝑎, 𝑎𝑎, …,𝑎𝑎𝑏, 𝑎𝑏𝑏,…𝑏, 𝑏𝑏, 𝑏𝑏𝑏………} a b a,b A b B a C
DFA D 𝐿= 𝑎 𝑛 𝑏 𝑚 𝑐 𝑙 |𝑛,𝑚,𝑙≥1 A B C D 𝐿={𝑎𝑏𝑐, 𝑎𝑎𝑏𝑐, 𝑎𝑏𝑏𝑐, 𝑎𝑏𝑐𝑐,………} c 𝐿= 𝑎 𝑛 𝑏 𝑚 𝑐 𝑙 |𝑛,𝑚,𝑙≥1 𝐿={𝑎𝑏𝑐, 𝑎𝑎𝑏𝑐, 𝑎𝑏𝑏𝑐, 𝑎𝑏𝑐𝑐,………} c a b A a B b C c D b, c c a a, b D a, b, c
DFA 𝐿= 𝑎 𝑛 𝑏 𝑚 𝑐 𝑙 |𝑛,𝑚,𝑙≥0 A B C D 𝐿= 𝑎 𝑛 𝑏 𝑚 𝑐 𝑙 |𝑛,𝑚,𝑙≥0 𝐿={𝜖,𝑎, 𝑏, 𝑐, 𝑎𝑏, 𝑎𝑐, 𝑏𝑐, 𝑎𝑏𝑐, 𝑎𝑎, 𝑏𝑏, 𝑐𝑐,…} b c a A b B c C c a, b a D a, b, c
DFA H 𝐿={ 𝑎 3 𝑏𝑤 𝑎 3 |𝑤∈ {𝑎,𝑏} ∗ } A B C D E F G I 𝐿={ 𝑎 3 𝑏𝜖 𝑎 3 , 𝑎 3 𝑏𝑎 𝑎 3 , 𝑎 3 𝑏𝑏 𝑎 3 , 𝑎 3 𝑏𝑎𝑎 𝑎 3 ,……} a b A B H a a C a D b E a F a G a b b b b a b I b a, b
Operations of DFA Different operations that we can apply on DFAs are - Union Concatenation Cross Product Complement Reversal
Union Union of two Regular language is also Regular Let, L1 = starts with ‘a’ and ends with ‘b’ L2 = starts with ‘b’ and ends with ‘a’ 𝐿=𝐿1∪𝐿2 = starts with different symbol Let, 𝑴𝟏= 𝑄 1 , Σ 1 , 𝛿 1 , 𝑞 0 1 , 𝐹 1 DFA for L1 , 𝑴𝟐= 𝑄 2 , Σ 2 , 𝛿 2 , 𝑞 0 2 , 𝐹 2 DFA for L2 We want to construct M for 𝑳=𝐿1∪𝐿2 𝑴= 𝑄,Σ, 𝛿, 𝑞 0 ,𝐹 DFA for L Where, 𝑸= 𝑄 1 × 𝑄 2 𝜮= Σ 1 ∪ Σ 2 𝜹 𝑟 1 , 𝑟 2 ,𝑎 = 𝛿 1 𝑟 1 ,𝑎 , 𝛿 2 𝑟 2 ,𝑎 𝒒 𝟎= 𝑞 0 1 , 𝑞 0 2 𝑭= 𝑟 1 , 𝑟 2 | 𝑟 1 ∈ 𝐹 1 𝑜𝑟 𝑟 2 ∈ 𝐹 2 𝑟 1 ∈ 𝑄 1 𝑎𝑛𝑑 𝑟 2 ∈ 𝑄 2
Union B A a b C D a, b B A b a C D a, b b B A a C
Concatenation 𝐿1= 𝑎,𝑎𝑎,𝑎𝑏,𝑎𝑎𝑏,𝑎𝑎𝑎, 𝑎𝑏𝑏,…… Concatenation of two Regular language is also Regular L1 = starts with ‘a’ L2 = ends with ‘b’ 𝐿=𝐿1∘𝐿2 = starts with ‘a’ and ends with ‘b’ 𝐿1= 𝑎,𝑎𝑎,𝑎𝑏,𝑎𝑎𝑏,𝑎𝑎𝑎, 𝑎𝑏𝑏,…… 𝐿2={𝑏,𝑎𝑏, 𝑏𝑏, 𝑏𝑏𝑏, 𝑎𝑎𝑏, 𝑎𝑏𝑏,……} 𝐿={𝑎𝑏, 𝑎𝑎𝑏, 𝑎𝑏𝑏, 𝑎𝑏𝑏𝑏,………} Let, 𝑴𝟏= 𝑄 1 , Σ 1 , 𝛿 1 , 𝑞 0 1 , 𝐹 1 DFA for L1 , 𝑴𝟐= 𝑄 2 , Σ 2 , 𝛿 2 , 𝑞 0 2 , 𝐹 2 DFA for L2 We want to construct M for 𝑳=𝐿1∘𝐿2 𝑴= 𝑄,Σ, 𝛿, 𝑞 0 ,𝐹 DFA for L
Concatenation A B A B C A B C D a, b a a b b a b a, b Final state of 1st DFA is initial state of 2nd DFA
Reversal A B C A B C L1= starts with ‘a’ 𝐿1={a, aa, ab, aaa, aab, aba,……} 𝐿1 𝑅 ={a, aa, ba, aaa, baa, aba,……} Steps: States are same as original Convert initial state to final state Convert final state to initial state If we reverse a DFA of a language we will get a reverse language, but we may or may not get reverse DFA. Reverse FA is either DFA or NFA. If we reverse a DFA of a language we will get a reverse language, but we may or may not get reverse DFA. Reverse FA is either DFA or NFA. A a a, b B b C a, b A a B b This is not DFA it is NFA C a, b