-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathcore.jl
431 lines (321 loc) · 9.23 KB
/
core.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
"""
AbstractPathState
An abstract type that provides information from shortest paths calculations.
"""
abstract type AbstractPathState end
"""
is_ordered(e)
Return true if the source vertex of edge `e` is less than or equal to
the destination vertex.
# Examples
```jldoctest
julia> using Graphs
julia> g = DiGraph(2);
julia> add_edge!(g, 2, 1);
julia> is_ordered(first(edges(g)))
false
```
"""
is_ordered(e::AbstractEdge) = src(e) <= dst(e)
"""
add_vertices!(g, n)
Add `n` new vertices to the graph `g`.
Return the number of vertices that were added successfully.
# Examples
```jldoctest
julia> using Graphs
julia> g = SimpleGraph()
{0, 0} undirected simple Int64 graph
julia> add_vertices!(g, 2)
2
```
"""
add_vertices!(g::AbstractGraph, n::Integer) = sum([add_vertex!(g) for i in 1:n])
# TODO the behaviour of indegree (and as well outdegree and degree) is very
# badly documented for the case indegree(g, vs) where vs is not a single vertex
# but rather a collection of vertices
"""
indegree(g[, v])
Return a vector corresponding to the number of edges which end at each vertex in
graph `g`. If `v` is specified, only return degrees for vertices in `v`.
# Examples
```jldoctest
julia> using Graphs
julia> g = DiGraph(3);
julia> add_edge!(g, 2, 3);
julia> add_edge!(g, 3, 1);
julia> indegree(g)
3-element Vector{Int64}:
1
0
1
```
"""
indegree(g::AbstractGraph, v::Integer) = length(inneighbors(g, v))
indegree(g::AbstractGraph, vs=vertices(g)) = [indegree(g, x) for x in vs]
"""
outdegree(g[, v])
Return a vector corresponding to the number of edges which start at each vertex in
graph `g`. If `v` is specified, only return degrees for vertices in `v`.
# Examples
```jldoctest
julia> using Graphs
julia> g = DiGraph(3);
julia> add_edge!(g, 2, 3);
julia> add_edge!(g, 3, 1);
julia> outdegree(g)
3-element Vector{Int64}:
0
1
1
```
"""
outdegree(g::AbstractGraph, v::Integer) = length(outneighbors(g, v))
outdegree(g::AbstractGraph, vs=vertices(g)) = [outdegree(g, x) for x in vs]
"""
degree(g[, v])
Return a vector corresponding to the number of edges which start or end at each
vertex in graph `g`. If `v` is specified, only return degrees for vertices in `v`.
For directed graphs, this value equals the incoming plus outgoing edges.
For undirected graphs, it equals the connected edges.
# Examples
```jldoctest
julia> using Graphs
julia> g = DiGraph(3);
julia> add_edge!(g, 2, 3);
julia> add_edge!(g, 3, 1);
julia> degree(g)
3-element Vector{Int64}:
1
1
2
```
"""
function degree end
function degree(g::AbstractGraph, v::Integer)
if !is_directed(g)
return outdegree(g, v)
end
return indegree(g, v) + outdegree(g, v)
end
degree(g::AbstractGraph, vs=vertices(g)) = [degree(g, x) for x in vs]
"""
Δout(g)
Return the maximum [`outdegree`](@ref) of vertices in `g`.
"""
Δout(g) = noallocextreme(outdegree, (>), typemin(Int), g)
"""
δout(g)
Return the minimum [`outdegree`](@ref) of vertices in `g`.
"""
δout(g) = noallocextreme(outdegree, (<), typemax(Int), g)
"""
Δin(g)
Return the maximum [`indegree`](@ref) of vertices in `g`.
"""
Δin(g) = noallocextreme(indegree, (>), typemin(Int), g)
"""
δin(g)
Return the minimum [`indegree`](@ref) of vertices in `g`.
"""
δin(g) = noallocextreme(indegree, (<), typemax(Int), g)
"""
Δ(g)
Return the maximum [`degree`](@ref) of vertices in `g`.
"""
Δ(g) = noallocextreme(degree, (>), typemin(Int), g)
"""
δ(g)
Return the minimum [`degree`](@ref) of vertices in `g`.
"""
δ(g) = noallocextreme(degree, (<), typemax(Int), g)
"""
noallocextreme(f, comparison, initial, g)
Compute the extreme value of `[f(g,i) for i=i:nv(g)]` without gathering them all
"""
function noallocextreme(f, comparison, initial, g)
value = initial
for i in vertices(g)
funci = f(g, i)
if comparison(funci, value)
value = funci
end
end
return value
end
"""
degree_histogram(g, degfn=degree)
Return a `Dict` with values representing the number of vertices that have degree
represented by the key.
Degree function (for example, [`indegree`](@ref) or [`outdegree`](@ref)) may be specified by
overriding `degfn`.
"""
function degree_histogram(g::AbstractGraph{T}, degfn=degree) where {T}
hist = Dict{T,Int}()
for v in vertices(g) # minimize allocations by
for d in degfn(g, v) # iterating over vertices
hist[d] = get(hist, d, 0) + 1
end
end
return hist
end
"""
neighbors(g, v)
Return a list of all neighbors reachable from vertex `v` in `g`.
For directed graphs, the default is equivalent to [`outneighbors`](@ref);
use [`all_neighbors`](@ref) to list inbound and outbound neighbors.
### Implementation Notes
Returns a reference to the current graph's internal structures, not a copy.
Do not modify result. If the graph is modified, the behavior is undefined:
the array behind this reference may be modified too, but this is not guaranteed.
# Examples
```jldoctest
julia> using Graphs
julia> g = DiGraph(3);
julia> add_edge!(g, 2, 3);
julia> add_edge!(g, 3, 1);
julia> neighbors(g, 1)
Int64[]
julia> neighbors(g, 2)
1-element Vector{Int64}:
3
julia> neighbors(g, 3)
1-element Vector{Int64}:
1
```
"""
neighbors(g::AbstractGraph, v::Integer) = outneighbors(g, v)
"""
all_neighbors(g, v)
Return a list of all inbound and outbound neighbors of `v` in `g`.
For undirected graphs, this is equivalent to both [`outneighbors`](@ref)
and [`inneighbors`](@ref).
### Implementation Notes
Returns a reference to the current graph's internal structures, not a copy.
Do not modify result. If the graph is modified, the behavior is undefined:
the array behind this reference may be modified too, but this is not guaranteed.
# Examples
```jldoctest
julia> using Graphs
julia> g = DiGraph(3);
julia> add_edge!(g, 2, 3);
julia> add_edge!(g, 3, 1);
julia> all_neighbors(g, 1)
1-element Vector{Int64}:
3
julia> all_neighbors(g, 2)
1-element Vector{Int64}:
3
julia> all_neighbors(g, 3)
2-element Vector{Int64}:
1
2
```
"""
function all_neighbors end
@traitfn function all_neighbors(g::::IsDirected, v::Integer)
return union(outneighbors(g, v), inneighbors(g, v))
end
@traitfn all_neighbors(g::::(!IsDirected), v::Integer) = neighbors(g, v)
"""
common_neighbors(g, u, v)
Return the neighbors common to vertices `u` and `v` in `g`.
### Implementation Notes
Returns a reference to the current graph's internal structures, not a copy.
Do not modify result. If the graph is modified, the behavior is undefined:
the array behind this reference may be modified too, but this is not guaranteed.
# Examples
```jldoctest
julia> using Graphs
julia> g = SimpleGraph(4);
julia> add_edge!(g, 1, 2);
julia> add_edge!(g, 2, 3);
julia> add_edge!(g, 3, 4);
julia> add_edge!(g, 4, 1);
julia> add_edge!(g, 1, 3);
julia> common_neighbors(g, 1, 3)
2-element Vector{Int64}:
2
4
julia> common_neighbors(g, 1, 4)
1-element Vector{Int64}:
3
```
"""
function common_neighbors(g::AbstractGraph, u::Integer, v::Integer)
return intersect(neighbors(g, u), neighbors(g, v))
end
"""
has_self_loops(g)
Return true if `g` has any self loops.
# Examples
```jldoctest
julia> using Graphs
julia> g = SimpleGraph(2);
julia> add_edge!(g, 1, 2);
julia> has_self_loops(g)
false
julia> add_edge!(g, 1, 1);
julia> has_self_loops(g)
true
```
"""
function has_self_loops(g::AbstractGraph)
return nv(g) == 0 ? false : any(v -> has_edge(g, v, v), vertices(g))
end
"""
num_self_loops(g)
Return the number of self loops in `g`.
# Examples
```jldoctest
julia> using Graphs
julia> g = SimpleGraph(2);
julia> add_edge!(g, 1, 2);
julia> num_self_loops(g)
0
julia> add_edge!(g, 1, 1);
julia> num_self_loops(g)
1
```
"""
num_self_loops(g::AbstractGraph) = nv(g) == 0 ? 0 : sum(v -> has_edge(g, v, v), vertices(g))
"""
density(g)
Return the density of `g`.
Density is defined as the ratio of the number of actual edges to the
number of possible edges (``|V|×(|V|-1)`` for directed graphs and
``\\frac{|V|×(|V|-1)}{2}`` for undirected graphs).
"""
function density end
@traitfn density(g::::IsDirected) = ne(g) / (nv(g) * (nv(g) - 1))
@traitfn density(g::::(!IsDirected)) = (2 * ne(g)) / (nv(g) * (nv(g) - 1))
"""
squash(g)
Return a copy of a graph with the smallest practical eltype that
can accommodate all vertices.
May also return the original graph if the eltype does not change.
"""
function squash(g::AbstractGraph)
# TODO this version check can be removed when we increase the required Julia version
deprecation_msg = "squash(::AbstractGraph) is deprecated in favor of methods that specialize on the graph type."
if VERSION >= v"1.5.2"
Base.depwarn(deprecation_msg, :squash; force=true)
else
Base.depwarn(deprecation_msg, :squash)
end
gtype = is_directed(g) ? DiGraph : Graph
validtypes = [UInt8, UInt16, UInt32, UInt64, Int64]
nvg = nv(g)
for T in validtypes
nvg < typemax(T) && return gtype{T}(g)
end
end
"""
weights(g)
Return the weights of the edges of a graph `g` as a matrix. Defaults
to [`Graphs.DefaultDistance`](@ref).
### Implementation Notes
In general, referencing the weight of a nonexistent edge is undefined behavior. Do not rely on the `weights` matrix
as a substitute for the graph's [`adjacency_matrix`](@ref).
"""
weights(g::AbstractGraph) = DefaultDistance(nv(g))