From dbbffcca361ddb77b090a680e8c651a4303bb652 Mon Sep 17 00:00:00 2001 From: Finn Smith Date: Tue, 19 Feb 2019 16:47:31 +0000 Subject: [PATCH] add DotHighlightedDigraph method --- doc/display.xml | 28 ++++++++++++++ doc/z-chap9.xml | 1 + gap/display.gd | 3 ++ gap/display.gi | 84 ++++++++++++++++++++++++++++++++++++++++ tst/standard/display.tst | 28 ++++++++++++++ 5 files changed, 144 insertions(+) diff --git a/doc/display.xml b/doc/display.xml index e58dbf879..ad2418a5e 100644 --- a/doc/display.xml +++ b/doc/display.xml @@ -244,3 +244,31 @@ gap> FileString("dot/preset.dot", DotProrderDigraph(gr)); <#/GAPDoc> + +<#GAPDoc Label="DotHighlightedDigraph"> + + + A string. + + DotHighlightedDigraph produces a graphical represenation of the + digraph digraph, where the vertices in the list verts, and + edges between them, are drawn with colour colour1 and all other + vertices and edges in digraph are drawn with colour colour2. + If colour1 and colour2 are not given then + DotHighlightedDigraph uses black and grey respectively.

+ + Note that DotHighlightedDigraph does not validate the colours + colour1 and colour2 - consult the GraphViz documentation to + see what is available. + + See for more details on the output.

+ + digraph := Digraph([[2, 3], [2], [1, 3]]); + +gap> FileString("dot/my_digraph.dot", +> DotHighlightedDigraph(digraph, [1, 2], "red", "black")); +264]]> + + +<#/GAPDoc> diff --git a/doc/z-chap9.xml b/doc/z-chap9.xml index 885769b44..e4ad3eb1a 100644 --- a/doc/z-chap9.xml +++ b/doc/z-chap9.xml @@ -6,6 +6,7 @@ <#Include Label="DotSymmetricDigraph"> <#Include Label="DotPartialOrderDigraph"> <#Include Label="DotPreorderDigraph"> + <#Include Label="DotHighlightedDigraph">

Reading and writing graphs to a file diff --git a/gap/display.gd b/gap/display.gd index 1fbdb423a..c422f9b07 100644 --- a/gap/display.gd +++ b/gap/display.gd @@ -14,3 +14,6 @@ DeclareAttribute("DotSymmetricDigraph", IsDigraph); DeclareAttribute("DotPartialOrderDigraph", IsDigraph); DeclareAttribute("DotPreorderDigraph", IsDigraph); DeclareSynonym("DotQuasiorderDigraph", DotPreorderDigraph); +DeclareOperation("DotHighlightedDigraph", [IsDigraph, IsList]); +DeclareOperation("DotHighlightedDigraph", + [IsDigraph, IsList, IsString, IsString]); diff --git a/gap/display.gi b/gap/display.gi index 7c6ce9305..ca68a93bc 100644 --- a/gap/display.gi +++ b/gap/display.gi @@ -271,3 +271,87 @@ function(digraph) Append(str, "}"); return str; end); + +InstallMethod(DotHighlightedDigraph, "for a digraph and list", +[IsDigraph, IsList], +function(digraph, list) + return DotHighlightedDigraph(digraph, list, "black", "grey"); +end); + +InstallMethod(DotHighlightedDigraph, "for a digraph, list, and two strings", +[IsDigraph, IsList, IsString, IsString], +function(graph, highverts, highcolour, lowcolour) + local lowverts, out, str, i, j; + + if not IsSubset(DigraphVertices(graph), highverts) then + ErrorNoReturn("Digraphs: DotHighlightedDigraph: usage,\n", + "the second argument must be a list of vertices of the ", + "first argument,"); + fi; + + if IsEmpty(highcolour) then + ErrorNoReturn("Digraphs: DotHighlightedDigraph: usage,\n", + "the third argument must be a string containing the name ", + "of a colour,"); + fi; + + if IsEmpty(lowcolour) then + ErrorNoReturn("Digraphs: DotHighlightedDigraph: usage,\n", + "the fourth argument must be a string containing the name ", + "of a colour,"); + fi; + + lowverts := Difference(DigraphVertices(graph), highverts); + out := OutNeighbours(graph); + str := "//dot\n"; + + Append(str, "digraph hgn{\n"); + + Append(str, "subgraph lowverts{\n"); + Append(str, Concatenation("node [shape=circle, color=", + lowcolour, + "]\n edge [color=", + lowcolour, + "]\n")); + + for i in lowverts do + Append(str, Concatenation(String(i), "\n")); + od; + + Append(str, "}\n"); + + Append(str, "subgraph highverts{\n"); + Append(str, Concatenation("node [shape=circle, color=", + highcolour, + "]\n edge [color=", + highcolour, + "]\n")); + + for i in highverts do + Append(str, Concatenation(String(i), "\n")); + od; + + Append(str, "}\n"); + + Append(str, "subgraph lowverts{\n"); + for i in lowverts do + for j in out[i] do + Append(str, Concatenation(String(i), " -> ", String(j), "\n")); + od; + od; + Append(str, "}\n"); + + Append(str, "subgraph highverts{\n"); + for i in highverts do + for j in out[i] do + Append(str, Concatenation(String(i), " -> ", String(j))); + if j in lowverts then + Append(str, Concatenation(" [color=", lowcolour, "]")); + fi; + Append(str, "\n"); + od; + od; + Append(str, "}\n}\n"); + + return str; +end); diff --git a/tst/standard/display.tst b/tst/standard/display.tst index c7171304c..0011359ef 100644 --- a/tst/standard/display.tst +++ b/tst/standard/display.tst @@ -201,6 +201,34 @@ node [shape=Mrecord, height=0.5, fixedsize=true]ranksep=1; 2 -> 1 } +# DotHighlightedDigraph +gap> gr := Digraph([[2, 3], [2], [1, 3]]); + +gap> Print(DotHighlightedDigraph(gr, [1, 2], "red", "black")); +//dot +digraph hgn{ +subgraph lowverts{ +node [shape=circle, color=black] + edge [color=black] +3 +} +subgraph highverts{ +node [shape=circle, color=red] + edge [color=red] +1 +2 +} +subgraph lowverts{ +3 -> 1 +3 -> 3 +} +subgraph highverts{ +1 -> 2 +1 -> 3 [color=black] +2 -> 2 +} +} + # DIGRAPHS_UnbindVariables gap> Unbind(adj); gap> Unbind(dot);