Skip to content

Commit

Permalink
optimize bfs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tortar authored Jan 29, 2024
1 parent 790572e commit 4f6bd50
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/iterators/bfs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mutable struct BFSVertexIteratorState <: AbstractIteratorState
visited::BitArray
queue::Vector{Int}
neighbor_idx::Int
n_visited::Int
end

BFSIterator(g::AbstractGraph) = BFSIterator(g, one(eltype(g)))
Expand All @@ -51,12 +52,12 @@ First iteration to visit each vertex in a graph using breadth-first search.
function Base.iterate(t::BFSIterator)
visited = falses(nv(t.graph))
visited[t.source] = true
return (t.source, BFSVertexIteratorState(visited, [t.source], 1))
return (t.source, BFSVertexIteratorState(visited, [t.source], 1, 1))
end
function Base.iterate(t::BFSIterator{<:AbstractArray})
visited = falses(nv(t.graph))
visited[first(t.source)] = true
return (first(t.source), BFSVertexIteratorState(visited, t.source, 1))
return (first(t.source), BFSVertexIteratorState(visited, t.source, 1, 1))
end

"""
Expand All @@ -66,9 +67,13 @@ Iterator to visit each vertex in a graph using breadth-first search.
"""
function Base.iterate(t::BFSIterator, state::BFSVertexIteratorState)
while !isempty(state.queue)
if state.n_visited == length(state.visited)
return nothing
end
node_start = first(state.queue)
if !state.visited[node_start]
state.visited[node_start] = true
state.n_visited += 1
return (node_start, state)
end
idx = state.neighbor_idx
Expand All @@ -79,6 +84,7 @@ function Base.iterate(t::BFSIterator, state::BFSVertexIteratorState)
if !state.visited[node]
push!(state.queue, node)
state.visited[node] = true
state.n_visited += 1
return (node, state)
end
else
Expand Down

0 comments on commit 4f6bd50

Please sign in to comment.