示例#1
0
 def test_minify_dfa(self):
     """Should minify a given DFA."""
     # This DFA accepts all words which are at least two characters long.
     # The states q1/q2 and q3/q4/q5/q6 are redundant.
     # The state q7 is not reachable.
     dfa = DFA(
         states={'q0', 'q1', 'q2', 'q3', 'q4', 'q5', 'q6', 'q7'},
         input_symbols={'0', '1'},
         transitions={
             'q0': {'0': 'q1', '1': 'q2'},
             'q1': {'0': 'q3', '1': 'q4'},
             'q2': {'0': 'q5', '1': 'q6'},
             'q3': {'0': 'q3', '1': 'q3'},
             'q4': {'0': 'q4', '1': 'q4'},
             'q5': {'0': 'q5', '1': 'q5'},
             'q6': {'0': 'q6', '1': 'q6'},
             'q7': {'0': 'q7', '1': 'q7'},
         },
         initial_state='q0',
         final_states={'q3', 'q4', 'q5', 'q6'}
     )
     minimal_dfa = dfa.minify()
     nose.assert_equal(minimal_dfa.states, {
         'q0', '{q1,q2}', '{q3,q4,q5,q6}'
     })
     nose.assert_equal(minimal_dfa.input_symbols, {'0', '1'})
     nose.assert_equal(minimal_dfa.transitions, {
         'q0': {'0': '{q1,q2}', '1': '{q1,q2}'},
         '{q1,q2}': {'0': '{q3,q4,q5,q6}', '1': '{q3,q4,q5,q6}'},
         '{q3,q4,q5,q6}': {'0': '{q3,q4,q5,q6}', '1': '{q3,q4,q5,q6}'}
     })
     nose.assert_equal(minimal_dfa.initial_state, 'q0')
     nose.assert_equal(minimal_dfa.final_states, {'{q3,q4,q5,q6}'})
示例#2
0
 def test_minify_dfa_initial_state(self):
     """Should minify a DFA where the initial state is being changed."""
     # This DFA accepts all words with ones and zeroes.
     # The two states can be merged into one.
     dfa = DFA(
         states={'q0', 'q1'},
         input_symbols={'0', '1'},
         transitions={
             'q0': {
                 '0': 'q1',
                 '1': 'q1'
             },
             'q1': {
                 '0': 'q0',
                 '1': 'q0'
             },
         },
         initial_state='q0',
         final_states={'q0', 'q1'},
     )
     minimal_dfa = dfa.minify()
     nose.assert_equal(minimal_dfa.states, {'{q0,q1}'})
     nose.assert_equal(minimal_dfa.input_symbols, {'0', '1'})
     nose.assert_equal(minimal_dfa.transitions, {
         '{q0,q1}': {
             '0': '{q0,q1}',
             '1': '{q0,q1}'
         },
     })
     nose.assert_equal(minimal_dfa.initial_state, '{q0,q1}')
     nose.assert_equal(minimal_dfa.final_states, {'{q0,q1}'})
示例#3
0
 def test_minify_minimal_dfa(self):
     """Should minify an already minimal DFA."""
     # This DFA just accepts words ending in 1.
     dfa = DFA(
         states={'q0', 'q1'},
         input_symbols={'0', '1'},
         transitions={
             'q0': {'0': 'q0', '1': 'q1'},
             'q1': {'0': 'q0', '1': 'q1'}
         },
         initial_state='q0',
         final_states={'q1'}
     )
     minimal_dfa = dfa.minify()
     nose.assert_equal(minimal_dfa.states, dfa.states)
     nose.assert_equal(minimal_dfa.input_symbols, dfa.input_symbols)
     nose.assert_equal(minimal_dfa.transitions, dfa.transitions)
     nose.assert_equal(minimal_dfa.initial_state, dfa.initial_state)
     nose.assert_equal(minimal_dfa.final_states, dfa.final_states)
示例#4
0
 def test_minify_dfa_no_final_states(self):
     dfa = DFA(
         states={'q0', 'q1'},
         input_symbols={'0', '1'},
         transitions={
             'q0': {'0': 'q1', '1': 'q1'},
             'q1': {'0': 'q0', '1': 'q0'},
         },
         initial_state='q0',
         final_states=set(),
     )
     minimal_dfa = dfa.minify()
     nose.assert_equal(minimal_dfa.states, {'{q0,q1}'})
     nose.assert_equal(minimal_dfa.input_symbols, {'0', '1'})
     nose.assert_equal(minimal_dfa.transitions, {
         '{q0,q1}': {'0': '{q0,q1}', '1': '{q0,q1}'},
     })
     nose.assert_equal(minimal_dfa.initial_state, '{q0,q1}')
     nose.assert_equal(minimal_dfa.final_states, set())
示例#5
0
 def test_minify_dfa_complex(self):
     dfa = DFA(
         states={'13', '56', '18', '10', '15', '26', '24', '54', '32', '27',
                 '5', '43', '8', '3', '17', '45', '57', '46', '35', '9',
                 '0', '21', '39', '51', '6', '55', '47', '11', '20', '12',
                 '59', '38', '44', '52', '16', '41', '1', '4', '28', '58',
                 '48', '23', '22', '2', '31', '36', '34', '49', '40', '7',
                 '25', '30', '53', '42', '33', '19', '50', '37', '14',
                 '29'},
         input_symbols={'L', 'U', 'R', 'D'},
         transitions={'55': {'L': '20', 'U': '49', 'R': '20', 'D': '49'},
                      '57': {'L': '5', 'U': '6', 'R': '1', 'D': '46'},
                      '35': {'L': '44', 'U': '32', 'R': '36', 'D': '33'},
                      '13': {'L': '45', 'U': '23', 'R': '45', 'D': '23'},
                      '43': {'L': '44', 'U': '32', 'R': '44', 'D': '33'},
                      '9': {'L': '5', 'U': '6', 'R': '1', 'D': '6'},
                      '53': {'L': '20', 'U': '33', 'R': '20', 'D': '32'},
                      '12': {'L': '40', 'U': '23', 'R': '25', 'D': '11'},
                      '42': {'L': '1', 'U': '49', 'R': '5', 'D': '49'},
                      '24': {'L': '40', 'U': '48', 'R': '25', 'D': '23'},
                      '27': {'L': '5', 'U': '46', 'R': '1', 'D': '6'},
                      '22': {'L': '40', 'U': '48', 'R': '25', 'D': '11'},
                      '19': {'L': '36', 'U': '32', 'R': '44', 'D': '33'},
                      '59': {'L': '40', 'U': '48', 'R': '45', 'D': '11'},
                      '39': {'L': '45', 'U': '48', 'R': '25', 'D': '11'},
                      '51': {'L': '20', 'U': '18', 'R': '20', 'D': '18'},
                      '34': {'L': '5', 'U': '4', 'R': '1', 'D': '31'},
                      '33': {'L': '44', 'U': '0', 'R': '36', 'D': '28'},
                      '23': {'L': '45', 'U': '8', 'R': '45', 'D': '8'},
                      '46': {'L': '44', 'U': '0', 'R': '44', 'D': '28'},
                      '58': {'L': '5', 'U': '4', 'R': '1', 'D': '4'},
                      '50': {'L': '20', 'U': '28', 'R': '20', 'D': '0'},
                      '54': {'L': '40', 'U': '8', 'R': '25', 'D': '41'},
                      '49': {'L': '1', 'U': '18', 'R': '5', 'D': '18'},
                      '21': {'L': '40', 'U': '26', 'R': '25', 'D': '8'},
                      '16': {'L': '5', 'U': '31', 'R': '1', 'D': '4'},
                      '6': {'L': '40', 'U': '26', 'R': '25', 'D': '41'},
                      '32': {'L': '36', 'U': '0', 'R': '44', 'D': '28'},
                      '48': {'L': '40', 'U': '26', 'R': '45', 'D': '41'},
                      '11': {'L': '45', 'U': '26', 'R': '25', 'D': '41'},
                      '15': {'L': '14', 'U': '49', 'R': '14', 'D': '49'},
                      '1': {'L': '56', 'U': '6', 'R': '37', 'D': '46'},
                      '3': {'L': '4', 'U': '32', 'R': '17', 'D': '33'},
                      '45': {'L': '8', 'U': '23', 'R': '8', 'D': '23'},
                      '52': {'L': '4', 'U': '32', 'R': '4', 'D': '33'},
                      '36': {'L': '56', 'U': '6', 'R': '37', 'D': '6'},
                      '20': {'L': '14', 'U': '33', 'R': '14', 'D': '32'},
                      '25': {'L': '47', 'U': '23', 'R': '10', 'D': '11'},
                      '29': {'L': '37', 'U': '49', 'R': '56', 'D': '49'},
                      '40': {'L': '47', 'U': '48', 'R': '10', 'D': '23'},
                      '5': {'L': '56', 'U': '46', 'R': '37', 'D': '6'},
                      '44': {'L': '47', 'U': '48', 'R': '10', 'D': '11'},
                      '38': {'L': '17', 'U': '32', 'R': '4', 'D': '33'},
                      '2': {'L': '47', 'U': '48', 'R': '8', 'D': '11'},
                      '30': {'L': '8', 'U': '48', 'R': '10', 'D': '11'},
                      '7': {'L': '14', 'U': '18', 'R': '14', 'D': '18'},
                      '37': {'L': '56', 'U': '4', 'R': '37', 'D': '31'},
                      '28': {'L': '4', 'U': '0', 'R': '17', 'D': '28'},
                      '8': {'L': '8', 'U': '8', 'R': '8', 'D': '8'},
                      '31': {'L': '4', 'U': '0', 'R': '4', 'D': '28'},
                      '17': {'L': '56', 'U': '4', 'R': '37', 'D': '4'},
                      '14': {'L': '14', 'U': '28', 'R': '14', 'D': '0'},
                      '10': {'L': '47', 'U': '8', 'R': '10', 'D': '41'},
                      '18': {'L': '37', 'U': '18', 'R': '56', 'D': '18'},
                      '47': {'L': '47', 'U': '26', 'R': '10', 'D': '8'},
                      '56': {'L': '56', 'U': '31', 'R': '37', 'D': '4'},
                      '4': {'L': '47', 'U': '26', 'R': '10', 'D': '41'},
                      '0': {'L': '17', 'U': '0', 'R': '4', 'D': '28'},
                      '26': {'L': '47', 'U': '26', 'R': '8', 'D': '41'},
                      '41': {'L': '8', 'U': '26', 'R': '10', 'D': '41'}},
         initial_state='55',
         final_states={'15', '24', '54', '32', '27', '5', '43', '57', '3',
                       '46', '35', '9', '21', '39', '51', '6', '55', '11',
                       '20', '12', '59', '38', '44', '52', '16', '1', '58',
                       '48', '22', '2', '36', '34', '49', '40', '25', '30',
                       '53', '42', '33', '19', '50', '29'})
     check_dfa = DFA(
         states={'5', '36', '1', '49', '40', '25', '46', '6', '55',
                 '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}', '33',
                 '11', '20', '48', '44', '32'},
         input_symbols={'L', 'U', 'R', 'D'},
         transitions={
             '48':
             {'L': '40',
              'U': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'R': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'D': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}'},
             '44':
             {'L': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'U': '48',
              'R': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'D': '11'},
             '40':
             {'L': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'U': '48',
              'R': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'D': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}'},
             '33':
             {'L': '44',
              'U': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'R': '36',
              'D': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}'},
             '55': {'L': '20', 'U': '49', 'R': '20', 'D': '49'},
             '32':
             {'L': '36',
              'U': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'R': '44',
              'D': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}'},
             '46':
             {'L': '44',
              'U': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'R': '44',
              'D': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}'},
             '25':
             {'L': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'U': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'R': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'D': '11'},
             '6':
             {'L': '40',
              'U': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'R': '25',
              'D': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}'},
             '11':
             {'L': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'U': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'R': '25',
              'D': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}'},
             '5':
             {'L': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'U': '46',
              'R': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'D': '6'},
             '49':
             {'L': '1',
              'U': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'R': '5',
              'D': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}'},
             '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}':
             {'L': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'U': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'R': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'D': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}'},
             '20':
             {'L': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'U': '33',
              'R': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'D': '32'},
             '36':
             {'L': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'U': '6',
              'R': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'D': '6'},
             '1':
             {'L': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'U': '6',
              'R': '{0,10,14,17,18,23,26,28,31,37,4,41,45,47,56,8}',
              'D': '46'}},
         initial_state='55',
         final_states={'5', '1', '36', '49', '40', '25', '46', '6', '55',
                       '33', '11', '20', '48', '44', '32'})
     minimal_dfa = dfa.minify()
     nose.assert_equal(minimal_dfa.states, check_dfa.states)
     nose.assert_equal(minimal_dfa.input_symbols, check_dfa.input_symbols)
     nose.assert_equal(minimal_dfa.transitions, check_dfa.transitions)
     nose.assert_equal(minimal_dfa.initial_state, check_dfa.initial_state)
     nose.assert_equal(minimal_dfa.final_states, check_dfa.final_states)
示例#6
0
                      '0': 'q0',
                      '1': 'q1'
                  },
                  'q1': {
                      '0': 'q0',
                      '1': 'q2'
                  },
                  'q2': {
                      '0': 'q2',
                      '1': 'q1'
                  }
              },
              initial_state='q0',
              final_states={'q1'})
    print('MINIMIZAÇÃO DE AUTOMATO DFA')
    minimal_dfa = dfa.minify()
    #print(minimal_dfa)

    print("Automata symbol", dfa.input_symbols)
    print("Automata states", dfa.states)
    print("Initiate state", dfa.initial_state)
    print("Transitions", dfa.transitions)
    print("Final states", dfa.final_states)

    print('\n')
    # =========================================================================================
    # DETERMINIZAÇÃO COM YPSILONE('ε')
    print("DETERMINIZATION OF AUTOMATA WITH YPSILONE('ε')")
    nfa = NFA(states={'q0', 'q1', 'q2'},
              input_symbols={'a', 'b'},
              transitions={