question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conversion from DFA to NFA does not properly handle state names of mixed data types

See original GitHub issue

I found the following bug: nfa._add_new_state introduces integers into the set of states.

Current code is:

    def _add_new_state(state_set, start=0):
        """Adds new state to the state set and returns it"""
        new_state = start
        while new_state in state_set:
            new_state += 1

        state_set.add(new_state)

        return new_state

which could be fixed, for example, in this way:

    def _add_new_state(state_set, start=0):
        """Adds new state to the state set and returns it"""
        new_state = start
        while str(new_state) in state_set:
            new_state += 1

        state_set.add(str(new_state))

        return str(new_state)

EDIT: this bugfix actually runs into other problems later - this is just an idea


This can lead to logic problems later. For example the following code fails:

from automata.fa.dfa import DFA
from automata.fa.nfa import NFA

A = NFA(
    states={'0', '1'},
    input_symbols={'0'},
    transitions={'0': {'0': {'1'}}, '1': {'0': {'1'}}},
    initial_state='0',
    final_states={'1'}
)

print(A.reverse().states)

B = DFA.from_nfa(A.reverse())

assert(A.accepts_input('00')==B.accepts_input('00'))

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
eliotwrobsoncommented, Oct 9, 2022

@caleb531 It looks like the test you wrote checks for the fact that the _add_new_state now has better support for integer string state names, but there are still issues when manually constructing an NFA.

The following (somewhat contrived) assertion fails:

A = NFA(
    states={1, '1'},
    input_symbols={'0'},
    transitions={1: {'0': {'1'}}, '1': {'0': {'1'}}},
    initial_state=1,
    final_states={'1'}
)

B = DFA.from_nfa(A)

assert A.accepts_input('00') == B.accepts_input('00')
1reaction
caleb531commented, Oct 6, 2022

@colette-b Good news! I was actually able to refactor _add_new_state to correctly handle non-string states, thus fixing your original example. All other automated tests are passing too, so there should be no regressions from this refactor.

In case you’re curious, here is the new implementation:

@staticmethod
def _add_new_state(state_set, start=0):
    """Adds new state to the state set and returns it"""
    new_state = start
    # coercing the state names to strings is the key!
    str_state_set = {str(state) for state in state_set}
    while str(new_state) in str_state_set:
        new_state += 1

    state_set.add(new_state)

    return new_state
Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · caleb531/automata - GitHub
Issues list ; Regex parsing efficiency enhancement. #61 opened ; Conversion from DFA to NFA does not properly handle state names of mixed...
Read more >
Can every DFA be converted into an NFA? - Quora
The simple answer is Yes. Every DFA is an NFA i.e every DFA can be converted into NFA but not vice versa. But...
Read more >
Converting Non-Deterministic Finite Automata to ... - YouTube
By adding ambiguities to a finite automaton based on a regular expression, we show how to convert a non -deterministic finite automaton (...
Read more >
"Simple" Conversions (DFA to NFA to PDA, RG to CFG)
Here we cover all of the "simple" conversions commonly taught in the theory of computation class: deterministic finite automaton ( DFA ) to ......
Read more >
Efficient algorithm for converting a character set into a nfa/dfa
If I use large character sets, like the any operator '.' or the unicode property {L}, the nfa (and also the dfa) contains...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found