-
Notifications
You must be signed in to change notification settings - Fork 1
/
M5.py
205 lines (171 loc) · 6.74 KB
/
M5.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# M3
#
# Authors:
# Carlo Alejandro Muñoz Amezquita
# Carolina Pérez-Vargas Pinson
# Gustavo Adolfo Rueda Enríquez
#
# Description:
# This module receives a DFA and returns its equivalent minimized DFA.
#
# Important Notes:
# An NFA object has 4 attributes:
# - initial_state
# - final_states
# - delta (transitions matrix)
# - mapped_alphabet
# ``` - alphabet
#
# If you print a DFA object, the format will be like this:
# alphabet symbol 1;alphabet symbol 2;...;alphabet symbol n;
# initial state
# final state 1;final state 2;...;final state n;
# transition deltas 1,
# transition deltas 2,
# .
# .
# .
# transition deltas n,
import math
class Automata:
def __init__(self, alphabet, delta, initial_state, final_states):
self.delta = delta
self.initial_state = initial_state
self.final_states = final_states
self.alphabet = alphabet
self.mapped_alphabet = dict()
for i in range(len(alphabet)):
self.mapped_alphabet[alphabet[i]] = i
def extended_delta(self, w):
new_w = []
for i in w:
if not i in self.alphabet: return False
new_w.append(self.mapped_alphabet[i])
s = self.__run_extended_delta(self.initial_state, new_w)
return s in self.final_states
#Calculate d(w)
def __run_extended_delta(self, s, w):
if len(w) == 1:
current_state = self.delta[s][w[0]]
return current_state
current_state = self.delta[self.__run_extended_delta(s, w[:-1])][w[-1]]
return current_state
def __str__(self):
alphabet = ""
for alphas in self.mapped_alphabet:
alphabet += alphas
alphabet += ";"
finalStates = ""
for finals in self.final_states:
finalStates += str(finals)
finalStates += ";"
transitions = ""
for trans in self.delta:
transitions += str(trans)
transitions += ",\n"
string = alphabet + "\n" + str(self.initial_state) + "\n" + finalStates + "\n" + transitions
return string
def reduce(self):
# 1 ) creates Q x Q table
noStates = len(self.delta)
current = [[s2, s1] for s1 in range(noStates) for s2 in range(noStates)]
# 2 ) deletes tuples in which peF and q noteF or viceversa from the table
t = 0
while(t < len(current)): #for t in range(len(current)): #t is the tuple
s1 = current[t][0]
s2 = current[t][1]
if s1 in self.final_states and s2 not in self.final_states or s2 in self.final_states and s1 not in self.final_states:
del current[t]
else: t+=1
# 3 ) deletes tuples if theyre not in current table
change = True
while change:
t = 0
change = False
while(t < len(current)): #for t in range(len(current)): #t is the tuple
deleted = False
for symbol in range(len(self.mapped_alphabet)):
a = current[t][0]
b = current[t][1]
p = self.delta [a] [symbol]
q = self.delta [b] [symbol]
if not [p,q] in current:
del current[t]
deleted = True
change = True
break
if not deleted: t+=1
#Delete (n, n)
t = 0
while(t < len(current)):
if [current[t][0]] == [current[t][1]]:
del current[t]
else: t+=1
#Delete (m, n) since we have its equivalent (n,m)
for t in range(len(current)):
if [current[t][0]] > [current[t][1]]:
aux = current[t][0]
current[t][0] = current[t][1]
current[t][1] = aux
newCurrent = []
for i in current:
if not i in newCurrent:
newCurrent.append(i)
current = newCurrent
del newCurrent
collsapedStates = []
for i in current:
if not i in collsapedStates:
collsapedStates.append(i[0])
collsapedStates.append(i[1])
for i in range(len(self.delta)):
if not i in collsapedStates:
current.append((i, ))
#Rename
rename = [-1 for i in range(len(self.delta))]
newInitialState = 0
newFinalStates = []
count=0
for i in range(len(current)):
exists = False
for state in range(len(current[i])):
if rename[current[i][state]] != -1:
exists = True
elif exists:
rename[current[i][state]] = rename[current[i][state - 1]] #(collapse states with transitivities)
else:
rename[current[i][state]] = count
if current[i][state] == self.initial_state:
newInitialState = count
if current[i][state] in self.final_states and not rename[current[i][state]] in newFinalStates:
newFinalStates.append(rename[current[i][state]])
if not exists: count+=1
self.initial_state = newInitialState
self.final_states = newFinalStates
newDelta =[[] for j in range(len(set(rename)))]
flags = [False] * len(set(rename))
for oldTransition in range(len(self.delta)):
if flags[rename[oldTransition]]:
continue
newTransition = [rename[self.delta[oldTransition][symbol]] for symbol in range(len(self.delta[oldTransition]))]
newDelta[rename[oldTransition]] += newTransition
flags[rename[oldTransition]] = True
self.delta = newDelta
def M5(automata: Automata):
automata.reduce()
if __name__ == '__main__':
print("Example 1:")
dfa1 = Automata(('a','b'), ((1,3),(2,1),(1,2),(4,3),(3,4)), 0, (1,3))
print("DFA \n"+str(dfa1))
M5(dfa1)
print("Minimized DFA \n"+str(dfa1))
print("Example 2:")
dfa2 = Automata(('a','b'), ((1,3),(2,4),(5,5),(4,2),(5,5),(5,5)), 0, (1,3,5))
print("DFA \n"+str(dfa2))
M5(dfa2)
print("Minimized DFA \n"+str(dfa2))
print("Example 3:")
dfa3 = Automata( ('a','b','c'), ((1,3,5),(2,2,2),(7,7,7),(4,4,4),(7,7,7),(6,6,6),(7,7,7),(7,7,7)), 0, (0,7) )
print("DFA \n"+str(dfa3))
M5(dfa3)
print("Minimized DFA \n"+str(dfa3))