Skip to content

Commit

Permalink
Improve performance of bfs functions (JuliaGraphs#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tortar committed Aug 31, 2023
1 parent c3eac7f commit aeb7e30
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/traversals/bfs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@ end
function _bfs_parents(g::AbstractGraph{T}, source, neighborfn::Function) where {T}
n = nv(g)
visited = falses(n)
parents = zeros(T, nv(g))
parents = zeros(T, n)
cur_level = Vector{T}()
sizehint!(cur_level, n)
next_level = Vector{T}()
sizehint!(next_level, n)
k = 0
@inbounds for s in source
visited[s] = true
push!(cur_level, s)
parents[s] = s
if !visited[s]
visited[s] = true
push!(cur_level, s)
parents[s] = s
k += 1
end
end
while !isempty(cur_level)
@inbounds for v in cur_level
Expand All @@ -60,8 +64,10 @@ function _bfs_parents(g::AbstractGraph{T}, source, neighborfn::Function) where {
push!(next_level, i)
parents[i] = v
visited[i] = true
k += 1
end
end
k == n && return parents
end
empty!(cur_level)
cur_level, next_level = next_level, cur_level
Expand Down Expand Up @@ -105,10 +111,14 @@ function gdistances!(g::AbstractGraph{T}, source, vert_level; sort_alg=QuickSort
sizehint!(cur_level, n)
next_level = Vector{T}()
sizehint!(next_level, n)
k = 0
@inbounds for s in source
vert_level[s] = zero(T)
visited[s] = true
push!(cur_level, s)
if !visited[s]
vert_level[s] = zero(T)
visited[s] = true
push!(cur_level, s)
k += 1
end
end
while !isempty(cur_level)
@inbounds for v in cur_level
Expand All @@ -117,8 +127,10 @@ function gdistances!(g::AbstractGraph{T}, source, vert_level; sort_alg=QuickSort
push!(next_level, i)
vert_level[i] = n_level
visited[i] = true
k += 1
end
end
k == n && return vert_level
end
n_level += one(T)
empty!(cur_level)
Expand Down

0 comments on commit aeb7e30

Please sign in to comment.