-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathattr.gi
More file actions
1680 lines (1513 loc) · 46.2 KB
/
attr.gi
File metadata and controls
1680 lines (1513 loc) · 46.2 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
#############################################################################
##
## attr.gi
## Copyright (C) 2014-17 James D. Mitchell
##
## Licensing information can be found in the README file of this package.
##
#############################################################################
##
InstallMethod(DigraphNrVertices, "for a dense digraph", [IsDenseDigraphRep],
function(D)
IsValidDigraph(D);
return DIGRAPH_NR_VERTICES(D);
end);
InstallMethod(OutNeighbours, "for a dense digraph", [IsDenseDigraphRep],
function(D)
IsValidDigraph(D);
return DIGRAPH_OUT_NEIGHBOURS(D);
end);
# The next method is (yet another) DFS as described in
# http://www.eecs.wsu.edu/~holder/courses/CptS223/spr08/slides/graphapps.pdf
InstallMethod(ArticulationPoints, "for a dense digraph", [IsDenseDigraphRep],
function(D)
local copy, nbs, counter, visited, num, low, parent, points, points_seen,
stack, depth, v, w, i;
IsValidDigraph(D);
if (HasIsConnectedDigraph(D) and not IsConnectedDigraph(D))
or DigraphNrVertices(D) <= 1 then
return [];
elif not IsSymmetricDigraph(D) then
copy := DigraphSymmetricClosure(DigraphMutableCopy(D));
else
copy := D;
fi;
nbs := OutNeighbours(copy);
counter := 0;
visited := BlistList([1 .. DigraphNrVertices(copy)], []);
num := [];
low := [];
parent := [1];
points := [];
points_seen := BlistList([1 .. DigraphNrVertices(copy)], []);
stack := [[1, 0]];
depth := 1;
while depth > 1 or not visited[1] do
v := stack[depth][1];
if visited[v] then
depth := depth - 1;
v := stack[depth][1];
w := nbs[v][stack[depth][2]];
if v <> 1 and low[w] >= num[v] and not points_seen[v] then
points_seen[v] := true;
Add(points, v);
fi;
if low[w] < low[v] then
low[v] := low[w];
fi;
else
visited[v] := true;
counter := counter + 1;
num[v] := counter;
low[v] := counter;
fi;
i := PositionProperty(nbs[v], w -> w <> v, stack[depth][2]);
while i <> fail do
w := nbs[v][i];
if not visited[w] then
parent[w] := v;
stack[depth][2] := i;
depth := depth + 1;
if not IsBound(stack[depth]) then
stack[depth] := [];
fi;
stack[depth][1] := w;
stack[depth][2] := 0;
break;
elif parent[v] <> w and num[w] < low[v] then
low[v] := num[w];
fi;
i := PositionProperty(nbs[v], w -> w <> v, i);
od;
od;
if counter = DigraphNrVertices(D) then
i := Position(parent, 1, 1);
if i <> fail and Position(parent, 1, i) <> fail then
Add(points, 1);
fi;
if IsAttributeStoringRep(D) then
SetIsConnectedDigraph(D, true);
fi;
return points;
else
if IsAttributeStoringRep(D) then
SetIsConnectedDigraph(D, false);
fi;
return [];
fi;
end);
InstallMethod(ChromaticNumber, "for a dense digraph", [IsDenseDigraphRep],
function(D)
local nr, comps, upper, chrom, tmp_comps, tmp_upper, n, comp, bound, clique,
c, i;
IsValidDigraph(D);
nr := DigraphNrVertices(D);
if DigraphHasLoops(D) then
ErrorNoReturn("the argument <D> must be a digraph with no loops,");
elif nr = 0 then
return 0; # chromatic number = 0 iff <D> has 0 verts
elif IsNullDigraph(D) then
return 1; # chromatic number = 1 iff <D> has >= 1 verts & no edges
elif IsBipartiteDigraph(D) then
return 2; # chromatic number = 2 iff <D> has >= 2 verts & is bipartite
# <D> has at least 2 vertices at this stage
fi;
# The chromatic number of <D> is at least 3 and at most nr
D := DigraphMutableCopy(D);
D := DigraphRemoveAllMultipleEdges(D);
D := DigraphSymmetricClosure(D);
if IsCompleteDigraph(D) then
# chromatic number = nr iff <D> has >= 2 verts & this cond.
return nr;
elif nr = 4 then
# if nr = 4, then 3 is only remaining possible chromatic number
return 3;
fi;
# The chromatic number of <D> is at least 3 and at most nr - 1
# The variable <chrom> is the current best known lower bound for the
# chromatic number of <D>.
chrom := 3;
# Prepare a list of connected components of D whose chromatic number we
# do not yet know.
if IsConnectedDigraph(D) then
comps := [D];
upper := [RankOfTransformation(DigraphGreedyColouring(D), nr)];
chrom := Maximum(CliqueNumber(D), chrom);
else
tmp_comps := [];
tmp_upper := [];
for comp in DigraphConnectedComponents(D).comps do
n := Length(comp);
if chrom < n then
# If chrom >= n, then we can colour the vertices of comp using any n of
# the required (at least) chrom colours, and we do not have to consider
# comp.
# Note that n > chrom >= 3 and so comp is not null, so no need to check
# for that.
comp := InducedSubdigraph(DigraphMutableCopy(D), comp);
if IsCompleteDigraph(comp) then
# Since n > chrom, this is an improved lower bound for the overall
# chromatic number.
chrom := n;
elif not IsBipartiteDigraph(comp) then
# If comp is bipartite, then its chromatic number is 2, and, since
# the chromatic number of D is >= 3, this component can be
# ignored.
bound := RankOfTransformation(DigraphGreedyColouring(comp),
DigraphNrVertices(comp));
if bound > chrom then
# If bound <= chrom, then comp can be coloured by at most chrom
# colours, and so we can ignore comp.
clique := CliqueNumber(comp);
if clique = bound then
# The chromatic number of this component is known, and it can be
# ignored, and clique = bound > chrom, and so clique is an
# improved lower bound for the chromatic number of D.
chrom := clique;
else
Add(tmp_comps, comp);
Add(tmp_upper, bound);
if clique > chrom then
chrom := clique;
fi;
fi;
fi;
fi;
fi;
od;
# Remove the irrelevant components since we have a possibly improved value
# of chrom.
comps := [];
upper := [];
for i in [1 .. Length(tmp_comps)] do
if chrom < DigraphNrVertices(tmp_comps[i]) and chrom < tmp_upper[i] then
Add(comps, tmp_comps[i]);
Add(upper, tmp_upper[i]);
fi;
od;
# Sort by size, since smaller components are easier to colour
SortParallel(comps, upper, {x, y} -> Size(x) < Size(y));
fi;
for i in [1 .. Length(comps)] do
# <c> is the current best upper bound for the chromatic number of comps[i]
c := upper[i];
while c > chrom and DigraphColouring(comps[i], c - 1) <> fail do
c := c - 1;
od;
if c > chrom then
chrom := c;
fi;
od;
return chrom;
end);
#
# The following method is currently useless, as the OutNeighbours are computed
# and set whenever a digraph is created. It could be reinstated later if we
# decide to allow digraphs to exist without known OutNeighbours.
#
# InstallMethod(OutNeighbours,
# "for a digraph with representative out neighbours and group",
# [IsDigraph and HasRepresentativeOutNeighbours and HasDigraphGroup],
# function(D)
# local gens, sch, reps, out, trace, word, i, w;
#
# gens := GeneratorsOfGroup(DigraphGroup(D));
# sch := DigraphSchreierVector(D);
# reps := RepresentativeOutNeighbours(D);
#
# out := EmptyPlist(DigraphNrVertices(D));
#
# for i in [1 .. Length(sch)] do
# if sch[i] < 0 then
# out[i] := reps[-sch[i]];
# fi;
#
# trace := DIGRAPHS_TraceSchreierVector(gens, sch, i);
# out[i] := out[trace.representative];
# word := trace.word;
# for w in word do
# out[i] := OnTuples(out[i], gens[w]);
# od;
# od;
# return out;
# end);
InstallMethod(DigraphAdjacencyFunction, "for a dense digraph", [IsDigraph],
function(D)
IsValidDigraph(D);
return {u, v} -> IsDigraphEdge(D, u, v);
end);
InstallMethod(AsTransformation, "for a dense digraph", [IsDenseDigraphRep],
function(D)
IsValidDigraph(D);
if not IsFunctionalDigraph(D) then
return fail;
fi;
return Transformation(Concatenation(OutNeighbours(D)));
end);
InstallMethod(ReducedDigraph, "for a dense mutable digraph",
[IsDenseDigraphRep and IsMutableDigraph],
function(D)
local v, niv, old, i;
if IsConnectedDigraph(D) then
return D;
fi;
v := DigraphVertices(D);
niv := BlistList(v, []);
old := OutNeighbours(D);
# First find the non-isolated vertices
for i in [1 .. Length(old)] do
if not IsEmpty(old[i]) then
niv[i] := true;
UniteBlistList(v, niv, old[i]);
fi;
od;
return InducedSubdigraph(D, ListBlist(v, niv));
end);
InstallMethod(ReducedDigraph, "for an immutable digraph", [IsImmutableDigraph],
function(D)
local C;
if IsConnectedDigraph(D) then
return D;
fi;
C := MakeImmutableDigraph(ReducedDigraph(DigraphMutableCopy(D)));
if IsVertexColoredDigraph(D) then
MakeVertexColoredDigraph(C,
DigraphVertexColors(D){DigraphVertexLabels(C)});
fi;
return C;
end);
InstallMethod(ReducedDigraphAttr, "for an immutable digraph",
[IsImmutableDigraph], ReducedDigraph);
InstallMethod(DigraphDual, "for a dense mutable digraph",
[IsDenseDigraphRep and IsMutableDigraph],
function(D)
local nodes, list, i;
if IsMultiDigraph(D) then
ErrorNoReturn("the argument <D> must be a digraph with no multiple ",
"edges,");
fi;
nodes := DigraphVertices(D);
list := D!.OutNeighbours;
for i in nodes do
list[i] := DifferenceLists(nodes, list[i]);
od;
ClearDigraphEdgeLabels(D);
return D;
end);
InstallMethod(DigraphDual, "for an immutable digraph", [IsImmutableDigraph],
function(D)
local C;
if HasDigraphDualAttr(D) then
return DigraphDualAttr(D);
fi;
C := DigraphMutableCopy(D);
C := MakeImmutableDigraph(DigraphDual(C));
if HasDigraphGroup(D) then
SetDigraphGroup(C, DigraphGroup(D));
fi;
if IsVertexColoredDigraph(D) then
MakeVertexColoredDigraph(C, DigraphVertexColors(D));
fi;
SetDigraphDualAttr(D, C);
return C;
end);
InstallMethod(DigraphDualAttr, "for an immutable digraph",
[IsImmutableDigraph], DigraphDual);
InstallMethod(DigraphNrEdges, "for a digraph", [IsDenseDigraphRep],
function(D)
IsValidDigraph(D);
return DIGRAPH_NREDGES(D);
end);
InstallMethod(DigraphEdges, "for a dense digraph", [IsDenseDigraphRep],
function(D)
local out, adj, nr, i, j;
IsValidDigraph(D);
out := EmptyPlist(DigraphNrEdges(D));
adj := OutNeighbours(D);
nr := 0;
for i in DigraphVertices(D) do
for j in adj[i] do
nr := nr + 1;
out[nr] := [i, j];
od;
od;
return out;
end);
# attributes for digraphs . . .
InstallMethod(AsGraph, "for a digraph", [IsDigraph],
function(D)
IsValidDigraph(D);
return Graph(D);
end);
InstallMethod(DigraphVertices, "for a digraph", [IsDigraph],
function(D)
IsValidDigraph(D);
return [1 .. DigraphNrVertices(D)];
end);
InstallMethod(DigraphRange, "for a dense digraph attribute storing digraph",
[IsDenseDigraphRep and IsAttributeStoringRep],
function(D)
IsValidDigraph(D);
if not IsBound(D!.DigraphRange) then
DIGRAPH_SOURCE_RANGE(D);
SetDigraphSource(D, D!.DigraphSource);
fi;
return D!.DigraphRange;
end);
InstallMethod(DigraphRange, "for a dense digraph attribute storing digraph",
[IsDenseDigraphRep and IsMutableDigraph],
function(D)
return DIGRAPH_SOURCE_RANGE(D).DigraphRange;
end);
InstallMethod(DigraphSource, "for a dense digraph attribute storing digraph",
[IsDenseDigraphRep and IsAttributeStoringRep],
function(D)
IsValidDigraph(D);
if not IsBound(D!.DigraphSource) then
DIGRAPH_SOURCE_RANGE(D);
SetDigraphRange(D, D!.DigraphRange);
fi;
return D!.DigraphSource;
end);
InstallMethod(DigraphSource, "for a dense digraph attribute storing digraph",
[IsDenseDigraphRep and IsMutableDigraph],
function(D)
return DIGRAPH_SOURCE_RANGE(D).DigraphSource;
end);
InstallMethod(InNeighbours, "for a digraph", [IsDenseDigraphRep],
function(D)
IsValidDigraph(D);
return DIGRAPH_IN_OUT_NBS(OutNeighbours(D));
end);
InstallMethod(AdjacencyMatrix, "for a digraph", [IsDenseDigraphRep],
function(D)
IsValidDigraph(D);
return ADJACENCY_MATRIX(D);
end);
InstallMethod(BooleanAdjacencyMatrix, "for a dense digraph",
[IsDenseDigraphRep],
function(D)
local n, nbs, mat, i, j;
IsValidDigraph(D);
n := DigraphNrVertices(D);
nbs := OutNeighbours(D);
mat := List(DigraphVertices(D), x -> BlistList([1 .. n], []));
for i in DigraphVertices(D) do
for j in nbs[i] do
mat[i][j] := true;
od;
od;
return mat;
end);
InstallMethod(DigraphShortestDistances, "for a dense digraph",
[IsDenseDigraphRep],
function(D)
local vertices, data, sum, distances, v, u;
IsValidDigraph(D);
if HasDIGRAPHS_ConnectivityData(D) then
vertices := DigraphVertices(D);
data := DIGRAPHS_ConnectivityData(D);
sum := 0;
for v in vertices do
if IsBound(data[v]) then
sum := sum + 1;
fi;
od;
if sum > Int(0.9 * DigraphNrVertices(D))
or (HasDigraphGroup(D) and not IsTrivial(DigraphGroup(D))) then
# adjust the constant 0.9 and possibly make a decision based on
# how big the group is
distances := [];
for u in vertices do
distances[u] := [];
for v in vertices do
distances[u][v] := DigraphShortestDistance(D, u, v);
od;
od;
return distances;
fi;
fi;
return DIGRAPH_SHORTEST_DIST(D);
end);
# returns the vertices (i.e. numbers) of <D> ordered so that there are no
# edges from <out[j]> to <out[i]> for all <i> greater than <j>.
InstallMethod(DigraphTopologicalSort, "for a dense digraph",
[IsDenseDigraphRep],
function(D)
IsValidDigraph(D);
return DIGRAPH_TOPO_SORT(OutNeighbours(D));
end);
InstallMethod(DigraphStronglyConnectedComponents, "for a dense digraph",
[IsDenseDigraphRep],
function(D)
local verts;
IsValidDigraph(D);
if HasIsAcyclicDigraph(D) and IsAcyclicDigraph(D) then
verts := DigraphVertices(D);
return rec(comps := List(verts, x -> [x]), id := verts * 1);
elif HasIsStronglyConnectedDigraph(D)
and IsStronglyConnectedDigraph(D) then
verts := DigraphVertices(D);
return rec(comps := [verts * 1], id := verts * 0 + 1);
fi;
return GABOW_SCC(OutNeighbours(D));
end);
InstallMethod(DigraphNrStronglyConnectedComponents, "for a digraph",
[IsDigraph],
function(D)
IsValidDigraph(D);
return Length(DigraphStronglyConnectedComponents(D).comps);
end);
InstallMethod(DigraphConnectedComponents, "for a dense digraph",
[IsDenseDigraphRep],
function(D)
IsValidDigraph(D);
return DIGRAPH_CONNECTED_COMPONENTS(D);
end);
InstallMethod(OutDegrees, "for a dense digraph", [IsDenseDigraphRep],
function(D)
local adj, degs, i;
IsValidDigraph(D);
adj := OutNeighbours(D);
degs := EmptyPlist(DigraphNrVertices(D));
for i in DigraphVertices(D) do
degs[i] := Length(adj[i]);
od;
return degs;
end);
InstallMethod(InDegrees, "for a digraph with in neighbours",
[IsDigraph and HasInNeighbours],
2, # to beat the method for IsDenseDigraphRep
function(D)
local inn, degs, i;
IsValidDigraph(D);
inn := InNeighbours(D);
degs := EmptyPlist(DigraphNrVertices(D));
for i in DigraphVertices(D) do
degs[i] := Length(inn[i]);
od;
return degs;
end);
InstallMethod(InDegrees, "for a dense digraph", [IsDenseDigraphRep],
function(D)
local adj, degs, x, i;
IsValidDigraph(D);
adj := OutNeighbours(D);
degs := [1 .. DigraphNrVertices(D)] * 0;
for x in adj do
for i in x do
degs[i] := degs[i] + 1;
od;
od;
return degs;
end);
InstallMethod(OutDegreeSequence, "for a digraph", [IsDigraph],
function(D)
IsValidDigraph(D);
D := ShallowCopy(OutDegrees(D));
Sort(D, {a, b} -> b < a);
return D;
# return SortedList(OutDegrees(D), {a, b} -> b < a);
end);
InstallMethod(OutDegreeSequence,
"for a dense digraph with known digraph group",
[IsDenseDigraphRep and HasDigraphGroup],
function(D)
local out, adj, orbs, orb;
IsValidDigraph(D);
out := [];
adj := OutNeighbours(D);
orbs := DigraphOrbits(D);
for orb in orbs do
Append(out, [1 .. Length(orb)] * 0 + Length(adj[orb[1]]));
od;
Sort(out, {a, b} -> b < a);
return out;
end);
InstallMethod(OutDegreeSet, "for a digraph", [IsDigraph],
function(D)
IsValidDigraph(D);
return Set(ShallowCopy(OutDegrees(D)));
end);
InstallMethod(InDegreeSequence, "for a digraph", [IsDigraph],
function(D)
IsValidDigraph(D);
D := ShallowCopy(InDegrees(D));
Sort(D, {a, b} -> b < a);
return D;
# return SortedList(OutDegrees(D), {a, b} -> b < a);
end);
InstallMethod(InDegreeSequence,
"for a digraph with known digraph group and in-neighbours",
[IsDigraph and HasDigraphGroup and HasInNeighbours],
function(D)
local out, adj, orbs, orb;
IsValidDigraph(D);
out := [];
adj := InNeighbours(D);
orbs := DigraphOrbits(D);
for orb in orbs do
Append(out, [1 .. Length(orb)] * 0 + Length(adj[orb[1]]));
od;
Sort(out, {a, b} -> b < a);
return out;
end);
InstallMethod(InDegreeSet, "for a digraph", [IsDigraph],
function(D)
IsValidDigraph(D);
return Set(ShallowCopy(InDegrees(D)));
end);
InstallMethod(DigraphSources, "for a digraph with in-degrees",
[IsDigraph and HasInDegrees], 3,
function(D)
local degs;
IsValidDigraph(D);
degs := InDegrees(D);
return Filtered(DigraphVertices(D), x -> degs[x] = 0);
end);
InstallMethod(DigraphSources, "for a digraph with in-neighbours",
[IsDigraph and HasInNeighbours],
2, # to beat the method for IsDenseDigraphRep
function(D)
local inn, sources, count, i;
IsValidDigraph(D);
inn := InNeighbours(D);
sources := EmptyPlist(DigraphNrVertices(D));
count := 0;
for i in DigraphVertices(D) do
if IsEmpty(inn[i]) then
count := count + 1;
sources[count] := i;
fi;
od;
ShrinkAllocationPlist(sources);
return sources;
end);
InstallMethod(DigraphSources, "for a dense digraph", [IsDenseDigraphRep],
function(D)
local out, seen, tmp, next, v;
IsValidDigraph(D);
out := OutNeighbours(D);
seen := BlistList(DigraphVertices(D), []);
for next in out do
for v in next do
seen[v] := true;
od;
od;
# FIXME use FlipBlist (when available)
tmp := BlistList(DigraphVertices(D), DigraphVertices(D));
SubtractBlist(tmp, seen);
return ListBlist(DigraphVertices(D), tmp);
end);
InstallMethod(DigraphSinks, "for a digraph with out-degrees",
[IsDigraph and HasOutDegrees],
2, # to beat the method for IsDenseDigraphRep
function(D)
local degs;
IsValidDigraph(D);
degs := OutDegrees(D);
return Filtered(DigraphVertices(D), x -> degs[x] = 0);
end);
InstallMethod(DigraphSinks, "for a dense digraph", [IsDenseDigraphRep],
function(D)
local out, sinks, count, i;
IsValidDigraph(D);
out := OutNeighbours(D);
sinks := [];
count := 0;
for i in DigraphVertices(D) do
if IsEmpty(out[i]) then
count := count + 1;
sinks[count] := i;
fi;
od;
return sinks;
end);
InstallMethod(DigraphPeriod, "for a digraph", [IsDenseDigraphRep],
function(D)
local comps, out, deg, nrvisited, period, stack, len, depth, current,
olddepth, i;
IsValidDigraph(D);
if HasIsAcyclicDigraph(D) and IsAcyclicDigraph(D) then
return 0;
fi;
comps := DigraphStronglyConnectedComponents(D).comps;
out := OutNeighbours(D);
deg := OutDegrees(D);
nrvisited := [1 .. Length(DigraphVertices(D))] * 0;
period := 0;
for i in [1 .. Length(comps)] do
stack := [comps[i][1]];
len := 1;
depth := EmptyPlist(Length(DigraphVertices(D)));
depth[comps[i][1]] := 1;
while len <> 0 do
current := stack[len];
if nrvisited[current] = deg[current] then
len := len - 1;
else
nrvisited[current] := nrvisited[current] + 1;
len := len + 1;
stack[len] := out[current][nrvisited[current]];
olddepth := depth[current];
if IsBound(depth[stack[len]]) then
period := GcdInt(period, depth[stack[len]] - olddepth - 1);
if period = 1 then
return period;
fi;
else
depth[stack[len]] := olddepth + 1;
fi;
fi;
od;
od;
if period = 0 then
SetIsAcyclicDigraph(D, true);
fi;
return period;
end);
InstallMethod(DIGRAPHS_ConnectivityData, "for a digraph", [IsDigraph],
function(D)
IsValidDigraph(D);
return [];
end);
BindGlobal("DIGRAPH_ConnectivityDataForVertex",
function(D, v)
local data, out_nbs, record, orbnum, reps, i, next, laynum, localGirth,
layers, sum, localParameters, nprev, nhere, nnext, lnum, localDiameter,
layerNumbers, x, y;
data := DIGRAPHS_ConnectivityData(D);
if IsBound(data[v]) then
return data[v];
fi;
out_nbs := OutNeighbours(D);
if HasDigraphGroup(D) then
record := DIGRAPHS_Orbits(DigraphStabilizer(D, v),
DigraphVertices(D));
orbnum := record.lookup;
reps := List(record.orbits, Representative);
i := 1;
next := [orbnum[v]];
laynum := [1 .. Length(reps)] * 0;
laynum[next[1]] := 1;
localGirth := -1;
layers := [next];
sum := 1;
localParameters := [];
else
orbnum := [1 .. DigraphNrVertices(D)];
reps := [1 .. DigraphNrVertices(D)];
i := 1;
next := [orbnum[v]];
laynum := [1 .. Length(reps)] * 0;
laynum[next[1]] := 1;
localGirth := -1;
layers := [next];
sum := 1;
localParameters := [];
fi;
# localDiameter is the length of the longest shortest path starting at v
#
# localParameters is a list of 3-tuples [a_{i - 1}, b_{i - 1}, c_{i - 1}] for
# each i between 1 and localDiameter where c_i (respectively a_i and b_i) is
# the number of vertices at distance i − 1 (respectively i and i + 1) from v
# that are adjacent to a vertex w at distance i from v.
while Length(next) > 0 do
next := [];
for x in layers[i] do
nprev := 0;
nhere := 0;
nnext := 0;
for y in out_nbs[reps[x]] do
lnum := laynum[orbnum[y]];
if i > 1 and lnum = i - 1 then
nprev := nprev + 1;
elif lnum = i then
nhere := nhere + 1;
elif lnum = i + 1 then
nnext := nnext + 1;
elif lnum = 0 then
AddSet(next, orbnum[y]);
nnext := nnext + 1;
laynum[orbnum[y]] := i + 1;
fi;
od;
if (localGirth = -1 or localGirth = 2 * i - 1) and nprev > 1 then
localGirth := 2 * (i - 1);
fi;
if localGirth = -1 and nhere > 0 then
localGirth := 2 * i - 1;
fi;
if not IsBound(localParameters[i]) then
localParameters[i] := [nprev, nhere, nnext];
else
if nprev <> localParameters[i][1] then
localParameters[i][1] := -1;
fi;
if nhere <> localParameters[i][2] then
localParameters[i][2] := -1;
fi;
if nnext <> localParameters[i][3] then
localParameters[i][3] := -1;
fi;
fi;
od;
if Length(next) > 0 then
i := i + 1;
layers[i] := next;
sum := sum + Length(next);
fi;
od;
if sum = Length(reps) then
localDiameter := Length(layers) - 1;
else
localDiameter := -1;
fi;
layerNumbers := [];
for i in [1 .. DigraphNrVertices(D)] do
layerNumbers[i] := laynum[orbnum[i]];
od;
data[v] := rec(layerNumbers := layerNumbers, localDiameter := localDiameter,
localGirth := localGirth, localParameters := localParameters,
layers := layers);
return data[v];
end);
BindGlobal("DIGRAPHS_DiameterAndUndirectedGirth",
function(D)
local outer_reps, diameter, girth, v, record, localGirth,
localDiameter, i;
#
# This function attempts to find the diameter and undirected girth of a given
# D, using its DigraphGroup. For some digraphs, the main algorithm will
# not produce a sensible answer, so there are checks at the start and end to
# alter the answer for the diameter/girth if necessary. This function is
# called, if appropriate, by DigraphDiameter and DigraphUndirectedGirth.
#
if DigraphNrVertices(D) = 0 then
SetDigraphDiameter(D, fail);
SetDigraphUndirectedGirth(D, infinity);
return rec(diameter := fail, girth := infinity);
fi;
# TODO improve this, really check if the complexity is better with the group
# or without, or if the group is not known, but the number of vertices makes
# the usual algorithm impossible.
outer_reps := DigraphOrbitReps(D);
diameter := 0;
girth := infinity;
for i in [1 .. Length(outer_reps)] do
v := outer_reps[i];
record := DIGRAPH_ConnectivityDataForVertex(D, v);
localGirth := record.localGirth;
localDiameter := record.localDiameter;
if localDiameter > diameter then
diameter := localDiameter;
fi;
if localGirth <> -1 and localGirth < girth then
girth := localGirth;
fi;
od;
# Checks to ensure both components are valid
if not IsStronglyConnectedDigraph(D) then
diameter := fail;
fi;
if DigraphHasLoops(D) then
girth := 1;
elif IsMultiDigraph(D) then
girth := 2;
fi;
SetDigraphDiameter(D, diameter);
SetDigraphUndirectedGirth(D, girth);
return rec(diameter := diameter, girth := girth);
end);
InstallMethod(DigraphDiameter, "for a digraph", [IsDigraph],
function(D)
IsValidDigraph(D);
if not IsStronglyConnectedDigraph(D) then
# Diameter undefined
return fail;
elif HasDigraphGroup(D) and Size(DigraphGroup(D)) > 1 then
# Use the group to calculate the diameter
return DIGRAPHS_DiameterAndUndirectedGirth(D).diameter;
fi;
# Use the C function
return DIGRAPH_DIAMETER(D);
end);
InstallMethod(DigraphUndirectedGirth, "for a digraph", [IsDigraph],
function(D)
IsValidDigraph(D);
# This is only defined on undirected graphs (i.e. symmetric digraphs)
if not IsSymmetricDigraph(D) then
ErrorNoReturn("the argument <D> must be a symmetric digraph,");
fi;
if DigraphHasLoops(D) then
# A loop is a cycle of length 1
return 1;
elif IsMultiDigraph(D) then
# A pair of multiple edges is a cycle of length 2
return 2;
fi;
# Otherwise D is simple
return DIGRAPHS_DiameterAndUndirectedGirth(D).girth;
end);
InstallMethod(DigraphGirth, "for a dense digraph", [IsDenseDigraphRep],
function(D)
local verts, girth, out, dist, i, j;
IsValidDigraph(D);
if DigraphHasLoops(D) then
return 1;
fi;
# Only consider one vertex from each orbit
if HasDigraphGroup(D) and not IsTrivial(DigraphGroup(D)) then
verts := DigraphOrbitReps(D);
else
verts := DigraphVertices(D);
fi;
girth := infinity;
out := OutNeighbours(D);
for i in verts do
for j in out[i] do
dist := DigraphShortestDistance(D, j, i);
# distance [j,i] + 1 equals the cycle length
if dist <> fail and dist + 1 < girth then
girth := dist + 1;
if girth = 2 then
return girth;
fi;
fi;
od;
od;
return girth;
end);
InstallMethod(DigraphLongestSimpleCircuit, "for a digraph", [IsDigraph],
function(D)
local circs, lens, max;
IsValidDigraph(D);
if IsAcyclicDigraph(D) then
return fail;
fi;
circs := DigraphAllSimpleCircuits(D);
lens := List(circs, Length);
max := Maximum(lens);
return circs[Position(lens, max)];
end);
# TODO (FLS): I've just added 1 as the edge label here, is this really desired?
InstallMethod(DigraphSymmetricClosure, "for a dense mutable digraph",
[IsDenseDigraphRep and IsMutableDigraph],
function(D)
local n, m, verts, mat, out, x, i, j, k;
n := DigraphNrVertices(D);
if n <= 1 or (HasIsSymmetricDigraph(D) and IsSymmetricDigraph(D)) then
return D;