/
staticgraphs.jl
806 lines (649 loc) · 19.7 KB
/
staticgraphs.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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# Parts of this code were taken / derived from NetworkX. See LICENSE for
# licensing details.
"""
complete_graph(n)
Create an undirected [complete graph](https://en.wikipedia.org/wiki/Complete_graph)
with `n` vertices.
# Examples
```jldoctest
julia> using Graphs
julia> complete_graph(5)
{5, 10} undirected simple Int64 graph
julia> complete_graph(Int8(6))
{6, 15} undirected simple Int8 graph
```
"""
function complete_graph(n::T) where {T<:Integer}
n <= 0 && return SimpleGraph{T}(0)
ne = Int(n * (n - 1) ÷ 2)
fadjlist = Vector{Vector{T}}(undef, n)
@inbounds @simd for u in 1:n
listu = Vector{T}(undef, n - 1)
listu[1:(u - 1)] = 1:(u - 1)
listu[u:(n - 1)] = (u + 1):n
fadjlist[u] = listu
end
return SimpleGraph(ne, fadjlist)
end
"""
complete_bipartite_graph(n1, n2)
Create an undirected [complete bipartite graph](https://en.wikipedia.org/wiki/Complete_bipartite_graph)
with `n1 + n2` vertices.
# Examples
```jldoctest
julia> using Graphs
julia> complete_bipartite_graph(3, 4)
{7, 12} undirected simple Int64 graph
julia> complete_bipartite_graph(Int8(3), Int8(4))
{7, 12} undirected simple Int8 graph
```
"""
function complete_bipartite_graph(n1::T, n2::T) where {T<:Integer}
(n1 < 0 || n2 < 0) && return SimpleGraph{T}(0)
Tw = widen(T)
nw = Tw(n1) + Tw(n2)
n = T(nw) # checks if T is large enough for n1 + n2
ne = Int(n1) * Int(n2)
fadjlist = Vector{Vector{T}}(undef, n)
range1 = 1:n1
range2 = (n1 + 1):n
@inbounds @simd for u in range1
fadjlist[u] = Vector{T}(range2)
end
@inbounds @simd for u in range2
fadjlist[u] = Vector{T}(range1)
end
return SimpleGraph(ne, fadjlist)
end
"""
complete_multipartite_graph(partitions)
Create an undirected [complete bipartite graph](https://en.wikipedia.org/wiki/Complete_bipartite_graph)
with `sum(partitions)` vertices. A partition with `0` vertices is skipped.
### Implementation Notes
Preserves the eltype of the partitions vector. Will error if the required number of vertices
exceeds the eltype.
# Examples
```jldoctest
julia> using Graphs
julia> complete_multipartite_graph([1,2,3])
{6, 11} undirected simple Int64 graph
julia> complete_multipartite_graph(Int8[5,5,5])
{15, 75} undirected simple Int8 graph
```
"""
function complete_multipartite_graph(partitions::AbstractVector{T}) where {T<:Integer}
any(x -> x < 0, partitions) && return SimpleGraph{T}(0)
length(partitions) == 1 && return SimpleGraph{T}(partitions[1])
length(partitions) == 2 && return complete_bipartite_graph(partitions[1], partitions[2])
n = sum(partitions)
ne = 0
for p in partitions # type stability fails if we use sum and a generator here
ne += p * (Int(n) - p) # overflow if we don't convert to Int
end
ne = div(ne, 2)
fadjlist = Vector{Vector{T}}(undef, n)
cur = 1
for p in partitions
currange = cur:(cur + p - 1) # all vertices in the current partition
lowerrange = 1:(cur - 1) # all vertices lower than the current partition
upperrange = (cur + p):n # all vertices higher than the current partition
@inbounds @simd for u in currange
fadjlist[u] = Vector{T}(undef, length(lowerrange) + length(upperrange))
fadjlist[u][1:length(lowerrange)] = lowerrange
fadjlist[u][(length(lowerrange) + 1):end] = upperrange
end
cur += p
end
return SimpleGraph{T}(ne, fadjlist)
end
"""
turan_graph(n, r)
Creates a [Turán Graph](https://en.wikipedia.org/wiki/Tur%C3%A1n_graph), a complete
multipartite graph with `n` vertices and `r` partitions.
# Examples
```jldoctest
julia> using Graphs
julia> turan_graph(6, 2)
{6, 9} undirected simple Int64 graph
julia> turan_graph(Int8(7), 2)
{7, 12} undirected simple Int8 graph
```
"""
function turan_graph(n::Integer, r::Integer)
!(1 <= r <= n) &&
throw(DomainError("n=$n and r=$r are invalid, must satisfy 1 <= r <= n"))
T = typeof(n)
partitions = Vector{T}(undef, r)
c = cld(n, r)
f = fld(n, r)
@inbounds @simd for i in 1:(n % r)
partitions[i] = c
end
@inbounds @simd for i in ((n % r) + 1):r
partitions[i] = f
end
return complete_multipartite_graph(partitions)
end
"""
complete_digraph(n)
Create a directed [complete graph](https://en.wikipedia.org/wiki/Complete_graph)
with `n` vertices.
# Examples
```jldoctest
julia> using Graphs
julia> complete_digraph(5)
{5, 20} directed simple Int64 graph
julia> complete_digraph(Int8(6))
{6, 30} directed simple Int8 graph
```
"""
function complete_digraph(n::T) where {T<:Integer}
n <= 0 && return SimpleDiGraph{T}(0)
ne = Int(n * (n - 1))
fadjlist = Vector{Vector{T}}(undef, n)
badjlist = Vector{Vector{T}}(undef, n)
@inbounds @simd for u in 1:n
listu = Vector{T}(undef, n - 1)
listu[1:(u - 1)] = 1:(u - 1)
listu[u:(n - 1)] = (u + 1):n
fadjlist[u] = listu
badjlist[u] = deepcopy(listu)
end
return SimpleDiGraph(ne, fadjlist, badjlist)
end
"""
star_graph(n)
Create an undirected [star graph](https://en.wikipedia.org/wiki/Star_(graph_theory))
with `n` vertices.
# Examples
```jldoctest
julia> using Graphs
julia> star_graph(3)
{3, 2} undirected simple Int64 graph
julia> star_graph(Int8(10))
{10, 9} undirected simple Int8 graph
```
"""
function star_graph(n::T) where {T<:Integer}
n <= 0 && return SimpleGraph{T}(0)
ne = Int(n - 1)
fadjlist = Vector{Vector{T}}(undef, n)
@inbounds fadjlist[1] = Vector{T}(2:n)
@inbounds @simd for u in 2:n
fadjlist[u] = T[1]
end
return SimpleGraph(ne, fadjlist)
end
"""
star_digraph(n)
Create a directed [star graph](https://en.wikipedia.org/wiki/Star_(graph_theory))
with `n` vertices.
# Examples
```jldoctest
julia> using Graphs
julia> star_digraph(3)
{3, 2} directed simple Int64 graph
julia> star_digraph(Int8(10))
{10, 9} directed simple Int8 graph
```
"""
function star_digraph(n::T) where {T<:Integer}
n <= 0 && return SimpleDiGraph{T}(0)
ne = Int(n - 1)
fadjlist = Vector{Vector{T}}(undef, n)
badjlist = Vector{Vector{T}}(undef, n)
@inbounds fadjlist[1] = Vector{T}(2:n)
@inbounds badjlist[1] = T[]
@inbounds @simd for u in 2:n
fadjlist[u] = T[]
badjlist[u] = T[1]
end
return SimpleDiGraph(ne, fadjlist, badjlist)
end
"""
path_graph(n)
Create an undirected [path graph](https://en.wikipedia.org/wiki/Path_graph)
with `n` vertices.
# Examples
```jldoctest
julia> using Graphs
julia> path_graph(5)
{5, 4} undirected simple Int64 graph
julia> path_graph(Int8(10))
{10, 9} undirected simple Int8 graph
```
"""
function path_graph(n::T) where {T<:Integer}
n <= 1 && return SimpleGraph(n)
ne = Int(n - 1)
fadjlist = Vector{Vector{T}}(undef, n)
@inbounds fadjlist[1] = T[2]
@inbounds fadjlist[n] = T[n - 1]
@inbounds @simd for u in 2:(n - 1)
fadjlist[u] = T[u - 1, u + 1]
end
return SimpleGraph(ne, fadjlist)
end
"""
path_digraph(n)
Creates a directed [path graph](https://en.wikipedia.org/wiki/Path_graph)
with `n` vertices.
# Examples
```jldoctest
julia> using Graphs
julia> path_digraph(5)
{5, 4} directed simple Int64 graph
julia> path_digraph(Int8(10))
{10, 9} directed simple Int8 graph
```
"""
function path_digraph(n::T) where {T<:Integer}
n <= 1 && return SimpleDiGraph(n)
ne = Int(n - 1)
fadjlist = Vector{Vector{T}}(undef, n)
badjlist = Vector{Vector{T}}(undef, n)
@inbounds fadjlist[1] = T[2]
@inbounds badjlist[1] = T[]
@inbounds fadjlist[n] = T[]
@inbounds badjlist[n] = T[n - 1]
@inbounds @simd for u in 2:(n - 1)
fadjlist[u] = T[u + 1]
badjlist[u] = T[u - 1]
end
return SimpleDiGraph(ne, fadjlist, badjlist)
end
"""
cycle_graph(n)
Create an undirected [cycle graph](https://en.wikipedia.org/wiki/Cycle_graph)
with `n` vertices.
# Examples
```jldoctest
julia> using Graphs
julia> cycle_graph(3)
{3, 3} undirected simple Int64 graph
julia> cycle_graph(Int8(5))
{5, 5} undirected simple Int8 graph
```
"""
function cycle_graph(n::T) where {T<:Integer}
n <= 1 && return SimpleGraph(n)
n == 2 && return SimpleGraph(Edge{T}.([(1, 2)]))
ne = Int(n)
fadjlist = Vector{Vector{T}}(undef, n)
@inbounds fadjlist[1] = T[2, n]
@inbounds fadjlist[n] = T[1, n - 1]
@inbounds @simd for u in 2:(n - 1)
fadjlist[u] = T[u - 1, u + 1]
end
return SimpleGraph(ne, fadjlist)
end
"""
cycle_digraph(n)
Create a directed [cycle graph](https://en.wikipedia.org/wiki/Cycle_graph)
with `n` vertices.
# Examples
```jldoctest
julia> using Graphs
julia> cycle_digraph(3)
{3, 3} directed simple Int64 graph
julia> cycle_digraph(Int8(5))
{5, 5} directed simple Int8 graph
```
"""
function cycle_digraph(n::T) where {T<:Integer}
n <= 1 && return SimpleDiGraph(n)
n == 2 && return SimpleDiGraph(Edge{T}.([(1, 2), (2, 1)]))
ne = Int(n)
fadjlist = Vector{Vector{T}}(undef, n)
badjlist = Vector{Vector{T}}(undef, n)
@inbounds fadjlist[1] = T[2]
@inbounds badjlist[1] = T[n]
@inbounds fadjlist[n] = T[1]
@inbounds badjlist[n] = T[n - 1]
@inbounds @simd for u in 2:(n - 1)
fadjlist[u] = T[u + 1]
badjlist[u] = T[u + -1]
end
return SimpleDiGraph(ne, fadjlist, badjlist)
end
"""
wheel_graph(n)
Create an undirected [wheel graph](https://en.wikipedia.org/wiki/Wheel_graph)
with `n` vertices.
# Examples
```jldoctest
julia> using Graphs
julia> wheel_graph(5)
{5, 8} undirected simple Int64 graph
julia> wheel_graph(Int8(6))
{6, 10} undirected simple Int8 graph
```
"""
function wheel_graph(n::T) where {T<:Integer}
n <= 1 && return SimpleGraph(n)
n <= 3 && return cycle_graph(n)
ne = Int(2 * (n - 1))
fadjlist = Vector{Vector{T}}(undef, n)
@inbounds fadjlist[1] = Vector{T}(2:n)
@inbounds fadjlist[2] = T[1, 3, n]
@inbounds fadjlist[n] = T[1, 2, n - 1]
@inbounds @simd for u in 3:(n - 1)
fadjlist[u] = T[1, u - 1, u + 1]
end
return SimpleGraph(ne, fadjlist)
end
"""
wheel_digraph(n)
Create a directed [wheel graph](https://en.wikipedia.org/wiki/Wheel_graph)
with `n` vertices.
# Examples
```jldoctest
julia> using Graphs
julia> wheel_digraph(5)
{5, 8} directed simple Int64 graph
julia> wheel_digraph(Int8(6))
{6, 10} directed simple Int8 graph
```
"""
function wheel_digraph(n::T) where {T<:Integer}
n <= 2 && return path_digraph(n)
n == 3 && return SimpleDiGraph(Edge{T}.([(1, 2), (1, 3), (2, 3), (3, 2)]))
ne = Int(2 * (n - 1))
fadjlist = Vector{Vector{T}}(undef, n)
badjlist = Vector{Vector{T}}(undef, n)
@inbounds fadjlist[1] = Vector{T}(2:n)
@inbounds badjlist[1] = T[]
@inbounds fadjlist[2] = T[3]
@inbounds badjlist[2] = T[1, n]
@inbounds fadjlist[n] = T[2]
@inbounds badjlist[n] = T[1, n - 1]
@inbounds @simd for u in 3:(n - 1)
fadjlist[u] = T[u + 1]
badjlist[u] = T[1, u - 1]
end
return SimpleDiGraph(ne, fadjlist, badjlist)
end
"""
grid(dims; periodic=false)
Create a ``|dims|``-dimensional cubic lattice, with length `dims[i]`
in dimension `i`.
### Optional Arguments
- `periodic=false`: If true, the resulting lattice will have periodic boundary
condition in each dimension.
# Examples
```jldoctest
julia> using Graphs
julia> grid([2,3])
{6, 7} undirected simple Int64 graph
julia> grid(Int8[2, 2, 2], periodic=true)
{8, 12} undirected simple Int8 graph
julia> grid((2,3))
{6, 7} undirected simple Int64 graph
```
"""
function grid(dims::AbstractVector{T}; periodic=false) where {T<:Integer}
# checks if T is large enough for product(dims)
Tw = widen(T)
n = one(T)
for d in dims
d <= 0 && return SimpleGraph{T}(0)
nw = Tw(n) * Tw(d)
n = T(nw)
end
if periodic
g = cycle_graph(dims[1])
for d in dims[2:end]
g = cartesian_product(cycle_graph(d), g)
end
else
g = path_graph(dims[1])
for d in dims[2:end]
g = cartesian_product(path_graph(d), g)
end
end
return g
end
grid(dims::Tuple; periodic=false) = grid(collect(dims); periodic=periodic)
"""
binary_tree(k::Integer)
Create a [binary tree](https://en.wikipedia.org/wiki/Binary_tree)
of depth `k`.
# Examples
```jldoctest
julia> using Graphs
julia> binary_tree(4)
{15, 14} undirected simple Int64 graph
julia> binary_tree(Int8(5))
{31, 30} undirected simple Int8 graph
```
"""
function binary_tree(k::T) where {T<:Integer}
k <= 0 && return SimpleGraph(0)
k == 1 && return SimpleGraph(1)
if Graphs.isbounded(k) && BigInt(2)^k - 1 > typemax(k)
throw(DomainError(k, "2^k - 1 not representable by type $T"))
end
n = T(2^k - 1)
ne = Int(n - 1)
fadjlist = Vector{Vector{T}}(undef, n)
@inbounds fadjlist[1] = T[2, 3]
@inbounds for i in 1:(k - 2)
@simd for j in (2^i):(2^(i + 1) - 1)
fadjlist[j] = T[j ÷ 2, 2j, 2j + 1]
end
end
i = k - 1
@inbounds @simd for j in (2^i):(2^(i + 1) - 1)
fadjlist[j] = T[j ÷ 2]
end
return SimpleGraph(ne, fadjlist)
end
"""
double_binary_tree(k::Integer)
Create a double complete binary tree with `k` levels.
### References
- Used as an example for spectral clustering by Guattery and Miller 1998.
# Examples
```jldoctest
julia> using Graphs
julia> double_binary_tree(4)
{30, 29} undirected simple Int64 graph
julia> double_binary_tree(Int8(5))
{62, 61} undirected simple Int8 graph
```
"""
function double_binary_tree(k::Integer)
gl = binary_tree(k)
gr = binary_tree(k)
g = blockdiag(gl, gr)
add_edge!(g, 1, nv(gl) + 1)
return g
end
"""
roach_graph(k)
Create a Roach graph of size `k`.
### References
- Guattery and Miller 1998
# Examples
```jldoctest
julia> using Graphs
julia> roach_graph(10)
{40, 48} undirected simple Int64 graph
```
"""
function roach_graph(k::Integer)
dipole = complete_graph(2)
nopole = SimpleGraph(2)
antannae = crosspath(k, nopole)
body = crosspath(k, dipole)
roach = blockdiag(antannae, body)
add_edge!(roach, nv(antannae) - 1, nv(antannae) + 1)
add_edge!(roach, nv(antannae), nv(antannae) + 2)
return roach
end
"""
clique_graph(k, n)
Create a graph consisting of `n` connected `k`-cliques.
# Examples
```jldoctest
julia> using Graphs
julia> clique_graph(4, 10)
{40, 70} undirected simple Int64 graph
julia> clique_graph(Int8(10), Int8(4))
{40, 184} undirected simple Int8 graph
```
"""
function clique_graph(k::T, n::T) where {T<:Integer}
Tw = widen(T)
knw = Tw(k) * Tw(n)
kn = T(knw) # checks if T is large enough for k * n
g = SimpleGraph(kn)
for c in 1:n
for i in ((c - 1) * k + 1):(c * k - 1), j in (i + 1):(c * k)
add_edge!(g, i, j)
end
end
for i in 1:(n - 1)
add_edge!(g, (i - 1) * k + 1, i * k + 1)
end
add_edge!(g, 1, (n - 1) * k + 1)
return g
end
"""
ladder_graph(n)
Create a [ladder graph](https://en.wikipedia.org/wiki/Ladder_graph) consisting of `2n` nodes and `3n-2` edges.
### Implementation Notes
Preserves the eltype of `n`. Will error if the required number of vertices
exceeds the eltype.
# Examples
```jldoctest
julia> using Graphs
julia> ladder_graph(3)
{6, 7} undirected simple Int64 graph
julia> ladder_graph(Int8(4))
{8, 10} undirected simple Int8 graph
```
"""
function ladder_graph(n::T) where {T<:Integer}
n <= 0 && return SimpleGraph{T}(0)
n == 1 && return path_graph(T(2))
Tw = widen(T)
temp = T(Tw(n) + Tw(n)) # test to check if T is large enough
fadjlist = Vector{Vector{T}}(undef, 2 * n)
@inbounds @simd for i in 2:(n - 1)
fadjlist[i] = T[i - 1, i + 1, i + n]
fadjlist[n + i] = T[i, n + i - 1, n + i + 1]
end
fadjlist[1] = T[2, n + 1]
fadjlist[n + 1] = T[1, n + 2]
fadjlist[n] = T[n - 1, 2 * n]
fadjlist[2 * n] = T[n, 2 * n - 1]
return SimpleGraph(3 * n - 2, fadjlist)
end
"""
circular_ladder_graph(n)
Create a [circular ladder graph](https://en.wikipedia.org/wiki/Ladder_graph#Circular_ladder_graph) consisting of `2n` nodes and `3n` edges.
This is also known as the [prism graph](https://en.wikipedia.org/wiki/Prism_graph).
### Implementation Notes
Preserves the eltype of the partitions vector. Will error if the required number of vertices
exceeds the eltype.
`n` must be at least 3 to avoid self-loops and multi-edges.
# Examples
```jldoctest
julia> using Graphs
julia> circular_ladder_graph(3)
{6, 9} undirected simple Int64 graph
julia> circular_ladder_graph(Int8(4))
{8, 12} undirected simple Int8 graph
```
"""
function circular_ladder_graph(n::Integer)
n < 3 && throw(DomainError("n=$n must be at least 3"))
g = ladder_graph(n)
add_edge!(g, 1, n)
add_edge!(g, n + 1, 2 * n)
return g
end
"""
barbell_graph(n1, n2)
Create a [barbell graph](https://en.wikipedia.org/wiki/Barbell_graph) consisting of a clique of size `n1` connected by an edge to a clique of size `n2`.
### Implementation Notes
Preserves the eltype of `n1` and `n2`. Will error if the required number of vertices
exceeds the eltype.
`n1` and `n2` must be at least 1 so that both cliques are non-empty.
The cliques are organized with nodes `1:n1` being the left clique and `n1+1:n1+n2` being the right clique. The cliques are connected by and edge `(n1, n1+1)`.
# Examples
```jldoctest
julia> using Graphs
julia> barbell_graph(3, 4)
{7, 10} undirected simple Int64 graph
julia> barbell_graph(Int8(5), Int8(5))
{10, 21} undirected simple Int8 graph
```
"""
function barbell_graph(n1::T, n2::T) where {T<:Integer}
(n1 < 1 || n2 < 1) && throw(DomainError("n1=$n1 and n2=$n2 must be at least 1"))
n = Base.Checked.checked_add(n1, n2) # check for overflow
fadjlist = Vector{Vector{T}}(undef, n)
ne = Int(n1) * (n1 - 1) ÷ 2 + Int(n2) * (n2 - 1) ÷ 2
@inbounds @simd for u in 1:n1
listu = Vector{T}(undef, n1 - 1)
listu[1:(u - 1)] = 1:(u - 1)
listu[u:(n1 - 1)] = (u + 1):n1
fadjlist[u] = listu
end
@inbounds for u in 1:n2
listu = Vector{T}(undef, n2 - 1)
listu[1:(u - 1)] = (n1 + 1):(n1 + (u - 1))
listu[u:(n2 - 1)] = (n1 + u + 1):(n1 + n2)
fadjlist[n1 + u] = listu
end
g = SimpleGraph(ne, fadjlist)
add_edge!(g, n1, n1 + 1)
return g
end
"""
lollipop_graph(n1, n2)
Create a [lollipop graph](https://en.wikipedia.org/wiki/Lollipop_graph) consisting of a clique of size `n1` connected by an edge to a path of size `n2`.
### Implementation Notes
Preserves the eltype of `n1` and `n2`. Will error if the required number of vertices
exceeds the eltype.
`n1` and `n2` must be at least 1 so that both the clique and the path have at least one vertex.
The graph is organized with nodes `1:n1` being the clique and `n1+1:n1+n2` being the path. The clique is connected to the path by an edge `(n1, n1+1)`.
# Examples
```jldoctest
julia> using Graphs
julia> lollipop_graph(2, 5)
{7, 6} undirected simple Int64 graph
julia> lollipop_graph(Int8(3), Int8(4))
{7, 7} undirected simple Int8 graph
```
"""
function lollipop_graph(n1::T, n2::T) where {T<:Integer}
(n1 < 1 || n2 < 1) && throw(DomainError("n1=$n1 and n2=$n2 must be at least 1"))
if n1 == 1
return path_graph(T(n2 + 1))
elseif n1 > 1 && n2 == 1
g = complete_graph(n1)
add_vertex!(g)
add_edge!(g, n1, n1 + 1)
return g
end
n = Base.Checked.checked_add(n1, n2) # check for overflow
fadjlist = Vector{Vector{T}}(undef, n)
ne = Int(Int(n1) * (n1 - 1) ÷ 2 + n2 - 1)
@inbounds @simd for u in 1:n1
listu = Vector{T}(undef, n1 - 1)
listu[1:(u - 1)] = 1:(u - 1)
listu[u:(n1 - 1)] = (u + 1):n1
fadjlist[u] = listu
end
@inbounds fadjlist[n1 + 1] = T[n1 + 2]
@inbounds fadjlist[n1 + n2] = T[n1 + n2 - 1]
@inbounds @simd for u in (n1 + 2):(n1 + n2 - 1)
fadjlist[u] = T[u - 1, u + 1]
end
g = SimpleGraph(ne, fadjlist)
add_edge!(g, n1, n1 + 1)
return g
end