Skip to content

Commit

Permalink
speed up has_path by avoiding popfirst! and push!
Browse files Browse the repository at this point in the history
  • Loading branch information
thchr committed Oct 31, 2024
1 parent 7994f2d commit 5059bc5
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/traversals/bfs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,18 @@ function has_path(
end
(seen[u] || seen[v]) && return false
u == v && return true # cannot be separated
next = Vector{T}()
push!(next, u)
next = Vector{T}(undef, nv(g))
front = back = 1
next[front] = u
seen[u] = true
while !isempty(next)
src = popfirst!(next) # get new element from queue
while front <= back
src = next[front]
front += 1 # dequeue; pop from queue
for vertex in outneighbors(g, src)
vertex == v && return true
if !seen[vertex]
push!(next, vertex) # push onto queue
back += 1
next[back] = vertex # enqueue; push onto queue
seen[vertex] = true
end
end
Expand Down

0 comments on commit 5059bc5

Please sign in to comment.