-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathoper.gi
More file actions
2772 lines (2398 loc) · 82.1 KB
/
oper.gi
File metadata and controls
2772 lines (2398 loc) · 82.1 KB
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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#############################################################################
##
## oper.gi
## Copyright (C) 2014-19 James D. Mitchell
##
## Licensing information can be found in the README file of this package.
##
#############################################################################
##
#############################################################################
# This file is organised as follows:
#
# 1. Adding and removing vertices
# 2. Adding, removing, and reversing edges
# 3. Ways of combining digraphs
# 4. Actions
# 5. Substructures and quotients
# 6. In and out degrees, neighbours, and edges of vertices
# 7. Copies of out/in-neighbours
# 8. IsSomething
# 9. Connectivity
# 10. Operations for vertices
#############################################################################
#############################################################################
# 1. Adding and removing vertices
#############################################################################
InstallMethod(DigraphAddVertex,
"for a mutable digraph by out-neighbours and an object",
[IsMutableDigraph and IsDigraphByOutNeighboursRep, IsObject],
function(D, label)
Add(D!.OutNeighbours, []);
SetDigraphVertexLabel(D, DigraphNrVertices(D), label);
if IsBound(D!.edgelabels) then
Add(D!.edgelabels, []);
fi;
return D;
end);
InstallMethod(DigraphAddVertex, "for a immutable digraph and an object",
[IsImmutableDigraph, IsObject],
{D, label} -> MakeImmutable(DigraphAddVertex(DigraphMutableCopy(D), label)));
InstallMethod(DigraphAddVertex, "for a mutable digraph",
[IsMutableDigraph],
D -> DigraphAddVertex(D, DigraphNrVertices(D) + 1));
InstallMethod(DigraphAddVertex, "for an immutable digraph",
[IsImmutableDigraph],
D -> MakeImmutable(DigraphAddVertex(DigraphMutableCopy(D))));
InstallMethod(DigraphAddVertices, "for a mutable digraph and list",
[IsMutableDigraph, IsList],
function(D, labels)
local label;
for label in labels do
DigraphAddVertex(D, label);
od;
return D;
end);
InstallMethod(DigraphAddVertices, "for an immutable digraph and list",
[IsImmutableDigraph, IsList],
function(D, labels)
if IsEmpty(labels) then
return D;
fi;
return MakeImmutable(DigraphAddVertices(DigraphMutableCopy(D), labels));
end);
InstallMethod(DigraphAddVertices, "for a mutable digraph and an integer",
[IsMutableDigraph, IsInt],
function(D, m)
local N;
if m < 0 then
ErrorNoReturn("the 2nd argument <m> must be a non-negative integer,");
fi;
N := DigraphNrVertices(D);
return DigraphAddVertices(D, [N + 1 .. N + m]);
end);
InstallMethod(DigraphAddVertices, "for an immutable digraph and an integer",
[IsImmutableDigraph, IsInt],
function(D, m)
if m = 0 then
return D;
fi;
return MakeImmutable(DigraphAddVertices(DigraphMutableCopy(D), m));
end);
# Included for backwards compatibility, even though the 2nd arg is redundant.
# See https://github.com/digraphs/Digraphs/issues/264
# This is deliberately kept undocumented.
InstallMethod(DigraphAddVertices, "for a digraph, an integer, and a list",
[IsDigraph, IsInt, IsList],
function(D, m, labels)
if m <> Length(labels) then
ErrorNoReturn("the list <labels> (3rd argument) must have length <m> ",
"(2nd argument),");
fi;
return DigraphAddVertices(D, labels);
end);
InstallMethod(DigraphRemoveVertex,
"for a mutable digraph by out-neighbours and positive integer",
[IsMutableDigraph and IsDigraphByOutNeighboursRep, IsPosInt],
function(D, u)
local pos, w, v;
if u > DigraphNrVertices(D) then
return D;
fi;
RemoveDigraphVertexLabel(D, u);
if IsBound(D!.edgelabels) then
Remove(D!.edgelabels, u);
fi;
Remove(D!.OutNeighbours, u);
for v in DigraphVertices(D) do
pos := 1;
while pos <= Length(D!.OutNeighbours[v]) do
w := D!.OutNeighbours[v][pos];
if w = u then
Remove(D!.OutNeighbours[v], pos);
RemoveDigraphEdgeLabel(D, v, pos);
elif w > u then
D!.OutNeighbours[v][pos] := w - 1;
pos := pos + 1;
else
pos := pos + 1;
fi;
od;
od;
return D;
end);
InstallMethod(DigraphRemoveVertex,
"for an immutable digraph and positive integer",
[IsImmutableDigraph, IsPosInt],
function(D, u)
local newD, weights;
if u > DigraphNrVertices(D) then
return D;
fi;
newD := DigraphImmutableCopyNoWeights(D);
newD := DigraphMutableCopy(newD);
DigraphRemoveVertex(newD, u);
MakeImmutable(newD);
if HasEdgeWeights(D) then
weights := EdgeWeightsMutableCopy(D);
if u <= Length(weights) then
Remove(weights, u);
fi;
SetEdgeWeights(newD, weights);
fi;
return newD;
end);
InstallMethod(DigraphRemoveVertices, "for a mutable digraph and a list",
[IsMutableDigraph, IsList],
function(D, list)
local v;
if not IsDuplicateFreeList(list) or not ForAll(list, IsPosInt) then
ErrorNoReturn("the 2nd argument <list> must be a ",
"duplicate-free list of positive integers,");
elif not IsMutable(list) then
list := ShallowCopy(list);
fi;
# The next line is essential since otherwise removing the 1st node,
# the 2nd node becomes the 1st node, and so removing the 2nd node we
# actually remove the 3rd node instead.
Sort(list, {x, y} -> x > y);
for v in list do
DigraphRemoveVertex(D, v);
od;
return D;
end);
InstallMethod(DigraphRemoveVertices, "for an immutable digraph and a list",
[IsImmutableDigraph, IsList],
{D, list} -> MakeImmutable(DigraphRemoveVertices(DigraphMutableCopy(D), list)));
#############################################################################
# 2. Adding and removing edges
#############################################################################
InstallMethod(DigraphAddEdge,
"for a mutable digraph by out-neighbours, a two positive integers",
[IsMutableDigraph and IsDigraphByOutNeighboursRep, IsPosInt, IsPosInt],
function(D, src, ran)
if not src in DigraphVertices(D)then
ErrorNoReturn("the 2nd argument <src> must be a vertex of the ",
"digraph <D> that is the 1st argument,");
elif not ran in DigraphVertices(D) then
ErrorNoReturn("the 2nd argument <ran> must be a vertex of the ",
"digraph <D> that is the 1st argument,");
fi;
Add(D!.OutNeighbours[src], ran);
if HaveEdgeLabelsBeenAssigned(D) and not IsMultiDigraph(D) then
SetDigraphEdgeLabel(D, src, ran, 1);
fi;
return D;
end);
InstallMethod(DigraphAddEdge,
"for an immutable digraph and two positive integers",
[IsImmutableDigraph, IsPosInt, IsPosInt],
{D, src, ran}
-> MakeImmutable(DigraphAddEdge(DigraphMutableCopy(D), src, ran)));
InstallMethod(DigraphAddEdge, "for a mutable digraph and a list",
[IsMutableDigraph, IsList],
function(D, edge)
if Length(edge) <> 2 then
ErrorNoReturn("the 2nd argument <edge> must be a list of length 2,");
fi;
return DigraphAddEdge(D, edge[1], edge[2]);
end);
InstallMethod(DigraphAddEdge, "for a mutable digraph and a list",
[IsImmutableDigraph, IsList],
{D, edge} -> MakeImmutable(DigraphAddEdge(DigraphMutableCopy(D), edge)));
InstallMethod(DigraphAddEdges, "for a mutable digraph and a list",
[IsMutableDigraph, IsList],
function(D, edges)
local edge;
for edge in edges do
DigraphAddEdge(D, edge);
od;
return D;
end);
InstallMethod(DigraphAddEdges, "for an immutable digraph and a list",
[IsImmutableDigraph, IsList],
{D, edges} -> MakeImmutable(DigraphAddEdges(DigraphMutableCopy(D), edges)));
InstallMethod(DigraphRemoveEdge,
"for a mutable digraph by out-neighbours and two positive integers",
[IsMutableDigraph and IsDigraphByOutNeighboursRep, IsPosInt, IsPosInt],
function(D, src, ran)
local pos;
if IsMultiDigraph(D) then
ErrorNoReturn("the 1st argument <D> must be a digraph with no multiple ",
"edges,");
elif not (IsPosInt(src) and src in DigraphVertices(D)) then
ErrorNoReturn("the 2nd argument <src> must be a vertex of the ",
"digraph <D> that is the 1st argument,");
elif not (IsPosInt(ran) and ran in DigraphVertices(D)) then
ErrorNoReturn("the 3rd argument <ran> must be a vertex of the ",
"digraph <D> that is the 1st argument,");
fi;
pos := Position(D!.OutNeighbours[src], ran);
if pos <> fail then
Remove(D!.OutNeighbours[src], pos);
RemoveDigraphEdgeLabel(D, src, pos);
fi;
return D;
end);
InstallMethod(DigraphRemoveEdge,
"for an immutable digraph and two positive integers",
[IsImmutableDigraph, IsPosInt, IsPosInt],
function(D, src, ran)
local newD, weights, pos, outs;
newD := DigraphImmutableCopyNoWeights(D);
newD := DigraphMutableCopy(newD);
DigraphRemoveEdge(newD, src, ran);
MakeImmutable(newD);
if HasEdgeWeights(D) then
weights := EdgeWeightsMutableCopy(D);
outs := OutNeighbours(D)[src];
pos := Position(outs, ran);
if pos <> fail and pos <= Length(weights[src]) then
Remove(weights[src], pos);
fi;
SetEdgeWeights(newD, weights);
fi;
return newD;
end);
InstallMethod(DigraphRemoveEdge, "for a mutable digraph and a list",
[IsMutableDigraph, IsList],
function(D, edge)
if Length(edge) <> 2 then
ErrorNoReturn("the 2nd argument <edge> must be a list of length 2,");
fi;
return DigraphRemoveEdge(D, edge[1], edge[2]);
end);
InstallMethod(DigraphRemoveEdge,
"for a immutable digraph and a list",
[IsImmutableDigraph, IsList],
{D, edge} -> MakeImmutable(DigraphRemoveEdge(DigraphMutableCopy(D), edge)));
InstallMethod(DigraphRemoveEdges, "for a digraph and a list",
[IsMutableDigraph, IsList],
function(D, edges)
Perform(edges, edge -> DigraphRemoveEdge(D, edge));
return D;
end);
InstallMethod(DigraphRemoveEdges, "for an immutable digraph and a list",
[IsImmutableDigraph, IsList],
{D, edges} -> MakeImmutable(DigraphRemoveEdges(DigraphMutableCopy(D), edges)));
InstallMethod(DigraphReverseEdge,
"for a mutable digraph by out-neighbours and two positive integers",
[IsMutableDigraph and IsDigraphByOutNeighboursRep, IsPosInt, IsPosInt],
function(D, u, v)
local pos;
if IsMultiDigraph(D) then
ErrorNoReturn("the 1st argument <D> must be a digraph with no ",
"multiple edges,");
fi;
pos := Position(D!.OutNeighbours[u], v);
if pos = fail then
ErrorNoReturn("there is no edge from ", u, " to ", v,
" in the digraph <D> that is the 1st argument,");
elif u = v then
return D;
fi;
Remove(D!.OutNeighbours[u], pos);
Add(D!.OutNeighbours[v], u);
if Length(Positions(D!.OutNeighbours[v], u)) > 1 then
# output is a multidigraph
ClearDigraphEdgeLabels(D);
elif IsBound(D!.edgelabels)
and IsBound(D!.edgelabels[u])
and IsBound(D!.edgelabels[u][pos]) then
SetDigraphEdgeLabel(D, v, u, D!.edgelabels[u][pos]);
RemoveDigraphEdgeLabel(D, u, pos);
fi;
return D;
end);
InstallMethod(DigraphReverseEdge,
"for an immutable digraph, positive integer, and positive integer",
[IsImmutableDigraph, IsPosInt, IsPosInt],
{D, u, v} -> MakeImmutable(DigraphReverseEdge(DigraphMutableCopy(D), u, v)));
InstallMethod(DigraphReverseEdge, "for a mutable digraph and list",
[IsMutableDigraph, IsList],
function(D, e)
if Length(e) <> 2 then
ErrorNoReturn("the 2nd argument <e> must be a list of length 2,");
fi;
return DigraphReverseEdge(D, e[1], e[2]);
end);
InstallMethod(DigraphReverseEdge, "for an immutable digraph and a list",
[IsImmutableDigraph, IsList],
{D, e} -> MakeImmutable(DigraphReverseEdge(DigraphMutableCopy(D), e)));
InstallMethod(DigraphReverseEdges, "for a mutable digraph and a list",
[IsMutableDigraph, IsList],
function(D, E)
Perform(E, e -> DigraphReverseEdge(D, e));
return D;
end);
InstallMethod(DigraphReverseEdges, "for an immutable digraph and a list",
[IsImmutableDigraph, IsList],
{D, E} -> MakeImmutable(DigraphReverseEdges(DigraphMutableCopy(D), E)));
InstallMethod(DigraphClosure,
"for a mutable digraph by out-neighbours and a positive integer",
[IsMutableDigraph and IsDigraphByOutNeighboursRep, IsPosInt],
function(D, k)
local list, mat, deg, n, stop, i, j;
if not IsSymmetricDigraph(D) or DigraphHasLoops(D) or IsMultiDigraph(D) then
ErrorNoReturn("the 1st argument <D> must be a symmetric digraph with no ",
"loops, and no multiple edges,");
fi;
list := D!.OutNeighbours;
mat := BooleanAdjacencyMatrixMutableCopy(D);
deg := ShallowCopy(InDegrees(D));
n := DigraphNrVertices(D);
repeat
stop := true;
for i in [1 .. n] do
for j in [1 .. n] do
if j <> i and (not mat[i][j]) and deg[i] + deg[j] >= k then
Add(list[i], j);
Add(list[j], i);
mat[i][j] := true;
mat[j][i] := true;
deg[i] := deg[i] + 1;
deg[j] := deg[j] + 1;
stop := false;
fi;
od;
od;
until stop;
ClearDigraphEdgeLabels(D);
return D;
end);
InstallMethod(DigraphClosure,
"for an immutable digraph by out-neighbours and a positive integer",
[IsImmutableDigraph, IsPosInt],
{D, k} -> MakeImmutable(DigraphClosure(DigraphMutableCopy(D), k)));
DIGRAPHS_CheckContractEdgeDigraph := function(D, u, v)
if not IsDigraphEdge(D, u, v) then # Check if [u, v] is an edge in digraph D
ErrorNoReturn("expected an edge between the 2nd and 3rd arguments ",
"(vertices) ", u, " and ", v, " but found none");
elif IsMultiDigraph(D) then
ErrorNoReturn("The 1st argument (a digraph) must not satisfy ",
"IsMultiDigraph");
elif u = v then # edge should not be contracted if u = v
ErrorNoReturn("The 2nd argument <u> must not be equal to the 3rd ",
"argument <v>");
fi;
end;
InstallMethod(DigraphContractEdge,
"for a mutable digraph and two positive integers",
[IsMutableDigraph, IsPosInt, IsPosInt],
function(D, u, v)
local w, neighbours, transformations_to_edge, transformations_to_old_edges,
old_edge, new_edge, neighbour, t, vertices, edge_label;
DIGRAPHS_CheckContractEdgeDigraph(D, u, v);
# if (v, u) is an edge, disallow loops, so remove (v, u)
if IsDigraphEdge(D, v, u) then
DigraphRemoveEdge(D, v, u);
fi;
# remove the contracted edge
DigraphRemoveEdge(D, u, v);
# Find vertex neighbours of u and v to construct new incident edges of w
neighbours := [Immutable(InNeighboursOfVertex(D, u)),
Immutable(InNeighboursOfVertex(D, v)),
Immutable(OutNeighboursOfVertex(D, u)),
Immutable(OutNeighboursOfVertex(D, v))];
# immutable reference to old edge labels to add to any
# new transformed incident edges
DigraphAddVertex(D, [DigraphVertexLabel(D, u),
DigraphVertexLabel(D, v)]); # add vertex w
vertices := DigraphVertices(D);
w := Length(vertices); # w is the new vertex identifier
# Handle loops from edges u or w, with the same source / range
# No relevant edges are removed from D until vertices are removed
# So old edge labls are taken from D
if IsDigraphEdge(D, u, u) and IsDigraphEdge(D, v, v) then
DigraphAddEdge(D, w, w);
edge_label := [DigraphEdgeLabel(D, u, u), DigraphEdgeLabel(D, w, w)];
SetDigraphEdgeLabel(D, w, w, edge_label);
elif IsDigraphEdge(D, u, u) then
DigraphAddEdge(D, w, w);
edge_label := DigraphEdgeLabel(D, u, u);
SetDigraphEdgeLabel(D, w, w, edge_label);
elif IsDigraphEdge(D, v, v) then
DigraphAddEdge(D, w, w);
edge_label := DigraphEdgeLabel(D, v, v);
SetDigraphEdgeLabel(D, w, w, edge_label);
fi;
# translate edges based on neighbours
# transformation functions translating neighbours to their new edge
transformations_to_edge := [{x, y} -> [x, y],
{x, y} -> [x, y],
{x, y} -> [y, x],
{x, y} -> [y, x]];
# transformation functions translating neighbours
# to their original edge in D
transformations_to_old_edges := [e -> [e, u],
e -> [e, v],
e -> [u, e],
e -> [v, e]];
# Add translated new edges, and setup edge labels
for t in [1 .. Length(transformations_to_edge)] do
for neighbour in neighbours[t] do
new_edge := transformations_to_edge[t](neighbour, w);
old_edge := transformations_to_old_edges[t](neighbour);
edge_label := DigraphEdgeLabel(D, old_edge[1], old_edge[2]);
DigraphAddEdge(D, new_edge);
SetDigraphEdgeLabel(D, new_edge[1], new_edge[2], edge_label);
od;
od;
DigraphRemoveVertices(D, [u, v]); # remove the vertices of the
# contracted edge (and therefore,
# all its incident edges)
return D;
end);
InstallMethod(DigraphContractEdge,
"for an immutable digraph and two positive integers",
[IsImmutableDigraph, IsPosInt, IsPosInt],
function(D, u, v)
local w, neighbours, transformations_to_edge, transformations_to_old_edges,
existing_edges, edges_to_not_include, new_digraph, new_edge, old_edge,
neighbour, t, vertices, edge_label;
DIGRAPHS_CheckContractEdgeDigraph(D, u, v);
# Incident edges to [u, v] that will be transformed to be incident to w
edges_to_not_include := [];
existing_edges := []; # existing edges that should be re added
# contracted edge should not be added to the new digraph
new_digraph := NullDigraph(IsMutableDigraph, 0);
# if (v, u) is an edge, disallow loops, so remove (v, u)
if IsDigraphEdge(D, v, u) then
D := DigraphRemoveEdge(D, v, u);
fi;
D := DigraphRemoveEdge(D, u, v); # remove the edge to be contracted
DigraphAddVertices(new_digraph, DigraphVertexLabels(D));
# add vertex w with combined labels from u and v
DigraphAddVertex(new_digraph, [DigraphVertexLabel(D, u),
DigraphVertexLabel(D, v)]); # add vertex w
vertices := DigraphVertices(new_digraph);
w := Length(vertices); # w is the new vertex identifier
# Handle loops from edges u or w, with the same source / range
if IsDigraphEdge(D, u, u) and IsDigraphEdge(D, v, v) then
DigraphAddEdge(new_digraph, w, w);
SetDigraphEdgeLabel(new_digraph, w, w, [DigraphEdgeLabel(D, u, u),
DigraphEdgeLabel(D, v, v)]);
elif IsDigraphEdge(D, u, u) then
DigraphAddEdge(new_digraph, w, w);
SetDigraphEdgeLabel(new_digraph, w, w,
DigraphEdgeLabel(D, u, u));
elif IsDigraphEdge(D, v, v) then
DigraphAddEdge(new_digraph, w, w);
SetDigraphEdgeLabel(new_digraph, w, w, DigraphEdgeLabel(D, v, v));
fi;
# if there were loops in D at the vertices u or w, don't include these edges,
# but include a new loop at w
Append(edges_to_not_include, [[u, u], [v, v]]);
# Find vertex neighbours of u and v to construct new incident edges of w
neighbours := [InNeighboursOfVertex(D, u),
InNeighboursOfVertex(D, v),
OutNeighboursOfVertex(D, u),
OutNeighboursOfVertex(D, v)];
# translate edges based on neighbours
# transformation functions translating neighbours to their new edge
transformations_to_edge := [{x, y} -> [x, y], {x, y} -> [x, y],
{x, y} -> [y, x], {x, y} -> [y, x]];
# transformation functions translating neighbours to their original edge in D
transformations_to_old_edges := [e -> [e, u],
e -> [e, v],
e -> [u, e],
e -> [v, e]];
# remove edges that will be adjusted
for t in [1 .. Length(transformations_to_old_edges)] do
Append(edges_to_not_include, List(neighbours[t],
transformations_to_old_edges[t]));
od;
# Find edges that should be included, but remain the same
Sort(edges_to_not_include);
Append(existing_edges, ShallowCopy(DigraphEdges(D)));
Sort(existing_edges);
SubtractSet(existing_edges, edges_to_not_include);
# Add translated new edges, and setup edge labels
for t in [1 .. Length(transformations_to_edge)] do
for neighbour in neighbours[t] do
new_edge := transformations_to_edge[t](neighbour, w);
# old_edge is what the new transformed edge was previously
old_edge := transformations_to_old_edges[t](neighbour);
edge_label := DigraphEdgeLabel(D, old_edge[1], old_edge[2]);
new_digraph := DigraphAddEdge(new_digraph, new_edge);
SetDigraphEdgeLabel(new_digraph, new_edge[1], new_edge[2], edge_label);
od;
od;
# Add the existing edges that have not changed,
# and set their edge labels to that of the previous digraph
for new_edge in existing_edges do
new_digraph := DigraphAddEdge(new_digraph, new_edge);
SetDigraphEdgeLabel(new_digraph, new_edge[1], new_edge[2],
DigraphEdgeLabel(D, new_edge[1], new_edge[2]));
od;
# remove the vertices of the contracted edge
return MakeImmutable(DigraphRemoveVertices(new_digraph, [u, v]));
end);
InstallMethod(DigraphContractEdge,
"for a digraph and a dense list",
[IsDigraph, IsDenseList],
function(D, edge)
if Length(edge) <> 2 then
ErrorNoReturn("the 2nd argument <edge> must be a list of length 2");
fi;
return DigraphContractEdge(D, edge[1], edge[2]);
end);
#############################################################################
# 3. Ways of combining digraphs
#############################################################################
InstallGlobalFunction(DIGRAPHS_CombinationOperProcessArgs,
function(arg...)
local copy, i;
arg := ShallowCopy(arg[1]);
if IsMutableDigraph(arg[1]) then
for i in [2 .. Length(arg)] do
if IsIdenticalObj(arg[1], arg[i]) then
if not IsBound(copy) then
copy := OutNeighboursMutableCopy(arg[1]);
fi;
arg[i] := copy;
else
arg[i] := OutNeighbours(arg[i]);
fi;
od;
arg[1] := arg[1]!.OutNeighbours;
else
arg[1] := OutNeighboursMutableCopy(arg[1]);
for i in [2 .. Length(arg)] do
arg[i] := OutNeighbours(arg[i]);
od;
fi;
return arg;
end);
InstallGlobalFunction(DigraphDisjointUnion,
function(arg...)
local D, offset, n, i, j;
# Allow the possibility of supplying arguments in a list.
if Length(arg) = 1 and IsList(arg[1]) then
arg := arg[1];
fi;
if not IsList(arg) or IsEmpty(arg)
or not ForAll(arg, IsDigraphByOutNeighboursRep) then
ErrorNoReturn("the arguments must be digraphs by out-neighbours, or a ",
"single non-empty list of digraphs by out-neighbours,");
fi;
D := arg[1];
arg := DIGRAPHS_CombinationOperProcessArgs(arg);
offset := DigraphNrVertices(D);
for i in [2 .. Length(arg)] do
n := Length(arg[i]);
for j in [1 .. n] do
arg[1][offset + j] := ShallowCopy(arg[i][j]) + offset;
od;
offset := offset + n;
od;
if IsMutableDigraph(D) then
SetDigraphVertexLabels(D, DigraphVertices(D));
# This above stops D keeping its old vertex labels after being changed
ClearDigraphEdgeLabels(D);
return D;
fi;
return ConvertToImmutableDigraphNC(arg[1]);
end);
InstallGlobalFunction(DigraphJoin,
function(arg...)
local D, tot, offset, n, list, i, v;
# Allow the possibility of supplying arguments in a list.
if Length(arg) = 1 and IsList(arg[1]) then
arg := arg[1];
fi;
if not IsList(arg) or IsEmpty(arg)
or not ForAll(arg, IsDigraphByOutNeighboursRep) then
ErrorNoReturn("the arguments must be digraphs by out-neighbours, or a ",
"single list of digraphs by out-neighbours,");
fi;
D := arg[1];
tot := Sum(arg, DigraphNrVertices);
offset := DigraphNrVertices(D);
arg := DIGRAPHS_CombinationOperProcessArgs(arg);
for list in arg[1] do
Append(list, [offset + 1 .. tot]);
od;
for i in [2 .. Length(arg)] do
n := Length(arg[i]);
for v in [1 .. n] do
arg[1][v + offset] := Concatenation([1 .. offset],
arg[i][v] + offset,
[offset + n + 1 .. tot]);
od;
offset := offset + n;
od;
if IsMutableDigraph(D) then
ClearDigraphEdgeLabels(D);
return D;
fi;
return ConvertToImmutableDigraphNC(arg[1]);
end);
InstallGlobalFunction(DigraphEdgeUnion,
function(arg...)
local D, n, i, v;
# Allow the possibility of supplying arguments in a list.
if Length(arg) = 1 and IsList(arg[1]) then
arg := arg[1];
fi;
if not IsList(arg) or IsEmpty(arg)
or not ForAll(arg, IsDigraphByOutNeighboursRep) then
ErrorNoReturn("the arguments must be digraphs by out-neighbours, or a ",
"single list of digraphs by out-neighbours,");
fi;
D := arg[1];
n := Maximum(List(arg, DigraphNrVertices));
arg := DIGRAPHS_CombinationOperProcessArgs(arg);
if IsMutableDigraph(D) then
DigraphAddVertices(D, n - DigraphNrVertices(D));
else
Append(arg[1], List([1 .. n - Length(arg[1])], x -> []));
fi;
for i in [2 .. Length(arg)] do
for v in [1 .. Length(arg[i])] do
if not IsEmpty(arg[i][v]) then
Append(arg[1][v], arg[i][v]);
fi;
od;
od;
if IsMutableDigraph(D) then
ClearDigraphEdgeLabels(D);
ClearDigraphVertexLabels(D);
return D;
fi;
return ConvertToImmutableDigraphNC(arg[1]);
end);
InstallGlobalFunction(DigraphCartesianProduct,
function(arg...)
local D, n, i, j, proj, m, labs;
# Allow the possibility of supplying arguments in a list.
if Length(arg) = 1 and IsList(arg[1]) then
arg := arg[1];
fi;
if not IsList(arg) or IsEmpty(arg)
or not ForAll(arg, IsDigraphByOutNeighboursRep) then
ErrorNoReturn("the arguments must be digraphs by out-neighbours, or a ",
"single list of digraphs by out-neighbours,");
fi;
labs := List(Cartesian(Reversed(List(arg, DigraphVertexLabels))), Reversed);
D := arg[1];
arg := DIGRAPHS_CombinationOperProcessArgs(arg);
m := Product(List(arg, Length));
proj := [Transformation([1 .. m], x -> RemInt(x - 1, Length(arg[1])) + 1)];
for i in [2 .. Length(arg)] do
n := Length(arg[1]);
Add(proj, Transformation([1 .. m],
x -> RemInt(QuoInt(x - 1, n), Length(arg[i])) * n + 1));
for j in [2 .. Length(arg[i])] do
arg[1]{[1 + n * (j - 1) .. n * j]} := List([1 .. n],
x -> Concatenation(arg[1][x] + n * (j - 1),
x + n * (arg[i][j] - 1)));
od;
for j in [1 .. n] do
Append(arg[1][j], j + n * (arg[i][1] - 1));
od;
od;
if IsMutableDigraph(D) then
ClearDigraphEdgeLabels(D);
else
D := DigraphNC(arg[1]);
fi;
SetDigraphCartesianProductProjections(D, proj);
SetDigraphVertexLabels(D, labs);
return D;
end);
InstallGlobalFunction(DigraphDirectProduct,
function(arg...)
local D, n, i, j, proj, m, labs;
# Allow the possibility of supplying arguments in a list.
if Length(arg) = 1 and IsList(arg[1]) then
arg := arg[1];
fi;
if not IsList(arg) or IsEmpty(arg)
or not ForAll(arg, IsDigraphByOutNeighboursRep) then
ErrorNoReturn("the arguments must be digraphs by out-neighbours, or a ",
"single list of digraphs by out-neighbours,");
fi;
labs := List(Cartesian(Reversed(List(arg, DigraphVertexLabels))), Reversed);
D := arg[1];
arg := DIGRAPHS_CombinationOperProcessArgs(arg);
m := Product(List(arg, Length));
proj := [Transformation([1 .. m], x -> RemInt(x - 1, Length(arg[1])) + 1)];
for i in [2 .. Length(arg)] do
n := Length(arg[1]);
Add(proj, Transformation([1 .. m],
x -> RemInt(QuoInt(x - 1, n), Length(arg[i])) * n + 1));
for j in [2 .. Length(arg[i])] do
arg[1]{[1 + n * (j - 1) .. n * j]} := List([1 .. n],
x -> List(Cartesian(arg[1][x], n * (arg[i][j] - 1)), Sum));
od;
for j in [1 .. n] do
arg[1][j] := List(Cartesian(arg[1][j], n * (arg[i][1] - 1)), Sum);
od;
od;
if IsMutableDigraph(D) then
ClearDigraphEdgeLabels(D);
else
D := DigraphNC(arg[1]);
fi;
SetDigraphDirectProductProjections(D, proj);
SetDigraphVertexLabels(D, labs);
return D;
end);
InstallMethod(ModularProduct, "for a digraph and digraph",
[IsDigraph, IsDigraph],
function(D1, D2)
local edge_function;
edge_function := function(u, v, m, n, map) # gaplint: disable=W046
# neither m nor n is used, but can't replace them both with _
local w, x, connections;
connections := [];
for w in OutNeighbours(D1)[u] do
for x in OutNeighbours(D2)[v] do
if (u = w) = (v = x) then
Add(connections, map(w, x));
fi;
od;
od;
for w in OutNeighbours(DigraphDual(D1))[u] do
for x in OutNeighbours(DigraphDual(D2))[v] do
if (u = w) = (v = x) then
Add(connections, map(w, x));
fi;
od;
od;
return connections;
end;
return DIGRAPHS_GraphProduct(D1, D2, edge_function);
end);
InstallMethod(StrongProduct, "for a digraph and digraph",
[IsDigraph, IsDigraph],
function(D1, D2)
local edge_function;
edge_function := function(u, v, m, n, map) # gaplint: disable=W046
# neither m nor n is used, but can't replace them both with _
local w, x, connections;
connections := [];
for x in OutNeighbours(D2)[v] do
AddSet(connections, map(u, x));
for w in OutNeighbours(D1)[u] do
AddSet(connections, map(w, x));
od;
od;
for w in OutNeighbours(D1)[u] do
AddSet(connections, map(w, v));
od;
return connections;
end;
return DIGRAPHS_GraphProduct(D1, D2, edge_function);
end);
InstallMethod(ConormalProduct, "for a digraph and digraph",
[IsDigraph, IsDigraph],
function(D1, D2)
local edge_function;
edge_function := function(u, v, m, n, map)
local w, x, connections;
connections := [];
for w in OutNeighbours(D1)[u] do
for x in [1 .. n] do
AddSet(connections, map(w, x));
od;
od;
for x in OutNeighbours(D2)[v] do
for w in [1 .. m] do
AddSet(connections, map(w, x));
od;
od;
return connections;
end;
return DIGRAPHS_GraphProduct(D1, D2, edge_function);
end);
InstallMethod(HomomorphicProduct, "for a digraph and digraph",
[IsDigraph, IsDigraph],
function(D1, D2)
local edge_function;
edge_function := function(u, v, _, n, map)
local w, x, connections;
connections := [];
for x in [1 .. n] do
AddSet(connections, map(u, x));
od;
for w in OutNeighbours(D1)[u] do
for x in OutNeighbours(DigraphDual(D2))[v] do
AddSet(connections, map(w, x));
od;
od;
return connections;
end;
return DIGRAPHS_GraphProduct(D1, D2, edge_function);
end);
InstallMethod(LexicographicProduct, "for a digraph and digraph",
[IsDigraph, IsDigraph],
function(D1, D2)
local edge_function;
edge_function := function(u, v, _, n, map)
local w, x, connections;
connections := [];
for w in OutNeighbours(D1)[u] do
for x in [1 .. n] do
AddSet(connections, map(w, x));
od;
od;
for x in OutNeighbours(D2)[v] do
AddSet(connections, map(u, x));
od;
return connections;
end;
return DIGRAPHS_GraphProduct(D1, D2, edge_function);
end);
InstallMethod(DIGRAPHS_GraphProduct,