# Class 08: From NFA to Optimal DFA

Held Wednesday, February 7, 2001

Summary

Today we continue our consideration of how to move from the readable but declarative regular exprssion notation to the executable but obtuse finite automaton notation.

Assignments

Notes

• Quiz! (May be continued in lab tomorrow.)
• How are things going?
• I've rearranged the syllabus slightly.

Overview

• From NFA to DFA
• From DFA to optimal DFA
• Reminder: Lexical analysis using automata

## From NFA to DFA

• How do we turn this lovely NFA into a deterministic computing machine?
• Basically, we build a DFA that simultaneously follows all the paths that we can go through in the NFA.
• Each state in the DFA is a set of states in the corresponding NFA.
• The algorithm is again fairly simple
• Terminology :
• qx is a state in the NFA and q0 is its start state
• QX is a state in the DFA and Q0 is its start state
• The eqsilon-closure of a DFA state consists of all the states in the NFA that we can get to with epsilon moves from the states in the DFA state.
```Q0 = { q0 }
// but there are some states we can reach from q0 at no cost
Q0 = epsilon-closure(Q0)
while there are states we haven't processed
pick one such state, Qn
for each symbol s
let tmp be a new set
for each q in Qn
add delta(q,s) to tmp
end for
tmp = epsilon-closure(tmp)
if tmp is not in the DFA then
let Qi be a new state
Qi = tmp
add Qi to the DFA
else
let Qi be the state equivalent to tmp
end if
add an edge from Qn to Qi in the automaton
end for
end while
for each Qi
if there is a q in Qi that is a final state then
Qi is a final state
end if
end for
```
• When we make the determination of a final state, we use the associated token type of the highest priority final state in the NFA.

## From DFA to Optimal DFA

• As you may be able to tell, the resulting DFA is fairly large.
• Can we build an equivalent DFA that's smaller? Yes!
• Strategy: Group states into sets of equivalent states.
```Assume all non-final states can be treated as the same state
Assume all final states can be treated as the same state
For each group of states treated as equivalent
as the same state
For each symbol, s
If there are two "equivalent" states q1,q2 such that
edge(q1,s) and edge(q2,s) lead to non-equivalent states,
split q1 and q2 into different equivalencies
figure out where the other states in the equivalency go
End For // each symbol
End for // each pair of states
```

## History

Monday, 22 January 2001

• Created as a blank outline.

Disclaimer: I usually create these pages on the fly. This means that they are rarely proofread and may contain bad grammar and incorrect details. It also means that I may update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This page was generated by Siteweaver on Mon Apr 30 10:51:48 2001.
This page may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2001S/outline.08.html`.
You may validate this page's HTML.
The source was last modified Mon Jan 22 10:39:10 2001.