forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.rb
52 lines (38 loc) · 1.17 KB
/
dijkstra.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true
# Algoritmo de Dijkstra em Ruby
# Carlos Alves
# https://github.com/EuCarlos
def dijkstra(matrix, vertices)
visited = Array.new(vertices)
(1..vertices - 1).each do |_i|
min = nil
min_value = nil
(1..vertices - 1).each do |j|
if !visited[j] && (!min_value || matrix[j][0] < min_value)
min = j
min_value = matrix[j][0]
end
end
visited[min] = true
(1..vertices - 1).each do |j|
matrix[j][0] = matrix[min][0] + matrix[min][j] if matrix[min][0] + matrix[min][j] < matrix[j][0]
end
end
end
def main
n_vertices = 5
matrix = []
ignore = 4_294_967_295
matrix.push([0, 6, 10, ignore, ignore])
matrix.push([6, 0, ignore, 2, ignore])
matrix.push([10, ignore, 0, 1, 3])
matrix.push([ignore, 2, 1, 0, 8])
matrix.push([ignore, ignore, 3, 8, 0])
matrix.to_a.each { |row| puts row.inspect }
dijkstra(matrix, n_vertices)
puts "============================================================"
puts "Total caminho mais curto do vertice 0 ao 4: #{matrix[4][0]}"
puts "============================================================"
matrix.to_a.each { |row| puts row.inspect }
end
main