Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "SimpleWeightedGraphs"
uuid = "47aef6b3-ad0c-573a-a1e2-d07658019622"
version = "1.1.1"
version = "2.0.0"

[deps]
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
Expand Down
5 changes: 4 additions & 1 deletion src/SimpleWeightedGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using LinearAlgebra
using Markdown
using SparseArrays

using Base.Iterators
import Base:
convert, eltype, show, ==, Pair, Tuple, copy, length, issubset, zero

Expand Down Expand Up @@ -113,9 +114,11 @@ end

function outneighbors(g::AbstractSimpleWeightedGraph, v::Integer)
mat = g.weights
return view(mat.rowval, mat.colptr[v]:mat.colptr[v+1]-1)
return Iterators.filter(!iszero, view(mat.rowval, mat.colptr[v]:mat.colptr[v+1]-1))
end

outdegree(g::AbstractSimpleWeightedGraph, v::Integer) = length(outneighbors(g, v).itr)

get_weight(g::AbstractSimpleWeightedGraph, u::Integer, v::Integer) = g.weights[v, u]

zero(g::T) where T<:AbstractSimpleWeightedGraph = T()
Expand Down
9 changes: 5 additions & 4 deletions src/simpleweighteddigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ SimpleWeightedDiGraph{T,U}(g::SimpleWeightedDiGraph) where {T <: Integer, U <: R
SimpleWeightedDiGraph(SparseMatrixCSC{U, T}(copy(g.weights)), permute=false)


ne(g::SimpleWeightedDiGraph) = nnz(g.weights)
ne(g::SimpleWeightedDiGraph) = count(!iszero, nonzeros(g.weights))

function SimpleWeightedDiGraph{T,U}(n::Integer = 0) where {T<:Integer, U<:Real}
weights = spzeros(U, T, T(n), T(n))
Expand Down Expand Up @@ -78,14 +78,15 @@ function SimpleWeightedDiGraph(i::AbstractVector{T}, j::AbstractVector{T}, v::Ab
SimpleWeightedDiGraph{T, U}(sparse(j, i, v, m, m, combine), permute=false)
end

LightGraphs.SimpleDiGraph(g::SimpleWeightedDiGraph) = SimpleDiGraph(g.weights)
LightGraphs.SimpleDiGraph(g::SimpleWeightedDiGraph) = SimpleDiGraph(g.weights')

edgetype(::SimpleWeightedDiGraph{T, U}) where T<:Integer where U<:Real = SimpleWeightedGraphEdge{T,U}

edges(g::SimpleWeightedDiGraph) = (SimpleWeightedEdge(x[2], x[1], x[3]) for x in zip(findnz(g.weights)...))
edges(g::SimpleWeightedDiGraph) = Iterators.filter(!iszero ∘ weight, SimpleWeightedEdge(x[2], x[1], x[3]) for x in zip(findnz(g.weights)...))
weights(g::SimpleWeightedDiGraph) = g.weights'

inneighbors(g::SimpleWeightedDiGraph, v::Integer) = g.weights[v,:].nzind
inneighbors(g::SimpleWeightedDiGraph, v::Integer) = Iterators.filter(!iszero, g.weights[v,:].nzind)
indegree(g::SimpleWeightedDiGraph, v::Integer) = length(inneighbors(g, v).itr)

# add_edge! will overwrite weights.
function add_edge!(g::SimpleWeightedDiGraph, e::SimpleWeightedGraphEdge)
Expand Down
6 changes: 3 additions & 3 deletions 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) = count(!iszero, nonzeros(g.weights)) ÷ 2

SimpleWeightedGraph{T}(adjmx::SparseMatrixCSC{U, T}) where {T <: Integer, U <: Real} =
SimpleWeightedGraph{T, U}(adjmx)
Expand Down Expand Up @@ -101,8 +101,8 @@ LightGraphs.SimpleGraph(g::SimpleWeightedGraph) = SimpleGraph(g.weights)

edgetype(::SimpleWeightedGraph{T, U}) where {T<:Integer, U<:Real} = SimpleWeightedGraphEdge{T,U}

edges(g::SimpleWeightedGraph) = (SimpleWeightedEdge(x[1], x[2], x[3]) for x in zip(findnz(triu(g.weights))...))
weights(g::SimpleWeightedGraph) = g.weights
edges(g::SimpleWeightedGraph) = Iterators.filter(!iszero ∘ weight, SimpleWeightedEdge(x[1], x[2], x[3]) for x in zip(findnz(triu(g.weights))...))
weights(g::AbstractSimpleWeightedGraph) = g.weights
inneighbors(g::SimpleWeightedGraph, x...) = outneighbors(g, x...)

# add_edge! will overwrite weights.
Expand Down
35 changes: 29 additions & 6 deletions test/simpleweightedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ using SimpleWeightedGraphs
gc = copy(g)
@test add_edge!(gc, 4, 1) && gc == SimpleWeightedGraph(cycle_graph(4))

@test @inferred(inneighbors(g, 2)) == @inferred(outneighbors(g, 2)) == @inferred(neighbors(g,2)) == [1,3]
@test @inferred(collect(inneighbors(g, 2))) == @inferred(collect(outneighbors(g, 2))) == @inferred(collect(neighbors(g,2))) == [1,3]
@test @inferred(add_vertex!(gc)) # out of order, but we want it for issubset
@test @inferred(g ⊆ gc)
@test @inferred(has_vertex(gc, 5))
Expand Down Expand Up @@ -115,8 +115,8 @@ using SimpleWeightedGraphs
@test SimpleWeightedEdge(2,3) in edges(g)
@test !(SimpleWeightedEdge(3,2) in edges(g))
@test @inferred(nv(g)) == 4
@test outneighbors(g, 2) == [3]
@test inneighbors(g, 2) == [1]
@test collect(outneighbors(g, 2)) == [3]
@test collect(inneighbors(g, 2)) == [1]

@test @inferred(has_edge(g, 2, 3))
@test @inferred(!has_edge(g, 3, 2))
Expand All @@ -130,8 +130,8 @@ using SimpleWeightedGraphs
gc = @inferred(copy(g))
@test @inferred(add_edge!(gc, 4, 1)) && gc == SimpleWeightedDiGraph(cycle_digraph(4))

@test @inferred(inneighbors(g, 2)) == [1]
@test @inferred(outneighbors(g, 2)) == @inferred(neighbors(g,2)) == [3]
@test @inferred(collect(inneighbors(g, 2))) == [1]
@test @inferred(collect(outneighbors(g, 2))) == @inferred(collect(neighbors(g,2))) == [3]
@test @inferred(add_vertex!(gc)) # out of order, but we want it for issubset
@test @inferred(g ⊆ gc)
@test @inferred(has_vertex(gc, 5))
Expand Down Expand Up @@ -182,7 +182,7 @@ using SimpleWeightedGraphs
@test @inferred(get_weight(g, 1, 2)) == 3

g = SimpleWeightedDiGraph(path_graph(5), 4.0)
@test sum(weights(g)) == ne(g) * 4.0
@test sum(collect(weights(g)))== ne(g) * 4.0

gx = Graph(4,3)
for g in testsimplegraphs(gx)
Expand Down Expand Up @@ -222,6 +222,16 @@ using SimpleWeightedGraphs
@test SimpleDiGraph(SimpleWeightedDiGraph(cycle_graph(4))) == SimpleDiGraph(cycle_graph(4))
@test SimpleGraph(SimpleWeightedGraph(path_graph(5))) == path_graph(5)


# test structural zeros.
g = SimpleWeightedDiGraph(path_digraph(5))
add_edge!(g, 2, 3, 5)
@test has_edge(g, 2, 3)
rem_edge!(g, 2, 3)
@test !has_edge(g, 2, 3)
@test SimpleWeightedEdge(2, 3) ∉ edges(g)
@test weights(g)[2, 3] == 0

@test SimpleWeightedGraph(cycle_graph(4)) == SimpleWeightedGraph(SimpleWeightedGraph(cycle_graph(4)))
@test SimpleWeightedDiGraph(cycle_digraph(4)) == SimpleWeightedDiGraph(SimpleWeightedDiGraph(cycle_digraph(4)))

Expand Down Expand Up @@ -319,4 +329,17 @@ using SimpleWeightedGraphs
@test g[1, 3, Val{:weight}()] ≈ 0
@test g[2, 3, Val{:weight}()] ≈ 0.5
end

# test digraph construction
g = LightGraphs.path_digraph(4)
h = SimpleWeightedDiGraph(g)
@test collect(edges(g)) == collect(edges(h))

# test degrees
@test outdegree(h) == [1, 1, 1, 0]
for v in 1:nv(h)
@test outdegree(h, v) == outdegree(h)[v]
@test indegree(h, v) == indegree(h)[v]
@test degree(h, v) == outdegree(h, v) + indegree(h, v)
end
end