Kruskal
Kruskal Complex Example


Kruskal algorithm begins by setting up a grid, where each cell is surrounded by walls. Initially, it is fully enclosed, with no passages between any of the cells, creating a solid block.

Kruskal Algorithm Image

As the next step each of the cells is assigned to the unique group. Each of them will be represented by separate color.

Kruskal Algorithm Image

When the groups are assigned the algorithm starts from listing all of the internal walls and selecting them at random order. In the given example the first wall got selected.

Kruskal Algorithm Image

If the both of the neighbouring cells are assigned to the different groups the wall is destroyed and all of the cells assigned to the second group are reassigned to the first cell's group.

Kruskal Algorithm Image

The same pattern applies for the next iterations. The random wall is selected and the cells groups are checked. It is important to note that each of the walls can be processed only once. Here is the illustration of next five walls being processed where all of the nodes were assigned to the different groups.

Kruskal Algorithm Image

The next example shows the case where nodes divided by the wall are assigned to the different groups, where each of the groups has more than one element.

Kruskal Algorithm Image

As an output we can see that the wall was successfully removed and the second group got totally replaced by the first one.

Kruskal Algorithm Image

In the further iteration the next wall got selected. This time both of the neighbouring cells are located in the same group so this wall must persist. The algorithm skips the wall removal and moves to the next iteration.

Kruskal Algorithm Image

The process goes on until every wall is visited. As an end result we achieve the complete maze with all of the passages created.

Kruskal Algorithm Image