fork download
  1. import networkx as nx
  2. import matplotlib.pyplot as plt
  3.  
  4. # สร้าง Directed Graph
  5. G = nx.DiGraph()
  6.  
  7. # เพิ่มโหนด (สถานะของ Turing Machine)
  8. states = ["q0", "q1", "qf"]
  9. G.add_nodes_from(states)
  10.  
  11. # เพิ่มเส้นเชื่อมตาม Transition Function
  12. edges = [
  13. ("q0", "q0", "a → A, R"),
  14. ("q0", "q0", "b → B, R"),
  15. ("q0", "q1", "c → C, R"),
  16. ("q1", "qf", "□ → □, L")
  17. ]
  18.  
  19. for start, end, label in edges:
  20. G.add_edge(start, end, label=label)
  21.  
  22. # วาดกราฟ
  23. pos = {"q0": (0, 1), "q1": (2, 1), "qf": (4, 1)}
  24. labels = {(u, v): d['label'] for u, v, d in G.edges(data=True)}
  25.  
  26. plt.figure(figsize=(6, 4))
  27. nx.draw(G, pos, with_labels=True, node_color="lightblue", node_size=2000, edge_color="black", font_size=12)
  28. nx.draw_networkx_edge_labels(G, pos, edge_labels=labels, font_size=10)
  29. plt.title("Turing Machine Transition Graph")
  30. plt.show()
  31.  
Success #stdin #stdout 0.02s 25856KB
stdin
Standard input is empty
stdout
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()