-
Notifications
You must be signed in to change notification settings - Fork 26
/
minimizeDFA.ts
53 lines (48 loc) · 1.86 KB
/
minimizeDFA.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import NFAToDFA from './NFAToDFA';
import fromTransitionTable from './fromTransitionTable';
import removeDeadStates from './removeDeadStates';
import removeEpsilons from './removeEpsilons';
import removeUnreachableStates from './removeUnreachableStates';
import toTransitionTable from './toTransitionTable';
import transposeTable from './transposeTable';
import type {NFA} from './NFA';
import type {TransitionTable} from './TransitionTable';
/**
* Minimizes a DFA (ie. returns an equivalent DFA that recognizes the same
* language using the minimum possible number of states).
*
* We remove "dead" states (states that don't lead to an accept state) and
* "unreachable" states (states that can't be reached from a start state), as
* described in: https://en.wikipedia.org/wiki/DFA_minimization
*
* We use Brzozowski's (1962) algorithm to merge non-distinguisable states:
*
* > Essentially, the algorithm computes the automaton D(R(D(R(A)))), where D(A)
* > computes the determinization of A by the well-known subset construction and
* > R(A) is the reverse automaton of A."
*
* From: "DFA minimization: from Brzozowski to Hopcroft", by García, López, and
* Vázquez de Parga
* (https://m.riunet.upv.es/bitstream/handle/10251/27623/partial%20rev%20determ.pdf)
*/
export default function minimizeDFA(dfa: NFA): NFA {
let table: TransitionTable;
let nfa: NFA;
// Reverse the DFA.
dfa = removeUnreachableStates(dfa);
dfa = removeDeadStates(dfa);
table = toTransitionTable(dfa);
table = transposeTable(table);
nfa = fromTransitionTable(table);
nfa = removeEpsilons(nfa);
dfa = NFAToDFA(nfa);
// Un-reverse the reversed DFA.
dfa = removeUnreachableStates(dfa);
dfa = removeDeadStates(dfa);
table = toTransitionTable(dfa);
table = transposeTable(table);
nfa = fromTransitionTable(table);
nfa = removeEpsilons(nfa);
dfa = NFAToDFA(nfa);
return dfa;
}