On page 379, this part of the Dijkstra shortest path code is supposed to determine the next unvisited city to visit based on it being the cheapest to get to from the starting city:
# We visit our next unvisited city. We choose the one that is cheapest # to get to from the STARTING city: current_city = unvisited_cities.min do |city| cheapest_prices_table[city.name] end end
I don’t understand this syntax; is anyone able to explain how this works?
unvisited_cities is an array of City objects and .min cannot be directly applied to this array.
And apparently “unvisited_cities.min do |city|” is different from something like “unvisited_cities.each do |city|”?
Is anyone able to walk me through what this bit of code does/how it works? I’m not understanding how this determines the cheapest unvisited city.