This repository has been archived by the owner on Jan 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
138 lines (122 loc) · 3.25 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def merge_states(state1, state2):
# Replace all occurences of state2 with state1 in table_finals
for state in table_finals:
for letter in alphabet:
if table_finals[state].get(letter) == state2 or table_finals[state].get(letter) == state1:
table_finals[state][letter] = state1 + ',' + state2
# Replace all occurences of state2 with state1 in table_nonfinals
for state in table_nonfinals:
for letter in alphabet:
if table_nonfinals[state].get(letter) == state2 or table_nonfinals[state].get(letter) == state1:
table_nonfinals[state][letter] = state1 + ',' + state2
if table_finals.get(state1):
table_finals[state1 + ',' + state2] = table_finals.pop(state1)
if table_nonfinals.get(state1):
table_nonfinals[state1 + ',' + state2] = table_nonfinals.pop(state1)
def try_merging(table):
for state1 in table.keys():
for state2 in table.keys():
if state1 != state2:
should_merge = True
for letter in alphabet:
if table[state1][letter] != table[state2][letter] and not (table[state1][letter] == state2 and table[state2][letter] == state1):
should_merge = False
if should_merge:
table.pop(state2)
merge_states(state1, state2)
try_merging(table)
return
DFA = {}
f = open('input.txt')
start_state = f.readline()
start_state = start_state.strip()
final_states = f.readline()
final_states = [elem.strip() for elem in final_states.split()]
alphabet = set()
all_states = set()
for line in f:
table = [elem.strip() for elem in line.split()]
all_states.add(table[0])
all_states.add(table[2])
alphabet.add(table[1])
if DFA.get(table[0]) == None:
DFA[table[0]] = {}
DFA[table[0]].update({table[1]: table[2]})
f.close()
# Find reachable states
reachable_states = set()
reachable_states.add(start_state)
new_elements = True
while new_elements == True:
new_elements = False
aux = reachable_states.copy()
for letter in alphabet:
for state in reachable_states:
if DFA[state][letter] not in reachable_states:
aux.add(DFA[state][letter])
new_elements = True
reachable_states = aux.copy()
# Remove unreachable states
for elem in all_states.difference(reachable_states):
DFA.pop(elem, None)
# Create transition tables
table_finals = {state: {letter: DFA[state][letter] for letter in alphabet}
for state in DFA.keys() if state in final_states}
table_nonfinals = {state: {letter: DFA[state][letter] for letter in alphabet}
for state in DFA.keys() if state not in final_states}
try_merging(table_finals)
try_merging(table_nonfinals)
table = table_finals | table_nonfinals
for key in table.items():
print(*key)
print()
''' input.txt presentation
0
5
0 1 2
0 0 1
1 0 0
1 1 3
2 0 5
2 1 4
3 1 4
3 0 5
4 0 4
4 1 4
5 0 5
5 1 4
'''
''' input.txt PDF
0
6
0 a 1
0 b 3
1 b 2
1 a 3
2 b 2
2 a 3
3 a 6
3 b 5
4 a 6
4 b 5
5 b 2
5 a 6
6 a 4
6 b 5
'''
''' input.txt https://www.javatpoint.com/minimization-of-dfa
0
3 5
0 0 1
0 1 3
1 1 3
1 0 0
2 1 4
2 0 1
3 0 5
3 1 5
5 0 5
5 1 5
4 0 3
4 1 3
'''