import networkx as nx
import matplotlib.pyplot as plt

# สร้าง Directed Graph
G = nx.DiGraph()

# เพิ่มโหนด (สถานะของ Turing Machine)
states = ["q0", "q1", "qf"]
G.add_nodes_from(states)

# เพิ่มเส้นเชื่อมตาม Transition Function
edges = [
    ("q0", "q0", "a → A, R"),
    ("q0", "q0", "b → B, R"),
    ("q0", "q1", "c → C, R"),
    ("q1", "qf", "□ → □, L")
]

for start, end, label in edges:
    G.add_edge(start, end, label=label)

# วาดกราฟ
pos = {"q0": (0, 1), "q1": (2, 1), "qf": (4, 1)}
labels = {(u, v): d['label'] for u, v, d in G.edges(data=True)}

plt.figure(figsize=(6, 4))
nx.draw(G, pos, with_labels=True, node_color="lightblue", node_size=2000, edge_color="black", font_size=12)
nx.draw_networkx_edge_labels(G, pos, edge_labels=labels, font_size=10)
plt.title("Turing Machine Transition Graph")
plt.show()
