-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex14.4.lua
More file actions
35 lines (32 loc) · 917 Bytes
/
ex14.4.lua
File metadata and controls
35 lines (32 loc) · 917 Bytes
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
function FindShortestPath(graph, from, to)
assert(graph[from])
assert(graph[to])
local distance = {}
local unvisited = {}
for name in pairs(graph) do
unvisited[name] = true
distance[name] = math.maxinteger
end
distance[from] = 0
local current = from
while not unvisited[to] and current do
local next = nil
local smallest_distance = math.maxinteger
for _, arc in pairs(graph[current].arcs) do
local current_distance = distance[current] + arc.label
local current_end = arc.node_to
if unvisited[current_end] then
if current_distance < distance[current_end] then
distance[current_end] = current_distance
end
end
if current_distance < smallest_distance then
smallest_distance = current_distance
next = arc.node_to
end
end
unvisited[current] = nil
current = next
end
return distance[to]
end