# We use strings for states and ints for lettersdef d_e_dfa(Q:set[state], S:set[symbol], d:dict, q_0:state, F:set) ->bool:
Take start state
# We use strings for states and ints for lettersdef d_e_dfa(Q:set[state], S:set[symbol], d:dict, q_0:state, F:set) ->bool: current = q_0
Track visited states
# We use strings for states and ints for lettersdef d_e_dfa(Q:set[state], S:set[symbol], d:dict, q_0:state, F:set) ->bool: current = q_0 visited = [q_0]
It is very important to keep those states in order…
Why?
Reachable states from q_0
# We use strings for states and ints for lettersdef d_e_dfa(Q:set[state], S:set[symbol], d:dict, q_0:state, F:set) ->bool: current = q_0 visited = [q_0] transitions = d[d_0]for transition in transitions:pass# do something
Quick Python Refresher
# how does dict iteration work?q_1, q_2, q_3 ="q_1", "q_2", "q_3"d = { q_1 : { 0:q_1, 1:q_2 }, q_2 : { 0:q_1, 1:q_3 }, q_3 : { 0:q_3, 1:q_3 }}for element in d[q_1]:print(element)
0
1
Reachable states from q_0
# We use strings for states and ints for lettersdef d_e_dfa(Q:set[state], S:set[symbol], d:dict, q_0:state, F:set) ->bool: current = q_0 visited = [q_0] transitions = d[d_0]for symbol in transitions:# we will get symbols, see where the symbols go state = transitions[symbol]# we will add that state to visted state visted.append(state)
Then what?
Reachable states from q_0
# We use strings for states and ints for lettersdef d_e_dfa(Q:set[state], S:set[symbol], d:dict, q_0:state, F:set) ->bool: current = q_0 visited = [q_0] transitions = d[q_0] count =0# track what states we've examinedwhile count <len(visited): # stop if we run out transitions = d[visited[count]]for symbol in transitions:# we will get symbols, see where the symbols go state = transitions[symbol]# we will add that state to visted state visted.append(state)
Is using len cheating?
What about for and while?
Imagine
def d_e_dfa(Q, S, d, q_0, F): visited = [q_0] count =0while count <len(visited): transitions = d[visited[count]]for symbol in transitions: state = transitions[symbol]if state notin visted: visted.append(state)
$D_{A_{DFA}} =$ "On input $M$ Track states on working tape 1Use head to track current stateWhile current not blank Place delta(current) on tape 2 Move head head 2 along delta copy all state names to tape 3 read all of tape 1 for each state if state is new, add to tape 1
Accept
def d_e_dfa(Q, S, d, q_0, F): visited = [q_0] count =0while count <len(visited): transitions = d[visited[count]]for symbol in transitions: state = transitions[symbol]if state notin visted:if state in F:returnTrue visted.append(state)returnFalse
Copy \(F\) to another tape, and check it before checking the visit tracker.
def d_e_dfa(Q, S, d, q_0, F): visited = [q_0] count =0while count <len(visited): transitions = d[visited[count]]for symbol in transitions: state = transitions[symbol]if state notin visted:if state in F:returnTrue visted.append(state)returnFalse