class: center, middle # CS-3110: Formal Languages and Automata ## Deterministic Finite Automata --- class: center, middle # Example 1: DFA interpretion --- # Example 1 Given the DFA `M` on the next slide, determine:
Then give an English description of the language the automaton accepts. --- # Example 1
--- # Example 1
* What does this automaton "do"? * Maybe we can find some sort of interpretation for the states? * How can we get to an accepting state? --- class: medium # Example 1
* We start in state `\(q_{00}\)` * We can add consecutive **pairs** of `0`s and `1`s to get back there * When we add the first `0`, we get to state `\(q_{10}\)` * The **only** way to get back to our accepting state is by adding (at least) one more `0` --- class: medium # Example 1
* What are the other options to get to `\(q_{10}\)`? * We can add a `1`, then a `0`, and then another `1` * We can add a `1`, then add **three** `0`s, and then another `1` * Suspicion: We always add an odd number of `0`s -- * ... and an even number of `1`s --- class: medium # Example 1
* Let's extend our suspicions to the other states * Each state is reachable after an even/odd number of `0`s/`1`s has been added * There is one state for each combination * Our accepting state corresponds to words with an even number of `0`s and an even number of `1`s --- class: medium # Example 1
### This automaton recognizes the language over `\(\Sigma = \{0,1\}\)` consisting of words with an even number of `0`s **and** an even number of `1`s --- class: center, middle # Example 2: DFA --- # Example 2 ## Construct a Deterministic Finite Automaton that accepts words over the alphabet `\(\Sigma = \{0,1\}\)` that represent binary numbers (without leading zeros) that are divisible by 3 --- # The Horror Remember from 2 weeks ago? $$ L_4 = \\{0\\} \cup 1(01^\ast0)^\ast10^\ast (1(01^\ast0)^\ast10^\ast)^\ast $$ As promised then, this is much nicer with an automaton! --- # Basic Idea * How did we come up with this monstrosity? * We considered how **adding digits** to a number would change its divisibility * We did this by keeping track of the remainder, and how it changes when we add a digit * We can do this "keeping track" using states in a DFA, and only accept when the remainder (=state) is 0! --- # Remainder vs. adding digits
Add a
0
1
Remainder
0
0
1
1
2
0
2
1
2
This is basically our transition function! We just need to add some extra pieces to handle the number "0" --- class: medium # Automaton Our automaton will consist of two parts * One part will handle the number "0": If we read a `0`, we go to an accepting state, but if we read **anything** after that, we go to a "bad" state * The second part will handle numbers starting with 1: For each digit we add, we keep track what the remainder for a division by 3 would be. We only accept if we are in the state representing "remainder 0" * From the start state, if we read a `0` we go to the first part, and if we read a `1` we go to the second part --- # Automaton: Recognizing 0
Only accept if you read a single `0`. Any `1` or any additional `0` will lead to the state `\(q_2\)`, which is not an accepting state. --- # Automaton: Divisibility
Add a
0
1
Remainder
0
0
1
1
2
0
2
1
2
--- # Full Automaton * Now we need to connect these two automata * Note: Our second automaton would allow leading `0`s. * Here's what we do: Use the start state from the first automaton, if we read a `0`, we stay there, but if we read a `1`, we move to the second automaton (and set our "current remainder" to 1) * We accept in either of the accepting states of the two automata --- # DFA
Start
Reset
Step backward
Read next
Read all
--- # Mojo * [noam](https://github.com/izuzak/noam) is the JavaScript library to simulate DFAs * [The FSM Simulator](http://ivanzuzak.info/noam/webapps/fsm_simulator/) served as inspiration * [FSM Simulator Source](https://github.com/izuzak/noam/tree/master/webapps/fsm_simulator) * Behind the scenes, [Viz.js](https://github.com/mdaines/viz.js) does the graph visualization