'How can I effectively use networkX to create a graph of my iterative function output? Towers of Hanoi Graph Theory
I am somewhat a novice with Python but I have been tasked with solving Towers of Hanoi problem in minimum number of moves for "n" disks. Below shows how I am able to print each individual move:
def hanoi(n, start, end):
if n == 1:
pm(start, end)
else:
other = 6 - (start + end)
hanoi(n - 1, start, other)
pm(start, end)
hanoi(n - 1, other, end)
def pm(start, end):
print(start, '->', end)
Now where I am struggling is representing this in a graph and I am attempting to leverage networkX python package to create the nodes and edges. I started with the following:
import networkx as nx
G = nx.Graph()
G.add_nodes_from([hanoi(3, 1, 3)])
This gives the error "ValueError: None cannot be a node" so I think my first challenge is to turn my hanoi function output into a list.
Please let me know if anyone has ideas on how to solve this, and if I am on the right track.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
