def mkp_dfa(Q, Sigma, Delta, q0, F): """In : Traits of a DFA Out: A DFA Check for partial consistency of the given DFA traits. If the check passes, make and return a DFA with a partial Delta. """ newDFA = {"Q": Q, "Sigma": Sigma, "Delta": Delta, "q0": q0, "F": F} assert ( is_partially_consistent_dfa(newDFA) ), "DFA given to mkp_dfa is not partially consistent. Plz check its components." return (newDFA)
def totalize_dfa(D): """In : Partially consistent DFA Out: A consistent DFA Given a partially specified DFA, make it total by transitioning to state BH wherever the incoming Delta has gaps. The returned DFA is structurally consistent. """ assert (is_partially_consistent_dfa(D) ), "DFA given to totalize_dfa is not partially consistent." if set(fn_dom(D["Delta"])) == set(product(D["Q"], D["Sigma"])): # It is already total! return D else: # We must introduce a BH state of not already present # and proceed from there incoming_Delta = D["Delta"].copy() # Gaps in incoming_Delta's transition function are sent # to the BH (black-hole) state gaps_in_Tr = {(q, c): "BH" for q in D["Q"] for c in D["Sigma"] if (q, c) not in D["Delta"]} # We are gonna add a new black-hole-state. # It must curl back to itself for every symbol in Sigma bh_self_absorbent_moves = {("BH", c): "BH" for c in D["Sigma"]} # Fill the gaps in incoming_Delta incoming_Delta.update(gaps_in_Tr) # Add in the moves where the black-hole state curls # back to itself incoming_Delta.update(bh_self_absorbent_moves) # All updates required are accomplished finished_Delta = incoming_Delta # See that we update D["Q"] with the "BH" (black-hole) # state; also return the fixed-up incoming_Delta return { "Q": D["Q"] | {"BH"}, "Sigma": D["Sigma"], "Delta": finished_Delta, "q0": D["q0"], "F": D["F"] }