====== 2. Deterministic Finite Automata ====== ===== 2.1. DFA practice ===== * Blue = final state * Orange = non-final state * Sink state = a state that is not final; once reached, there is no transition that leaves it, thus the DFA will reject; there is a transition that loops in this state for each possible character === Write DFAs for the following languages: === **2.1.1.** $ L=\{w \in \{0,1\}^* \text{ | w contains an odd number of ones} \} $ {{:lfa:2022:lfa2022_lab2_ex1.png?400|}} * State 0 = currently counted an even number of 1s [initial state] * State 1 = currently counted an odd number of 1s [final state] * When we read a 0, we stay on the same state, as we are only counting ones. * When we find a one: * If we were on state 0, it means we counted until the previous step an even number of ones. Now we have an odd number of ones, so we need to move to state 1. * If we were on state 1, it means we counted until the previous step an odd number of ones. Now we have an even number of ones, so we need to move to state 0. **2.1.2.** The language of binary words which contain **exactly** two ones {{:lfa:2022:lfa2022_lab2_ex2.png?550|}} * State 0: no ones have been found yet [initial state] * State 1: 1 one have been found so far * State 2: 2 ones have been found so far [final state] * State 3: more than 2 ones have been found so far [sink state] **2.1.3.** The language of binary words which encode odd numbers (the last digit is least significative) {{:lfa:2022:lfa2022_lab2_ex3.png?300|}} * We pass through the word, marking the parity of the last character that we have read. **2.1.4.** (hard) The language of words which encode numbers divisible by 3 {{:lfa:2022:lfa2022_lab2_ex4.png?400|}} * State 0: 3k [initial state and final state] * State 1: 3k + 1 * State 2: 3k + 2 * When we read a 0, the number we had so far is multiplied by 2: * 3k --(*2)--> 3k * 3k + 1 --(*2)--> 3k + 2 * 3k + 2 --(*2)--> 3k + 1 * When we read a 1, the number we had so far is multiplied by 2 and we also add 1: * 3k --(*2)--> 3k --(+1)--> 3k + 1 * 3k + 1 --(*2)--> 3k + 2 --(+1)--> 3k * 3k + 2 --(*2)--> 3k + 1 --(+1)--> 3k + 2 **2.1.5.** The language of words that encode a calendar date (DD/MM/YYYY) (do not overthink about the actual number of days in a month) {{:lfa:2022:lfa2022_lab2_ex5.png?600|}} * Note: Checking for months with 30 or 31 days can be done with a slightly bigger DFA. * Note: Even checking for bisect years (29 Feb) can still be done with a DFA, but it's too big to do as a seminar example. **2.1.6.** (HARD) $ L=\{w \in \{0,1,2,3\}^* \text{ | w follows the rule that every zero is immediately followed by a sequence of at least 2 consecutive threes and every one is immediately followed by a sequence of at most 2 consecutive twos} \} $ {{:lfa:2022:lfa2022_lab2_ex6_v2.png?400|}} {{:lfa:2022:lfa2022_lab2_ex6.png?400|}} * injuraturile redirectati-le catre AU :)) (si MP) * All missing transitions lead to a sink state (where there is a transition that loops in that state for any character) **2.1.7.** The set of all binary strings having the substring 00101 {{:lfa:2022:ex_8_dfa.png?400|}} * Every state corresponds to a certain current configuration (eg. state **B** translates to a sequence of **0**; **C** to a seq of **00**; **D** -> **001** and so on) * Analyze how each incoming character changes the current available sequence. For example, if we are in state **E** and we read character **1** we reach a final state, but if we read **0** we go back to state **C** since the available seq will be **00** === Describe the language of the following DFAs and simulate them on the given inputs: === **2.1.8.** w1 = 10011110, w2 = 00110010 {{:lfa:2022:lfa2022_lab2_ex8.png?400|}} * The language of binary words where there are no sequences in which the same character repeats 3 or more times. * regex: $math[(11 \cup 1 \cup \epsilon) ( (00 \cup 0) 1 (1 \cup \epsilon) )^* (00 \cup 0 \cup \epsilon)] * (0, **10011110**) |--A (11, 0011110) |--A (01, 011110) |--A (02, 11110) |--A (11, 1110) |--A (12, 110) [There is no transition, so we go to a sink state which is not figured on the drawing.] |--A (sink, 10) |--A (sink, 0) |--A (sink, $math[\epsilon]) The sink state is not a final state, so this word is **rejected**. * (0, **00110010**) |--A (01, 0110010) |--A (02, 110010) |--A (11, 10010) |--A (12, 0010) |--A (01, 010) |--A (02, 10) |--A (11, 0) |--A (01, $math[\epsilon]) The word is **accepted** by the DFA because we reached a final state (blue).