Deleting subsets in the list of sets

161 Views Asked by At

If we have a list like $[[2,1],[5,2,1],[6,5,2,1],[9,10]]$ and I want to find the result in the form of $[[6,5,2,1], [9,10]]$ and want to delete all those list that are contained in another. How may I program this

I do like let FV := $[[2,1],[5,2,1],[6,5,2,1],[9,10]]$;

Intersection:=function(FV)
  local A, B, a;
  for A in FV do
    for B in FV do
      if Intersection(A,B) = B then
        a := Position(FV,B);
        Unbind(FV[a]);
      fi;
    od;
  od;
  return FV;
end;

However, I find the empty list after running this program. I need the answer like $[[6,5,2,1],[9,10]]$

1

There are 1 best solutions below

2
On BEST ANSWER

It can be achieved as follows:

smaz_Intersection:=function(FV)
  return Filtered(FV,L->not ForAny(FV,M->M<>L and IsSubset(M,L)));
end;;

In the running example:

gap> A:=[[2,1],[5,2,1],[6,5,2,1],[9,10]];;
gap> smaz_Intersection(A);
[ [ 6, 5, 2, 1 ], [ 9, 10 ] ]

Note: Intersection is a poor choice of function name: GAP already has an Intersection function and your Intersection function looks like it's intending to use GAP's Intersection function. This might have some unpredictable results.