@@ -59,14 +59,18 @@ function a_star_algorithm(g::LightGraphs.AbstractGraph{U}, # the g
59
59
heuristic:: Function = (u,v) -> zero (T)) where {T, U}
60
60
nvg = nv (g)
61
61
checkbounds (distmx, Base. OneTo (nvg), Base. OneTo (nvg))
62
- frontier = DataStructures. PriorityQueue {Tuple{T, U},T} ()
63
- frontier[(zero (T), U (s))] = zero (T)
62
+ frontier = DataStructures. PriorityQueue {U,T} ()
63
+ # The value should be `heuristic(s, t)` but it does not matter since it will
64
+ # be `dequeue!`d in the first iteration independently of the value.
65
+ frontier[U (s)] = zero (T)
64
66
dists = fill (typemax (T), nvg)
67
+ dists[s] = zero (T)
65
68
parents = zeros (U, nvg)
66
69
colormap = zeros (UInt8, nvg)
67
70
colormap[s] = 1
68
71
@inbounds while ! isempty (frontier)
69
- (cost_so_far, u) = dequeue! (frontier)
72
+ u = dequeue! (frontier)
73
+ cost_so_far = dists[u]
70
74
u == t && (return OpenStreetMapX. extract_a_star_route (parents,s,u), cost_so_far)
71
75
for v in LightGraphs. outneighbors (g, u)
72
76
col = colormap[v]
@@ -77,13 +81,11 @@ function a_star_algorithm(g::LightGraphs.AbstractGraph{U}, # the g
77
81
if iszero (col)
78
82
parents[v] = u
79
83
dists[v] = path_cost
80
- enqueue! (frontier,
81
- (path_cost, v),
82
- path_cost + heuristic (v,t))
84
+ enqueue! (frontier, v, path_cost + heuristic (v,t))
83
85
elseif path_cost < dists[v]
84
86
parents[v] = u
85
87
dists[v] = path_cost
86
- frontier[path_cost, v] = path_cost + heuristic (v,t)
88
+ frontier[v] = path_cost + heuristic (v,t)
87
89
end
88
90
end
89
91
end
0 commit comments