Question

I'm working on an assignment for automata class Here's the example code in Python: def dfa_accept(M,...

I'm working on an assignment for automata class

Here's the example code in Python:

def dfa_accept(M, w):
    """ Runs the DFA M = (Q, Sigma, delta, q0, F) on the string w.
    Returns True if all characters in w are in Sigma
    and the DFA accepts the word. Also checks if
    all states returned by delta are in Q.
    Returns False otherwise.
    """
    (Q, Sigma, delta, q0, F) = M
    q = q0
    if q not in Q:
        return False
    s = w
    while s != "":
        a = s[0]
        s = s[1:]
        if a not in Sigma:
            return False
        q = delta(q, a)
        if q not in Q:
            return False
    if q in F:
        return True
    return False
def delta_odd(q, a):
    if q == 0:
        if a == '0':
            return 0
        elif a == '1':
            return 1
        else:
            return 2
    elif q == 1:
        if a == '0':
            return 1
        elif a == '1':
            return 0
        else:
            return 2
    else:
        return 2
print("Odd? ",dfa_accept(({ 0, 1 }, { '0', '1' }, delta_odd, 0, { 0 }),
"0001010101011"))  # True
print("Odd? ",dfa_accept(({ 0, 1 }, { '0', '1' }, delta_odd, 0, { 0 }),
"0001010101010"))  # False

This code satisfied the first problem in this assignment:

For this assignment, you must hand in valid code for the following problems:

1, A functional model for DFAs, as shown in the example above, in the form of a function taking a DFA and a string in argument and returning a boolean indicating whether the string is in the language of the DFA

Now I need to figure out the code for the second problem:

2. A function (or method) that takes a model of a Nondeterministic Finite Automaton (NFA) in argument and returns a valid DFA, which the function above is able to execute.

I'm stumped by this problem...I wonder if anyone can help me with it. Appreciate for all the help I can get!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def convertingNFA_DFA(N):

q0=frozenset([n.q0])

Q=set([q0])

unprocessedQ=Q.copy()

delta={}

F=[]

Sigma=N.alphabet()

while len(unprocessedQ)>0:

qSet=unprocessedQ.pop()

delta[qSet]={}

for a in Sigma:

nextStates=reduce(lambda x,y:x|y,[N.deltaHat(q,a) for q in qSet])

nextStates=frozenset(nextStates)

delta[qSet][a]=nextStates

if not nextStates in Q:

Q.add(nextStates)

unprocessedQ.add(nextStates)

for qSet in Q:

if len(qSet & N.F)>0:

F.append(qSet)

M=DFA(delta,q0,F)

return M

delta={'q0':{'0':set(['q0','q1']),'1':set(['q0'])},'q1':{'1':set(['q2'])}}

N=NFA(delta,'q0',['q2'])

N.deltaHat('q0','0001')

print[(x,N.inLanguage(x)) for x in ['0001','00010','100101']]

M=convertNFAtoDFA(N)

print[(x,M.inLanguage(x)) for x in ['0001','00010','100101']]

Add a comment
Know the answer?
Add Answer to:
I'm working on an assignment for automata class Here's the example code in Python: def dfa_accept(M,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT