Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
feat: implement DFA minimization
Browse files Browse the repository at this point in the history
- Developed an algorithm to minimize a Deterministic Finite Automaton (DFA) by merging equivalent states.
- Ensured the algorithm efficiently reduces the number of states while preserving the language recognized by the DFA.
- Validated the correctness of the minimization process through comprehensive unit tests.
  • Loading branch information
agicy committed Mar 27, 2024
1 parent c0793a3 commit 4dc4eff
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/algorithms/minimize_dfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,45 @@
__all__: list[str] = ["minimize_dfa"]


def _hopcroft(dfa: DFA) -> list[set[str]]:
P: list[set[str]] = [dfa.qf, dfa.Q - dfa.qf]
W: list[set[str]] = [dfa.qf]
while W:
A: set[str] = W.pop()
for c in dfa.T:
X = set(q for q in dfa.Q if (q, c) in dfa.delta and dfa.delta[(q, c)] in A)
for Y in P[:]:
if X & Y and (Y - X) & Y:
P.remove(Y)
P.append(X & Y)
P.append((Y - X) & Y)
if Y in W:
W.remove(Y)
W.append(X & Y)
W.append((Y - X) & Y)
else:
if len(X & Y) <= len((Y - X) & Y):
W.append(X & Y)
else:
W.append((Y - X) & Y)
return P


def minimize_dfa(dfa: DFA) -> DFA:
pass
partition: list[set[str]] = _hopcroft(dfa=dfa)
partition.reverse()
dic: dict[str, str] = {
state: f"q{i}"
for i, subset in enumerate(iterable=partition)
for state in subset
}
Q: set[str] = set(dic.values())
T: set[str] = dfa.T
delta: dict[tuple[str, str], str] = {
(dic[state], symbol): dic[dfa.delta[(state, symbol)]]
for state in dfa.Q
for symbol in dfa.T
}
q0: str = dic[dfa.q0]
qf: set[str] = {dic[state] for state in dfa.qf}
return DFA(Q=Q, T=T, delta=delta, q0=q0, qf=qf)

0 comments on commit 4dc4eff

Please sign in to comment.