Skip to content

Instantly share code, notes, and snippets.

@inaniwaudon
Created June 29, 2023 18:04
Show Gist options
  • Save inaniwaudon/a9726baf4a4207fcf91bdbd9540077f7 to your computer and use it in GitHub Desktop.
Save inaniwaudon/a9726baf4a4207fcf91bdbd9540077f7 to your computer and use it in GitHub Desktop.
from graphviz import Digraph
# editable
transitions = [
[0, "e", 1],
[1, "a", 2],
[1, "b", 3],
[2, "e", 4],
[3, "e", 4],
[4, "e", 1],
[4, "e", 5],
[0, "e", 5],
[5, "a", 6],
[6, "b", 7],
[7, "b", 8],
]
start = {0}
states = ["a", "b"]
def transition(points: set[int], state: str):
starts = points
result = set()
temp_result = set()
while True:
for t in transitions:
if t[0] in starts and t[1] == state:
temp_result.add(t[2])
result |= temp_result
if state != "e" or len(temp_result) == 0:
break
starts = temp_result
temp_result = set()
return result
def get_points(points: set[int], state: str):
if state == "e":
return transition(points, state)
else:
points = transition(points, state)
return points | transition(points, "e")
# main
alphabets = "ABCDEFGHIJKLMN"
x = start | get_points(start, "e")
points = [x]
table = {}
i = 0
while i < len(points):
table[alphabets[i]] = {}
for state in states:
y = get_points(points[i], state)
if y not in points:
points.append(y)
table[alphabets[i]][state] = alphabets[points.index(y)]
i += 1
# draw graph
g = Digraph()
g.attr("node", shape="circle")
g.attr("graph", rankdir="LR")
for t in transitions:
g.edge(str(t[0]), str(t[2]), t[1])
g.view()
# output
for i, p in enumerate(points):
print(alphabets[i], p)
print("")
print(" " + " ".join([s for s in states]))
print(" ".join(["--" for _ in range(len(states) + 1)]))
for row in table:
print(f"{row} ", end="")
for state in states:
print(f"{table[row][state]} ", end="")
print("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment