Skip to content

Instantly share code, notes, and snippets.

@mschauer
Created August 2, 2017 20:25
Show Gist options
  • Save mschauer/263d84267f21c276db44df3c13eb848f to your computer and use it in GitHub Desktop.
Save mschauer/263d84267f21c276db44df3c13eb848f to your computer and use it in GitHub Desktop.
function has_path(g::AbstractGraph, u::Integer, v::Integer; exclude_vertices::AbstractVector=Vector{eltype(g)}())
T = eltype(g)
seen = falses(nv(g))
next = Vector{T}()
push!(next, u)
excludeset = Set(exclude_vertices)
while !isempty(next)
src = shift!(next) #Get first element
src in excludeset && continue
vertexneighbors = neighbors(g, src)
for vertex in vertexneighbors
if vertex == v
return true
end
#If not already set, and is not found in the queue.
if !seen[vertex]
push!(next, vertex) #Push onto queue
seen[vertex] = true
end
end
end
return false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment