1
1
2
-
3
2
"""
4
3
SimpleWeightedGraph{T, U}
5
4
@@ -10,7 +9,7 @@ see MetaGraphs.jl for possible alternatives.
10
9
"""
11
10
mutable struct SimpleWeightedGraph{T<: Integer , U<: Real } <: AbstractSimpleWeightedGraph{T, U}
12
11
weights:: SparseMatrixCSC{U,T}
13
- function SimpleWeightedGraph {T, U} (adjmx:: SparseMatrixCSC{U, T} ) where T<: Integer where U<: Real
12
+ function SimpleWeightedGraph {T, U} (adjmx:: SparseMatrixCSC{U, T} ) where { T<: Integer , U<: Real }
14
13
dima,dimb = size (adjmx)
15
14
isequal (dima,dimb) || error (" Adjacency / distance matrices must be square" )
16
15
issymmetric (adjmx) || error (" Adjacency / distance matrices must be symmetric" )
21
20
22
21
ne (g:: SimpleWeightedGraph ) = nnz (g. weights) ÷ 2
23
22
24
- SimpleWeightedGraph {T} (adjmx:: SparseMatrixCSC{U, T} ) where T <: Integer where U <: Real =
23
+ SimpleWeightedGraph {T} (adjmx:: SparseMatrixCSC{U, T} ) where {T <: Integer , U <: Real } =
25
24
SimpleWeightedGraph {T, U} (adjmx)
26
25
27
- SimpleWeightedGraph (adjmx:: SparseMatrixCSC{U, T} ) where T <: Integer where U <: Real =
26
+ SimpleWeightedGraph (adjmx:: SparseMatrixCSC{U, T} ) where {T <: Integer , U <: Real } =
28
27
SimpleWeightedGraph {T, U} (adjmx)
29
28
30
- SimpleWeightedGraph (m:: AbstractMatrix{U} ) where U <: Real =
29
+ SimpleWeightedGraph (m:: AbstractMatrix{U} ) where { U <: Real } =
31
30
SimpleWeightedGraph {Int, U} (SparseMatrixCSC {U, Int} (m))
32
31
SimpleWeightedGraph {T} (m:: AbstractMatrix{U} ) where T<: Integer where U<: Real =
33
32
SimpleWeightedGraph {T, U} (SparseMatrixCSC {U, T} (m))
34
33
SimpleWeightedGraph {T, U} (m:: AbstractMatrix ) where T<: Integer where U<: Real =
35
34
SimpleWeightedGraph {T, U} (SparseMatrixCSC {U, T} (m))
36
35
37
36
38
- SimpleWeightedGraph (g:: SimpleWeightedGraph ) = SimpleWeightedGraph (g. weights)
39
- SimpleWeightedGraph {T,U} (g:: SimpleWeightedGraph ) where T<: Integer where U<: Real =
40
- SimpleWeightedGraph (SparseMatrixCSC {U, T} (g. weights))
41
-
37
+ SimpleWeightedGraph (g:: SimpleWeightedGraph ) = SimpleWeightedGraph (copy ( g. weights) )
38
+ function SimpleWeightedGraph {T,U} (g:: SimpleWeightedGraph ) where { T<: Integer , U<: Real }
39
+ return SimpleWeightedGraph (SparseMatrixCSC {U, T} (copy ( g. weights) ))
40
+ end
42
41
43
42
# Graph{UInt8}(6), Graph{Int16}(7), Graph{UInt8}()
44
43
function (:: Type{SimpleWeightedGraph{T, U}} )(n:: Integer = 0 ) where T<: Integer where U<: Real
45
44
weights = spzeros (U, T, T (n), T (n))
46
45
return SimpleWeightedGraph {T, U} (weights)
47
46
end
48
47
49
-
50
48
# Graph()
51
49
SimpleWeightedGraph () = SimpleWeightedGraph (Matrix {Float64} (undef, 0 , 0 ))
52
50
@@ -57,39 +55,51 @@ SimpleWeightedGraph(n::T) where T<:Integer = SimpleWeightedGraph{T, Float64}(n)
57
55
SimpleWeightedGraph (:: Type{T} ) where T<: Integer = SimpleWeightedGraph {T, Float64} (zero (T))
58
56
59
57
# Graph(UInt8, Float32)
60
- SimpleWeightedGraph (:: Type{T} , :: Type{U} ) where T<: Integer where U<: Real = SimpleWeightedGraph {U, T } (zero (T))
58
+ SimpleWeightedGraph (:: Type{T} , :: Type{U} ) where { T<: Integer , U<: Real } = SimpleWeightedGraph {T, U } (zero (T))
61
59
62
60
# Graph(SimpleGraph)
63
- SimpleWeightedGraph (g:: LightGraphs.SimpleGraphs.SimpleGraph{T} , :: Type{U} = Float64) where T <: Integer where U <: Real =
64
- SimpleWeightedGraph {T, U} (adjacency_matrix (g, U))
65
61
66
- # Graph(SimpleDiGraph)
67
- SimpleWeightedGraph (g:: LightGraphs.SimpleGraphs.SimpleDiGraph{T} , :: Type{U} = Float64) where T <: Integer where U <: Real =
68
- SimpleWeightedGraph {T, U} (adjacency_matrix (LightGraphs. SimpleGraphs. SimpleGraph (g), U))
69
-
70
- # Graph(SimpleGraph, defaultweight)
71
- SimpleWeightedGraph (g:: LightGraphs.SimpleGraphs.SimpleGraph{T} , x:: U ) where T <: Integer where U <: Real =
72
- SimpleWeightedGraph {T, U} (x.* adjacency_matrix (g, U))
62
+ function SimpleWeightedGraph (g:: LightGraphs.AbstractGraph{T} , :: Type{U} = Float64) where {T <: Integer , U <: Real }
63
+ adj_matrix = if LightGraphs. is_directed (g)
64
+ # TODO abstract function instead of SimpleGraph constructor
65
+ adjacency_matrix (LightGraphs. SimpleGraphs. SimpleGraph (g), U)
66
+ else
67
+ adjacency_matrix (g, U)
68
+ end
69
+ return SimpleWeightedGraph {T, U} (adj_matrix)
70
+ end
73
71
74
- # Graph(SimpleDiGraph, defaultweight)
75
- SimpleWeightedGraph (g:: LightGraphs.SimpleGraphs.SimpleDiGraph{T} , x:: U ) where T <: Integer where U <: Real =
76
- SimpleWeightedGraph {T, U} (x.* adjacency_matrix (LightGraphs. SimpleGraphs. SimpleGraph (g), U))
72
+ function SimpleWeightedGraph (g:: LightGraphs.AbstractGraph{T} , x:: U ) where {T <: Integer , U <: Real }
73
+ adj_matrix = if LightGraphs. is_directed (g)
74
+ # TODO abstract function instead of SimpleGraph constructor
75
+ adjacency_matrix (LightGraphs. SimpleGraphs. SimpleGraph (g), U)
76
+ else
77
+ adjacency_matrix (g, U)
78
+ end
79
+ return SimpleWeightedGraph {T, U} (x .* adj_matrix)
80
+ end
77
81
78
82
# SimpleWeightedGraph{T, U}(SimpleGraph)
79
- function (:: Type{SimpleWeightedGraph{T, U}} )(g:: LightGraphs.SimpleGraphs.SimpleGraph ) where T<: Integer where U <: Real
80
- SimpleWeightedGraph {T, U} (adjacency_matrix (LightGraphs. SimpleGraphs. SimpleGraph {T} (g), U))
83
+ function (:: Type{SimpleWeightedGraph{T, U}} )(g:: LightGraphs.AbstractGraph ) where {T<: Integer , U <: Real }
84
+ adj_matrix = if LightGraphs. is_directed (g)
85
+ # TODO abstract function instead of SimpleGraph constructor
86
+ adjacency_matrix (LightGraphs. SimpleGraphs. SimpleGraph {T} (g), U)
87
+ else
88
+ adjacency_matrix (g, U)
89
+ end
90
+ return SimpleWeightedGraph {T, U} (adj_matrix)
81
91
end
82
92
83
93
# Graph(srcs, dsts, weights)
84
- function SimpleWeightedGraph (i:: AbstractVector{T} , j:: AbstractVector{T} , v:: AbstractVector{U} ; combine = + ) where T<: Integer where U<: Real
94
+ function SimpleWeightedGraph (i:: AbstractVector{T} , j:: AbstractVector{T} , v:: AbstractVector{U} ; combine = + ) where { T<: Integer , U<: Real }
85
95
m = max (maximum (i), maximum (j))
86
96
s = sparse (vcat (i,j), vcat (j,i), vcat (v,v), m, m, combine)
87
97
SimpleWeightedGraph {T, U} (s)
88
98
end
89
99
90
100
LightGraphs. SimpleGraph (g:: SimpleWeightedGraph ) = SimpleGraph (g. weights)
91
101
92
- edgetype (:: SimpleWeightedGraph{T, U} ) where T<: Integer where U<: Real = SimpleWeightedGraphEdge{T,U}
102
+ edgetype (:: SimpleWeightedGraph{T, U} ) where { T<: Integer , U<: Real } = SimpleWeightedGraphEdge{T,U}
93
103
94
104
edges (g:: SimpleWeightedGraph ) = (SimpleWeightedEdge (x[1 ], x[2 ], x[3 ]) for x in zip (findnz (triu (g. weights))... ))
95
105
weights (g:: SimpleWeightedGraph ) = g. weights
@@ -125,12 +135,12 @@ end
125
135
126
136
== (g:: SimpleWeightedGraph , h:: SimpleWeightedGraph ) = g. weights == h. weights
127
137
138
+ is_directed (:: Type{<:SimpleWeightedGraph} ) = false
128
139
129
- """
130
- is_directed(g)
140
+ function Base. getindex (g:: SimpleWeightedGraph{T, U} , e:: AbstractEdge , :: Val{:weight} ) where {T, U, S}
141
+ return g. weights[src (e), dst (e)]
142
+ end
131
143
132
- Return `true` if `g` is a directed graph.
133
- """
134
- is_directed (:: Type{SimpleWeightedGraph} ) = false
135
- is_directed (:: Type{SimpleWeightedGraph{T, U}} ) where T where U = false
136
- is_directed (g:: SimpleWeightedGraph ) = false
144
+ function Base. getindex (g:: SimpleWeightedGraph{T, U} , i:: Integer , j:: Integer , :: Val{:weight} ) where {T, U, S}
145
+ return g. weights[i, j]
146
+ end
0 commit comments