Skip to content

Commit 26163d4

Browse files
committed
fix ne for self-loops
1 parent d20d948 commit 26163d4

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/simpleweightedgraph.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mutable struct SimpleWeightedGraph{T<:Integer, U<:Real} <: AbstractSimpleWeighte
1818

1919
end
2020

21-
ne(g::SimpleWeightedGraph) = nnz(g.weights) ÷ 2
21+
ne(g::SimpleWeightedGraph) = (nnz(g.weights) + count(diag(g.weights) .!= 0)) ÷ 2
2222

2323
SimpleWeightedGraph{T}(adjmx::SparseMatrixCSC{U, T}) where {T <: Integer, U <: Real} =
2424
SimpleWeightedGraph{T, U}(adjmx)

test/simpleweightedgraph.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ using SimpleWeightedGraphs
22

33
@testset "SimpleWeightedGraphs" begin
44
@info("Ignore warnings relating to adding and removing vertices and edges")
5-
adjmx1 = [0 1 0; 1 0 1; 0 1 0] # SimpleWeightedGraph
6-
adjmx2 = [0 1 0; 1 0 1; 1 1 0] # SimpleWeightedDiGraph
5+
adjmx1 = [0 1 0; 1 1 1; 0 1 0] # SimpleWeightedGraph
6+
adjmx2 = [0 1 0; 1 1 1; 1 1 0] # SimpleWeightedDiGraph
77
# specific concrete generators - no need for loop
88
@test @inferred(eltype(SimpleWeightedGraph())) == Int
99
@test @inferred(eltype(SimpleWeightedGraph(adjmx1))) == Int
1010
@test_throws ErrorException SimpleWeightedGraph(adjmx2)
1111

1212
@test @inferred(ne(SimpleWeightedGraph(path_digraph(5)))) == 4
13+
@test @inferred(ne(SimpleWeightedGraph(adjmx1))) == 3
1314
@test @inferred(!is_directed(SimpleWeightedGraph))
1415

1516
@test @inferred(eltype(SimpleWeightedDiGraph())) == Int
1617
@test @inferred(eltype(SimpleWeightedDiGraph(adjmx2))) == Int
1718
@test @inferred(ne(SimpleWeightedDiGraph(path_graph(5)))) == 8
19+
@test @inferred(ne(SimpleWeightedDiGraph(adjmx2))) == 6
1820
@test @inferred(is_directed(SimpleWeightedDiGraph))
1921

2022

0 commit comments

Comments
 (0)