-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_dfa.py
96 lines (77 loc) · 2.91 KB
/
min_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
'''
Author : Shreyak Upadhyay
Email : [email protected]
Subject : algorithm for minimising dfa
Description:
Minimizing DFA using equivalent blocks and returning multi dimensional list which contains equivalent blocks.
'''
import json
graphFile = open('../graph2.json','r')
json_obj = json.loads(graphFile.read())
'''
check if a value(0,1) exists in a dictionary
'''
def checkval(dic,x,val,nodes,stability1,stability2): # transition function to know for input 0,1 to which node(stable,unstable) output is going.
if val in dic[x].values():
if(dic[x]["to"] in nodes): # getting "to" values
return stability1
if(dic[x]["to"] not in nodes):
return stability2
else: return False
'''
generating list containing the conditions whether the transitions with inputs (0,1)
is stable or unstable and comparing them to find equivalence.
'''
def checkequi(node1,node2,nodes,stability1,stability2):
alphabet = [0,1] # or 1
node1_val = []
node2_val = []
for val in alphabet:
for x in range(len(node1)):
value = checkval(node1,x,val,nodes,stability1,stability2)
if(value!=False):
node1_val.append(value)
for x in range(len(node2)):
value = checkval(node2,x,val,nodes,stability1,stability2)
if(value!=False):
node2_val.append(value) # node1 = "D"
if(node1_val==node2_val):
print "HURRah"
return True
else:
print "NO"
return False
'''
converting multidimensional list into 1-D list
'''
def listsearch(blocks):
nodes_blocks = []
for ele_1 in blocks:
for ele_2 in ele_1:
nodes_blocks.append(ele_2)
return nodes_blocks
'''
main function to call other functions with various arguments
'''
def transition(stability1,stability2):
nodes = json_obj[stability1][0].keys() # nodes with particular stability
blocks = []
for idx_1 in range(0,len(nodes)-2):
equi_nodes = [nodes[idx_1]]
for idx_2 in range(idx_1+1,len(nodes)): # selecting two nodes to compare for quivalence
node1 = json_obj[stability1][0][nodes[idx_1]] # "D"
node2 = json_obj[stability1][0][nodes[idx_2]] # "F"
result_equi = checkequi(node1,node2,nodes,stability1,stability2)
if(result_equi):
# equi_nodes.append(nodes[idx_1]) # generating equivalence blocks
equi_nodes.append(nodes[idx_2])
blocks.append(equi_nodes)
return blocks
#if __name__ == "__main__":
def main():
unstable_block = transition("unstable","stable")
stable_block = transition("stable","unstable")
all_nodes = json_obj["stable"][0].keys() + json_obj["unstable"][0].keys() # all nodes
all_nodes = [x for x in all_nodes if x not in listsearch(stable_block)] # removing stable blocks(blocks generated from stable nodes list) from all_nodes
all_nodes = [x for x in all_nodes if x not in listsearch(unstable_block)] # removing unstable blocks(blocks generated from unstable nodes list) from all_nodes
return all_nodes + unstable_block + stable_block # generating list required of reduction [u'G', u'B', [u'A'], [u'C', u'E'], [u'D', u'F']]