-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathdigraph.gi
More file actions
1174 lines (1041 loc) · 36.8 KB
/
Copy pathdigraph.gi
File metadata and controls
1174 lines (1041 loc) · 36.8 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
#############################################################################
##
## digraph.gi
## Copyright (C) 2014 James D. Mitchell
##
## Licensing information can be found in the README file of this package.
##
#############################################################################
##
########################################################################
# This file is organised as follows:
#
# 0. IsValidDigraph
# 1. Types
# 2. Digraph no-check constructors
# 3. Digraph copies
# 4. MakeImmutableDigraph
# 5. Digraph constructors
# 6. Printing, viewing, strings
# 7. Operators
# 8. Digraph by-something constructors
# 9. Converters to/from other types -> digraph
# 10. Random digraphs
#
########################################################################
########################################################################
# 0. IsValidDigraph
########################################################################
InstallGlobalFunction(IsValidDigraph,
function(arg)
local D;
for D in arg do
if not (IsMutableDigraph(D) or IsImmutableDigraph(D)) then
ErrorNoReturn("digraph in an invalid state! Did you return a ",
"mutable digraph from a method for an attribute, ",
"or MakeImmutable(a mutable digraph)??");
fi;
od;
end);
########################################################################
# 1. Digraph types
########################################################################
BindGlobal("DenseDigraphType", NewType(DigraphFamily,
IsDenseDigraphRep));
########################################################################
# 2. Digraph no-check constructors
########################################################################
InstallMethod(ConvertToMutableDigraphNC, "for a record", [IsRecord],
function(record)
local D;
Assert(1, IsBound(record.OutNeighbours));
Assert(1, Length(NamesOfComponents(record)) = 1);
D := Objectify(DenseDigraphType, record);
SetFilterObj(D, IsMutable);
return D;
end);
InstallMethod(ConvertToMutableDigraphNC, "for a dense list of out-neighbours",
[IsDenseList],
function(list)
local record;
record := rec(OutNeighbours := list);
Perform(record.OutNeighbours, IsSet);
return ConvertToMutableDigraphNC(record);
end);
InstallMethod(ConvertToImmutableDigraphNC, "for a record", [IsRecord],
function(record)
return MakeImmutableDigraph(ConvertToMutableDigraphNC(record));
end);
InstallMethod(ConvertToImmutableDigraphNC,
"for a dense list of out-neighbours",
[IsDenseList],
function(list)
return MakeImmutableDigraph(ConvertToMutableDigraphNC(list));
end);
InstallMethod(DigraphConsNC, "for IsMutableDigraph and a record",
[IsMutableDigraph, IsRecord],
function(filt, record)
local out;
Assert(1, IsBound(record.DigraphNrVertices));
Assert(1, IsBound(record.DigraphRange));
Assert(1, IsBound(record.DigraphSource));
out := DIGRAPH_OUT_NEIGHBOURS_FROM_SOURCE_RANGE(record.DigraphNrVertices,
record.DigraphSource,
record.DigraphRange);
return ConvertToMutableDigraphNC(out);
end);
InstallMethod(DigraphConsNC,
"for IsMutableDigraph and a dense list of out-neighbours",
[IsMutableDigraph, IsDenseList],
function(filt, list)
Assert(1, not IsMutable(list));
return ConvertToMutableDigraphNC(List(list, ShallowCopy));
end);
InstallMethod(DigraphConsNC,
"for IsMutableDigraph and a dense mutable list of out-neighbours",
[IsMutableDigraph, IsDenseList and IsMutable],
function(filt, list)
return ConvertToMutableDigraphNC(StructuralCopy(list));
end);
InstallMethod(DigraphConsNC, "for IsImmutableDigraph and a record",
[IsImmutableDigraph, IsRecord],
function(filt, record)
local D, nm;
Assert(1, IsBound(record.DigraphNrVertices));
Assert(1, IsBound(record.DigraphRange));
Assert(1, IsBound(record.DigraphSource));
for nm in RecNames(record) do
if not nm in ["DigraphRange", "DigraphSource", "DigraphNrVertices"] then
Info(InfoWarning, 1, "ignoring record component \"", nm, "\"!");
fi;
od;
D := MakeImmutableDigraph(DigraphConsNC(IsMutableDigraph, record));
SetDigraphSource(D, StructuralCopy(record.DigraphSource));
SetDigraphRange(D, StructuralCopy(record.DigraphRange));
return D;
end);
InstallMethod(DigraphConsNC,
"for IsImmutableDigraph and a dense list of adjacencies",
[IsImmutableDigraph, IsDenseList],
function(filt, list)
return MakeImmutableDigraph(DigraphConsNC(IsMutableDigraph, list));
end);
InstallMethod(DigraphConsNC,
"for IsVertexColoredDigraph, object, and dense list of colours",
[IsVertexColoredDigraph, IsObject, IsDenseList],
function(filt, obj, colors)
local D;
D := DigraphConsNC(IsImmutableDigraph, obj);
SetFilterObj(D, IsVertexColoredDigraph);
SetDigraphVertexColors(D, colors);
return D;
end);
InstallGlobalFunction(DigraphNC,
function(arg)
if Length(arg) = 0 then
ErrorNoReturn("there must be at least 1 argument,");
elif not IsFilter(arg[1]) then
CopyListEntries(arg, 1, 1, arg, 2, 1, Length(arg));
arg[1] := IsImmutableDigraph;
fi;
return CallFuncList(DigraphConsNC, arg);
end);
########################################################################
# 3. Digraph copies
########################################################################
InstallMethod(DigraphMutableCopy, "for a dense digraph", [IsDenseDigraphRep],
function(D)
local copy;
IsValidDigraph(D);
copy := ConvertToMutableDigraphNC(OutNeighboursMutableCopy(D));
SetDigraphVertexLabels(copy, StructuralCopy(DigraphVertexLabels(D)));
SetDigraphEdgeLabelsNC(copy, StructuralCopy(DigraphEdgeLabelsNC(D)));
return copy;
end);
InstallMethod(DigraphCopy, "for a dense digraph", [IsDenseDigraphRep],
function(D)
local copy;
IsValidDigraph(D);
copy := ConvertToImmutableDigraphNC(OutNeighboursMutableCopy(D));
SetDigraphVertexLabels(copy, StructuralCopy(DigraphVertexLabels(D)));
SetDigraphEdgeLabelsNC(copy, StructuralCopy(DigraphEdgeLabelsNC(D)));
return copy;
end);
InstallMethod(DigraphCopy, "for a dense vertex-colored digraph",
[IsDenseDigraphRep and IsVertexColoredDigraph],
function(D)
local copy;
IsValidDigraph(D);
copy := ConvertToImmutableDigraphNC(OutNeighboursMutableCopy(D));
SetDigraphVertexLabels(copy, StructuralCopy(DigraphVertexLabels(D)));
SetDigraphEdgeLabelsNC(copy, StructuralCopy(DigraphEdgeLabelsNC(D)));
SetFilterObj(copy, IsVertexColoredDigraph);
SetDigraphVertexColors(copy, DigraphVertexColors(D));
return copy;
end);
InstallMethod(DigraphCopyIfMutable, "for a mutable digraph",
[IsMutableDigraph], DigraphMutableCopy);
InstallMethod(DigraphCopyIfMutable, "for an immutable digraph",
[IsImmutableDigraph], IdFunc);
InstallMethod(DigraphCopyIfImmutable, "for a mutable digraph",
[IsMutableDigraph], IdFunc);
InstallMethod(DigraphCopyIfImmutable, "for an immutable digraph",
[IsImmutableDigraph], DigraphCopy);
########################################################################
# 4. MakeImmutableDigraph
########################################################################
InstallMethod(MakeImmutableDigraph, "for a mutable dense digraph",
[IsMutableDigraph and IsDenseDigraphRep],
function(D)
MakeImmutable(D);
SetFilterObj(D, IsAttributeStoringRep);
SetFilterObj(D, IsImmutableDigraph);
MakeImmutable(D!.OutNeighbours);
return D;
end);
InstallMethod(MakeVertexColoredDigraph,
"for an immutable digraph and a dense list",
[IsImmutableDigraph, IsDenseList],
function(D, colors)
colors := DIGRAPHS_ValidateVertexColouring(colors);
SetDigraphVertexColors(D, colors);
SetFilterObj(D, IsVertexColoredDigraph);
return D;
end);
########################################################################
# 5. Digraph constructors
########################################################################
InstallMethod(DigraphCons, "for IsMutableDigraph and a record",
[IsMutableDigraph, IsRecord],
function(filt, record)
local D, cmp, labels, i;
if IsGraph(record) then
# IsGraph is a function not a filter, so we cannot have a separate method
D := DigraphNC(IsMutableDigraph,
List(Vertices(record), x -> Adjacency(record, x)));
if IsBound(record.names) then
SetDigraphVertexLabels(D, StructuralCopy(record.names));
fi;
return D;
fi;
if not (IsBound(record.DigraphSource)
and IsBound(record.DigraphRange)
and (IsBound(record.DigraphVertices) or
IsBound(record.DigraphNrVertices))) then
ErrorNoReturn("the argument <record> must be a record with components ",
"'DigraphSource', 'DigraphRange', and either ",
"'DigraphVertices' or 'DigraphNrVertices' (but not both),");
elif not IsList(record.DigraphSource)
or not IsList(record.DigraphRange) then
ErrorNoReturn("the record components 'DigraphSource' and 'DigraphRange' ",
"must be lists,");
elif Length(record.DigraphSource) <> Length(record.DigraphRange) then
ErrorNoReturn("the record components 'DigraphSource' and 'DigraphRange' ",
"must have equal length,");
elif IsBound(record.DigraphVertices)
and IsBound(record.DigraphNrVertices) then
ErrorNoReturn("the record must only have one of the components ",
"'DigraphVertices' and 'DigraphNrVertices', not both,");
fi;
if IsBound(record.DigraphNrVertices) then
if (not IsInt(record.DigraphNrVertices))
or record.DigraphNrVertices < 0 then
ErrorNoReturn("the record component 'DigraphNrVertices' ",
"must be a non-negative integer,");
fi;
cmp := x -> IsPosInt(x) and x < record.DigraphNrVertices + 1;
else
Assert(1, IsBound(record.DigraphVertices));
if not IsList(record.DigraphVertices) then
ErrorNoReturn("the record component 'DigraphVertices' must be a list,");
elif not IsDuplicateFreeList(record.DigraphVertices) then
ErrorNoReturn("the record component 'DigraphVertices' must be ",
"duplicate-free,");
fi;
cmp := x -> x in record.DigraphVertices;
record.DigraphNrVertices := Length(record.DigraphVertices);
fi;
if not ForAll(record.DigraphSource, x -> cmp(x)) then
ErrorNoReturn("the record component 'DigraphSource' is invalid,");
elif not ForAll(record.DigraphRange, x -> cmp(x)) then
ErrorNoReturn("the record component 'DigraphRange' is invalid,");
fi;
record := StructuralCopy(record);
# Rewrite the vertices to numbers
if IsBound(record.DigraphVertices) then
if record.DigraphVertices <> [1 .. record.DigraphNrVertices] then
for i in [1 .. Length(record.DigraphSource)] do
record.DigraphRange[i] := Position(record.DigraphVertices,
record.DigraphRange[i]);
record.DigraphSource[i] := Position(record.DigraphVertices,
record.DigraphSource[i]);
od;
labels := record.DigraphVertices;
Unbind(record.DigraphVertices);
fi;
fi;
record.DigraphRange := Permuted(record.DigraphRange,
Sortex(record.DigraphSource));
D := DigraphNC(IsMutableDigraph, record);
if IsBound(labels) then
SetDigraphVertexLabels(D, labels);
fi;
return D;
end);
InstallMethod(DigraphCons,
"for IsMutableDigraph and a dense list of out-neighbours",
[IsMutableDigraph, IsDenseList],
function(filt, list)
local sublist, v;
for sublist in list do
if not IsHomogeneousList(sublist) then
ErrorNoReturn("the argument <list> must be a list of lists of positive ",
"integers not exceeding the length of the argument,");
fi;
for v in sublist do
if not IsPosInt(v) or v > Length(list) then
ErrorNoReturn("the argument <list> must be a list of lists of ",
"positive integers not exceeding the length of ",
"the argument,");
fi;
od;
od;
return DigraphNC(IsMutableDigraph, list);
end);
InstallMethod(DigraphCons, "for IsMutableDigraph, a list, and function",
[IsMutableDigraph, IsList, IsFunction],
function(filt, list, func)
local N, out, x, i, j;
N := Size(list); # number of vertices
out := List([1 .. N], x -> []);
for i in [1 .. N] do
x := list[i];
for j in [1 .. N] do
if func(x, list[j]) then
Add(out[i], j);
fi;
od;
od;
return ConvertToMutableDigraphNC(out);
end);
InstallMethod(DigraphCons,
"for IsMutableDigraph, a number of vertices, source, and range",
[IsMutableDigraph, IsInt, IsList, IsList],
function(filt, N, src, ran)
return DigraphCons(IsMutableDigraph, rec(DigraphNrVertices := N,
DigraphSource := src,
DigraphRange := ran));
end);
InstallMethod(DigraphCons,
"for IsMutableDigraph, a list of vertices, source, and range",
[IsMutableDigraph, IsList, IsList, IsList],
function(filt, domain, src, ran)
local D;
D := DigraphCons(IsMutableDigraph, rec(DigraphVertices := domain,
DigraphSource := src,
DigraphRange := ran));
SetDigraphVertexLabels(D, domain);
return D;
end);
InstallMethod(DigraphCons, "for IsImmutableDigraph and a record",
[IsImmutableDigraph, IsRecord],
function(filt, record)
local D;
D := MakeImmutableDigraph(DigraphCons(IsMutableDigraph, record));
if IsGraph(record) then
# IsGraph is a function not a filter, so we cannot have a separate method
# for this.
if not IsTrivial(record.group) then
Assert(1, IsPermGroup(record.group));
SetDigraphGroup(D, record.group);
SetDigraphSchreierVector(D, record.schreierVector);
SetRepresentativeOutNeighbours(D, record.adjacencies);
if IsBound(record.colourClasses) then
MakeVertexColoredDigraph(D, record.colourClasses);
fi;
fi;
else
SetDigraphNrEdges(D, Length(record.DigraphSource));
fi;
return D;
end);
InstallMethod(DigraphCons,
"for IsImmutableDigraph and a dense list of out-neighbours",
[IsImmutableDigraph, IsDenseList],
function(filt, list)
return MakeImmutableDigraph(DigraphCons(IsMutableDigraph, list));
end);
InstallMethod(DigraphCons, "for IsImmutableDigraph, a list, and function",
[IsImmutableDigraph, IsList, IsFunction],
function(filt, list, func)
local D;
D := MakeImmutableDigraph(DigraphCons(IsMutableDigraph, list, func));
SetDigraphAdjacencyFunction(D, {u, v} -> func(list[u], list[v]));
SetFilterObj(D, IsDigraphWithAdjacencyFunction);
SetDigraphVertexLabels(D, list);
return D;
end);
InstallMethod(DigraphCons,
"for IsImmutableDigraph, a number of vertices, source, and range",
[IsImmutableDigraph, IsInt, IsList, IsList],
function(filt, N, src, ran)
return MakeImmutableDigraph(DigraphCons(IsMutableDigraph, N, src, ran));
end);
InstallMethod(DigraphCons,
"for IsImmutableDigraph, a list of vertices, source, and range",
[IsImmutableDigraph, IsList, IsList, IsList],
function(filt, domain, src, ran)
return MakeImmutableDigraph(DigraphCons(IsMutableDigraph, domain, src, ran));
end);
InstallMethod(DigraphCons,
"for IsVertexColoredDigraph, an object, and a dense list",
[IsVertexColoredDigraph, IsObject, IsDenseList],
function(filt, obj, colors)
return MakeVertexColoredDigraph(DigraphCons(IsImmutableDigraph, obj),
colors);
end);
InstallMethod(DigraphCons,
"for IsVertexColoredDigraph, a list, a function, and a dense list",
[IsVertexColoredDigraph, IsList, IsFunction, IsDenseList],
function(filt, list, func, colors)
return MakeVertexColoredDigraph(DigraphCons(IsImmutableDigraph, list, func),
colors);
end);
InstallMethod(DigraphCons,
"for IsVertexColoredDigraph, an integer, a list, a list, and a dense list",
[IsVertexColoredDigraph, IsInt, IsList, IsList, IsDenseList],
function(filt, n, src, ran, colors)
return MakeVertexColoredDigraph(DigraphCons(IsImmutableDigraph, n, src, ran),
colors);
end);
InstallMethod(DigraphCons,
"for IsVertexColoredDigraph, a list, a list, a list, and a dense list",
[IsVertexColoredDigraph, IsInt, IsList, IsList, IsDenseList],
function(filt, dom, src, ran, colors)
return MakeVertexColoredDigraph(DigraphCons(IsImmutableDigraph,
dom,
src,
ran),
colors);
end);
InstallGlobalFunction(Digraph,
function(arg)
if Length(arg) = 0 then
ErrorNoReturn("there must be at least 1 argument,");
elif not IsFilter(arg[1]) then
CopyListEntries(arg, 1, 1, arg, 2, 1, Length(arg));
arg[1] := IsImmutableDigraph;
fi;
return CallFuncList(DigraphCons, arg);
end);
########################################################################
# 6. Printing, viewing, strings
########################################################################
InstallMethod(ViewString, "for a digraph", [IsDigraph],
function(D)
local str, n, m;
IsValidDigraph(D);
str := "<";
if IsMutableDigraph(D) then
Append(str, "mutable ");
elif IsImmutableDigraph(D) then
Append(str, "immutable ");
fi;
if IsVertexColoredDigraph(D) then
Append(str, "vertex colored ");
fi;
if IsMultiDigraph(D) then
Append(str, "multi");
fi;
n := DigraphNrVertices(D);
m := DigraphNrEdges(D);
Append(str, "digraph with ");
Append(str, String(n));
if n = 1 then
Append(str, " vertex, ");
else
Append(str, " vertices, ");
fi;
Append(str, String(m));
if m = 1 then
Append(str, " edge>");
else
Append(str, " edges>");
fi;
return str;
end);
InstallMethod(PrintString, "for a dense immutable digraph",
[IsImmutableDigraph and IsDenseDigraphRep],
function(D)
return Concatenation("Digraph( IsImmutableDigraph, ",
PrintString(OutNeighbours(D)),
" )");
end);
InstallMethod(PrintString, "for a dense mutable digraph",
[IsMutableDigraph and IsDenseDigraphRep],
function(D)
return Concatenation("Digraph( IsMutableDigraph, ",
PrintString(OutNeighbours(D)),
" )");
end);
InstallMethod(PrintString, "for a vertex colored dense digraph",
[IsVertexColoredDigraph and IsDenseDigraphRep],
function(D)
return Concatenation("Digraph( IsVertexColoredDigraph,",
PrintString(OutNeighbours(D)),
", ",
PrintString(DigraphVertexColors(D)),
" )");
end);
InstallMethod(String, "for a dense immutable digraph",
[IsImmutableDigraph and IsDenseDigraphRep],
function(D)
return Concatenation("Digraph( IsImmutableDigraph, ",
String(OutNeighbours(D)),
" )");
end);
InstallMethod(String, "for a dense mutable digraph",
[IsMutableDigraph and IsDenseDigraphRep],
function(D)
return Concatenation("Digraph( IsMutableDigraph, ",
String(OutNeighbours(D)),
" )");
end);
InstallMethod(String, "for a vertex colored dense digraph",
[IsVertexColoredDigraph and IsDenseDigraphRep],
function(D)
return Concatenation("Digraph( IsVertexColoredDigraph, ",
String(OutNeighbours(D)),
", ",
String(DigraphVertexColors(D)),
" )");
end);
########################################################################
# 7. Operators
########################################################################
InstallMethod(\=, "for two digraphs", [IsDigraph, IsDigraph],
function(C, D)
IsValidDigraph(C);
IsValidDigraph(D);
return DIGRAPH_EQUALS(C, D);
end);
InstallMethod(\<, "for two digraphs", [IsDigraph, IsDigraph],
function(C, D)
IsValidDigraph(C);
IsValidDigraph(D);
return DIGRAPH_LT(C, D);
end);
InstallMethod(\=, "for a digraph and vertex colored digraph",
[IsDigraph, IsVertexColoredDigraph], ReturnFalse);
InstallMethod(\=, "for vertex colored digraph and digraph",
[IsVertexColoredDigraph, IsDigraph], ReturnFalse);
InstallMethod(\=, "for vertex colored digraphs",
[IsVertexColoredDigraph, IsVertexColoredDigraph],
function(C, D)
return DIGRAPH_EQUALS(C, D)
and DigraphVertexColors(C) = DigraphVertexColors(D);
end);
InstallMethod(\<, "for a digraph and vertex colored digraph",
[IsDigraph, IsVertexColoredDigraph], ReturnTrue);
InstallMethod(\<, "for a vertex colored digraph and digraph",
[IsVertexColoredDigraph, IsDigraph], ReturnFalse);
InstallMethod(\<, "for vertex colored digraphs",
[IsVertexColoredDigraph, IsVertexColoredDigraph],
function(C, D)
return DIGRAPH_LT(C, D) or (DIGRAPH_EQUALS(C, D)
and DigraphVertexColors(C) <
DigraphVertexColors(D));
end);
########################################################################
# 8. Digraph by-something constructors
########################################################################
InstallMethod(DigraphByAdjacencyMatrixConsNC,
"for IsMutableDigraph and a homogeneous list",
[IsMutableDigraph, IsHomogeneousList],
function(filt, mat)
local add_edge, n, list, i, j;
if IsInt(mat[1][1]) then
add_edge := function(i, j)
local k;
for k in [1 .. mat[i][j]] do
Add(list[i], j);
od;
end;
else # boolean matrix
add_edge := function(i, j)
if mat[i][j] then
Add(list[i], j);
fi;
end;
fi;
n := Length(mat);
list := EmptyPlist(n);
for i in [1 .. n] do
list[i] := [];
for j in [1 .. n] do
add_edge(i, j);
od;
od;
return DigraphNC(IsMutableDigraph, list);
end);
InstallMethod(DigraphByAdjacencyMatrixCons,
"for IsMutableDigraph and a homogeneous list",
[IsMutableDigraph, IsHomogeneousList],
function(filt, mat)
local n, i, j;
n := Length(mat);
if not IsRectangularTable(mat) or Length(mat[1]) <> n then
ErrorNoReturn("the argument <mat> must be a square matrix,");
elif not IsBool(mat[1][1]) then
for i in [1 .. n] do
for j in [1 .. n] do
if not (IsInt(mat[i][j]) and mat[i][j] >= 0) then
ErrorNoReturn("the argument <mat> must be a matrix of ",
"non-negative integers,");
fi;
od;
od;
fi;
return DigraphByAdjacencyMatrixConsNC(IsMutableDigraph, mat);
end);
InstallMethod(DigraphByAdjacencyMatrixCons,
"for IsMutableDigraph and an empty list",
[IsMutableDigraph, IsList and IsEmpty], DigraphByAdjacencyMatrixConsNC);
InstallMethod(DigraphByAdjacencyMatrixConsNC,
"for IsMutableDigraph and an empty list",
[IsMutableDigraph, IsList and IsEmpty],
function(filt, dummy)
return EmptyDigraph(IsMutableDigraph, 0);
end);
InstallMethod(DigraphByAdjacencyMatrixCons,
"for IsImmutableDigraph and a homogeneous list",
[IsImmutableDigraph, IsHomogeneousList],
function(filt, mat)
local D;
D := MakeImmutableDigraph(
DigraphByAdjacencyMatrixCons(IsMutableDigraph, mat));
if IsEmpty(mat) or IsInt(mat[1][1]) then
SetAdjacencyMatrix(D, mat);
else
Assert(1, IsBool(mat[1][1]));
SetBooleanAdjacencyMatrix(D, mat);
fi;
return D;
end);
InstallMethod(DigraphByAdjacencyMatrix,
"for a function and a homogeneous list",
[IsFunction, IsHomogeneousList],
function(func, mat)
return DigraphByAdjacencyMatrixCons(func, mat);
end);
InstallMethod(DigraphByAdjacencyMatrix, "for a homogeneous list",
[IsHomogeneousList],
function(mat)
return DigraphByAdjacencyMatrixCons(IsImmutableDigraph, mat);
end);
InstallMethod(DigraphByEdgesCons,
"for IsMutableDigraph, a list, and an integer",
[IsMutableDigraph, IsList, IsInt],
function(filt, edges, n)
local list, edge;
if IsEmpty(edges) then
return NullDigraph(IsMutableDigraph, n);
fi;
for edge in edges do
if Length(edge) <> 2 then
ErrorNoReturn("the 1st argument <edges> must be a list of pairs,");
elif ForAny(edge, x -> not (IsPosInt(x) and x <= n)) then
ErrorNoReturn("the 1st argument <edges> must not contain values ",
"greater than ", n, ", the 2nd argument <n>,");
fi;
od;
list := List([1 .. n], x -> []);
for edge in edges do
Add(list[edge[1]], edge[2]);
od;
return DigraphNC(IsMutableDigraph, list);
end);
InstallMethod(DigraphByEdgesCons, "for IsMutableDigraph and a list",
[IsMutableDigraph, IsList],
function(filt, edges)
local n;
if IsEmpty(edges) then
n := 0;
else
n := Maximum(List(edges, Maximum));
fi;
return DigraphByEdgesCons(IsMutableDigraph, edges, n);
end);
InstallMethod(DigraphByEdgesCons, "for an ImmutableDigraph and a list",
[IsImmutableDigraph, IsList],
function(filt, edges)
local D;
D := MakeImmutableDigraph(DigraphByEdges(IsMutableDigraph, edges));
SetDigraphEdges(D, edges);
SetDigraphNrEdges(D, Length(edges));
return D;
end);
InstallMethod(DigraphByEdgesCons,
"for IsImmutableDigraph, a list, and an integer",
[IsImmutableDigraph, IsList, IsInt],
function(filt, edges, n)
local D;
D := MakeImmutableDigraph(DigraphByEdges(IsMutableDigraph, edges, n));
SetDigraphEdges(D, edges);
SetDigraphNrEdges(D, Length(edges));
return D;
end);
InstallMethod(DigraphByEdges, "for a list and an integer",
[IsList, IsInt],
function(edges, n)
return DigraphByEdgesCons(IsImmutableDigraph, edges, n);
end);
InstallMethod(DigraphByEdges, "for a list", [IsList],
function(edges)
return DigraphByEdgesCons(IsImmutableDigraph, edges);
end);
InstallMethod(DigraphByEdges, "for a function, a list, and an integer",
[IsFunction, IsList, IsInt],
function(func, edges, n)
return DigraphByEdgesCons(func, edges, n);
end);
InstallMethod(DigraphByEdges, "for a function and a list",
[IsFunction, IsList],
function(func, edges)
return DigraphByEdgesCons(func, edges);
end);
InstallMethod(DigraphByInNeighboursCons, "for IsMutableDigraph, and a list",
[IsMutableDigraph, IsList],
function(filt, list)
local n, x;
n := Length(list); # number of vertices
for x in list do
if not ForAll(x, i -> IsPosInt(i) and i <= n) then
ErrorNoReturn("the argument <list> must be a list of lists of positive ",
"integers not exceeding the length of the argument,");
fi;
od;
return DigraphByInNeighboursConsNC(IsMutableDigraph, list);
end);
InstallMethod(DigraphByInNeighboursCons, "for IsImmutableDigraph and a list",
[IsImmutableDigraph, IsList],
function(filt, list)
local D;
D := MakeImmutableDigraph(DigraphByInNeighboursCons(IsMutableDigraph, list));
SetInNeighbours(D, list);
return D;
end);
InstallMethod(DigraphByInNeighboursConsNC, "for IsMutableDigraph and a list",
[IsMutableDigraph, IsList],
function(filt, list)
return DigraphNC(IsMutableDigraph, DIGRAPH_IN_OUT_NBS(list));
end);
InstallMethod(DigraphByInNeighboursConsNC, "for IsImmutableDigraph and a list",
[IsImmutableDigraph, IsList],
function(filt, list)
local D;
D := MakeImmutableDigraph(DigraphByInNeighboursConsNC(IsMutableDigraph,
list));
SetInNeighbours(D, list);
return D;
end);
InstallMethod(DigraphByInNeighbours, "for a list", [IsList],
function(list)
return DigraphByInNeighboursCons(IsImmutableDigraph, list);
end);
InstallMethod(DigraphByInNeighbours, "for a function and a list",
[IsFunction, IsList],
function(func, list)
return DigraphByInNeighboursCons(func, list);
end);
########################################################################
# 9. Converters to/from other types -> digraph . . .
########################################################################
InstallMethod(AsDigraphCons, "for IsMutableDigraph and a binary relation",
[IsMutableDigraph, IsBinaryRelation],
function(filt, rel)
local dom, list, i;
dom := GeneratorsOfDomain(UnderlyingDomainOfBinaryRelation(rel));
if not IsRange(dom) or dom[1] <> 1 then
ErrorNoReturn("the argument <rel> must be a binary relation ",
"on the domain [1 .. n] for some positive integer n,");
fi;
list := EmptyPlist(Length(dom));
for i in dom do
list[i] := ImagesElm(rel, i);
od;
return Digraph(IsMutableDigraph, list);
end);
InstallMethod(AsDigraphCons, "for IsImmutableDigraph and a binary relation",
[IsImmutableDigraph, IsBinaryRelation],
function(filt, rel)
local D;
D := MakeImmutableDigraph(AsDigraph(IsMutableDigraph, rel));
SetIsMultiDigraph(D, false);
if HasIsReflexiveBinaryRelation(rel) then
SetIsReflexiveDigraph(D, IsReflexiveBinaryRelation(rel));
fi;
if HasIsSymmetricBinaryRelation(rel) then
SetIsSymmetricDigraph(D, IsSymmetricBinaryRelation(rel));
fi;
if HasIsTransitiveBinaryRelation(rel) then
SetIsTransitiveDigraph(D, IsTransitiveBinaryRelation(rel));
fi;
if HasIsAntisymmetricBinaryRelation(rel) then
SetIsAntisymmetricDigraph(D, IsAntisymmetricBinaryRelation(rel));
fi;
return D;
end);
InstallMethod(AsDigraph, "for a binary relation", [IsBinaryRelation],
function(rel)
return AsDigraphCons(IsImmutableDigraph, rel);
end);
InstallMethod(AsDigraph, "for a function and a binary relation",
[IsFunction, IsBinaryRelation],
function(func, rel)
return AsDigraphCons(func, rel);
end);
InstallMethod(AsDigraphCons,
"for IsMutableDigraph, a transformation, and an integer",
[IsMutableDigraph, IsTransformation, IsInt],
function(filt, f, n)
local list, x, i;
if n < 0 then
ErrorNoReturn("the 2nd argument <n> should be a non-negative integer,");
fi;
list := EmptyPlist(n);
for i in [1 .. n] do
x := i ^ f;
if x > n then
return fail;
fi;
list[i] := [x];
od;
return DigraphNC(IsMutableDigraph, list);
end);
InstallMethod(AsDigraphCons,
"for IsImmutableDigraph, a transformation, and an integer",
[IsImmutableDigraph, IsTransformation, IsInt],
function(filt, f, n)
local D;
D := AsDigraph(IsMutableDigraph, f, n);
if D <> fail then
D := MakeImmutableDigraph(D);
SetDigraphNrEdges(D, n);
SetIsMultiDigraph(D, false);
SetIsFunctionalDigraph(D, true);
fi;
return D;
end);
InstallMethod(AsDigraph,
"for a function, a transformation, and an integer",
[IsFunction, IsTransformation, IsInt],
function(func, t, n)
return AsDigraphCons(func, t, n);
end);
InstallMethod(AsDigraph, "for a transformation and an integer",
[IsTransformation, IsInt],
function(t, n)
return AsDigraphCons(IsImmutableDigraph, t, n);
end);
InstallMethod(AsDigraph, "for a function and a transformation",
[IsFunction, IsTransformation],
function(func, t)
return AsDigraphCons(func, t, DegreeOfTransformation(t));
end);
InstallMethod(AsDigraph, "for a transformation", [IsTransformation],
function(t)
return AsDigraphCons(IsImmutableDigraph, t, DegreeOfTransformation(t));
end);
InstallMethod(AsBinaryRelation, "for a digraph", [IsDenseDigraphRep],
function(D)
local rel;
if DigraphNrVertices(D) = 0 then
ErrorNoReturn("the argument <D> must be a digraph with at least 1 ",
"vertex,");
elif IsMultiDigraph(D) then
ErrorNoReturn("the argument <D> must be a digraph with no multiple edges");
fi;
# Can translate known attributes of <D> to the relation, e.g. symmetry
rel := BinaryRelationOnPointsNC(OutNeighbours(D));
if HasIsReflexiveDigraph(D) then
SetIsReflexiveBinaryRelation(rel, IsReflexiveDigraph(D));
fi;
if HasIsSymmetricDigraph(D) then
SetIsSymmetricBinaryRelation(rel, IsSymmetricDigraph(D));
fi;
if HasIsTransitiveDigraph(D) then
SetIsTransitiveBinaryRelation(rel, IsTransitiveDigraph(D));
fi;
if HasIsAntisymmetricDigraph(D) then
SetIsAntisymmetricBinaryRelation(rel, IsAntisymmetricDigraph(D));
fi;
return rel;
end);
InstallMethod(AsSemigroup, "for a function and a digraph",
[IsFunction, IsDigraph],
function(filt, D)
local red, top, im, max, n, gens, i;
if filt <> IsPartialPermSemigroup then
TryNextMethod();
elif not IsJoinSemilatticeDigraph(D) then
if IsMeetSemilatticeDigraph(D) then
return AsSemigroup(IsPartialPermSemigroup,
DigraphReverse(DigraphCopyIfMutable(D)));
fi;
ErrorNoReturn("the 2nd argument <D> must be digraph that is a join or ",
"meet semilattice,");
fi;
D := DigraphCopyIfMutable(D);