Implementing Eller's Algorithm
Learn how to implement Eller's algorithm using Ruby.
The Ellers class
Eller’s algorithm works strictly one row at a time, so our implementation will take advantage of that by leaning on a row-centric state object. Once we have that state object, the rest of the algorithm comes together quickly. We’ll start with the RowState class, which we’ll place under the namespace of the Ellers class. So, let’s first create the RowState class.
The RowState class
C++
class RowStatedef initialize(starting_set=0)@cells_in_set = {}@set_for_cell = []@next_set = starting_setenddef record(set, cell)@set_for_cell[cell.column] = set@cells_in_set[set] = [] if !@cells_in_set[set]@cells_in_set[set].push cellenddef set_for(cell)if !@set_for_cell[cell.column]record(@next_set, cell)@next_set += 1end@set_for_cell[cell.column]enddef merge(winner, loser)@cells_in_set[loser].each do |cell|@set_for_cell[cell.column] = winner@cells_in_set[winner].push cellend@cells_in_set.delete(loser)enddef nextRowState.new(@next_set)enddef each_set@cells_in_set.each { |set, cells| yield set, cells }selfendend
Code explanation
Lines 2–6: The initialize method has a starting_set parameter (defaulting to 0), which is used to determine what value is used for new ...
Ask