Skip to content

Instantly share code, notes, and snippets.

@mpakus
Created October 27, 2021 14:19
Show Gist options
  • Save mpakus/56fadf8e4e6a8c26c7e702975389171d to your computer and use it in GitHub Desktop.
Save mpakus/56fadf8e4e6a8c26c7e702975389171d to your computer and use it in GitHub Desktop.
Dijkstra's algorithm to search shortest path in Graph
class Graph
def initialize
@graph = {
"Odin" => [["Thor", 1], ["Heimdall", 1]],
"Fregg" => [["Heimdall", 1], ["Thor", 1]],
"Heimdall" => [["Odin", 1], ["Fregg", 1]],
"Thor" => [["Odin", 1], ["Loki", 1], ["Fregg", 1]],
"Loki" => [["Thor", 1]]
}
end
def search(from, to)
@from = from
@to = to
travel_by(calculate_distances, from, to).reverse
end
def travel_by(distances, from, to)
path = []
path << to
return path if from == to
path += travel_by(distances, from, distances[to][0])
path
end
def calculate_distances
distance = 0
unvisited = []
distances = {}
graph.each do |k, _v|
unvisited << k
distances[k] = ["", Float::INFINITY]
end
current = from
while unvisited.length > 0
neighbors = graph[current]
neighbors.each do |node|
node_name, node_length = node
if (distance + node_length) <= distances[node_name][1]
distances[node_name] = [current, distance + node_length]
end
end
unvisited.delete(current)
break if unvisited.length == 0
unvisited.sort_by {|node| distances[node][1] }
current = unvisited[0]
distance = distances[current][1]
end
distances
end
private
attr_accessor :graph
attr_reader :from, :to
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment