-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathdistance.jl
200 lines (158 loc) · 5.24 KB
/
distance.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# used in shortest path calculations
"""
DefaultDistance
An array-like structure that provides distance values of `1` for any `src, dst` combination.
"""
struct DefaultDistance <: AbstractMatrix{Int}
nv::Int
DefaultDistance(nv::Int=typemax(Int)) = new(nv)
end
DefaultDistance(nv::Integer) = DefaultDistance(Int(nv))
function show(io::IO, x::DefaultDistance)
return print(io, "$(x.nv) × $(x.nv) default distance matrix (value = 1)")
end
show(io::IO, z::MIME"text/plain", x::DefaultDistance) = show(io, x)
getindex(::DefaultDistance, s::Integer, d::Integer) = 1
getindex(::DefaultDistance, s::UnitRange, d::UnitRange) = DefaultDistance(length(s))
size(d::DefaultDistance) = (d.nv, d.nv)
transpose(d::DefaultDistance) = d
adjoint(d::DefaultDistance) = d
"""
eccentricity(g[, v][, distmx])
eccentricity(g[, vs][, distmx])
Return the eccentricity[ies] of a vertex / vertex list `v` or a set of vertices
`vs` defaulting to the entire graph. An optional matrix of edge distances may
be supplied; if missing, edge distances default to `1`.
The eccentricity of a vertex is the maximum shortest-path distance between it
and all other vertices in the graph.
The output is either a single float (when a single vertex is provided) or a
vector of floats corresponding to the vertex vector. If no vertex vector is
provided, the vector returned corresponds to each vertex in the graph.
### Performance
Because this function must calculate shortest paths for all vertices supplied
in the argument list, it may take a long time.
### Implementation Notes
The eccentricity vector returned by `eccentricity()` may be used as input
for the rest of the distance measures below. If an eccentricity vector is
provided, it will be used. Otherwise, an eccentricity vector will be calculated
for each call to the function. It may therefore be more efficient to calculate,
store, and pass the eccentricities if multiple distance measures are desired.
An infinite path length is represented by the `typemax` of the distance matrix.
# Examples
```jldoctest
julia> using Graphs
julia> g = SimpleGraph([0 1 0; 1 0 1; 0 1 0]);
julia> eccentricity(g, 1)
2
julia> eccentricity(g, [1; 2])
2-element Vector{Int64}:
2
1
julia> eccentricity(g, [1; 2], [0 2 0; 0.5 0 0.5; 0 2 0])
2-element Vector{Float64}:
2.5
0.5
```
"""
function eccentricity(
g::AbstractGraph, v::Integer, distmx::AbstractMatrix{T}=weights(g)
) where {T<:Number}
e = maximum(dijkstra_shortest_paths(g, v, distmx).dists)
e == typemax(T) && @warn("Infinite path length detected for vertex $v")
return e
end
function eccentricity(g::AbstractGraph, vs=vertices(g), distmx::AbstractMatrix=weights(g))
return [eccentricity(g, v, distmx) for v in vs]
end
function eccentricity(g::AbstractGraph, distmx::AbstractMatrix)
return eccentricity(g, vertices(g), distmx)
end
"""
diameter(eccentricities)
diameter(g, distmx=weights(g))
Given a graph and optional distance matrix, or a vector of precomputed
eccentricities, return the maximum eccentricity of the graph.
# Examples
```jldoctest
julia> using Graphs
julia> diameter(star_graph(5))
2
julia> diameter(path_graph(5))
4
```
"""
diameter(eccentricities::Vector) = maximum(eccentricities)
function diameter(g::AbstractGraph, distmx::AbstractMatrix=weights(g))
return maximum(eccentricity(g, distmx))
end
"""
periphery(eccentricities)
periphery(g, distmx=weights(g))
Given a graph and optional distance matrix, or a vector of precomputed
eccentricities, return the set of all vertices whose eccentricity is
equal to the graph's diameter (that is, the set of vertices with the
largest eccentricity).
# Examples
```jldoctest
julia> using Graphs
julia> periphery(star_graph(5))
4-element Vector{Int64}:
2
3
4
5
julia> periphery(path_graph(5))
2-element Vector{Int64}:
1
5
```
"""
function periphery(eccentricities::Vector)
diam = maximum(eccentricities)
return filter(x -> eccentricities[x] == diam, 1:length(eccentricities))
end
function periphery(g::AbstractGraph, distmx::AbstractMatrix=weights(g))
return periphery(eccentricity(g, distmx))
end
"""
radius(eccentricities)
radius(g, distmx=weights(g))
Given a graph and optional distance matrix, or a vector of precomputed
eccentricities, return the minimum eccentricity of the graph.
# Examples
```jldoctest
julia> using Graphs
julia> radius(star_graph(5))
1
julia> radius(path_graph(5))
2
```
"""
radius(eccentricities::Vector) = minimum(eccentricities)
function radius(g::AbstractGraph, distmx::AbstractMatrix=weights(g))
return minimum(eccentricity(g, distmx))
end
"""
center(eccentricities)
center(g, distmx=weights(g))
Given a graph and optional distance matrix, or a vector of precomputed
eccentricities, return the set of all vertices whose eccentricity is equal
to the graph's radius (that is, the set of vertices with the smallest eccentricity).
# Examples
```jldoctest
julia> using Graphs
julia> center(star_graph(5))
1-element Vector{Int64}:
1
julia> center(path_graph(5))
1-element Vector{Int64}:
3
```
"""
function center(eccentricities::Vector)
rad = radius(eccentricities)
return filter(x -> eccentricities[x] == rad, 1:length(eccentricities))
end
function center(g::AbstractGraph, distmx::AbstractMatrix=weights(g))
return center(eccentricity(g, distmx))
end