Skip to content

Commit ce57e10

Browse files
Add RootsOfPartitionDS
There is already the IteratorOfRootsOfPartitionDS, but sometimes we just want the roots as a list. This operation is essentially the same as IteratorOfRootsOfPartitionDS but without the overhead of the iterator code.
1 parent 1551555 commit ce57e10

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

gap/union-find.gd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ DeclareOperation("Unite",[IsPartitionDS and IsMutable, IsPosInt, IsPosInt]);
9595
#! @Returns an iterator
9696
DeclareOperation("RootsIteratorOfPartitionDS", [IsPartitionDS]);
9797

98+
#! @Description
99+
#! Returns a list of the canonical representatives of the parts
100+
#! of the partition <A>unionfind</A>.
101+
#! @Arguments unionfind
102+
#! @Returns A list.
103+
DeclareOperation("RootsOfPartitionDS", [IsPartitionDS]);
104+
98105
#! @Description
99106
#! Returns the number of parts of the partition <A>unionfind</A>.
100107
#! @Arguments unionfind

gap/union-find.gi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,26 @@ InstallMethod(RootsIteratorOfPartitionDS, [IsPartitionDSRep and IsPartitionDS],
148148
end));
149149
end);
150150

151+
InstallMethod(RootsOfPartitionDS, [IsPartitionDSRep and IsPartitionDS],
152+
function(uf)
153+
local pt, gp, data, n, result;
154+
pt := 0;
155+
gp := UF.getParent;
156+
data := uf!.data;
157+
n := SizeUnderlyingSetDS(uf);
158+
result := [];
159+
while pt <= n do
160+
pt := pt + 1;
161+
while pt <= n and gp(data[pt]) <> pt do
162+
pt := pt + 1;
163+
od;
164+
if pt <= n then
165+
Add(result, pt);
166+
fi;
167+
od;
168+
return result;
169+
end);
170+
151171
InstallMethod(PartsOfPartitionDS, [IsPartitionDS],
152172
function(u)
153173
local p, i, r, x;

tst/uf.tst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ gap> for x in i do Print(x,"\n"); od;
5757
8
5858
9
5959
10
60+
gap> RootsOfPartitionDS(u);
61+
[ 2, 4, 5, 6, 7, 8, 9, 10 ]
6062
gap> for x in i2 do Print(x,"\n"); od;
6163
4
6264
5
@@ -111,6 +113,8 @@ gap> Unite(u,9,12);
111113
gap> PartsOfPartitionDS(u);
112114
[ [ 1, 2, 3, 4 ], [ 5 ], [ 6 ], [ 7 ], [ 8 ], [ 9, 10, 11, 12 ], [ 13 ],
113115
[ 14 ], [ 15 ], [ 16 ] ]
116+
gap> RootsOfPartitionDS(u);
117+
[ 2, 5, 6, 7, 8, 10, 13, 14, 15, 16 ]
114118

115119
#
116120
gap> STOP_TEST( "uf.tst", 1);

0 commit comments

Comments
 (0)