akumm2k / finite-automata

MIT License
1 stars 0 forks source link

Incorrect NFA to DFA conversion #1

Closed akumm2k closed 2 years ago

akumm2k commented 2 years ago

The DFA produced here is incorrect.

> d = reg_to_dfa "ab*|ba*"
> d
Q: [0,1] 
delta: [(0 - a -> 1),(0 - b -> 1),(1 - a -> 1),(1 - b -> 1)] 
q0: 0 
F: [1]
> accepts d "aba"
True

The NFA produced here correctly accepts "aba".

> n = reg_to_nfa "ab*|ba*"
> n
Q: [0,1,2,3,4,5,6,7,8,9,10] 
delta: [(0 - \ -> 1, 6),(1 - a -> 2),(2 - \ -> 3),(3 - \ -> 4, 5),(5 - \ -> 3),(4 - b -> 5),(6 - b -> 7),(7 - \ -> 8),(8 - \ -> 9, 10),(10 - \ -> 8),(9 - a -> 10)] 
S0: [0] 
F: [5,10]
> accepts n "aba"
False
akumm2k commented 2 years ago

The bug seems to be in the minimization of dfa. reg_to_dfa = minimize . nfa_to_dfa . reg_to_nfa

> n = reg_to_nfa "ab*|ba*"
> d' = nfa_to_dfa n
> d' `accepts` "aba"
False

The dfa from the nfa to dfa conversion rejects "aba" correctly.

The bug is manifested in minimization here:

> d
Q: [0,1,2] 
delta: [(0 - a -> 1),(0 - b -> 2),(1 - b -> 1),(2 - a -> 2)] 
q0: 0 
F: [1,2]
> d `accepts` "aba"
False
> d' = minimize d
> d'
Q: [0,1] 
delta: [(0 - a -> 1),(0 - b -> 1),(1 - a -> 1),(1 - b -> 1)] 
q0: 0 
F: [1]
> d' `accepts` "aba"
True