Skip to content

Instantly share code, notes, and snippets.

@kjwierenga
Created June 1, 2011 07:55
Show Gist options
  • Save kjwierenga/1001947 to your computer and use it in GitHub Desktop.
Save kjwierenga/1001947 to your computer and use it in GitHub Desktop.
Dijkstra's Algorithm in Ruby
# From http://www.algolist.com/code/java/Dijkstra%27s_algorithm
# Here you can find the algorithm in Java, C, C++, Python, Ruby and PHP.
#
# You need pqueue: gem install pqueue
#
require 'pqueue'
class Algorithm
INFINITY = 1 << 32
def self.dijkstra(source, edges, weights, n)
visited = Array.new(n, false)
shortest_distances = Array.new(n, INFINITY)
previous = Array.new(n, nil)
pq = PQueue.new(proc {|x,y| shortest_distances[x] < shortest_distances[y]})
pq.push(source)
visited = true
shortest_distances = 0
while pq.size != 0
v = pq.pop
visited[v] = true
if edges[v]
edges[v].each do |w|
if !visited[w] and shortest_distances[w] > shortest_distances[v] + weights[v][w]
shortest_distances[w] = shortest_distances[v] + weights[v][w]
previous[w] = v
pq.push(w)
end
end
end
end
return [shortest_distances, previous]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment