Skip to content

Instantly share code, notes, and snippets.

@Sasszem
Created November 27, 2020 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sasszem/360a1d6b02fcf30a4763ecc5e88ca813 to your computer and use it in GitHub Desktop.
Save Sasszem/360a1d6b02fcf30a4763ecc5e88ca813 to your computer and use it in GitHub Desktop.
Simple Paul-Unger state table optimalization helper for single input sequential networks
from collections import deque
"""
Input your (not) fully specified state table here
"""
T = [
["a", ["h", "1"], ["f", "-"]],
["b", ["c", "1"], ["h", "1"]],
["c", ["b", "0"], ["a", "0"]],
["d", ["d", "1"], ["-", "0"]],
["e", ["-", "1"], ["g", "-"]],
["f", ["e", "1"], ["d", "-"]],
["g", ["c", "-"], ["e", "0"]],
["h", ["-", "-"], ["b", "-"]],
]
def ekv(a, b):
if (a[1][1]!=b[1][1] and a[1][1]!="-" and b[1][1]!="-") or (a[2][1]!=b[2][1] and a[2][1]!="-" and b[2][1]!="-"):
return False
r = []
s = [a[0], b[0]]
e = [a[1][0], b[1][0]]
if e[0]!=e[1] and e!=s and e!=s[::-1] and not "-" in e:
r.append(e)
e = [a[2][0], b[2][0]]
if e[0]!=e[1] and e!=s and e!=s[::-1] and not "-" in e:
r.append(e)
return r
N = len(T)
lepcsos = [[[] for _ in range(N)] for _ in range(N)]
x = deque()
for i in range(N-1):
for j in range(i+1, N):
lepcsos[i][j-1] = ekv(T[i], T[j])
if lepcsos[i][j-1]==False:
x.append([i, j])
#print(lepcsos)
#print(x)
while x:
d = x.pop()
pair = [T[d[0]][0], T[d[1]][0]]
for i in range(N-1):
for j in range(i+1, N):
if lepcsos[i][j-1]==False:
continue
if pair in lepcsos[i][j-1] or pair[::-1] in lepcsos[i][j-1]:
lepcsos[i][j-1] = False
x.append([i, j])
#print(lepcsos)
def cleanup(n):
r = []
for a in n:
s = True
for b in n:
if b!=a:
if set(a).issubset(set(b)):
s = False
if s:
r.append(a)
return r
curr = [list(range(N))]
for i in range(N):
#print(curr)
n = []
for el in curr:
if i in el:
ne = el[:]
ne.remove(i)
n.append(ne)
ne = [i]
for j in el:
if isinstance(lepcsos[i][j-1], list) and i!=j:
ne.append(j)
n.append(ne)
else:
n.append(el)
curr = cleanup(n)
#print(curr)
for t in curr:
print("".join(T[x][0] for x in t))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment