CS 322 D: Formal languages and automata theory Tutorial NFA DFA Regular Expression T. Najla Arfawi 2 nd Term - 26
Finite Automata Finite Automata. Q - States 2. S - Alphabets 3. d - Transitions 4. q - Initial state 5. F - Final state(s) / Accepting state(s) 2 T. Najla Arfawi - 2nd Term 26
Deterministic Finite Automata There must be a transition corresponding to each alphabet at each state 3 T. Najla Arfawi - 2nd Term 26
Nondeterministic Finite Automata Can be at multiple states at the same time Can go to multiple states on one alphabet May have no transitions on an alphabet (die) Every NFA can be converted into a DFA for the same language. 4 T. Najla Arfawi - 2nd Term 26
NFA DFA in two easy steps ➊ Eliminate e-transitions ➋ Convert simplified NFA We do this first 5 T. Najla Arfawi - 2nd Term 26
NFA DFA: states, NFA: q q q 2 DFA: {q } {q, q } {q } {q, q 2 } {q, q, q 2 } {q 2 } {q, q 2 } DFA has a state for every subset of NFA states 6 T. Najla Arfawi - 2nd Term 26
NFA DFA: transitions, NFA: q q q 2 DFA:, {q } {q } {q, q } {q, q 2 } {q, q, q 2 } {q 2 } {q, q 2 } 7 T. Najla Arfawi - 2nd Term 26
NFA DFA: accepting states, NFA: q q q 2 DFA:, {q } {q } {q, q } {q, q 2 } {q, q, q 2 } {q 2 } {q, q 2 } NFA accepts if it contains a DFA final state 8 T. Najla Arfawi - 2nd Term 26
NFA DFA: dead state elimination, NFA: q q q 2 DFA:, {q } {q } {q, q } {q, q 2 } {q, q, q 2 } {q 2 } {q, q 2 } At the end, you can eliminate the unreachable states 9 T. Najla Arfawi - 2nd Term 26
General method NFA DFA states q, q,, q n q }, {q }, {q,q },, {q,,q n } initial state q q } one for each subset of states in the NFA transitions d d ({q i,,q ik }, a) = accepting states d(q i, a) d(q ik, a) F Q F = {S: S contains some state in F} T. Najla Arfawi - 2nd Term 26
NFA DFA in two easy steps ➊ Eliminate e-transitions ➋ Convert simplified NFA T. Najla Arfawi - 2nd Term 26
Eliminating e-transitions NFA: e, q q q 2 e NFA without es: q q q 2 {q, q, q 2 } {q, q, q 2 } {q, q 2 } Accepting states: q 2, q, q 2 T. Najla Arfawi - 2nd Term 26
Eliminating e-transitions NFA: e, q q q 2 e new NFA: q q q 2 {q, q, q 2 } {q, q, q 2 } {q, q 2 } 3 T. Najla Arfawi - 2nd Term 26, q q q 2,
Eliminating e-transitions Paths with es are replaced by a single transition q e 5 q a q e 2 q e q 3 q 3 e q 4 a e q5 q a 5 q 3 States that can reach final state by e are all accepting q 3 a 4 T. Najla Arfawi - 2nd Term 26 q 9 q 7 e q 3 e q 2 e
Regular Expression The symbols and e are regular expressions Every a in S is a regular expression If R and S are regular expressions, so are RUS, RS and R* Remember for R*, * could be 5 T. Najla Arfawi - 2nd Term 26
Regular Languages Languages that can be represented by a DFA / NFA / RE DFA = NFA = RE (in term of equivalence) When asked if a language L is regular, represent L using a DFA / NFA / RE 6 T. Najla Arfawi - 2nd Term 26
Checklist. Always pay attention to S 2. For DFA, out degree of each state must be equal S e.g. S = {,, 2}, L = {w: w begins with } 3. For NFA, pay attention to e-transitions 4. Remember to specify the initial state 5. Make sure you have considered the input e e.g. L = {w: w contains even number of } 7 T. Najla Arfawi - 2nd Term 26
Exercise S = {, } L = {w: w is non-empty and the sum of the digits in w is divisible by 5} 8 T. Najla Arfawi - 2nd Term 26
Idea Need to keep track of the sum of digits Arrive at the state q k when sum of digits = k q q q 2 q 3 q 4 q 5 9 T. Najla Arfawi - 2nd Term 26
Idea Need to keep track of the sum of digits Arrive at the state q k when sum of digits = k k divisible by 5 iff k mod 5 = Keep track of the remainder instead q q q 2 q 3 q 4 2 T. Najla Arfawi - 2nd Term 26
Answer Handle the input e q s q q q 2 q 3 q 4 2 T. Najla Arfawi - 2nd Term 26
Converting an NFA to a DFA Eliminate e q q e q 2 for each state i for each alphabet x for each state j q q q 2 if i can reach j using one x and e(s) add an edge from i to j with label x e q q q 2 q q q 2 22 e q q q 2 T. Najla Arfawi - 2nd Term 26 e q 3 q q q 2 q 3
Converting an NFA to a DFA Eliminate e Remember to consider the case when the initial state can reach accepting states on e(s) e q q q 2 q q q 2 23 T. Najla Arfawi - 2nd Term 26
Converting an NFA to a DFA Every possible subsets of Q is a state in the DFA Going to multiple states at the same time in the NFA = going to a subset of Q, which is now a state in the DFA q q q 2 {q } {q, q 2 } 24 T. Najla Arfawi - 2nd Term 26
Exercise q e q, q 2 25 T. Najla Arfawi - 2nd Term 26
Eliminating e-transitions q can reach q on (q -> q -> q ) q can reach q on (q -> q ) q can reach q on (q -> q ) q can reach q on (q -> q ) The rest of the transitions remain unchanged q e q q q, q q 2, q q, q q 2 q q, q 2 26 q 2 T. Najla Arfawi - 2nd Term 26
Eliminating e-transitions q is the initial state q can reach an accepting state on e (i.e. q ) therefore q is also an accepting state q q, q q 2 q q, q q e q q q q 2 q q, q 2,, 27 T. Najla Arfawi - 2nd Term 26 q 2 q 2
Converting NFA to DFA {q } {q, q } {q 2 } {q } {q, q } {q 2 } {q } {q, q 2 } q q, q 2 28 T. Najla Arfawi - 2nd Term 26
Converting NFA to DFA Starting from initial state {q } {q } {q, q } {q 2 } {q } {q, q } {q 2 } {q } {q, q 2 } {q } {q, q } {q 2 } 29 T. Najla Arfawi - 2nd Term 26
Converting NFA to DFA Check if every state has S outgoing transitions No Fix {q 2 } and {q, q } {q, q } goes to {q, q } {q, q } = {q, q } on {q, q } goes to {q 2 } = {q 2 } on {q } {q, q } {q } {q, q } {q 2 } {q } {q, q } {q 2 } {q } {q, q 2 } {q } {q 2 } 3 T. Najla Arfawi - 2nd Term 26 {q, q 2 }
Converting NFA to DFA Check if every state has S outgoing transitions No Fix {q } and {q, q 2 } {q, q 2 } goes to {q, q } {q } = {q, q } on {q, q 2 } goes to {q, q 2 } = {q, q 2 } on {q } {q, q } {q 2 } {q } {q, q } {q 2 } {q } {q, q 2 } {q } {q, q } {q 2 } {q } 3 T. Najla Arfawi - 2nd Term 26 {q, q 2 }
Converting NFA to DFA Check if every state has S outgoing transitions No Fix = die {q } {q, q } {q } {q, q } {q 2 } {q } {q, q } {q } {q 2 } {q } {q, q 2 } {q, 2 } 32 T. Najla Arfawi - 2nd Term 26 {q, q 2 }
Converting NFA to DFA Check if every state has S outgoing transitions Yes Accepting states = states that contain q or q q q, q 2 {q } {q, q } {q 2 } {q } {q, q } {q 2 } {q } {q, q 2 } {q } {q, q } {q 2 } {q }, 33 T. Najla Arfawi - 2nd Term 26 {q, q 2 }
Converting NFA to DFA Done q e q {q } {q, q }, q 2 {q 2 } {q }, {q, q 2 } 34 T. Najla Arfawi - 2nd Term 26
Regular expressions 35 T. Najla Arfawi - 2nd Term 26
String concatenation s = abb t = bab st st = abbbab abbbab ts ts = bababb bababb ss ss = abbabb abbabb sst sst = abbabbbab abbabbbab s = x x n t = y y m st = x x n y y m 36 T. Najla Arfawi - 2nd Term 26
Operations on languages The concatenation of languages L and L 2 is The n-th power of L n is The union of L and L 2 is L L 2 = {st: s L, t L 2 } L n = {s s 2...s n : s, s 2,..., s n L} L L 2 = {s: s L or s L 2 } 37 T. Najla Arfawi - 2nd Term 26
Operations on languages The star of L are all strings made up of zero or more chunks from L: L * = L L L 2 Example: L = {, }, L 2 = {e,,,, }. What is L * and L 2*? 38 T. Najla Arfawi - 2nd Term 26
Example L = {, } L * : is in L * is not in L * L = {e} L = {, } L 2 = {,,, } L 3 = {,,,,,,, } is not in L * L * are all strings that start with and do not contain consecutive s (plus the empty string) 39 T. Najla Arfawi - 2nd Term 26
Example L 2 = {e,,,, } any number of s L 2 = {e} L 2 * = L 2 L 2 L 2 2 L 2 = L 2 L 2 2 = L 2 L 2 n = L 2 (n ) = {e} L 2 L 2 2 = L 2 L 2 * = L 2 4 T. Najla Arfawi - 2nd Term 26
Combining languages We can construct languages by starting with simple ones, like {}, {} and combining them {}({} {})* ( + )* all strings that start with ({}{}*) ({}{}*) * + * followed by any number of s, or followed by any number of s 4 T. Najla Arfawi - 2nd Term 26
Regular expressions A regular expression over S is an expression formed using the following rules: The symbols and e are regular expressions Every a in S is a regular expression If R and S are regular expressions, so are R+S, RS and R*. + is union operator U A language is regular if it is represented by a regular expression 42 T. Najla Arfawi - 2nd Term 26
Analyzing regular expressions S = {, } * = (*) = {,,,, } followed by any number of s (*)() = {,,,, } followed by any number of s and then 43 T. Najla Arfawi - 2nd Term 26
Analyzing regular expressions + = {, } strings of length (+)* = {e,,,,,,, } any string (+)* any string that ends in (+)*(+)* any string that contatins the pattern 44 T. Najla Arfawi - 2nd Term 26
Analyzing regular expressions ((+)(+))*+((+)(+)(+))* all strings whose length is even or a mutliple of 3 = strings of length, 2, 3, 4, 6, 8, 9,, 2,... ((+)(+))* strings of even length (+)(+) strings of length 2 ((+)(+)(+))* strings of length a multiple of 3 (+)(+)(+) strings of length 3 45 T. Najla Arfawi - 2nd Term 26
Analyzing regular expressions ((+)(+)+(+)(+)(+))* strings that can be broken in blocks, where each block has length 2 or 3 (+)(+)+(+)(+)(+) strings of length 2 or 3 (+)(+) (+)(+)(+) strings of length 2 strings of length 3 46 T. Najla Arfawi - 2nd Term 26
Analyzing regular expressions ((+)(+)+(+)(+)(+))* strings that can be broken in blocks, where each block has length 2 or 3 e this includes all strings except those of length ((+)(+)+(+)(+)(+))* = all strings except and 47 T. Najla Arfawi - 2nd Term 26
Analyzing regular expressions (++)*(e++) ends in at most two s there can be at most two s between consecutive s there are never three consecutive s Guess: (++)*(e++) = {x: x does not contain } e 48 T. Najla Arfawi - 2nd Term 26
Writing regular expressions Write a regular expression for all strings with two consecutive s. S = {, } (anything) (anything else) (+)*(+)* 49 T. Najla Arfawi - 2nd Term 26
Writing regular expressions Write a regular expression for all strings that do not contain two consecutive s. S = {, } some s at every maybe a at the end the beginning followed by one or more s... every middle followed by one or more s... and at most one in the last block (*) (e + ) *(*)*(e + ) 5 T. Najla Arfawi - 2nd Term 26
Writing regular expressions Write a regular expression for all strings with an even number of s. S = {, } even number of zeros = (two zeros)* two zeros = *** (***)* 5 T. Najla Arfawi - 2nd Term 26