Implementing the Aldous-Broder Algorithm
Learn to implement the Aldous-Broder algorithm in Ruby.
We'll cover the following...
The AldousBroder
class
As might be expected, the random walk forms the core of our implementation, repeatedly visiting neighboring cells until no unvisited cells remain. It comes together without any surprises, just as described.
We'll create a new file named aldous_broder.rb
. As before, we’ll put the algorithm in its own class so we can reuse it more easily.
Press + to interact
class AldousBroderdef self.on(grid)cell = grid.random_cellunvisited = grid.size - 1while unvisited > 0neighbor = cell.neighbors.sampleif neighbor.links.empty?cell.link(neighbor)unvisited -= 1endcell = neighborendgridendend
Code explanation
Lines 4–5: We start ...