Skip to content

Commit 6954605

Browse files
committed
speed up has_path by avoiding popfirst! and push!
1 parent 24539fd commit 6954605

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/traversals/bfs.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,18 @@ function has_path(
182182
end
183183
(seen[u] || seen[v]) && return false
184184
u == v && return true # cannot be separated
185-
next = Vector{T}()
186-
push!(next, u)
185+
next = Vector{T}(undef, nv(g))
186+
front = back = 1
187+
next[front] = u
187188
seen[u] = true
188-
while !isempty(next)
189-
src = popfirst!(next) # get new element from queue
189+
while front <= back
190+
src = next[front]
191+
front += 1 # dequeue; pop from queue
190192
for vertex in outneighbors(g, src)
191193
vertex == v && return true
192194
if !seen[vertex]
193-
push!(next, vertex) # push onto queue
195+
back += 1
196+
next[back] = vertex # enqueue; push onto queue
194197
seen[vertex] = true
195198
end
196199
end

0 commit comments

Comments
 (0)