-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathdisplay.gi
More file actions
316 lines (273 loc) · 10.4 KB
/
display.gi
File metadata and controls
316 lines (273 loc) · 10.4 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
#############################################################################
##
## display.gi
## Copyright (C) 2014-24 James D. Mitchell
##
## Licensing information can be found in the README file of this package.
##
#############################################################################
##
# TODO:
# * add graph6 string or whatever as a comment at the start of the string
# * check JupyterInterface Splash function
# * for edge colored non-digraphs, should ensure that the edge colors are
# symmetric, i.e. the same colors for x -> y and y -> x
#############################################################################
# Graphs and digraphs
#############################################################################
InstallOtherMethod(GraphvizDigraph, "for a digraph by out-neighbours",
[IsDigraphByOutNeighboursRep],
function(D)
local gv, x, y;
gv := GraphvizDigraph("hgn");
GraphvizSetAttr(gv, "node [shape=circle]");
for x in DigraphVertices(D) do
GraphvizAddNode(gv, x);
od;
for x in DigraphVertices(D) do
for y in OutNeighboursOfVertexNC(D, x) do
GraphvizAddEdge(gv, x, y);
od;
od;
return gv;
end);
InstallOtherMethod(GraphvizGraph, "for a digraph by out-neighbours",
[IsDigraphByOutNeighboursRep],
function(D)
local gv, x, y;
if not IsSymmetricDigraph(D) then
ErrorNoReturn("the argument (a digraph) must be symmetric");
fi;
gv := GraphvizGraph("hgn");
GraphvizSetAttr(gv, "node [shape=circle]");
for x in DigraphVertices(D) do
GraphvizAddNode(gv, x);
od;
for x in DigraphVertices(D) do
for y in OutNeighboursOfVertexNC(D, x) do
if x > y then
GraphvizAddEdge(gv, x, y);
fi;
od;
od;
return gv;
end);
#############################################################################
# Vertex coloured graphs and digraphs
#############################################################################
InstallMethod(GraphvizVertexColoredDigraph, "for a digraph and a list",
[IsDigraph, IsList],
{D, colors} -> GraphvizSetNodeColors(GraphvizDigraph(D), colors));
InstallMethod(GraphvizVertexColoredGraph, "for a digraph and a list",
[IsDigraph, IsList],
# IsSymmetricDigraph checked by GraphvizGraph
{D, colors} -> GraphvizSetNodeColors(GraphvizGraph(D), colors));
#############################################################################
# Edge coloured graphs and digraphs
#############################################################################
# This function is here rather than graphviz b/c otherwise if D has multiple
# edges we can't reliably get the corresponding graphviz edge from the head and
# tail of the edge from gv.
BindGlobal("DIGRAPHS_ErrorIfNotEdgeColoring",
function(D, colors)
local out, i;
out := OutNeighbours(D);
if Length(colors) <> Length(out) then
ErrorFormatted("the 2nd argument (edge colors) must have ",
"the same number of entries as the 1st argument ",
"(a digraph) has nodes, expected {} but found {}",
Length(out),
Length(colors));
fi;
for i in [1 .. Length(colors)] do
if not IsList(colors[i]) then
ErrorFormatted("the 2nd argument (edge colors) must be ",
"a list of lists, found {} in position {}",
TNAM_OBJ(colors[i]),
i);
elif Length(out[i]) <> Length(colors[i]) then
ErrorFormatted("the 2nd argument (edge colors) must have ",
"the same shape as the out neighbours of the 1st ",
"argument (a digraph), in position {} expected ",
"a list of length {} but found list of length {}",
i,
Length(out[i]),
Length(colors[i]));
fi;
Perform(colors[i], ErrorIfNotValidColor);
od;
end);
BindGlobal("DIGRAPHS_AddEdgesAndColorsNC",
function(D, gv, colors)
local out, e, n, i;
# This duplicates code in the GraphvizDigraph function because otherwise if D
# has multiple edges we can't reliably get the corresponding graphviz edge
# from the head and tail of the edge from gv.
out := OutNeighbours(D);
for n in DigraphVertices(D) do
for i in [1 .. Length(out[n])] do
if IsGraphvizDigraph(gv) or n > out[n][i] then
e := GraphvizAddEdge(gv, n, out[n][i]);
GraphvizSetAttr(e, "color", colors[n][i]);
fi;
od;
od;
return gv;
end);
InstallMethod(GraphvizEdgeColoredDigraph,
"for a digraph by out-neighbours and a list",
[IsDigraphByOutNeighboursRep, IsList],
function(D, colors)
local gv;
DIGRAPHS_ErrorIfNotEdgeColoring(D, colors);
gv := GraphvizDigraph(NullDigraph(DigraphNrVertices(D)));
return DIGRAPHS_AddEdgesAndColorsNC(D, gv, colors);
end);
InstallMethod(GraphvizEdgeColoredGraph,
"for a digraph by out-neighbours and a list",
[IsDigraphByOutNeighboursRep, IsList],
function(D, colors)
local gv;
if not IsSymmetricDigraph(D) then
ErrorNoReturn("the argument (a digraph) must be symmetric");
fi;
DIGRAPHS_ErrorIfNotEdgeColoring(D, colors);
gv := GraphvizGraph(NullDigraph(DigraphNrVertices(D)));
return DIGRAPHS_AddEdgesAndColorsNC(D, gv, colors);
end);
#############################################################################
# Vertex and edge coloured graphs and digraphs
#############################################################################
InstallMethod(GraphvizColoredDigraph,
"for a digraph, list, and list",
[IsDigraph, IsList, IsList],
{D, n_colors, e_colors} -> GraphvizSetNodeColors(
GraphvizEdgeColoredDigraph(D, e_colors),
n_colors));
InstallMethod(GraphvizColoredGraph,
"for a digraph, list, and list",
[IsDigraph, IsList, IsList],
# IsSymmetricDigraph checked by GraphvizEdgeColoredGraph
{D, n_colors, e_colors} -> GraphvizSetNodeColors(
GraphvizEdgeColoredGraph(D, e_colors),
n_colors));
#############################################################################
# Vertex labelled graphs and digraphs
#############################################################################
InstallMethod(GraphvizVertexLabelledDigraph, "for a digraph",
[IsDigraph],
D -> GraphvizSetNodeLabels(GraphvizDigraph(D), DigraphVertexLabels(D)));
InstallMethod(GraphvizVertexLabelledGraph, "for a digraph",
[IsDigraph],
# symmetry checked in GraphvizGraph
D -> GraphvizSetNodeLabels(GraphvizGraph(D), DigraphVertexLabels(D)));
#############################################################################
# Partial and preorder digraphs
#############################################################################
InstallMethod(GraphvizPartialOrderDigraph, "for a partial order digraph",
[IsDigraph],
function(D)
if not IsPartialOrderDigraph(D) then
ErrorNoReturn("the argument (a digraph) must be a partial order");
fi;
D := DigraphMutableCopyIfMutable(D);
return GraphvizDigraph(DigraphReflexiveTransitiveReduction(D));
end);
InstallMethod(GraphvizPreorderDigraph, "for a preorder digraph",
[IsDigraph],
function(D)
local comps, gv, label, node, nodes, c, x, e;
if not IsPreorderDigraph(D) then
ErrorNoReturn("the argument (a digraph) must be a preorder");
fi;
# Quotient by the strongly connected components to get a partial order
# D and draw this without loops or edges implied by transitivity.
comps := DigraphStronglyConnectedComponents(D).comps;
D := DigraphMutableCopy(D);
DigraphRemoveAllMultipleEdges(QuotientDigraph(D, comps));
DigraphReflexiveTransitiveReduction(D);
gv := GraphvizDigraph("graphname");
GraphvizSetAttr(gv, "node [shape=\"Mrecord\"]");
GraphvizSetAttr(gv, "height=\"0.5\"");
GraphvizSetAttr(gv, "fixedsize=\"true\"");
GraphvizSetAttr(gv, "ranksep=\"1\"");
for c in [1 .. Length(comps)] do
label := "\"";
Append(label, String(comps[c][1]));
for x in comps[c]{[2 .. Length(comps[c])]} do
Append(label, "|");
Append(label, String(x));
od;
Append(label, "\"");
node := GraphvizAddNode(gv, c);
GraphvizSetAttr(node, "label", label);
GraphvizSetAttr(node, "width", Float(Length(comps[c]) / 2));
od;
nodes := GraphvizNodes(gv);
for e in DigraphEdges(D) do
GraphvizAddEdge(gv, nodes[e[1]], nodes[e[2]]);
od;
return gv;
end);
#############################################################################
# Highlighted subdigraphs
#############################################################################
BindGlobal("DIGRAPHS_GraphvizHighlight",
function(D, gv, hi_verts, hi, lo)
local node, color, out, nodes, edge, v, i, j;
if IsMultiDigraph(D) then
ErrorNoReturn("the 1st argument (a digraph) must not have multiple edges");
elif not IsSubset(DigraphVertices(D), hi_verts) then
ErrorNoReturn("the 2nd argument (list) must consist of vertices ",
"of the 1st argument (a digraph)");
fi;
ErrorIfNotValidColor(hi);
ErrorIfNotValidColor(lo);
GraphvizSetAttr(gv, "shape", "circle");
for v in DigraphVertices(D) do
node := GraphvizAddNode(gv, v);
if v in hi_verts then
color := hi;
else
color := lo;
fi;
GraphvizSetAttr(node, "color", color);
od;
out := OutNeighbours(D);
nodes := GraphvizNodes(gv);
for i in DigraphVertices(D) do
for j in out[i] do
if IsGraphvizDigraph(gv) or i > j then
edge := GraphvizAddEdge(gv, nodes[i], nodes[j]);
if i in hi_verts and j in hi_verts then
color := hi;
else
color := lo;
fi;
GraphvizSetAttr(edge, "color", color);
fi;
od;
od;
return gv;
end);
InstallMethod(GraphvizHighlightedDigraph,
"for a digraph by out-neighbours, list, and two strings",
[IsDigraphByOutNeighboursRep, IsList, IsString, IsString],
{D, hi_verts, hi, lo} ->
DIGRAPHS_GraphvizHighlight(D, GraphvizDigraph(), hi_verts, hi, lo));
InstallMethod(GraphvizHighlightedDigraph, "for a digraph and list",
[IsDigraph, IsList],
{D, list} -> GraphvizHighlightedDigraph(D, list, "black", "grey"));
InstallMethod(GraphvizHighlightedGraph,
"for a digraph by out-neighbours, list, and two strings",
[IsDigraphByOutNeighboursRep, IsList, IsString, IsString],
function(D, hi_verts, hi, lo)
if not IsSymmetricDigraph(D) then
ErrorNoReturn("the argument (a digraph) must be symmetric");
fi;
return DIGRAPHS_GraphvizHighlight(D, GraphvizGraph(), hi_verts, hi, lo);
end);
InstallMethod(GraphvizHighlightedGraph, "for a digraph and list",
[IsDigraph, IsList],
# IsSymmetricDigraph checked in GraphvizHighlightedGraph
{D, list} -> GraphvizHighlightedGraph(D, list, "black", "grey"));