Skip to content
Closed
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
24 changes: 18 additions & 6 deletions src/graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,27 @@
return neighbors_v
end

function degree(g::AdjacencyGraph, v::Integer)
d = 0
for u in neighbors(g, v)
if u != v
d += 1
function degree(g::AdjacencyGraph{T,true}, v::Integer) where {T}
S = pattern(g)
rvS = rowvals(S)
d = S.colptr[v + 1] - S.colptr[v]
for index in nzrange(S, v)
row = rvS[index]
if row >= v
(row == v) && (d -= 1)
break
end
end
return d
end

function nb_edges(g::AdjacencyGraph)
function degree(g::AdjacencyGraph{T,false}, v::Integer) where {T}
S = pattern(g)
d = S.colptr[v + 1] - S.colptr[v]
return d

Check warning on line 241 in src/graph.jl

View check run for this annotation

Codecov / codecov/patch

src/graph.jl#L238-L241

Added lines #L238 - L241 were not covered by tests
end

function nb_edges(g::AdjacencyGraph{T,false}) where {T}
ne = 0
for v in vertices(g)
for u in neighbors(g, v)
Expand All @@ -241,6 +251,8 @@
return ne ÷ 2
end

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

maximum_degree(g::AdjacencyGraph) = maximum(Base.Fix1(degree, g), vertices(g))
minimum_degree(g::AdjacencyGraph) = minimum(Base.Fix1(degree, g), vertices(g))

Expand Down