Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 8 additions & 6 deletions src/graphs/BasicGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,13 @@ multiplicity*. To get the unique neighbors, call `unique(neighbors(g))`.

""" In-neighbors of vertex in a graph.
"""
@inline inneighbors(g::AbstractGraph, v::Int) = @inbounds subpart(g, incident(g, v, :tgt), :src)
inneighbors(g::AbstractGraph, v::Int) =
(subpart(g, e, :src) for e in incident(g, v, :tgt))

""" Out-neighbors of vertex in a graph.
"""
@inline outneighbors(g::AbstractGraph, v::Int) = @inbounds subpart(g, incident(g, v, :src), :tgt)
outneighbors(g::AbstractGraph, v::Int) =
(subpart(g, e, :tgt) for e in incident(g, v, :src))

""" Union of in-neighbors and out-neighbors in a graph.
"""
Expand Down Expand Up @@ -286,10 +288,10 @@ rem_edges!(g::AbstractSymmetricGraph, es) =
rem_parts!(g, :E, unique!(sort!([es; inv(g, es)])))

neighbors(g::AbstractSymmetricGraph, v::Int) =
subpart(g, incident(g, v, :src), :tgt)
inneighbors(g::AbstractSymmetricGraph, v::Int) = neighbors(g, v)
outneighbors(g::AbstractSymmetricGraph, v::Int) = neighbors(g, v)
all_neighbors(g::AbstractSymmetricGraph, v::Int) = neighbors(g, v)
(subpart(g, e, :tgt) for e in incident(g, v, :src))
@inline inneighbors(g::AbstractSymmetricGraph, v::Int) = neighbors(g, v)
@inline outneighbors(g::AbstractSymmetricGraph, v::Int) = neighbors(g, v)
@inline all_neighbors(g::AbstractSymmetricGraph, v::Int) = neighbors(g, v)

# Reflexive graphs
##################
Expand Down
2 changes: 1 addition & 1 deletion src/graphs/GraphAlgorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function topological_sort(g::ACSet, ::TopologicalSortByDFS)
push!(stack, v)
while !isempty(stack)
u = first(stack)
u_out = outneighbors(g, u)
u_out = collect(outneighbors(g, u))
i = findfirst(u_out) do w
marking[w] != TempMarked || error("Graph is not acyclic: $g")
marking[w] == Unmarked
Expand Down
12 changes: 7 additions & 5 deletions src/graphs/Searching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ implementations which are marginally faster in practice for smaller graphs,
but the performance improvements using this implementation on large graphs
can be significant.
"""
bfs_parents(g::ACSet, s::Int; dir = :out) =
(dir == :out) ? _bfs_parents(g, s, outneighbors) : _bfs_parents(g, s, inneighbors)
function bfs_parents(g::ACSet, s::Int; dir = :out)
_bfs_parents(g, s, dir == :out ? outneighbors : inneighbors)
end

function _bfs_parents(g::ACSet, source, neighborfn::Function)
n = nv(g)
Expand All @@ -52,7 +53,7 @@ function _bfs_parents(g::ACSet, source, neighborfn::Function)
end
while !isempty(cur_level)
@inbounds for v in cur_level
@inbounds @simd for i in neighborfn(g, v)
@inbounds for i in neighborfn(g, v)
if !visited[i]
push!(next_level, i)
parents[i] = v
Expand Down Expand Up @@ -86,8 +87,9 @@ use the corresponding edge direction (`:in` and `:out` are acceptable values).
### Implementation Notes
This version of DFS is iterative.
"""
dfs_parents(g::ACSet, s::Integer; dir=:out) =
(dir == :out) ? _dfs_parents(g, s, outneighbors) : _dfs_parents(g, s, inneighbors)
function dfs_parents(g::ACSet, s::Integer; dir=:out)
_dfs_parents(g, s, dir == :out ? outneighbors : inneighbors)
end

function _dfs_parents(g::ACSet, s::Int, neighborfn::Function)
parents = zeros(Int, nv(g))
Expand Down
18 changes: 9 additions & 9 deletions test/graphs/BasicGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ add_edge!(g, 2, 3)
@test ne(g, 1, 2) == 1
@test has_edge(g, 1, 2)
@test !has_edge(g, 1, 3)
@test outneighbors(g, 2) == [3]
@test inneighbors(g, 2) == [1]
@test collect(outneighbors(g, 2)) == [3]
@test collect(inneighbors(g, 2)) == [1]
@test degree(g, 2) == 2
@test collect(all_neighbors(g, 2)) == [1,3]

add_edge!(g, 1, 2)
@test ne(g) == 3
@test ne(g, 1, 2) == 2
@test collect(edges(g, 1, 2)) == [1,3]
@test outneighbors(g, 1) == [2,2]
@test inneighbors(g, 1) == []
@test collect(outneighbors(g, 1)) == [2,2]
@test isempty(inneighbors(g, 1))
@test degree(g, 1) == 2
@test SimpleGraphs.DiGraph(g) == SimpleGraphs.path_digraph(3)

Expand Down Expand Up @@ -76,9 +76,9 @@ add_edge!(g, 1, 2)
add_edge!(g, 2, 3)
@test ne(g) == 4
@test collect(edges(g, 1, 2)) == [1]
@test neighbors(g, 1) == [2]
@test neighbors(g, 2) == [1,3]
@test neighbors(g, 3) == [2]
@test collect(neighbors(g, 1)) == [2]
@test collect(neighbors(g, 2)) == [1,3]
@test collect(neighbors(g, 3)) == [2]
@test SimpleGraphs.Graph(g) == SimpleGraphs.path_graph(3)
@test SymmetricGraph(SimpleGraphs.path_graph(3)) == g

Expand All @@ -89,8 +89,8 @@ lg = SimpleGraphs.DiGraph(map(SimpleGraphs.Edge, [1,2,3,2,3,4], [2,3,4,1,2,3]))

rem_edge!(g, 3, 4)
@test ne(g) == 4
@test neighbors(g, 3) == [2]
@test neighbors(g, 4) == []
@test collect(neighbors(g, 3)) == [2]
@test isempty(neighbors(g, 4))
rem_vertex!(g, 2)
@test nv(g) == 3
@test ne(g) == 0
Expand Down