Research Article

Implementation and Comparison of Maze Solving Algorithms in OCaml

Qipeng Han*
Northeastern University

* Corresponding author: [email protected]

Abstract

With the continuous development and application needs of robots, games and intelligent navigation, maze-solving algorithms have also begun to be researched in relation to traditional graph-search algorithms. The three maze-solving algorithms and their corresponding results in this paper are DFS, BFS and A*, and they are all based on graph-search methods for 2D-grid mazes. A* is based on the Manhattan distance. Experiments are carried out on the three types of mazes (sparse, dense and complex) and the three scales (10x10, 20x20 and 50x50), and the main indicators are execution time, path length, visited nodes and scalability. BFS and A* both return the shortest path; however, A* has visited fewer nodes than BFS. For example, 419 vs. 1480 in the 50x50 maze; DFS can find the maze in the shortest time but has a longer path. As OCaml has pattern matching and recursion, it can be used to implement the above search algorithm.

Keywords: Maze solving; pathfinding; OCaml; search algorithms
Published: August 12, 2026
DOI: 10.54254/2753-8818/2026.PU36012
Volume: TNS Vol.185
pp. 23-33
Download PDF

References

  1. Rohan, S., Shandilya, S., Rawat, S., & Choudhary, A. (2025). Comparative analysis of BFS, Dijkstra, and A* pathfinding algorithms in 2D maze: A time-based approach. In2025 International Conference on Intelligent and Secure Engineering Solutions (CISES)(pp. 1205–1208). IEEE. https://doi.org/10.1109/CISES66934.2025.11265425
  2. AlKahtani, R., Alhabdan, A., Alosami, M., Alshammari, W., Abdo, A. A., & Hamdi, L. (2025). Comparative analysis between BFS and DFS-shortest path algorithms. In2025 IEEE Open Conference of Electrical, Electronic and Information Sciences (eStream)(pp. 1–5). IEEE. https://doi.org/10.1109/eStream66938.2025.11016860
  3. Espejon, K. T., Jayoma, J. M., & Empang, K. P. A. (2024). Hybrid A* and inverse ant algorithm for adaptive pathfinding in constrained node networks. In2024 IEEE 16th International Conference on Humanoid, Nanotechnology, Information Technology, Communication and Control, Environment, and Management (HNICEM)(pp. 1–6). IEEE. https://doi.org/10.1109/HNICEM64917.2024.11258860
  4. Giegerich, R., & Hughes, J. (2021). Functional programming in the real world. InDagstuhl Seminar 9420: Functional programming. Schloss Dagstuhl–Leibniz-Zentrum für Informatik.
  5. Hickey, J. (2008). Introduction to Objective Caml. California Institute of Technology. http://www.cs.caltech.edu/courses/cs134/cs134b/book.pdf
  6. Doligez, A., et al. (2020). Objective Caml for multicore architectures.arXiv. https://doi.org/10.48550/arXiv.2006.05862
  7. Wang, Q. (2024). Comparative analysis of maze solving algorithms based on OCaml.AIP Conference Proceedings, 3194(1), 040001. https://doi.org/10.1063/5.0222821
  8. Wang, X. (2024). OCaml-based recursive backtracking and Aldous Broder algorithms in solving maze challenges.AIP Conference Proceedings, 3194(1), 050011. https://doi.org/10.1063/5.0226607
  9. Wu, C.-M., Liaw, D.-C., & Lee, H.-T. (2018). A method for finding the routes of mazes. In2018 International Automatic Control Conference (CACS)(pp. 1–4). IEEE. https://doi.org/10.1109/CACS.2018.8606753
  10. Russell, S. J., & Norvig, P. (2010).Artificial intelligence: A modern approach(3rd ed.). Pearson Education.
  11. Minsky, Y., Madhavapeddy, A., & Hickey, J. (2013).Real world OCaml: Functional programming for the masses. O'Reilly Media.
  12. Gabrovšek, P. (2019). Analysis of maze generation algorithms.IPSI Transactions on Internet Research, 15(1), 23–30.
  13. Vijaya, J., Gopu, A., Vinayak, K., et al. (2024). Analysis of maze generation algorithms. In2024 5th International Conference on Innovative Trends in Information Technology (ICITIIT)(pp. 1–6). IEEE.
  14. Hart, P. E., Nilsson, N. J., & Raphael, B. (1968). A formal basis for the heuristic determination of minimum cost paths.IEEE Transactions on Systems Science and Cybernetics, 4(2), 100–107. https://doi.org/10.1109/TSSC.1968.300136