# Review NFA Definition NFA is nondeterministic in what

• Slides: 7

• Review: – NFA Definition – NFA is non-deterministic in what sense? – Time complexity of the algorithm to determine whether a string can be recognized by an NFA. – Algorithm to convert a regular expression to an NFA.

• The algorithm that recognizes the language accepted by NFA(revisit). – Input: an NFA (transition table) and a string x (terminated by eof). – output “yes” if accepted, “no” otherwise. S = e-closure({s 0}); a = nextchar; while a != eof do begin S = e-closure(move(S, a)); a : = next char; end if (intersect (S, F) != empty) then return “yes” else return “no” Time complexity O(|S|^2|x|) : With move({s}, a) may also be a set. transition e-closure is a set, Converting a NFA to a DFA that recognizes the same language: starting from and assign each set to a new state. Example: Figure 3. 27 in page 120.

• Algorithm to convert an NFA to a DFA that accepts the same language (algorithm 3. 2, page 118) initially e-closure(s 0) is the only state in Dstates and it is marked while there is an unmarked state T in Dstates do begin mark T; for each input symbol a do begin U : = e-closure(move(T, a)); if (U is not in Dstates) then add U as an unmarked state to Dstates; Dtran[T, a] : = U; end; Initial state = e-closure(s 0), Final state = ?

• Question: – for a NFA with |S| states, at most how many states can its corresponding DFA have? – Using DFA or NFA? ? Trade-off between space and time!!

• The number of states determines the space complexity. • A DFA can potentially have a large number of states. • Converting an NFA to a DFA may not result in the minimum-state DFA. • In the final product, we would like to construct a DFA with the minimum number of states (while still recognizing the same language). – Basic idea: assuming all states have a transition on every input symbol (what if this is not the case? ? ), find all groups of states that can be distinguished by some input strings. An input string w distinguishes two states s and t, if starting from s and feeding w, we end up in a non-accepting state while starting from t and feeding w, we end up in an accepting state, or vice versa.

• Algorithm (3. 6, page 142): – Input: a DFA M – output: a minimum state DFA M’ • If some states in M ignore some inputs, add transitions to a “dead” state. • Let P = {All accepting states, All nonaccepting states} • Let P’ = {} • Loop: for each group G in P do Partition G into subgroups so that s and t (in G) belong to the same subgroup if and only if each input a moves s and t to the same state of the same group in P put the new subgroups in P’ if (P != P’) {P = P’; goto loop} • Remove any dead states and unreachable states (transition between groups can be inferred).

– Example: minimize the DFA for Fig 3. 29 (pages 121) • Lex implementation: – Regular expression NFA DFA optimized DFA – How to deal with multiple regular expressions?