Implementing Randomized Kruskal’s Algorithm

To implement this, we’re going to create a new class to represent the state of the algorithm—the collection of information that it will use internally. Once we’ve got this State class written, the rest of Kruskal’s algorithm becomes almost trivial to implement. Don’t take my word for it, though. Watch and see!

As you saw in the previous section, Kruskal’s initially requires that each cell be associated with a unique set. This suggests that, at a minimum, our State class is going to need to track that association. We’ll need a way to query the set that a particular cell belongs to, as well as the list of cells that belong to a given set. Also, the algorithm wants to select random pairs of neighboring ...

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.