Skip to content

Generating with Kruskal

Alex Lopez edited this page Oct 20, 2023 · 3 revisions

kruskal-loop

I lump Kruskal, Prim, and Eller together as quite similar but their visual animations are different. Kruskal reasons about the cells and walls in a maze in terms of Disjoint Sets. In fact, I was able to learn about this data structure through this algorithm. I will not go into full detail on what it is, only explain how it is used in this algorithm. The algorithm goes something like this.

load all square into a disjoint set as single unique sets

shuffle all the walls in the maze randomly

for every wall in the maze

	if the current wall seperates a square above and below

		if a disjoint set union find by rank merges these squares

			break the wall between these squares and join them

	else if the current wall seperates a square left and right

		if a disjoint set union find by rank merges these squares

			break the wall between these squares and join them

I am not doing a good job of respecting my space efficiency restriction with this algorithm. I will have to learn more about different approaches because the Disjoint set, walls, and lookup table for squares and their Disjoint set ids takes much space. This is a fun algorithm to watch, however, because of the popping in of maze paths all over the grid.