/
connectivity.jl
735 lines (588 loc) · 19.9 KB
/
connectivity.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# Parts of this code were taken / derived from Graphs.jl. See LICENSE for
# licensing details.
"""
connected_components!(label, g)
Fill `label` with the `id` of the connected component in the undirected graph
`g` to which it belongs. Return a vector representing the component assigned
to each vertex. The component value is the smallest vertex ID in the component.
### Performance
This algorithm is linear in the number of edges of the graph.
"""
function connected_components!(label::AbstractVector, g::AbstractGraph{T}) where T
for u in vertices(g)
label[u] != zero(T) && continue
label[u] = u
Q = Vector{T}()
push!(Q, u)
while !isempty(Q)
src = popfirst!(Q)
for vertex in all_neighbors(g, src)
if label[vertex] == zero(T)
push!(Q, vertex)
label[vertex] = u
end
end
end
end
return label
end
"""
components_dict(labels)
Convert an array of labels to a map of component id to vertices, and return
a map with each key corresponding to a given component id
and each value containing the vertices associated with that component.
"""
function components_dict(labels::Vector{T}) where T <: Integer
d = Dict{T,Vector{T}}()
for (v, l) in enumerate(labels)
vec = get(d, l, Vector{T}())
push!(vec, v)
d[l] = vec
end
return d
end
"""
components(labels)
Given a vector of component labels, return a vector of vectors representing the vertices associated
with a given component id.
"""
function components(labels::Vector{T}) where T <: Integer
d = Dict{T,T}()
c = Vector{Vector{T}}()
i = one(T)
for (v, l) in enumerate(labels)
index = get!(d, l, i)
if length(c) >= index
push!(c[index], v)
else
push!(c, [v])
i += 1
end
end
return c, d
end
"""
connected_components(g)
Return the [connected components](https://en.wikipedia.org/wiki/Connectivity_(graph_theory))
of an undirected graph `g` as a vector of components, with each element a vector of vertices
belonging to the component.
For directed graphs, see [`strongly_connected_components`](@ref) and
[`weakly_connected_components`](@ref).
# Examples
```jldoctest
julia> g = SimpleGraph([0 1 0; 1 0 1; 0 1 0]);
julia> connected_components(g)
1-element Array{Array{Int64,1},1}:
[1, 2, 3]
julia> g = SimpleGraph([0 1 0 0 0; 1 0 1 0 0; 0 1 0 0 0; 0 0 0 0 1; 0 0 0 1 0]);
julia> connected_components(g)
2-element Array{Array{Int64,1},1}:
[1, 2, 3]
[4, 5]
```
"""
function connected_components(g::AbstractGraph{T}) where T
label = zeros(T, nv(g))
connected_components!(label, g)
c, d = components(label)
return c
end
"""
is_connected(g)
Return `true` if graph `g` is connected. For directed graphs, return `true`
if graph `g` is weakly connected.
# Examples
```jldoctest
julia> g = SimpleGraph([0 1 0; 1 0 1; 0 1 0]);
julia> is_connected(g)
true
julia> g = SimpleGraph([0 1 0 0 0; 1 0 1 0 0; 0 1 0 0 0; 0 0 0 0 1; 0 0 0 1 0]);
julia> is_connected(g)
false
julia> g = SimpleDiGraph([0 1 0; 0 0 1; 1 0 0]);
julia> is_connected(g)
true
```
"""
function is_connected(g::AbstractGraph)
mult = is_directed(g) ? 2 : 1
return mult * ne(g) + 1 >= nv(g) && length(connected_components(g)) == 1
end
"""
weakly_connected_components(g)
Return the weakly connected components of the graph `g`. This
is equivalent to the connected components of the undirected equivalent of `g`.
For undirected graphs this is equivalent to the [`connected_components`](@ref) of `g`.
# Examples
```jldoctest
julia> g = SimpleDiGraph([0 1 0; 1 0 1; 0 0 0]);
julia> weakly_connected_components(g)
1-element Array{Array{Int64,1},1}:
[1, 2, 3]
```
"""
weakly_connected_components(g) = connected_components(g)
"""
is_weakly_connected(g)
Return `true` if the graph `g` is weakly connected. If `g` is undirected,
this function is equivalent to [`is_connected(g)`](@ref).
# Examples
```jldoctest
julia> g = SimpleDiGraph([0 1 0; 0 0 1; 1 0 0]);
julia> is_weakly_connected(g)
true
julia> g = SimpleDiGraph([0 1 0; 1 0 1; 0 0 0]);
julia> is_connected(g)
true
julia> is_strongly_connected(g)
false
julia> is_weakly_connected(g)
true
```
"""
is_weakly_connected(g) = is_connected(g)
"""
strongly_connected_components(g)
Compute the strongly connected components of a directed graph `g`.
Return an array of arrays, each of which is the entire connected component.
### Implementation Notes
The order of the components is not part of the API contract.
# Examples
```jldoctest
julia> g = SimpleDiGraph([0 1 0; 1 0 1; 0 0 0]);
julia> strongly_connected_components(g)
2-element Array{Array{Int64,1},1}:
[3]
[1, 2]
julia> g=SimpleDiGraph(11)
{11, 0} directed simple Int64 graph
julia> edge_list=[(1,2),(2,3),(3,4),(4,1),(3,5),(5,6),(6,7),(7,5),(5,8),(8,9),(9,8),(10,11),(11,10)];
julia> g = SimpleDiGraph(Edge.(edge_list))
{11, 13} directed simple Int64 graph
julia> strongly_connected_components(g)
4-element Array{Array{Int64,1},1}:
[8, 9]
[5, 6, 7]
[1, 2, 3, 4]
[10, 11]
```
"""
function strongly_connected_components end
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
@traitfn function strongly_connected_components(g::AG::IsDirected) where {T<:Integer, AG <: AbstractGraph{T}}
zero_t = zero(T)
one_t = one(T)
nvg = nv(g)
count = one_t
index = zeros(T, nvg) # first time in which vertex is discovered
stack = Vector{T}() # stores vertices which have been discovered and not yet assigned to any component
onstack = zeros(Bool, nvg) # false if a vertex is waiting in the stack to receive a component assignment
lowlink = zeros(T, nvg) # lowest index vertex that it can reach through back edge (index array not vertex id number)
parents = zeros(T, nvg) # parent of every vertex in dfs
components = Vector{Vector{T}}() # maintains a list of scc (order is not guaranteed in API)
dfs_stack = Vector{T}()
@inbounds for s in vertices(g)
if index[s] == zero_t
index[s] = count
lowlink[s] = count
onstack[s] = true
parents[s] = s
push!(stack, s)
count = count + one_t
# start dfs from 's'
push!(dfs_stack, s)
while !isempty(dfs_stack)
v = dfs_stack[end] #end is the most recently added item
u = zero_t
@inbounds for v_neighbor in outneighbors(g, v)
if index[v_neighbor] == zero_t
# unvisited neighbor found
u = v_neighbor
break
#GOTO A push u onto DFS stack and continue DFS
elseif onstack[v_neighbor]
# we have already seen n, but can update the lowlink of v
# which has the effect of possibly keeping v on the stack until n is ready to pop.
# update lowest index 'v' can reach through out neighbors
lowlink[v] = min(lowlink[v], index[v_neighbor])
end
end
if u == zero_t
# All out neighbors already visited or no out neighbors
# we have fully explored the DFS tree from v.
# time to start popping.
popped = pop!(dfs_stack)
lowlink[parents[popped]] = min(lowlink[parents[popped]], lowlink[popped])
if index[v] == lowlink[v]
# found a cycle in a completed dfs tree.
component = Vector{T}()
while !isempty(stack) #break when popped == v
# drain stack until we see v.
# everything on the stack until we see v is in the SCC rooted at v.
popped = pop!(stack)
push!(component, popped)
onstack[popped] = false
# popped has been assigned a component, so we will never see it again.
if popped == v
# we have drained the stack of an entire component.
break
end
end
reverse!(component)
push!(components, component)
end
else #LABEL A
# add unvisited neighbor to dfs
index[u] = count
lowlink[u] = count
onstack[u] = true
parents[u] = v
count = count + one_t
push!(stack, u)
push!(dfs_stack, u)
# next iteration of while loop will expand the DFS tree from u.
end
end
end
end
return components
end
"""
strongly_connected_components_kosaraju(g)
Compute the strongly connected components of a directed graph `g` using Kosaraju's Algorithm.
(https://en.wikipedia.org/wiki/Kosaraju%27s_algorithm).
Return an array of arrays, each of which is the entire connected component.
### Performance
Time Complexity : O(|E|+|V|)
Space Complexity : O(|V|) {Excluding the memory required for storing graph}
|V| = Number of vertices
|E| = Number of edges
### Examples
```jldoctest
julia> g=SimpleDiGraph(3)
{3, 0} directed simple Int64 graph
julia> g = SimpleDiGraph([0 1 0 ; 0 0 1; 0 0 0])
{3, 2} directed simple Int64 graph
julia> strongly_connected_components_kosaraju(g)
3-element Array{Array{Int64,1},1}:
[1]
[2]
[3]
julia> g=SimpleDiGraph(11)
{11, 0} directed simple Int64 graph
julia> edge_list=[(1,2),(2,3),(3,4),(4,1),(3,5),(5,6),(6,7),(7,5),(5,8),(8,9),(9,8),(10,11),(11,10)]
13-element Array{Tuple{Int64,Int64},1}:
(1, 2)
(2, 3)
(3, 4)
(4, 1)
(3, 5)
(5, 6)
(6, 7)
(7, 5)
(5, 8)
(8, 9)
(9, 8)
(10, 11)
(11, 10)
julia> g = SimpleDiGraph(Edge.(edge_list))
{11, 13} directed simple Int64 graph
julia> strongly_connected_components_kosaraju(g)
4-element Array{Array{Int64,1},1}:
[11, 10]
[2, 3, 4, 1]
[6, 7, 5]
[9, 8]
```
"""
function strongly_connected_components_kosaraju end
@traitfn function strongly_connected_components_kosaraju(g::AG::IsDirected) where {T<:Integer, AG <: AbstractGraph{T}}
nvg = nv(g)
components = Vector{Vector{T}}() # Maintains a list of strongly connected components
order = Vector{T}() # Vector which will store the order in which vertices are visited
sizehint!(order, nvg)
color = zeros(UInt8, nvg) # Vector used as for marking the colors during dfs
dfs_stack = Vector{T}() # Stack used for dfs
# dfs1
@inbounds for v in vertices(g)
color[v] != 0 && continue
color[v] = 1
# Start dfs from v
push!(dfs_stack, v) # Push v to the stack
while !isempty(dfs_stack)
u = dfs_stack[end]
w = zero(T)
for u_neighbor in outneighbors(g, u)
if color[u_neighbor] == 0
w = u_neighbor
break
end
end
if w != 0
push!(dfs_stack, w)
color[w] = 1
else
push!(order, u) #Push back in vector to store the order in which the traversal finishes(Reverse Topological Sort)
color[u] = 2
pop!(dfs_stack)
end
end
end
@inbounds for i in vertices(g)
color[i] = 0 # Marking all the vertices from 1 to n as unvisited for dfs2
end
# dfs2
@inbounds for i in 1:nvg
v = order[end-i+1] # Reading the order vector in the decreasing order of finish time
color[v] != 0 && continue
color[v] = 1
component=Vector{T}() # Vector used to store the vertices of one component temporarily
# Start dfs from v
push!(dfs_stack, v) # Push v to the stack
while !isempty(dfs_stack)
u = dfs_stack[end]
w = zero(T)
for u_neighbor in inneighbors(g, u)
if color[u_neighbor] == 0
w = u_neighbor
break
end
end
if w != 0
push!(dfs_stack, w)
color[w] = 1
else
color[u] = 2
push!(component, u) # Push u to the vector component
pop!(dfs_stack)
end
end
push!(components, component)
end
return components
end
"""
is_strongly_connected(g)
Return `true` if directed graph `g` is strongly connected.
# Examples
```jldoctest
julia> g = SimpleDiGraph([0 1 0; 0 0 1; 1 0 0]);
julia> is_strongly_connected(g)
true
```
"""
function is_strongly_connected end
@traitfn is_strongly_connected(g::::IsDirected) = length(strongly_connected_components(g)) == 1
"""
period(g)
Return the (common) period for all vertices in a strongly connected directed graph.
Will throw an error if the graph is not strongly connected.
# Examples
```jldoctest
julia> g = SimpleDiGraph([0 1 0; 0 0 1; 1 0 0]);
julia> period(g)
3
```
"""
function period end
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
@traitfn function period(g::AG::IsDirected) where {T, AG <: AbstractGraph{T}}
!is_strongly_connected(g) && throw(ArgumentError("Graph must be strongly connected"))
# First check if there's a self loop
has_self_loops(g) && return 1
g_bfs_tree = bfs_tree(g, 1)
levels = gdistances(g_bfs_tree, 1)
edge_values = Vector{T}()
divisor = 0
for e in edges(g)
has_edge(g_bfs_tree, src(e), dst(e)) && continue
@inbounds value = levels[src(e)] - levels[dst(e)] + 1
divisor = gcd(divisor, value)
isequal(divisor, 1) && return 1
end
return divisor
end
"""
condensation(g[, scc])
Return the condensation graph of the strongly connected components `scc`
in the directed graph `g`. If `scc` is missing, generate the strongly
connected components first.
# Examples
```jldoctest
julia> g = SimpleDiGraph([0 1 0 0 0; 0 0 1 0 0; 1 0 0 1 0; 0 0 0 0 1; 0 0 0 1 0])
{5, 6} directed simple Int64 graph
julia> strongly_connected_components(g)
2-element Array{Array{Int64,1},1}:
[4, 5]
[1, 2, 3]
julia> foreach(println, edges(condensation(g)))
Edge 2 => 1
```
"""
function condensation end
@traitfn function condensation(g::::IsDirected, scc::Vector{Vector{T}}) where T <: Integer
h = DiGraph{T}(length(scc))
component = Vector{T}(undef, nv(g))
for (i, s) in enumerate(scc)
@inbounds component[s] .= i
end
@inbounds for e in edges(g)
s, d = component[src(e)], component[dst(e)]
if (s != d)
add_edge!(h, s, d)
end
end
return h
end
@traitfn condensation(g::::IsDirected) = condensation(g, strongly_connected_components(g))
"""
attracting_components(g)
Return a vector of vectors of integers representing lists of attracting
components in the directed graph `g`.
The attracting components are a subset of the strongly
connected components in which the components do not have any leaving edges.
# Examples
```jldoctest
julia> g = SimpleDiGraph([0 1 0 0 0; 0 0 1 0 0; 1 0 0 1 0; 0 0 0 0 1; 0 0 0 1 0])
{5, 6} directed simple Int64 graph
julia> strongly_connected_components(g)
2-element Array{Array{Int64,1},1}:
[4, 5]
[1, 2, 3]
julia> attracting_components(g)
1-element Array{Array{Int64,1},1}:
[4, 5]
```
"""
function attracting_components end
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
@traitfn function attracting_components(g::AG::IsDirected) where {T, AG <: AbstractGraph{T}}
scc = strongly_connected_components(g)
cond = condensation(g, scc)
attracting = Vector{T}()
for v in vertices(cond)
if outdegree(cond, v) == 0
push!(attracting, v)
end
end
return scc[attracting]
end
"""
neighborhood(g, v, d, distmx=weights(g))
Return a vector of each vertex in `g` at a geodesic distance less than or equal to `d`, where distances
may be specified by `distmx`.
### Optional Arguments
- `dir=:out`: If `g` is directed, this argument specifies the edge direction
with respect to `v` of the edges to be considered. Possible values: `:in` or `:out`.
# Examples
```jldoctest
julia> g = SimpleDiGraph([0 1 0 0 0; 0 0 1 0 0; 1 0 0 1 0; 0 0 0 0 1; 0 0 0 1 0]);
julia> neighborhood(g, 1, 2)
3-element Array{Int64,1}:
1
2
3
julia> neighborhood(g, 1, 3)
4-element Array{Int64,1}:
1
2
3
4
julia> neighborhood(g, 1, 3, [0 1 0 0 0; 0 0 1 0 0; 1 0 0 0.25 0; 0 0 0 0 0.25; 0 0 0 0.25 0])
5-element Array{Int64,1}:
1
2
3
4
5
```
"""
neighborhood(g::AbstractGraph{T}, v::Integer, d, distmx::AbstractMatrix{U}=weights(g); dir=:out) where T <: Integer where U <: Real =
first.(neighborhood_dists(g, v, d, distmx; dir=dir))
"""
neighborhood_dists(g, v, d, distmx=weights(g))
Return a a vector of tuples representing each vertex which is at a geodesic distance less than or equal to `d`, along with
its distance from `v`. Non-negative distances may be specified by `distmx`.
### Optional Arguments
- `dir=:out`: If `g` is directed, this argument specifies the edge direction
with respect to `v` of the edges to be considered. Possible values: `:in` or `:out`.
# Examples
```jldoctest
julia> g = SimpleDiGraph([0 1 0 0 0; 0 0 1 0 0; 1 0 0 1 0; 0 0 0 0 1; 0 0 0 1 0]);
julia> neighborhood_dists(g, 1, 3)
4-element Array{Tuple{Int64,Int64},1}:
(1, 0)
(2, 1)
(3, 2)
(4, 3)
julia> neighborhood_dists(g, 1, 3, [0 1 0 0 0; 0 0 1 0 0; 1 0 0 0.25 0; 0 0 0 0 0.25; 0 0 0 0.25 0])
5-element Array{Tuple{Int64,Float64},1}:
(1, 0.0)
(2, 1.0)
(3, 2.0)
(4, 2.25)
(5, 2.5)
julia> neighborhood_dists(g, 4, 3)
2-element Array{Tuple{Int64,Int64},1}:
(4, 0)
(5, 1)
julia> neighborhood_dists(g, 4, 3, dir=:in)
5-element Array{Tuple{Int64,Int64},1}:
(4, 0)
(3, 1)
(5, 1)
(2, 2)
(1, 3)
```
"""
neighborhood_dists(g::AbstractGraph{T}, v::Integer, d, distmx::AbstractMatrix{U}=weights(g); dir=:out) where T <: Integer where U <: Real =
(dir == :out) ? _neighborhood(g, v, d, distmx, outneighbors) : _neighborhood(g, v, d, distmx, inneighbors)
function _neighborhood(g::AbstractGraph{T}, v::Integer, d::Real, distmx::AbstractMatrix{U}, neighborfn::Function) where T <: Integer where U <: Real
Q = Vector{Tuple{T,U}}()
d < zero(U) && return Q
push!(Q, (v,zero(U),) )
seen = fill(false,nv(g)); seen[v] = true #Bool Vector benchmarks faster than BitArray
for (src,currdist) in Q
currdist >= d && continue
for dst in neighborfn(g,src)
if !seen[dst]
seen[dst]=true
if currdist+distmx[src,dst] <= d
push!(Q, (dst , currdist+distmx[src,dst],))
end
end
end
end
return Q
end
"""
isgraphical(degs)
Return true if the degree sequence `degs` is graphical.
A sequence of integers is called graphical, if there exists a graph where the degrees of its vertices form that same sequence.
### Performance
Time complexity: ``\\mathcal{O}(|degs|*\\log(|degs|))``.
### Implementation Notes
According to Erdös-Gallai theorem, a degree sequence ``\\{d_1, ...,d_n\\}`` (sorted in descending order) is graphic iff the sum of vertex degrees is even and the sequence obeys the property -
```math
\\sum_{i=1}^{r} d_i \\leq r(r-1) + \\sum_{i=r+1}^n min(r,d_i)
```
for each integer r <= n-1
"""
function isgraphical(degs::Vector{<:Integer})
iseven(sum(degs)) || return false
sorted_degs = sort(degs, rev = true)
n = length(sorted_degs)
cur_sum = zero(UInt64)
mindeg = Vector{UInt64}(undef, n)
@inbounds for i = 1:n
mindeg[i] = min(i, sorted_degs[i])
end
cum_min = sum(mindeg)
@inbounds for r = 1:(n - 1)
cur_sum += sorted_degs[r]
cum_min -= mindeg[r]
cond = cur_sum <= (r * (r - 1) + cum_min)
cond || return false
end
return true
end