Implementing Dijkstra's Algorithm

The Distances class

To bring a simplified version of Dijkstra’s together, we’re going to lean on a new class, called Distances, which will keep track of how far each cell is from the reference cell (the cell where we start counting from). Once we’ve implemented this class, we’ll add our actual implementation of Dijkstra’s to the Cell class, which will let us apply it almost anywhere we need to.

So, first off, let’s add that Distances class. For now, it’s just a simple wrapper around a Hash instance, but we’ll soon be making it more useful by creating a new file named distances.rband adding the following code to it.

Get hands-on with 1400+ tech skills courses.