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

Commit bbfd872

Browse files
authored
Merge pull request #32 from JuliaGraphs/sbromberger/retooling
Sbromberger/retooling
2 parents dd9b433 + 01ec237 commit bbfd872

File tree

5 files changed

+73
-80
lines changed

5 files changed

+73
-80
lines changed

src/SimpleWeightedGraphs.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@ function rem_vertex!(g::AbstractSimpleWeightedGraph, v::Integer)
114114
return true
115115
end
116116

117-
function outneighbors(g::AbstractSimpleWeightedGraph)
118-
mat = g.weights
119-
return [mat.rowval[mat.colptr[i]:mat.colptr[i+1]-1] for i in 1:nv(g)]
120-
end
121-
122117
function outneighbors(g::AbstractSimpleWeightedGraph, v::Integer)
123118
mat = g.weights
124119
return mat.rowval[mat.colptr[v]:mat.colptr[v+1]-1]
@@ -144,4 +139,12 @@ include("persistence.jl")
144139
const WGraph = SimpleWeightedGraph
145140
const WDiGraph = SimpleWeightedDiGraph
146141

142+
SimpleWeightedDiGraph(g::SimpleWeightedGraph) = SimpleWeightedDiGraph(g.weights)
143+
SimpleWeightedDiGraph{T,U}(g::SimpleWeightedGraph) where T<:Integer where U<:Real =
144+
SimpleWeightedDiGraph(SparseMatrixCSC{U, T}(g.weights))
145+
146+
SimpleWeightedGraph(g::SimpleWeightedDiGraph) = SimpleWeightedGraph(g.weights .+ g.weights')
147+
SimpleWeightedGraph{T,U}(g::SimpleWeightedDiGraph) where T<:Integer where U<:Real =
148+
SimpleWeightedGraph(SparseMatrixCSC{U, T}(g.weights .+ g.weights'))
149+
147150
end # module

src/simpleweighteddigraph.jl

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,35 @@ A type representing a directed graph with weights of type `U`.
55
"""
66
mutable struct SimpleWeightedDiGraph{T<:Integer, U<:Real} <: AbstractSimpleWeightedGraph{T, U}
77
weights::SparseMatrixCSC{U, T} # indexed by [dst, src]
8+
function SimpleWeightedDiGraph{T, U}(adjmx::SparseMatrixCSC{U,T}; permute=true) where T <: Integer where U <: Real
9+
dima,dimb = size(adjmx)
10+
isequal(dima,dimb) || error("Adjacency / distance matrices must be square")
11+
permute ? new{T, U}(permutedims(adjmx)) : new{T, U}(adjmx)
12+
end
13+
814
end
915

16+
SimpleWeightedDiGraph{T}(adjmx::SparseMatrixCSC{U, T}; permute=true) where T<:Integer where U<:Real =
17+
SimpleWeightedDiGraph{T, U}(adjmx; permute=permute)
18+
19+
SimpleWeightedDiGraph(adjmx::SparseMatrixCSC{U, T}; permute=true) where T<:Integer where U<:Real =
20+
SimpleWeightedDiGraph{T, U}(adjmx; permute=permute)
21+
22+
SimpleWeightedDiGraph(m::AbstractMatrix{U}) where U <: Real =
23+
SimpleWeightedDiGraph{Int, U}(SparseMatrixCSC{U, Int}(m))
24+
SimpleWeightedDiGraph{T}(m::AbstractMatrix{U}) where T<:Integer where U<:Real =
25+
SimpleWeightedDiGraph{T, U}(SparseMatrixCSC{U, T}(m))
26+
SimpleWeightedDiGraph{T, U}(m::AbstractMatrix) where T<:Integer where U<:Real =
27+
SimpleWeightedDiGraph{T, U}(SparseMatrixCSC{U, T}(m))
28+
29+
SimpleWeightedDiGraph(g::SimpleWeightedDiGraph) = SimpleWeightedDiGraph(g.weights, false)
30+
SimpleWeightedDiGraph{T,U}(g::SimpleWeightedDiGraph) where T<:Integer where U<:Real =
31+
SimpleWeightedDiGraph(SparseMatrixCSC{U, T}(g.weights); permute=false)
32+
33+
1034
ne(g::SimpleWeightedDiGraph) = nnz(g.weights)
1135

12-
# Graph{UInt8}(6), Graph{Int16}(7), Graph{UInt8}()
13-
function (::Type{SimpleWeightedDiGraph{T, U}})(n::Integer = 0) where T<:Integer where U<:Real
36+
function SimpleWeightedDiGraph{T,U}(n::Integer = 0) where T<:Integer where U<:Real
1437
weights = spzeros(U, T, T(n), T(n))
1538
return SimpleWeightedDiGraph{T, U}(weights)
1639
end
@@ -30,61 +53,27 @@ SimpleWeightedDiGraph(::Type{T}, ::Type{U}) where T<:Integer where U<:Real = Sim
3053
# DiGraph(AbstractSimpleGraph)
3154
function SimpleWeightedDiGraph(g::LightGraphs.SimpleGraphs.AbstractSimpleGraph, ::Type{U}=Float64) where U <: Real
3255
T = eltype(g)
33-
return SimpleWeightedDiGraph{T, U}(adjacency_matrix(g)')
56+
return SimpleWeightedDiGraph{T}(adjacency_matrix(g, U))
3457
end
3558

3659
# DiGraph(AbstractSimpleGraph, defaultweight)
3760
function SimpleWeightedDiGraph(g::LightGraphs.SimpleGraphs.AbstractSimpleGraph, x::U) where U <: Real
3861
T = eltype(g)
39-
return SimpleWeightedDiGraph{T, U}(x.*adjacency_matrix(g, U)')
40-
end
41-
42-
# SimpleWeightedGraph{T, U}(SimpleGraph)
43-
function (::Type{SimpleWeightedDiGraph{T, U}})(g::LightGraphs.SimpleGraphs.SimpleDiGraph) where T<:Integer where U <: Real
44-
SimpleWeightedDiGraph{T, U}(adjacency_matrix(LightGraphs.SimpleGraphs.SimpleDiGraph{T}(g), U))
62+
return SimpleWeightedDiGraph{T, U}(x.*adjacency_matrix(g, U))
4563
end
4664

4765
# DiGraph(srcs, dsts, weights)
4866
function SimpleWeightedDiGraph(i::AbstractVector{T}, j::AbstractVector{T}, v::AbstractVector{U}; combine = +) where T<:Integer where U<:Real
4967
m = max(maximum(j), maximum(i))
50-
SimpleWeightedDiGraph{T, U}(sparse(j, i, v, m, m, combine))
68+
SimpleWeightedDiGraph{T, U}(sparse(j, i, v, m, m, combine); permute=false)
5169
end
5270

53-
# Graph{UInt8}(adjmx)
54-
# function (::Type{SimpleWeightedDiGraph{T, U}})(adjmx::AbstractMatrix) where T<:Integer where U <: Real
55-
# dima,dimb = size(adjmx)
56-
# isequal(dima,dimb) || error("Adjacency / distance matrices must be square")
57-
# issymmetric(adjmx) || error("Adjacency / distance matrices must be symmetric")
58-
# g = SimpleWeightedDiGraph(U.(LinearAlgebra.fillstored!(copy(adjmx), 1)))
59-
# end
60-
61-
# converts Graph{Int} to Graph{Int32}
62-
# function (::Type{SimpleWeightedDiGraph{T, U}})(g::SimpleWeightedDiGraph) where T<:Integer where U<:Real
63-
# h_fadj = [Vector{T}(x) for x in fadj(g)]
64-
# return SimpleGraph(ne(g), h_fadj)
65-
# end
66-
67-
68-
# Graph(adjmx)
69-
SimpleWeightedDiGraph(adjmx::AbstractMatrix) = SimpleWeightedDiGraph{Int, eltype(adjmx)}(adjmx')
70-
71-
# Graph(digraph). Weights will be added.
72-
# TODO: uncomment this.
73-
SimpleWeightedDiGraph(g::AbstractSimpleWeightedGraph) = SimpleWeightedDiGraph(g.weights)
74-
7571
edgetype(::SimpleWeightedDiGraph{T, U}) where T<:Integer where U<:Real = SimpleWeightedGraphEdge{T,U}
7672

7773
edges(g::SimpleWeightedDiGraph) = (SimpleWeightedEdge(x[2], x[1], x[3]) for x in zip(findnz(g.weights)...))
7874
weights(g::SimpleWeightedDiGraph) = g.weights'
79-
function inneighbors(g::SimpleWeightedDiGraph)
80-
mat = SparseMatrixCSC(g.weights')
81-
return [mat.rowval[mat.colptr[i]:mat.colptr[i + 1] - 1] for i in 1:nv(g)]
82-
end
8375

84-
function inneighbors(g::SimpleWeightedDiGraph, v::Integer)
85-
mat = SparseMatrixCSC(g.weights')
86-
return mat.rowval[mat.colptr[v]:mat.colptr[v + 1] - 1]
87-
end
76+
inneighbors(g::SimpleWeightedDiGraph, v::Integer) = g.weights[v,:].nzind
8877

8978
# add_edge! will overwrite weights.
9079
function add_edge!(g::SimpleWeightedDiGraph, e::SimpleWeightedGraphEdge)
@@ -113,7 +102,7 @@ function rem_edge!(g::SimpleWeightedDiGraph, e::SimpleWeightedGraphEdge)
113102
end
114103

115104

116-
copy(g::SimpleWeightedDiGraph) = SimpleWeightedDiGraph(copy(g.weights))
105+
copy(g::SimpleWeightedDiGraph) = SimpleWeightedDiGraph(copy(g.weights'))
117106

118107
==(g::SimpleWeightedDiGraph, h::SimpleWeightedDiGraph) = g.weights == h.weights
119108

src/simpleweightededge.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ Tuple(e::AbstractSimpleWeightedEdge) = (src(e), dst(e), weight(e))
3434
# Convenience functions - note that these do not use weight.
3535
reverse(e::T) where T<:AbstractSimpleWeightedEdge = T(dst(e), src(e), weight(e))
3636
==(e1::AbstractSimpleWeightedEdge, e2::AbstractSimpleWeightedEdge) = (src(e1) == src(e2) && dst(e1) == dst(e2))
37+
==(e1::AbstractSimpleWeightedEdge, e2::AbstractEdge) = (src(e1) == src(e2) && dst(e1) == dst(e2))
38+
==(e1::AbstractEdge, e2::AbstractSimpleWeightedEdge) = (src(e1) == src(e2) && dst(e1) == dst(e2))

src/simpleweightedgraph.jl

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,36 @@
66
A type representing an undirected graph with weights of type `U`.
77
"""
88
mutable struct SimpleWeightedGraph{T<:Integer, U<:Real} <: AbstractSimpleWeightedGraph{T, U}
9-
weights::SparseMatrixCSC{U, T} # indexed by [dst, src]
9+
weights::SparseMatrixCSC{U,T}
10+
function SimpleWeightedGraph{T, U}(adjmx::SparseMatrixCSC{U, T}) where T<:Integer where U<:Real
11+
dima,dimb = size(adjmx)
12+
isequal(dima,dimb) || error("Adjacency / distance matrices must be square")
13+
issymmetric(adjmx) || error("Adjacency / distance matrices must be symmetric")
14+
new{T, U}(adjmx)
15+
end
16+
1017
end
1118

1219
ne(g::SimpleWeightedGraph) = nnz(g.weights) ÷ 2
1320

21+
SimpleWeightedGraph{T}(adjmx::SparseMatrixCSC{U, T}) where T<:Integer where U<:Real =
22+
SimpleWeightedGraph{T, U}(adjmx)
23+
24+
SimpleWeightedGraph(adjmx::SparseMatrixCSC{U, T}) where T<:Integer where U<:Real =
25+
SimpleWeightedGraph{T, U}(adjmx)
26+
27+
SimpleWeightedGraph(m::AbstractMatrix{U}) where U <: Real =
28+
SimpleWeightedGraph{Int, U}(SparseMatrixCSC{U, Int}(m))
29+
SimpleWeightedGraph{T}(m::AbstractMatrix{U}) where T<:Integer where U<:Real =
30+
SimpleWeightedGraph{T, U}(SparseMatrixCSC{U, T}(m))
31+
SimpleWeightedGraph{T, U}(m::AbstractMatrix) where T<:Integer where U<:Real =
32+
SimpleWeightedGraph{T, U}(SparseMatrixCSC{U, T}(m))
33+
34+
35+
SimpleWeightedGraph(g::SimpleWeightedGraph) = SimpleWeightedGraph(g.weights)
36+
SimpleWeightedGraph{T,U}(g::SimpleWeightedGraph) where T<:Integer where U<:Real =
37+
SimpleWeightedGraph(SparseMatrixCSC{U, T}(g.weights))
38+
1439

1540
# Graph{UInt8}(6), Graph{Int16}(7), Graph{UInt8}()
1641
function (::Type{SimpleWeightedGraph{T, U}})(n::Integer = 0) where T<:Integer where U<:Real
@@ -20,7 +45,7 @@ end
2045

2146

2247
# Graph()
23-
SimpleWeightedGraph() = SimpleWeightedGraph{Int, Float64}()
48+
SimpleWeightedGraph() = SimpleWeightedGraph(Matrix{Float64}(undef, 0, 0))
2449

2550
# Graph(6), Graph(0x5)
2651
SimpleWeightedGraph(n::T) where T<:Integer = SimpleWeightedGraph{T, Float64}(n)
@@ -33,11 +58,11 @@ SimpleWeightedGraph(::Type{T}, ::Type{U}) where T<:Integer where U<:Real = Simpl
3358

3459
# Graph(SimpleGraph)
3560
SimpleWeightedGraph(g::LightGraphs.SimpleGraphs.SimpleGraph{T}, ::Type{U}=Float64) where T <: Integer where U <: Real =
36-
SimpleWeightedGraph{T, U}(adjacency_matrix(g))
61+
SimpleWeightedGraph{T, U}(adjacency_matrix(g, U))
3762

3863
# Graph(SimpleDiGraph)
3964
SimpleWeightedGraph(g::LightGraphs.SimpleGraphs.SimpleDiGraph{T}, ::Type{U}=Float64) where T <: Integer where U <: Real =
40-
SimpleWeightedGraph{T, U}(adjacency_matrix(LightGraphs.SimpleGraphs.SimpleGraph(g)))
65+
SimpleWeightedGraph{T, U}(adjacency_matrix(LightGraphs.SimpleGraphs.SimpleGraph(g), U))
4166

4267
# Graph(SimpleGraph, defaultweight)
4368
SimpleWeightedGraph(g::LightGraphs.SimpleGraphs.SimpleGraph{T}, x::U) where T <: Integer where U <: Real =
@@ -52,38 +77,12 @@ function (::Type{SimpleWeightedGraph{T, U}})(g::LightGraphs.SimpleGraphs.SimpleG
5277
SimpleWeightedGraph{T, U}(adjacency_matrix(LightGraphs.SimpleGraphs.SimpleGraph{T}(g), U))
5378
end
5479

55-
# DiGraph(srcs, dsts, weights)
80+
# Graph(srcs, dsts, weights)
5681
function SimpleWeightedGraph(i::AbstractVector{T}, j::AbstractVector{T}, v::AbstractVector{U}; combine = +) where T<:Integer where U<:Real
5782
m = max(maximum(i), maximum(j))
5883
s = sparse(vcat(i,j), vcat(j,i), vcat(v,v), m, m, combine)
5984
SimpleWeightedGraph{T, U}(s)
6085
end
61-
# Graph{UInt8}(adjmx)
62-
# function (::Type{SimpleWeightedGraph{T, U}})(adjmx::AbstractMatrix) where T<:Integer where U <: Real
63-
# dima,dimb = size(adjmx)
64-
# isequal(dima,dimb) || error("Adjacency / distance matrices must be square")
65-
# issymmetric(adjmx) || error("Adjacency / distance matrices must be symmetric")
66-
# g = SimpleWeightedGraph(U.(LinearAlgebra.fillstored!(copy(adjmx), 1)))
67-
# end
68-
69-
# converts Graph{Int} to Graph{Int32}
70-
# function (::Type{SimpleWeightedGraph{T, U}})(g::SimpleWeightedGraph) where T<:Integer where U<:Real
71-
# h_fadj = [Vector{T}(x) for x in fadj(g)]
72-
# return SimpleGraph(ne(g), h_fadj)
73-
# end
74-
75-
76-
# Graph(adjmx)
77-
function SimpleWeightedGraph(adjmx::AbstractMatrix)
78-
dima,dimb = size(adjmx)
79-
isequal(dima,dimb) || error("Adjacency / distance matrices must be square")
80-
issymmetric(adjmx) || error("Adjacency / distance matrices must be symmetric")
81-
SimpleWeightedGraph{Int, eltype(adjmx)}(adjmx')
82-
end
83-
84-
# Graph(digraph). Weights will be added.
85-
86-
SimpleWeightedGraph(g::SimpleWeightedDiGraph) = SimpleWeightedGraph(g.weights .+ g.weights')
8786

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

test/simpleweightedgraph.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using SimpleWeightedGraphs
22

33
@testset "SimpleWeightedGraphs" begin
4+
@info("Ignore warnings relating to adding and removing vertices and edges")
45
adjmx1 = [0 1 0; 1 0 1; 0 1 0] # SimpleWeightedGraph
56
adjmx2 = [0 1 0; 1 0 1; 1 1 0] # SimpleWeightedDiGraph
67
# specific concrete generators - no need for loop
@@ -50,7 +51,6 @@ using SimpleWeightedGraphs
5051
@test @inferred(vertices(g)) == 1:4
5152
@test SimpleWeightedEdge(2,3) in edges(g)
5253
@test @inferred(nv(g)) == 4
53-
@test @inferred(outneighbors(g)) == inneighbors(g)
5454
@test @inferred(outneighbors(g,2)) == inneighbors(g,2) == neighbors(g,2)
5555
@test @inferred(has_edge(g, 2, 3))
5656
@test @inferred(has_edge(g, 3, 2))
@@ -115,8 +115,8 @@ using SimpleWeightedGraphs
115115
@test SimpleWeightedEdge(2,3) in edges(g)
116116
@test !(SimpleWeightedEdge(3,2) in edges(g))
117117
@test @inferred(nv(g)) == 4
118-
@test @inferred(outneighbors(g)[2]) == outneighbors(g, 2) == [3]
119-
@test @inferred(inneighbors(g)[2]) == inneighbors(g, 2) == [1]
118+
@test outneighbors(g, 2) == [3]
119+
@test inneighbors(g, 2) == [1]
120120

121121
@test @inferred(has_edge(g, 2, 3))
122122
@test @inferred(!has_edge(g, 3, 2))

0 commit comments

Comments
 (0)