Skip to content

Commit d6df31a

Browse files
committed
Speed up LF order
1 parent 5572dab commit d6df31a

3 files changed

Lines changed: 10 additions & 12 deletions

File tree

src/graph.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,9 @@ end
270270
degree(g::AdjacencyGraph{T,false}, v::Integer) where {T} = g.S.colptr[v + 1] - g.S.colptr[v]
271271

272272
function degree(g::AdjacencyGraph{T,true}, v::Integer) where {T}
273-
d = 0
274-
for u in neighbors(g, v)
275-
if u != v
276-
d += 1
277-
end
278-
end
279-
return d
273+
neigh = neighbors(g, v)
274+
has_selfloop = insorted(v, neigh)
275+
return g.S.colptr[v + 1] - g.S.colptr[v] - has_selfloop
280276
end
281277

282278
nb_edges(g::AdjacencyGraph{T,false}) where {T} = nnz(g.S) ÷ 2

src/order.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ Instance of [`AbstractOrder`](@ref) which sorts vertices using their degree in t
8080
struct LargestFirst <: AbstractOrder end
8181

8282
function vertices(g::AdjacencyGraph, ::LargestFirst)
83-
criterion(v) = degree(g, v)
83+
degrees = map(Base.Fix1(degree, g), vertices(g))
84+
criterion(v) = degrees[v]
8485
return sort(vertices(g); by=criterion, rev=true)
8586
end
8687

test/order.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ end;
6363
@testset "LargestFirst" begin
6464
for has_diagonal in (false, true)
6565
A = sparse([
66-
0 1 0
67-
1 0 1
68-
0 1 0
66+
0 1 0 0
67+
1 0 1 1
68+
0 1 0 1
69+
0 1 1 0
6970
])
7071
ag = AdjacencyGraph(A; has_diagonal)
71-
@test vertices(ag, LargestFirst()) == [2, 1, 3]
72+
@test vertices(ag, LargestFirst()) == [2, 3, 4, 1]
7273
end
7374

7475
A = sparse([

0 commit comments

Comments
 (0)