Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partially implement vertex colors #189

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions gap/attr.gi
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,16 @@ end);

InstallMethod(ReducedDigraph, "for an immutable digraph", [IsImmutableDigraph],
function(D)
local C;
if IsConnectedDigraph(D) then
return D;
fi;
D := ReducedDigraph(DigraphMutableCopy(D));
return MakeImmutableDigraph(D);
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",
Expand Down Expand Up @@ -332,6 +337,9 @@ function(D)
if HasDigraphGroup(D) then
SetDigraphGroup(C, DigraphGroup(D));
fi;
if IsVertexColoredDigraph(D) then
MakeVertexColoredDigraph(C, DigraphVertexColors(D));
fi;
SetDigraphDualAttr(D, C);
return C;
end);
Expand Down Expand Up @@ -1472,6 +1480,9 @@ function(D)
fi;
C := DigraphMutableCopy(D);
C := MakeImmutableDigraph(MaximalSymmetricSubdigraph(C));
if IsVertexColoredDigraph(D) then
MakeVertexColoredDigraph(C, DigraphVertexColors(D));
fi;
SetDigraphVertexLabels(C, DigraphVertexLabels(D));
SetMaximalSymmetricSubdigraphAttr(D, C);
return C;
Expand Down
36 changes: 19 additions & 17 deletions gap/digraph.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ DeclareCategory("IsDigraphWithAdjacencyFunction", IsDigraph);
DeclareSynonym("IsMutableDigraph", IsDigraph and IsMutable);
DeclareCategory("IsImmutableDigraph", IsDigraph);

DeclareCategory("IsVertexColoredDigraph", IsImmutableDigraph);
DeclareAttribute("DigraphVertexColors", IsVertexColoredDigraph);

DeclareGlobalFunction("IsValidDigraph");

# Family
Expand All @@ -34,12 +37,10 @@ DeclareOperation("ConvertToImmutableDigraphNC", [IsList]);

DeclareConstructor("DigraphConsNC", [IsDigraph, IsRecord]);
DeclareConstructor("DigraphConsNC", [IsDigraph, IsDenseList]);
DeclareConstructor("DigraphConsNC",
[IsVertexColoredDigraph, IsObject, IsDenseList]);

DeclareOperation("DigraphNC", [IsFunction, IsRecord]);
DeclareOperation("DigraphNC", [IsFunction, IsDenseList]);

DeclareOperation("DigraphNC", [IsRecord]);
DeclareOperation("DigraphNC", [IsDenseList]);
DeclareGlobalFunction("DigraphNC");

# Copies
DeclareOperation("DigraphMutableCopy", [IsDigraph]);
Expand All @@ -50,25 +51,26 @@ DeclareOperation("DigraphCopyIfImmutable", [IsDigraph]);

# Converter
DeclareOperation("MakeImmutableDigraph", [IsDigraph]);
DeclareOperation("MakeVertexColoredDigraph", [IsImmutableDigraph, IsDenseList]);

# Constructors
DeclareConstructor("DigraphCons", [IsDigraph, IsRecord]);
DeclareConstructor("DigraphCons", [IsDigraph, IsDenseList]);
DeclareConstructor("DigraphCons", [IsDigraph, IsList, IsFunction]);
DeclareConstructor("DigraphCons", [IsDigraph, IsInt, IsList, IsList]);
DeclareConstructor("DigraphCons", [IsDigraph, IsList, IsList, IsList]);

DeclareOperation("Digraph", [IsFunction, IsRecord]);
DeclareOperation("Digraph", [IsFunction, IsList]);
DeclareOperation("Digraph", [IsFunction, IsList, IsFunction]);
DeclareOperation("Digraph", [IsFunction, IsInt, IsList, IsList]);
DeclareOperation("Digraph", [IsFunction, IsList, IsList, IsList]);

DeclareOperation("Digraph", [IsRecord]);
DeclareOperation("Digraph", [IsList]);
DeclareOperation("Digraph", [IsList, IsFunction]);
DeclareOperation("Digraph", [IsInt, IsList, IsList]);
DeclareOperation("Digraph", [IsList, IsList, IsList]);
DeclareConstructor("DigraphCons",
[IsVertexColoredDigraph, IsObject, IsDenseList]);
DeclareConstructor("DigraphCons",
[IsVertexColoredDigraph, IsList, IsFunction, IsDenseList]);
DeclareConstructor("DigraphCons",
[IsVertexColoredDigraph, IsInt, IsList, IsList,
IsDenseList]);
DeclareConstructor("DigraphCons",
[IsVertexColoredDigraph, IsList, IsList, IsList,
IsDenseList]);

DeclareGlobalFunction("Digraph");

# Constructors "by" something . . .
DeclareConstructor("DigraphByAdjacencyMatrixCons",
Expand Down
194 changes: 131 additions & 63 deletions gap/digraph.gi
Original file line number Diff line number Diff line change
Expand Up @@ -136,26 +136,26 @@ function(filt, list)
return MakeImmutableDigraph(DigraphConsNC(IsMutableDigraph, list));
end);

InstallMethod(DigraphNC, "for a function and a dense list",
[IsFunction, IsDenseList],
function(func, list)
return DigraphConsNC(func, list);
end);

InstallMethod(DigraphNC, "for a dense list", [IsDenseList],
function(list)
return DigraphConsNC(IsImmutableDigraph, list);
end);

InstallMethod(DigraphNC, "for a function and a record",
[IsFunction, IsRecord],
function(func, record)
return DigraphConsNC(func, record);
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);

InstallMethod(DigraphNC, "for a record", [IsRecord],
function(record)
return DigraphConsNC(IsImmutableDigraph, record);
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);

########################################################################
Expand All @@ -182,6 +182,19 @@ function(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);

Expand All @@ -208,6 +221,16 @@ function(D)
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
########################################################################
Expand Down Expand Up @@ -369,6 +392,9 @@ function(filt, record)
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));
Expand Down Expand Up @@ -408,58 +434,50 @@ function(filt, domain, src, ran)
return MakeImmutableDigraph(DigraphCons(IsMutableDigraph, domain, src, ran));
end);

InstallMethod(Digraph, "for a filter and a record", [IsFunction, IsRecord],
function(filt, record)
return DigraphCons(filt, record);
end);

InstallMethod(Digraph, "for a record", [IsRecord],
function(record)
return DigraphCons(IsImmutableDigraph, record);
end);

InstallMethod(Digraph, "for a filter and a list", [IsFunction, IsList],
function(func, list)
return DigraphCons(func, list);
end);

InstallMethod(Digraph, "for a list", [IsList],
function(list)
return DigraphCons(IsImmutableDigraph, list);
end);

InstallMethod(Digraph, "for a filter, a list, and a function",
[IsFunction, IsList, IsFunction],
function(filt, list, func)
return DigraphCons(filt, list, func);
end);

InstallMethod(Digraph, "for a list and a function", [IsList, IsFunction],
function(list, func)
return DigraphCons(IsImmutableDigraph, list, func);
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(Digraph, "for a filter, integer, list, and list",
[IsFunction, IsInt, IsList, IsList],
function(filt, n, src, ran)
return DigraphCons(filt, n, src, ran);
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(Digraph, "for an integer, list, and list",
[IsInt, IsList, IsList],
function(n, src, ran)
return DigraphCons(IsImmutableDigraph, n, src, ran);
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(Digraph, "for a filter, list, list, and list",
[IsFunction, IsList, IsList, IsList],
function(filt, dom, src, ran)
return DigraphCons(filt, dom, src, ran);
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);

InstallMethod(Digraph, "for a list, list, and list", [IsList, IsList, IsList],
function(dom, src, ran)
return DigraphCons(IsImmutableDigraph, dom, src, ran);
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);

########################################################################
Expand All @@ -476,7 +494,9 @@ function(D)
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;
Expand Down Expand Up @@ -516,7 +536,18 @@ function(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, ",
Expand All @@ -532,6 +563,16 @@ function(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
########################################################################
Expand All @@ -550,6 +591,33 @@ function(C, 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
########################################################################
Expand Down
Loading