I hve a set S consisting of four terms.

S = {a b, c d, c e a, a b c, b c d}

I need a code to delete terms within the set S when the following condition is met: delete any terms containing a two-element term also in the set.

For example

$a\,b \subset a\,b\,c,\ c\,d \subset b\,c\,d$

then we delete a b c and b c d, reducing S to {a b,c d,c e a}

Please help.

  • $\begingroup$ But a b contains a two-element term also in the set (a b). Why didn't you delete that? Also: never use an upper-case letter to denote anything in Mathematica as it may conflict with the naming conventions in the language. $\endgroup$ – David G. Stork Jul 11 '17 at 1:19
  • $\begingroup$ Are the a, b, c, etc. supposed to commute? $\endgroup$ – JEM_Mosig Jul 11 '17 at 2:37

It's unclear what the Mathematica form of the set is. I will assume that the set elements are just symbols:


{ab, cd, cea}

Update to new form of $S$

    {a b, c d, c e a, a b c, b c d},

{a b, c d, a c e}

This assumes that shorter symbols are ordered before longer symbols.


Build a graph firstly

g = RelationGraph[StringContainsQ[##] && UnsameQ[##] &, ToString /@ S]

Mathematica graphics

The sink vertex is what you are after


{a c e, c d, a b}



S = {a b, c d, c e a, a b c, b c d};
sets = List @@ S; 
supersets[s_] := Select[sets, Intersection[#, s] == s && Length[#] > 2 &];
tobedeleted = Flatten[Map[supersets, Select[sets, Length[#] == 2 &]], 1];
DeleteCases[sets, Alternatives @@ tobedeleted]

gives you {a b, c d, a c e}

Test this carefully before you depend on it


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy