Skip to content

Commit

Permalink
Optimize a bit bfs and dfs iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
Tortar authored Jun 3, 2024
1 parent 43f9f18 commit fbebdc3
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions src/iterators/bfs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ First iteration to visit vertices in a graph using breadth-first search.
function Base.iterate(t::BFSIterator{<:Integer})
visited = falses(nv(t.graph))
visited[t.source] = true
return (t.source, BFSVertexIteratorState(visited, [t.source], 1, 1))
state = BFSVertexIteratorState(visited, [t.source], 1, 1)
return (t.source, state)
end

function Base.iterate(t::BFSIterator{<:AbstractArray})
Expand All @@ -75,35 +76,33 @@ Iterator to visit vertices in a graph using breadth-first search.
"""
function Base.iterate(t::BFSIterator, state::BFSVertexIteratorState)
graph, visited, queue = t.graph, state.visited, state.queue
while !isempty(queue)
if state.n_visited == nv(graph)
return nothing
end
@inbounds while !isempty(queue) && state.n_visited != nv(graph)
# we visit the first node in the queue
node_start = first(queue)
if !visited[node_start]
visited[node_start] = true
curr_node = first(queue)
if !visited[curr_node]
visited[curr_node] = true
state.n_visited += 1
return (node_start, state)
return (curr_node, state)
end
# which means we arrive here when the first node was visited.
neigh = outneighbors(graph, node_start)
if state.neighbor_idx <= length(neigh)
node = neigh[state.neighbor_idx]
neigh = outneighbors(graph, curr_node)
neighbor_idx = state.neighbor_idx
while neighbor_idx <= length(neigh)
adj_node = neigh[neighbor_idx]
# we update the idx of the neighbor we will visit,
# if it is already visited, we repeat
state.neighbor_idx += 1
if !visited[node]
push!(queue, node)
state.visited[node] = true
neighbor_idx += 1
if !visited[adj_node]
push!(queue, adj_node)
state.visited[adj_node] = true
state.n_visited += 1
return (node, state)
state.neighbor_idx = neighbor_idx
return (adj_node, state)
end
else
# when the first node and its neighbors are visited
# we remove the first node of the queue
popfirst!(queue)
state.neighbor_idx = 1
end
# when the first node and its neighbors are visited
# we remove the first node of the queue
popfirst!(queue)
state.neighbor_idx = 1
end
end

0 comments on commit fbebdc3

Please sign in to comment.