-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdfa.py
219 lines (142 loc) · 4.81 KB
/
dfa.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
206
207
208
209
210
211
212
213
from collections import defaultdict
from disjoint_set import DisjointSet
import networkx as nx
import matplotlib.pyplot as plt
from graphviz import Source
class DFA(object):
def __init__(self,states_or_filename,terminals=None,start_state=None, \
transitions=None,final_states=None):
if terminals is None:
self._get_graph_from_file(states_or_filename)
else:
assert isinstance(states_or_filename,list) or \
isinstance(states_or_filename,tuple)
self.states = states_or_filename
assert isinstance(terminals,list) or isinstance(terminals,tuple)
self.terminals = terminals
assert isinstance(start_state,str)
self.start_state = start_state
assert isinstance(transitions,dict)
self.transitions = transitions
assert isinstance(final_states,list) or \
isinstance(final_states,tuple)
self.final_states = final_states
def draw(self):
'''
Draws the dfa using networkx and matplotlib
'''
g = nx.DiGraph()
for x in self.states:
g.add_node(x,shape='doublecircle' if x in self.final_states else 'circle',fillcolor='grey' if x == self.start_state else 'white',style='filled')
temp = defaultdict(list)
for k,v in self.transitions.items():
temp[(k[0],v)].append(k[1])
for k,v in temp.items():
g.add_edge(k[0],k[1],label=','.join(v))
return Source(nx.drawing.nx_agraph.to_agraph(g))
def _remove_unreachable_states(self):
'''
Removes states that are unreachable from the start state
'''
g = defaultdict(list)
for k,v in self.transitions.items():
g[k[0]].append(v)
# do DFS
stack = [self.start_state]
reachable_states = set()
while stack:
state = stack.pop()
if state not in reachable_states:
stack += g[state]
reachable_states.add(state)
self.states = [state for state in self.states \
if state in reachable_states]
self.final_states = [state for state in self.final_states \
if state in reachable_states]
self.transitions = { k:v for k,v in self.transitions.items() \
if k[0] in reachable_states}
def minimize(self):
self._remove_unreachable_states()
def order_tuple(a,b):
return (a,b) if a < b else (b,a)
table = {}
sorted_states = sorted(self.states)
# initialize the table
for i,item in enumerate(sorted_states):
for item_2 in sorted_states[i+1:]:
table[(item,item_2)] = (item in self.final_states) != (item_2\
in self.final_states)
flag = True
# table filling method
while flag:
flag = False
for i,item in enumerate(sorted_states):
for item_2 in sorted_states[i+1:]:
if table[(item,item_2)]:
continue
# check if the states are distinguishable
for w in self.terminals:
t1 = self.transitions.get((item,w),None)
t2 = self.transitions.get((item_2,w),None)
if t1 is not None and t2 is not None and t1 != t2:
marked = table[order_tuple(t1,t2)]
flag = flag or marked
table[(item,item_2)] = marked
if marked:
break
d = DisjointSet(self.states)
# form new states
for k,v in table.items():
if not v:
d.union(k[0],k[1])
self.states = [str(x) for x in range(1,1+len(d.get()))]
new_final_states = []
self.start_state = str(d.find_set(self.start_state))
for s in d.get():
for item in s:
if item in self.final_states:
new_final_states.append(str(d.find_set(item)))
break
self.transitions = {(str(d.find_set(k[0])),k[1]):str(d.find_set(v))
for k,v in self.transitions.items()}
self.final_states = new_final_states
def __str__(self):
'''
String representation
'''
num_of_state = len(self.states)
start_state = self.start_state
num_of_final = len(self.final_states)
return '{} states. {} final states. start state - {}'.format( \
num_of_state,num_of_final,start_state)
def _get_graph_from_file(self,filename):
'''
Load the graph from file
'''
with open(filename,'r') as f:
try:
lines = f.readlines()
states,terminals,start_state,final_states = lines[:4]
if states:
self.states = states[:-1].split()
else:
raise Exception('Invalid file format: cannot read states')
if terminals:
self.terminals = terminals[:-1].split()
else:
raise Exception('Invalid file format: cannot read terminals')
if start_state:
self.start_state = start_state[:-1]
else:
raise Exception('Invalid file format: cannot read start state')
if final_states:
self.final_states = final_states[:-1].split()
else:
raise Exception('Invalid file format: cannot read final states')
lines = lines[4:]
self.transitions = {}
for line in lines:
current_state,terminal,next_state = line[:-1].split()
self.transitions[(current_state,terminal)] = next_state
except Exception as e:
print("ERROR: ",e)