def testMinimize(self): # https://en.wikipedia.org/wiki/DFA_minimization tt = { frozenset(['a']): { '0': frozenset(['b']), '1': frozenset(['c']) }, frozenset(['b']): { '0': frozenset(['a']), '1': frozenset(['d']) }, frozenset(['c']): { '0': frozenset(['e']), '1': frozenset(['f']) }, frozenset(['d']): { '0': frozenset(['e']), '1': frozenset(['f']) }, frozenset(['e']): { '0': frozenset(['e']), '1': frozenset(['f']) }, frozenset(['f']): { '0': frozenset(['f']), '1': frozenset(['f']) } } sigma = set(['0', '1']) start = 'a' end = set([frozenset(['c']), frozenset(['d']), frozenset(['e'])]) dfa = DFA(tt, start, end) self.assertEqual(len(dfa.ends), 3) self.assertEqual(len(dfa.transitions), 6) eqCls = dfa.findEquivalenceClasses(sigma) dfa.mergeEquivalentStates(eqCls) self.assertEqual(len(dfa.ends), 1) self.assertEqual(len(dfa.transitions), 3)
def showMinimization4(): tt = { frozenset(['a']): { '0': frozenset(['b']), '1': frozenset(['c']) }, frozenset(['b']): { '0': frozenset(['e']), '1': frozenset(['f']) }, frozenset(['c']): { '0': frozenset(['a']), '1': frozenset(['a']) }, frozenset(['d']): { '0': frozenset(['f']), '1': frozenset(['e']) }, frozenset(['e']): { '0': frozenset(['d']), '1': frozenset(['f']) }, frozenset(['f']): { '0': frozenset(['d']), '1': frozenset(['e']) } } sigma = set(['0', '1']) start = 'a' end = set([frozenset(['e']), frozenset(['f'])]) dfa = DFA(tt, start, end) dfa.visualize() eqCls = dfa.findEquivalenceClasses(sigma) dfa.mergeEquivalentStates(eqCls) dfa.visualize(fname='fsm1.gv')