Skip to content

Commit 85a2d03

Browse files
authored
Don't store dist as key in frontier (#54)
* Don't store dist as key in frontier * Set dists[s] to zero
1 parent 4854c4d commit 85a2d03

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/a_star.jl

+9-7
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,18 @@ function a_star_algorithm(g::LightGraphs.AbstractGraph{U}, # the g
5959
heuristic::Function = (u,v) -> zero(T)) where {T, U}
6060
nvg = nv(g)
6161
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)
6466
dists = fill(typemax(T), nvg)
67+
dists[s] = zero(T)
6568
parents = zeros(U, nvg)
6669
colormap = zeros(UInt8, nvg)
6770
colormap[s] = 1
6871
@inbounds while !isempty(frontier)
69-
(cost_so_far, u) = dequeue!(frontier)
72+
u = dequeue!(frontier)
73+
cost_so_far = dists[u]
7074
u == t && (return OpenStreetMapX.extract_a_star_route(parents,s,u), cost_so_far)
7175
for v in LightGraphs.outneighbors(g, u)
7276
col = colormap[v]
@@ -77,13 +81,11 @@ function a_star_algorithm(g::LightGraphs.AbstractGraph{U}, # the g
7781
if iszero(col)
7882
parents[v] = u
7983
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))
8385
elseif path_cost < dists[v]
8486
parents[v] = u
8587
dists[v] = path_cost
86-
frontier[path_cost, v] = path_cost + heuristic(v,t)
88+
frontier[v] = path_cost + heuristic(v,t)
8789
end
8890
end
8991
end

0 commit comments

Comments
 (0)