Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Fix ne for self-loops #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/simpleweightedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mutable struct SimpleWeightedGraph{T<:Integer, U<:Real} <: AbstractSimpleWeighte

end

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

SimpleWeightedGraph{T}(adjmx::SparseMatrixCSC{U, T}) where {T <: Integer, U <: Real} =
SimpleWeightedGraph{T, U}(adjmx)
Expand Down
6 changes: 4 additions & 2 deletions test/simpleweightedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ using SimpleWeightedGraphs

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

@test @inferred(ne(SimpleWeightedGraph(path_digraph(5)))) == 4
@test @inferred(ne(SimpleWeightedGraph(adjmx1))) == 3
@test @inferred(!is_directed(SimpleWeightedGraph))

@test @inferred(eltype(SimpleWeightedDiGraph())) == Int
@test @inferred(eltype(SimpleWeightedDiGraph(adjmx2))) == Int
@test @inferred(ne(SimpleWeightedDiGraph(path_graph(5)))) == 8
@test @inferred(ne(SimpleWeightedDiGraph(adjmx2))) == 6
@test @inferred(is_directed(SimpleWeightedDiGraph))


Expand Down