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

cliques: some perf improvements #635

Merged
merged 1 commit into from
Apr 17, 2024
Merged
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
10 changes: 5 additions & 5 deletions doc/cliques.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

A <E>clique</E> of a digraph is a set of mutually adjacent vertices of the
digraph. Loops and multiple edges are ignored for the purpose of
determining the clique number of a digraph.
determining the clique number of a digraph.
<Example><![CDATA[
gap> D := CompleteDigraph(4);;
gap> CliqueNumber(D);
Expand Down Expand Up @@ -285,8 +285,8 @@ gap> IsSymmetricDigraph(D);
true
gap> G := AutomorphismGroup(D);
Group([ (5,6), (1,2), (1,5)(2,6)(3,4) ])
gap> DigraphMaximalCliques(D);
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 3, 4 ] ]
gap> AsSet(DigraphMaximalCliques(D));
[ [ 1, 2, 3 ], [ 3, 4 ], [ 4, 5, 6 ] ]
gap> DigraphMaximalCliquesReps(D);
[ [ 1, 2, 3 ], [ 3, 4 ] ]
gap> Orbit(G, [1, 2, 3], OnSets);
Expand All @@ -306,8 +306,8 @@ gap> IsSymmetricDigraph(D);
true
gap> G := AutomorphismGroup(D);
Group([ (5,6), (1,2), (1,5)(2,6)(3,4) ])
gap> DigraphMaximalCliques(D);
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 3, 4 ] ]]]></Example>
gap> AsSet(DigraphMaximalCliques(D));
[ [ 1, 2, 3 ], [ 3, 4 ], [ 4, 5, 6 ] ]]]></Example>
</Description>
</ManSection>
<#/GAPDoc>
Expand Down
49 changes: 31 additions & 18 deletions gap/cliques.gi
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@
#############################################################################
##

BindGlobal("AddOrbitToHashMap",
function(G, set, hashmap)
local gens, o, im, pt, g;

gens := GeneratorsOfGroup(G);
o := [set];
Assert(1, not set in hashmap);
hashmap[set] := true;
for pt in o do
for g in gens do
im := OnSets(pt, g);
if not im in hashmap then
hashmap[im] := true;
Add(o, im);
fi;
od;
od;
return o;
end);

InstallMethod(CliqueNumber, "for a digraph", [IsDigraph],
D -> Maximum(List(DigraphMaximalCliquesReps(D), Length)));

Expand Down Expand Up @@ -474,7 +494,7 @@ DigraphMaximalCliques);

InstallGlobalFunction(DigraphMaximalCliques,
function(arg)
local D, include, exclude, limit, size, cliques, sub, G, out, orbits, orb, c;
local D, include, exclude, limit, size, cliques, sub, G, out, orbits, c;

if IsEmpty(arg) then
ErrorNoReturn("there must be at least 1 argument,");
Expand Down Expand Up @@ -519,16 +539,13 @@ function(arg)
out := cliques;
else
# Act on the representatives to find all
orbits := [];
out := [];
orbits := HashMap();
for c in cliques do
if not ForAny(orbits, x -> c in x) then
orb := Orb(G, c, OnSets);
Enumerate(orb);
Add(orbits, orb);
Append(out, orb);
if not c in orbits then
AddOrbitToHashMap(G, c, orbits);
fi;
od;
out := Keys(orbits);
fi;
if IsImmutableDigraph(D) then
SetDigraphMaximalCliquesAttr(D, out);
Expand Down Expand Up @@ -673,7 +690,7 @@ function(digraph, hook, user_param, limit, include, exclude, max, size, reps)
else

# Function to find the valid cliques of an orbit given an orbit rep
found_orbits := [];
found_orbits := HashMap();
num_found := 0;
if hook = fail then
hook := Add;
Expand All @@ -683,10 +700,8 @@ function(digraph, hook, user_param, limit, include, exclude, max, size, reps)
local orbit, n, new_found, i;

new_found := 0;
if not ForAny(found_orbits, x -> clique in x) then
orbit := Orb(group, clique, OnSets);
Enumerate(orbit);
Add(found_orbits, orbit);
if not clique in found_orbits then
orbit := AddOrbitToHashMap(group, clique, found_orbits);
n := Length(orbit);

if invariant_include and invariant_exclude then
Expand Down Expand Up @@ -860,7 +875,7 @@ function(D, hook, param, lim, inc, exc, max, size, reps, inc_var, exc_var)
grp := AutomorphismGroup(D);
fi;

found_orbits := [];
found_orbits := HashMap();

# Function to find the valid cliques of an orbit given an orbit rep
add := function(c)
Expand All @@ -871,10 +886,8 @@ function(D, hook, param, lim, inc, exc, max, size, reps, inc_var, exc_var)
hook(param, c);
num := num + 1;
return;
elif not ForAny(found_orbits, x -> c in x) then
orb := Orb(grp, c, OnSets);
Enumerate(orb);
Add(found_orbits, orb);
elif not c in found_orbits then
orb := AddOrbitToHashMap(grp, c, found_orbits);
n := Length(orb);

if invariant then # we're not just looking for orbit reps, but inc and
Expand Down
28 changes: 28 additions & 0 deletions tst/extreme/cliques.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#############################################################################
##
#W extreme/cliques.tst
#Y Copyright (C) 2024 James D. Mitchell
##
## Licensing information can be found in the README file of this package.
##
#############################################################################
##
gap> START_TEST("Digraphs package: extreme/cliques.tst");
gap> LoadPackage("digraphs", false);;

#
gap> DIGRAPHS_StartTest();
gap> D :=
> DigraphFromGraph6String("hJ\\zz{vJw~jvV^]^mvz\\~N^|{~xt~zJ~|N~~Y~~yf\
> ~~m~~~]~~~HR|nbmm~w^N}~Rmzv{inlnwcezjyBx{F{w}p~~VM]~~zH}T~~mvz~^~Lv~\
> }~~Y~~z^~nNv~n~yf~~n~~???????");
<immutable symmetric digraph with 41 vertices, 1212 edges>
gap> Length(DigraphCliques(D));
1651734

# DIGRAPHS_UnbindVariables
gap> Unbind(D);

#
gap> DIGRAPHS_StopTest();
gap> STOP_TEST("Digraphs package: standard/cliques.tst", 0);
47 changes: 23 additions & 24 deletions tst/standard/cliques.tst
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,15 @@ gap> DigraphIndependentSets(gr, [], [], 1, 1);
gap> gr := CompleteDigraph(10);;
gap> DigraphMaximalIndependentSetsRepsAttr(gr);
[ [ 1 ] ]
gap> DigraphMaximalIndependentSetsAttr(gr);
gap> Set(DigraphMaximalIndependentSetsAttr(gr));
[ [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ], [ 8 ], [ 9 ], [ 10 ] ]

# DigraphMaximalIndependentSets and DigraphIndependentSets
gap> gr := ChainDigraph(2);;
gap> DigraphMaximalIndependentSets(gr);
gap> Set(DigraphMaximalIndependentSets(gr));
[ [ 1 ], [ 2 ] ]
gap> gr := CompleteDigraph(2);;
gap> DigraphMaximalIndependentSets(gr);
gap> Set(DigraphMaximalIndependentSets(gr));
[ [ 1 ], [ 2 ] ]
gap> gr := DigraphFromDigraph6String("&FWsK?WSKC?");
<immutable digraph with 7 vertices, 14 edges>
Expand Down Expand Up @@ -269,11 +269,11 @@ gap> DigraphMaximalCliquesReps();
Error, there must be at least 1 argument,
gap> DigraphMaximalCliquesReps(gr);
[ [ 1 ] ]
gap> DigraphMaximalCliquesAttr(gr);
gap> Set(DigraphMaximalCliquesAttr(gr));
[ [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ] ]
gap> DigraphMaximalCliques();
Error, there must be at least 1 argument,
gap> DigraphMaximalCliques(gr);
gap> Set(DigraphMaximalCliques(gr));
[ [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ] ]
gap> gr := EmptyDigraph(1);;
gap> DigraphMaximalCliques(gr);
Expand Down Expand Up @@ -455,18 +455,18 @@ gap> gr := DigraphFromSparse6String(":~?@c__EC?_F");
<immutable symmetric digraph with 100 vertices, 6 edges>
gap> DigraphMaximalCliquesReps(gr);
[ [ 1 ], [ 2, 3, 5 ] ]
gap> DigraphMaximalCliques(gr);
[ [ 1 ], [ 4 ], [ 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 ], [ 2, 3, 5 ] ]
gap> Set(DigraphMaximalCliques(gr));
[ [ 1 ], [ 2, 3, 5 ], [ 4 ], [ 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 ] ]

# Test CliqueNumber
gap> CliqueNumber(NullDigraph(10));
Expand Down Expand Up @@ -497,8 +497,7 @@ gap> cliques := DigraphMaximalCliquesReps(D);
[ [ 1, 2, 3 ], [ 3, 4 ] ]
gap> IsMutable(cliques) or ForAny(cliques, IsMutable);
false
gap> cliques := DigraphMaximalCliques(D);
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 3, 4 ] ]
gap> cliques := DigraphMaximalCliques(D);;
gap> IsMutable(cliques) or ForAny(cliques, IsMutable);
false
gap> cliques := DigraphMaximalCliques(D, [1]);
Expand All @@ -512,17 +511,17 @@ gap> CliquesFinder(
[ [ 1 ] ]
gap> gr := DigraphSymmetricClosure(ChainDigraph(513));
<immutable symmetric digraph with 513 vertices, 1024 edges>
gap> CliquesFinder(gr, fail, [], 4, [], [], true, fail, false);
gap> CliquesFinder(gr, fail, [], 4, [], [], true, fail, false);
[ [ 1, 2 ], [ 512, 513 ], [ 2, 3 ], [ 511, 512 ] ]
gap> CliquesFinder(gr, fail, [], 4, [1], [], true, fail, false);
gap> CliquesFinder(gr, fail, [], 4, [1], [], true, fail, false);
[ [ 1, 2 ] ]
gap> CliquesFinder(gr, fail, [], 4, [], [1], true, fail, false);
gap> CliquesFinder(gr, fail, [], 4, [], [1], true, fail, false);
[ [ 512, 513 ], [ 2, 3 ], [ 511, 512 ], [ 3, 4 ] ]
gap> CliquesFinder(gr, fail, [], 4, [1], [2], true, fail, false);
[ ]
gap> CliquesFinder(gr, fail, [], 4, [1], [3], true, fail, false);
[ [ 1, 2 ] ]
gap> CliquesFinder(gr, fail, [], 4, [1, 2], [], false, 1, false);
gap> CliquesFinder(gr, fail, [], 4, [1, 2], [], false, 1, false);
[ ]
gap> CliquesFinder(gr, fail, [], 4, [1, 513], [], false, 1, false);
[ ]
Expand Down Expand Up @@ -590,7 +589,7 @@ nteger,
gap> DigraphsCliquesFinder(NullDigraph(1), fail, [], 4, [], [], true, fail, 0);
Error, the 9th argument <aut_grp> must be a permutation group or fail, not int\
eger,
gap> DigraphsCliquesFinder(NullDigraph(3), fail, [], 4, [], [], true, fail,
gap> DigraphsCliquesFinder(NullDigraph(3), fail, [], 4, [], [], true, fail,
> Group((1, 2, 3), (1, 2), (1, 3)));
Error, expected at most 2 generators in the 9th argument but got 3,
gap> DigraphsCliquesFinder(NullDigraph(2), fail, [], 4, [], [], true, fail, SymmetricGroup(3));
Expand Down
Loading