Skip to content

Commit

Permalink
individual working steps
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyakupadhyay committed Mar 12, 2017
0 parents commit ad4e4ac
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 0 deletions.
2 changes: 2 additions & 0 deletions code/graph_position.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import gv,sys
gv.graph("shreyak")
6 changes: 6 additions & 0 deletions code/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import json

graphFile = open('../graph.json','r')
print json.loads(graphFile.read())["graph"][0].keys()
# with open(...) as libFile:
# for line in libFile:
95 changes: 95 additions & 0 deletions code/min_dfa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

'''
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__":
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

print 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']]


77 changes: 77 additions & 0 deletions code/reduction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

'''
Author : Harika Rajavaram
Email : [email protected]
Subject : reducing nodes
Description: Reduction of nodes to minimized number of nodes by updating the dictionary loaded from
the json file and giving new output as json.
'''
# [[A],[B],[C,E],[D,F],[G]]

import json

graphFile = open('../graph2.json','r')
# loading the file in json format and reading it
readFile = graphFile.read()

# file_string = json.loads(readFile)
string = '''{
"stable":[{
"D":[{"val":1,"to":"E"},{"val":0,"to":"D"}],
"F":[{"val":1,"to":"E"},{"val":0,"to":"D"}],
"G":[{"val":1,"to":"G"},{"val":0,"to":"F"}]
}],
"unstable":[{
"A":[{"val":0,"to":"B"},{"val":1,"to":"C"}],
"B":[{"val":1,"to":"E"},{"val":0,"to":"D"}],
"C":[{"val":0,"to":"F"},{"val":1,"to":"G"}],
"E":[{"val":1,"to":"G"},{"val":0,"to":"F"}]
}]
}'''
file_string = json.loads(string)
newNodes = ["A","B",["C","E"],["D","F"],"G"]
# print newNodes[0]



def fucn(stablility):
for node in newNodes:
if(len(node) == 1):
continue
else:
updated_node = ','.join(map(str, node))
equinode = node[0]
print updated_node,equinode
update_json(updated_node,equinode,"unstable") #update node to updated_node


def update_json(updated_node,node,stablility):
stable_nodes = file_string[stablility][0].keys()
count = 0
for key in stable_nodes:
if (key in updated_node and count==1):
del file_string[stablility][0][key]
print key,"if"
elif (key in updated_node and count==0):
file_string[stablility][0][updated_node] = file_string[stablility][0][node]
del file_string[stablility][0][node]
count = 1
print key,"elif"
# print file_string["stable"]
print file_string[stablility]

fucn("stable")

# afterRed(newNodes)

# nodes = stable_nodes + unstable_nodes

# for i in range(0,len(stable_nodes)):
# if(file_string["stable"][0][i] == "D" or file_string["stable"][0][i] == "F"):
# print "in if"
# file_string["stable"][0]["D,F"] = file_string["stable"][0].pop("D")
# del file_string["stable"][0].keys()[i]


# print file_string["stable"][0].keys()
13 changes: 13 additions & 0 deletions graph2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"stable":[{
"D":[{"val":1,"to":"E"},{"val":0,"to":"D"}],
"F":[{"val":1,"to":"E"},{"val":0,"to":"D"}],
"G":[{"val":1,"to":"G"},{"val":0,"to":"F"}]
}],
"unstable":[{
"A":[{"val":0,"to":"B"},{"val":1,"to":"C"}],
"B":[{"val":1,"to":"E"},{"val":0,"to":"D"}],
"C":[{"val":0,"to":"F"},{"val":1,"to":"G"}],
"E":[{"val":1,"to":"G"},{"val":0,"to":"F"}]
}]
}
11 changes: 11 additions & 0 deletions graph_output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"stable":[{
"D,F":[{"val":1,"to":"E"},{"val":0,"to":"D"}],
"G":[{"val":1,"to":"G"},{"val":0,"to":"D,F"}]
}],
"unstable":[{
"A":[{"val":0,"to":"B"},{"val":1,"to":"C,E"}],
"B":[{"val":1,"to":"C,E"},{"val":0,"to":"D,F"}],
"C,E":[{"val":0,"to":"D,F"},{"val":1,"to":"G"}],
}]
}
13 changes: 13 additions & 0 deletions to_do.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
1. json_parsing.
2. optimizing DFA.
3. Latex code.
4. graph positioning.

Thinking Part 3:
1. Libraries will be in a separate file so that each time when we are generating code we do not have to output those
lines also. Print the libraries once for once complete sharelatex document.
2.


to_do Part 3:
1.

0 comments on commit ad4e4ac

Please sign in to comment.