class: center, middle # CS-3110: Formal Languages and Automata ## Nondeterministic Finite Automata ### Chapter 3.5 --- # Non-Determinism * Recall **Deterministic** Finite Automata: The transition function tells us exactly what to do next * What about an "or" in a regular expression? * For example: In our `formatTaxes` example we can have `form1040` *or* `form1041` as a parameter * Another case: After one parameter there can be another paramter *or* a closing parenthesis --- # Non-Determinism * It is possible to "encode" these decisions in a deterministic transition function, but it is not straightforward * Instead, what if we allow our automaton to "try" multiple options? * When *any* of these options results in an accept, the automaton returns "yes"/"accept" --- # A Simpler Example Take the regular expression `a*ab(ab)*` * This does not even contain an "or" * Basically, each word consists of two parts: - Arbitrarily many `a`s - Arbitrarily many (but at least one) `ab`s * We can recognize each part pretty easily with separate automata * The "trick" is when we should switch from one machine to the other --- # A Simpler Example
--- # A Simpler Example: Combination .left-column[
] .right-column[
a
b
q0a
q0a,q1b
q1a
q1a
q1a
q1a
q0b
q1b
q3b
q1b
q3b
q2b
q2b
q1b
q3b
q3b
q3b
q3b
] --- class: medium # Non-Determinism * Instead of explicitly choosing when to switch from one part to the other, we let the machine "try" * Basically: For every `a` we read we try if it is followed by a `b` (then we are in the second part), or if it is not * The machine will accept if it reaches the accepting state of the second part with *any* of these attempts * We will see this idea of constructing an automaton in "parts" that are then connected again later --- # Definition of a Non-Deterministic Finite Automaton A Non-Deterministic Finite Automaton consists of: * A finite set of states Q * A finite alphabet `\(\Sigma\)` * A transition function `\(\partial: Q \times \Sigma \mapsto P(Q)\)` * An initial state `\(q_0 \in Q\)` * A set of accepting states `\(F \subseteq Q\)` --- # Definition of a Non-Deterministic Finite Automaton A Non-Deterministic Finite Automaton consists of: * A finite set of states Q * A finite alphabet `\(\Sigma\)` * A transition function `\(\partial: Q \times \Sigma \mapsto {\color{red} {P(Q)}}\)` * An initial state `\(q_0 \in Q\)` * A set of accepting states `\(F \subseteq Q\)` --- # Some Observations * Every DFA is also an NFA, with `\(\partial(q,a) = \{\delta(q,a)\}\)` * The power set of Q contains the empty set: In any state reading a character may result in **no** successor states * In a DFA we defined an extended transition function `\(\delta^*\)` to determine the state we are in when we start in a particular state and pass a sequence of characters to the automaton --- # Another Example
This automaton ensures that the second-to-last character is an `a`. Let's write down the transition function. --- # Extended Transition Function * What would the extended transition function be? * Starting in a particular state, and passing a sequence of characters to our automaton, which states would the automaton be in? $$ \partial^\ast(q_0, \varepsilon) = \\{q_0\\}\\\\ \partial^\ast(q_0, w\cdot{}a) = \\{q | \exists q_w: q_w \in \partial^\ast(q_0, w) \wedge q \in \partial(q_w,a)\\} $$ Basically, we follow all possible "paths" defined by `\(\partial\)` and return the set of **all** possible states we end up in. --- # Back to our example .left-column[
] .right-column[
a
b
q0
q0,q1
q0
q1
q2
q2
q2
]
--- # Language Defined by an NFA * The idea behind the non-determinism was to let the automaton "guess" which way may be the correct one * `\(\partial^*\)` gives us all **possible** states * We accept if at least one of them is an accepting state $$ L(M) = \\{w\in \Sigma^\ast | \partial^*(q_0, w) \cap F \neq \emptyset\\} $$ --- # Epsilon Transitions * If you look at the textbook, the definition I have given so far is *slightly* different * Here is one more thing we could allow: Change states **without** reading input * Or, expressed another way: Treat `\(\varepsilon\)` as a character that may appear anywhere * This has some advantages when assembling NFAs --- # Definition of an `\(\varepsilon\)`-NFA A Non-Deterministic Finite Automaton with `\(\varepsilon\)` transitions consists of: * A finite set of states Q * A finite alphabet `\(\Sigma\)` * A transition function `\(\delta: Q \times (\Sigma \cup \{ \varepsilon \}) \mapsto P(Q)\)` * An initial state `\(q_0 \in Q\)` * A set of accepting states `\(F \subseteq Q\)` --- # Definition of an `\(\varepsilon\)`-NFA A Non-Deterministic Finite Automaton with `\(\varepsilon\)` transitions consists of: * A finite set of states Q * A finite alphabet `\(\Sigma\)` * A transition function `\(\delta: Q \times (\Sigma \color{red}{\cup \{ \varepsilon \}}) \mapsto P(Q)\)` * An initial state `\(q_0 \in Q\)` * A set of accepting states `\(F \subseteq Q\)` --- # Epsilon Transitions * `\(\varepsilon\)`-transitions don't actually add any capabilities * They just make writing the automata nicer * We will see how they make combining NFAs easier * But they may add some complications to our formulas --- # Extended Transition Function Recall: $$ \partial^\ast(q_0, \varepsilon) = \\{q_0\\}\\\\ \partial^\ast(q_0, w\cdot{}a) = \\{q | \exists q_w: q_w \in \partial^\ast(q_0, w) \wedge q \in \partial(q_w,a)\\} $$ With `\(\varepsilon\)`-transitions this is no longer accurate! `\(\partial^\ast(q_0, \varepsilon)\)` **may** include other states! --- class: medium # Getting rid of `\(\varepsilon\)` * As mentioned, `\(\varepsilon\)`-transitions don't add any "capabilities" * We can convert an automaton with `\(\varepsilon\)`-transitions to one without * To do this, we need to calculate the `\(\varepsilon\)`-closure of every state: The states that are reachable by only following `\(\varepsilon\)`-transitions * Then we just replace each state with its `\(\varepsilon\)`-closure Whenever we want to prove or construct something, we can freely choose between using/allowing `\(\varepsilon\)`-transitions or not. --- # Another Example ### Construct an NFA that accepts all words over the alphabet `\(\Sigma = \{0,1\}\)` that contain an even number of `0`s or an even number of `1`s. "or" sounds a lot like "non-determinism" ... We just need the two parts --- # First step First, let us construct an automaton that accepts words with an even number of `0`s:
-- The automaton that accepts words with an even number of `1`s looks very similar:
--- # Combination To combine them, we just need to allow the automaton to choose "both":
--- # Removing Epsilon Transitions We replace the state `q0` with a new state that is a combination of all states reachable with `\(\varepsilon\)`-transitions:
--- # DFAs, NFAs and Regular Expressions * Non-Deterministic Automata can represent any regular expression * Any NFA can also be translated to an equivalent DFA! * We will see both of these conversions next time --- # References * [Finite State Automata Simulator](http://ivanzuzak.info/noam/webapps/fsm_simulator/)