@@ -5,12 +5,35 @@ A type representing a directed graph with weights of type `U`.
5
5
"""
6
6
mutable struct SimpleWeightedDiGraph{T<: Integer , U<: Real } <: AbstractSimpleWeightedGraph{T, U}
7
7
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
+
8
14
end
9
15
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
+
10
34
ne (g:: SimpleWeightedDiGraph ) = nnz (g. weights)
11
35
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
14
37
weights = spzeros (U, T, T (n), T (n))
15
38
return SimpleWeightedDiGraph {T, U} (weights)
16
39
end
@@ -30,61 +53,27 @@ SimpleWeightedDiGraph(::Type{T}, ::Type{U}) where T<:Integer where U<:Real = Sim
30
53
# DiGraph(AbstractSimpleGraph)
31
54
function SimpleWeightedDiGraph (g:: LightGraphs.SimpleGraphs.AbstractSimpleGraph , :: Type{U} = Float64) where U <: Real
32
55
T = eltype (g)
33
- return SimpleWeightedDiGraph {T, U } (adjacency_matrix (g) ' )
56
+ return SimpleWeightedDiGraph {T} (adjacency_matrix (g, U) )
34
57
end
35
58
36
59
# DiGraph(AbstractSimpleGraph, defaultweight)
37
60
function SimpleWeightedDiGraph (g:: LightGraphs.SimpleGraphs.AbstractSimpleGraph , x:: U ) where U <: Real
38
61
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))
45
63
end
46
64
47
65
# DiGraph(srcs, dsts, weights)
48
66
function SimpleWeightedDiGraph (i:: AbstractVector{T} , j:: AbstractVector{T} , v:: AbstractVector{U} ; combine = + ) where T<: Integer where U<: Real
49
67
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 )
51
69
end
52
70
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
-
75
71
edgetype (:: SimpleWeightedDiGraph{T, U} ) where T<: Integer where U<: Real = SimpleWeightedGraphEdge{T,U}
76
72
77
73
edges (g:: SimpleWeightedDiGraph ) = (SimpleWeightedEdge (x[2 ], x[1 ], x[3 ]) for x in zip (findnz (g. weights)... ))
78
74
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
83
75
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
88
77
89
78
# add_edge! will overwrite weights.
90
79
function add_edge! (g:: SimpleWeightedDiGraph , e:: SimpleWeightedGraphEdge )
@@ -113,7 +102,7 @@ function rem_edge!(g::SimpleWeightedDiGraph, e::SimpleWeightedGraphEdge)
113
102
end
114
103
115
104
116
- copy (g:: SimpleWeightedDiGraph ) = SimpleWeightedDiGraph (copy (g. weights))
105
+ copy (g:: SimpleWeightedDiGraph ) = SimpleWeightedDiGraph (copy (g. weights' ))
117
106
118
107
== (g:: SimpleWeightedDiGraph , h:: SimpleWeightedDiGraph ) = g. weights == h. weights
119
108
0 commit comments