Skip to content

Commit e6896d6

Browse files
committed
PERF: Return iterator from neighbors functions.
1 parent 8f0ce9b commit e6896d6

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

src/graphs/BasicGraphs.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,13 @@ multiplicity*. To get the unique neighbors, call `unique(neighbors(g))`.
198198

199199
""" In-neighbors of vertex in a graph.
200200
"""
201-
inneighbors(g::AbstractGraph, v::Int) = view(g, incident(g, v, :tgt), :src)
201+
inneighbors(g::AbstractGraph, v::Int) =
202+
(subpart(g, e, :src) for e in incident(g, v, :tgt))
202203

203204
""" Out-neighbors of vertex in a graph.
204205
"""
205-
outneighbors(g::AbstractGraph, v::Int) = view(g, incident(g, v, :src), :tgt)
206+
outneighbors(g::AbstractGraph, v::Int) =
207+
(subpart(g, e, :tgt) for e in incident(g, v, :src))
206208

207209
""" Union of in-neighbors and out-neighbors in a graph.
208210
"""
@@ -285,7 +287,8 @@ rem_edge!(g::AbstractSymmetricGraph, e::Int) = rem_edges!(g, e:e)
285287
rem_edges!(g::AbstractSymmetricGraph, es) =
286288
rem_parts!(g, :E, unique!(sort!([es; inv(g, es)])))
287289

288-
neighbors(g::AbstractSymmetricGraph, v::Int) = view(g, incident(g, v, :src), :tgt)
290+
neighbors(g::AbstractSymmetricGraph, v::Int) =
291+
(subpart(g, e, :tgt) for e in incident(g, v, :src))
289292
@inline inneighbors(g::AbstractSymmetricGraph, v::Int) = neighbors(g, v)
290293
@inline outneighbors(g::AbstractSymmetricGraph, v::Int) = neighbors(g, v)
291294
@inline all_neighbors(g::AbstractSymmetricGraph, v::Int) = neighbors(g, v)

src/graphs/GraphAlgorithms.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function topological_sort(g::ACSet, ::TopologicalSortByDFS)
6262
push!(stack, v)
6363
while !isempty(stack)
6464
u = first(stack)
65-
u_out = outneighbors(g, u)
65+
u_out = collect(outneighbors(g, u))
6666
i = findfirst(u_out) do w
6767
marking[w] != TempMarked || error("Graph is not acyclic: $g")
6868
marking[w] == Unmarked

src/graphs/Searching.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ implementations which are marginally faster in practice for smaller graphs,
3434
but the performance improvements using this implementation on large graphs
3535
can be significant.
3636
"""
37-
bfs_parents(g::ACSet, s::Int; dir = :out) =
38-
(dir == :out) ? _bfs_parents(g, s, outneighbors) : _bfs_parents(g, s, inneighbors)
37+
function bfs_parents(g::ACSet, s::Int; dir = :out)
38+
_bfs_parents(g, s, dir == :out ? outneighbors : inneighbors)
39+
end
3940

4041
function _bfs_parents(g::ACSet, source, neighborfn::Function)
4142
n = nv(g)
@@ -52,7 +53,7 @@ function _bfs_parents(g::ACSet, source, neighborfn::Function)
5253
end
5354
while !isempty(cur_level)
5455
@inbounds for v in cur_level
55-
@inbounds @simd for i in neighborfn(g, v)
56+
@inbounds for i in neighborfn(g, v)
5657
if !visited[i]
5758
push!(next_level, i)
5859
parents[i] = v
@@ -86,8 +87,9 @@ use the corresponding edge direction (`:in` and `:out` are acceptable values).
8687
### Implementation Notes
8788
This version of DFS is iterative.
8889
"""
89-
dfs_parents(g::ACSet, s::Integer; dir=:out) =
90-
(dir == :out) ? _dfs_parents(g, s, outneighbors) : _dfs_parents(g, s, inneighbors)
90+
function dfs_parents(g::ACSet, s::Integer; dir=:out)
91+
_dfs_parents(g, s, dir == :out ? outneighbors : inneighbors)
92+
end
9193

9294
function _dfs_parents(g::ACSet, s::Int, neighborfn::Function)
9395
parents = zeros(Int, nv(g))

test/graphs/BasicGraphs.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ add_edge!(g, 2, 3)
2121
@test ne(g, 1, 2) == 1
2222
@test has_edge(g, 1, 2)
2323
@test !has_edge(g, 1, 3)
24-
@test outneighbors(g, 2) == [3]
25-
@test inneighbors(g, 2) == [1]
24+
@test collect(outneighbors(g, 2)) == [3]
25+
@test collect(inneighbors(g, 2)) == [1]
2626
@test degree(g, 2) == 2
2727
@test collect(all_neighbors(g, 2)) == [1,3]
2828

2929
add_edge!(g, 1, 2)
3030
@test ne(g) == 3
3131
@test ne(g, 1, 2) == 2
3232
@test collect(edges(g, 1, 2)) == [1,3]
33-
@test outneighbors(g, 1) == [2,2]
34-
@test inneighbors(g, 1) == []
33+
@test collect(outneighbors(g, 1)) == [2,2]
34+
@test isempty(inneighbors(g, 1))
3535
@test degree(g, 1) == 2
3636
@test SimpleGraphs.DiGraph(g) == SimpleGraphs.path_digraph(3)
3737

@@ -76,9 +76,9 @@ add_edge!(g, 1, 2)
7676
add_edge!(g, 2, 3)
7777
@test ne(g) == 4
7878
@test collect(edges(g, 1, 2)) == [1]
79-
@test neighbors(g, 1) == [2]
80-
@test neighbors(g, 2) == [1,3]
81-
@test neighbors(g, 3) == [2]
79+
@test collect(neighbors(g, 1)) == [2]
80+
@test collect(neighbors(g, 2)) == [1,3]
81+
@test collect(neighbors(g, 3)) == [2]
8282
@test SimpleGraphs.Graph(g) == SimpleGraphs.path_graph(3)
8383
@test SymmetricGraph(SimpleGraphs.path_graph(3)) == g
8484

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

9090
rem_edge!(g, 3, 4)
9191
@test ne(g) == 4
92-
@test neighbors(g, 3) == [2]
93-
@test neighbors(g, 4) == []
92+
@test collect(neighbors(g, 3)) == [2]
93+
@test isempty(neighbors(g, 4))
9494
rem_vertex!(g, 2)
9595
@test nv(g) == 3
9696
@test ne(g) == 0

0 commit comments

Comments
 (0)