Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the assumption is, that ne is kind of fast. Ideally we would solve this by either storing the number of edges or the number of self-loops in a separate field, but I guess for now we can also just query for the diagonal elements.

But diag does also allocate a new Vector, so I think we should at least some non-allocating function here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe nselfloop helps?


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