From 738f9cac228c2c547a547932588673920a75a8ef Mon Sep 17 00:00:00 2001 From: Leon Rische Date: Thu, 3 Mar 2016 16:07:24 +0100 Subject: [PATCH] code for chapter 7 in ruby --- .../ruby/01_dijkstras_algorithm.rb | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 07_dijkstras_algorithm/ruby/01_dijkstras_algorithm.rb diff --git a/07_dijkstras_algorithm/ruby/01_dijkstras_algorithm.rb b/07_dijkstras_algorithm/ruby/01_dijkstras_algorithm.rb new file mode 100644 index 0000000..bb9001f --- /dev/null +++ b/07_dijkstras_algorithm/ruby/01_dijkstras_algorithm.rb @@ -0,0 +1,69 @@ +# the graph +graph = {} +graph["start"] = {} +graph["start"]["a"] = 6 +graph["start"]["b"] = 2 + +graph["a"] = {} +graph["a"]["fin"] = 1 + +graph["b"] = {} +graph["b"]["a"] = 3 +graph["b"]["fin"] = 5 + +graph["fin"] = {} + +# the costs table +costs = {} +costs["a"] = 6 +costs["b"] = 2 +costs["fin"] = Float::INFINITY + +# the parents table +parents = {} +parents["a"] = "start" +parents["b"] = "start" +parents["fin"] = nil + +@processed = [] + +def find_lowest_cost_node(costs) + lowest_cost = Float::INFINITY + lowest_cost_node = nil + # Go through each node. + costs.each do |node, cost| + # If it's the lowest cost so far and hasn't been processed yet... + if cost < lowest_cost && !@processed.member?(node) + # ... set it as the new lowest-cost node. + lowest_cost = cost + lowest_cost_node = node + end + end + lowest_cost_node +end + +# Find the lowest-cost node that you haven't processed yet. +node = find_lowest_cost_node(costs) +# If you've processed all the nodes, this while loop is done. +until node.nil? + cost = costs[node] + # Go through all the neighbors of this node. + neighbors = graph[node] + neighbors.keys.each do |n| + new_cost = cost + neighbors[n] + # If it's cheaper to get to this neighbor by going through this node... + if costs[n] > new_cost + # ... update the cost for this node. + costs[n] = new_cost + # This node becomes the new parent for this neighbor. + parents[n] = node + end + end + # Mark the node as processed. + @processed << node + # Find the next node to process, and loop. + node = find_lowest_cost_node(costs) +end + +puts "Cost from the start to each node:" +puts costs