Hanoi Puzzle, Complexity Analysis of the Recursive, Iterative, and RBC Approaches - El Ghabzouri, Mohammad; Es Salhi, Abdenacer (Integrated STEM Education Conference (Online), 2025)
This paper focuses mainly on the time and space complexity computation of the Tower of Hanoi, this mathematical game or puzzle is a well-known problem for its exponential time consumption, which is challenging in terms of performance, especially with the rising of its implications in education purposes, in addition to its exploration in machine learning algorithms for artificial intelligence ingenuities. The present work contrasts the exploited hardware resource. Hence, the running time and the space consumption of both the recursive and the iterative algorithms will be compared with the proposed gray code-based solution. Using the very popular programming language Python, the results of the performed measurements show a very high consumption of the gray code-based algorithm compared to the recursive and the iterative ones, either in terms of time or space complexity. Furthermore, the bytecode of these algorithms in terms of instructions is much lighter than the reflected binary code technique. Finally, regarding these results, the authors highly recommend using the iterative approach in machine learning when handling large data sets, while the elegant recursive algorithm is still fine when dealing with small or moderate data volumes, whereas the gray code-based algorithm must be considered mostly for educational uses or for scientific purposes.