Skip to content

Improve performance of bfs functions #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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