Introducing Prim’s Algorithm

Prim’s algorithm was first developed in 1930 by a Czech mathematician named Vojtěch Jarník, but it gets its name from Robert C. Prim, a computer scientist who rediscovered it independently in 1957. It works similarly to Dijkstra’s algorithm, starting at one point in the grid and then moving outward like water flowing, but in Prim’s case it does more than just measure distances and costs. The end result of Prim’s is one of those fancy minimum spanning trees—or, in our case, a maze.

As with Kruskal’s algorithm, Prim’s works by considering the weights on the connections between cells—passage costs—rather than on the individual cells themselves. We’ll look at how this comes together, but then we’ll take a step back ...

Get Mazes for Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.