Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Commit

Permalink
Sbromberger/inbounds bfs (#754)
Browse files Browse the repository at this point in the history
* get rid of unions in edge iters

* f

* inbounds on critical BFS loops

* test fix
  • Loading branch information
sbromberger authored Sep 16, 2017
1 parent 3345727 commit 907e6ab
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/graphtypes/simplegraphs/simpleedgeiter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ end
eltype(::Type{SimpleEdgeIter{SimpleGraph{T}}}) where {T} = SimpleGraphEdge{T}
eltype(::Type{SimpleEdgeIter{SimpleDiGraph{T}}}) where {T} = SimpleDiGraphEdge{T}

function edge_start(g::Union{SimpleGraph{T}, SimpleDiGraph{T}}) where {T <: Integer}
function edge_start(g::AbstractSimpleGraph)
T = eltype(g)
s = one(T)
while s <= nv(g)
isempty(fadj(g, s)) || return SimpleEdgeIterState(s, 1)
Expand All @@ -26,7 +27,7 @@ function edge_start(g::Union{SimpleGraph{T}, SimpleDiGraph{T}}) where {T <: Inte
return SimpleEdgeIterState(zero(T), 1)
end

function edge_next(g::Union{SimpleGraph{T}, SimpleDiGraph{T}},
function edge_next(g::AbstractSimpleGraph,
state::SimpleEdgeIterState{T}) where {T <: Integer}
s = state.s
di = state.di
Expand Down
11 changes: 7 additions & 4 deletions src/traversals/bfs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,26 @@ For vertices in disconnected components the default distance is -1.
function gdistances!(g::AbstractGraph, source, dists)
T = eltype(g)
n = nv(g)
fill!(dists, -1)
fill!(dists, typemax(T))
seen = zeros(Bool, n)
queue = Vector{T}(n)
for i in 1:length(source)
@inbounds for i in 1:length(source)
queue[i] = source[i]
dists[source[i]] = 0
seen[source[i]] = true
end
head = 1
tail = length(source)
while head <= tail
current = queue[head]
distance = dists[current] + 1
head += 1
for j in out_neighbors(g, current)
if dists[j] == -1
@inbounds for j in out_neighbors(g, current)
if !seen[j]
dists[j] = distance
tail += 1
queue[tail] = j
seen[j] = true
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/traversals/bfs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import LightGraphs: tree
for g in testgraphs(g6)
@test @inferred(gdistances(g, 2)) == [1, 0, 2, 1, 2]
@test @inferred(gdistances(g, [1, 2])) == [0, 0, 1, 1, 2]
@test @inferred(gdistances(g, [])) == [-1, -1, -1, -1, -1]
@test @inferred(gdistances(g, [])) == fill(typemax(eltype(g)), 5)
end

gx = Graph(5)
Expand Down

0 comments on commit 907e6ab

Please sign in to comment.