Decision, Computation and Language Non-Deterministic Finite Automata (NFA) Dr. Muhammad S Khan (mskhan@liv.ac.uk) Ashton Building, Room G22 http://www.csc.liv.ac.uk/~khan/comp218
Finite State Automata (FSA) Deterministic On each input there is one and only one state to which the automaton can transition from its current state Nondeterministic An automaton can be in several states at once M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 2
Non-Deterministic Finite Automata nondeterministic - an input string does not determine the final state. This is because given a current state and an input letter, we may specify a set of allowable new states, not just one. Then we say that a NFA accepts an input word w if there exists a sequence of transitions labelled by symbols in w, starting from initial state and ending at some accepting state. M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 3
Non-Deterministic Finite Automata A NFA has the power to be in several states at once Each NFA accepts a language that is also accepted by some DFA NFA are often more succinct and easier than DFAs We can always convert an NFA to a DFA, but the latter may have exponentially more states than the NFA. The difference between the DFA and the NFA is the type of transition function δ For a NFA δ is a function that takes a state and input symbol as arguments (like the DFA transition function), but returns a set of zero or more states (rather than returning exactly one state, as the DFA must) M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 4
Non-deterministic Finite Automata (NFA) A Non-deterministic Finite Automaton (NFA) is of course non-deterministic Implying that the machine can exist in more than one state at the same time Transitions could be non-deterministic q i 1 1 q j q k Each transition function therefore maps to a set of states M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 5
Non-deterministic Finite Automata (NFA) A Non-deterministic Finite Automaton (NFA) consists of: Q is a finite set of states is a finite set of input symbols (alphabet) q 0 Q is the start state F F Q is a set of final or accepting states δ, the transition function is a function that takes a state Q and an input symbol in as argument and returns a subset of Q, a mapping between Q S Q. An NFA is also a quintuple: {Q,, q 0,F, δ } The only difference between a NFA and a DFA is in the type of value that δ returns M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 6
The Formal Definition M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 7
How to use an NFA? Input: a word w in * Question: Is w acceptable by the NFA? Steps: Start at the start state q 0 For every input symbol in the sequence w do Determine all possible next states from all current states, given the current input symbol in w and the transition function If after all symbols in w are consumed and if at least one of the current states is a final state then accept w; Otherwise, reject w. M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 8
states NFA for strings containing 0,1 Regular expression: (0+1)*01(0+1)* Why is this non-deterministic? start 0,1 0,1 q 0 0 q 1 1 q 2 Final state What will happen if at state q 1 an input of 0 is received? Q = {q 0,q 1,q 2 } = {0,1} start state = q 0 F = {q 2 } Transition table symbols 0 1 q 0 {q 0,q 1 } {q 0 } q 1 Φ {q 2 } *q 2 {q 2 } {q 2 } M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 9
What is a dead state? Note: Explicitly specifying dead states is just a matter of design convenience, and this feature does not make a machine deterministic or non-deterministic. A DFA for recognizing the key word while q 0 w q 1 h q 2 i q 3 l q 4 e q 5 Any other input symbol An NFA for the same purpose: q dead Any symbol q 0 w q 1 h q 2 i q 3 l q 4 e q 5 Transitions into a dead state are implicit M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 10
Example M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 11
Example M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 12
Example Build an NFA for the following language: L = { w w ends in 01} Other examples Keyword recognizer (e.g., if, then, else, while, for, include, etc.) M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 13
Extension of δ/ø to NFA Paths M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 14
Notation: accepting / rejecting paths M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 15
Example M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 16
Equivalent DFA M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 17
Differences: DFA vs. NFA But, DFAs and NFAs are equivalent in their power to capture languages!! DFA 1. All transitions are deterministic Each transition leads to exactly one state 2. For each state, transition on all possible symbols (alphabet) should be defined 3. Accepts input if the last state is in F 4. Sometimes harder to construct because of the number of states 5. Practical implementation is feasible NFA 1. Some transitions could be nondeterministic A transition could lead to a subset of states 2. Not all symbol transitions need to be defined explicitly (if undefined will go to a dead state this is just a design convenience, not to be confused with non-determinism ) 3. Accepts input if one of the last states is in F 4. Generally easier than a DFA to construct 5. Practical implementation has to be deterministic (convert to DFA) or in the form of parallelism M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 18
A DFA is a NFA......in the sense that a DFA is a restricted form of NFA. In a DFA, the set of all new states that can be reached in a single transition from any given state and any letter in the alphabet, is of size 0 or 1. (In a NFA, we allow the set of new states to be larger.) Hence, any language accepted by a DFA is accepted by an NFA. The DFA in practice has about as many states as the NFA, although it has more transitions. M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 19
A few subtle properties of DFAs and NFAs The machine never really terminates. It is always waiting for the next input symbol or making transitions. The machine decides when to consume the next symbol from the input and when to ignore it. (but the machine can never skip a symbol) => A transition can happen even without really consuming an input symbol (think of consuming as a free token) if this happens, then it becomes an -NFA (see next few slides). A single transition cannot consume more than one (non- ) symbol. M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 20
FA with -Transitions We can allow explicit -transitions in finite automata i.e., a transition from one state to another state without consuming any additional input symbol Makes it easier sometimes to construct NFAs Definition: -NFAs are those NFAs with at least one explicit -transition defined. -NFAs have one more column in their transition table M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 21
Example of an -NFA L = {w w is empty, or if non-empty will end in 01} 0,1 start q 0 0 q 0 q 1 δ E 0 1 *q 0 Ø Ø {q 0,q 0 } 1 q 0 {q 0,q 1 } {q 0 } {q 0 } q 1 Ø {q 2 } {q 1 } q 2 ECLOSE(q 0 ) ECLOSE(q 0 ) ECLOSE(q 1 ) -closure of a state q, ECLOSE(q), is the set of all states (including itself) that can be reached from q by repeatedly making an arbitrary number of - transitions. *q 2 Ø Ø {q 2 } ECLOSE(q 2 ) M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 22
Example of an -NFA L = {w w is empty, or if non-empty will end in 01} 0,1 Simulate for w=101: 0 q 0 q 1 1 q 2 q 0 start q 0 q 0 1 q 0 1 δ E 0 1 *q 0 Ø Ø {q 0,q 0 } ECLOSE(q 0 ) Ø x 0 1 q 0 q 1 q 0 {q 0,q 1 } {q 0 } {q 0 } ECLOSE(q 0 ) q 2 q 1 Ø {q 2 } {q 1 } *q 2 Ø Ø {q 2 } M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 23
Example of another -NFA start q 0 0,1 0 q 0 q 1 q 3 1 1 q 2 δ E 0 1 *q 0 Ø Ø {q 0,q 0,q 3 } q 0 {q 0,q 1 } {q 0 } {q 0, q 3 } q 1 Ø {q 2 } {q 1 } *q 2 Ø Ø {q 2 } q 3 Ø {q 2 } {q 3 } M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 24
Equivalency of DFA, NFA, -NFA Theorem: A language L is accepted by some -NFA if and only if L is accepted by some DFA Implication: DFA NFA -NFA (all accept Regular Languages) M S Khan (Univ. of Liverpool) COMP218 Decision, Computation and Language 25