# Delete terms from set that met a stated condition

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}

• 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. – David G. Stork Jul 11 '17 at 1:19
• Are the a, b, c, etc. supposed to commute? – 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:

DeleteDuplicates[
{ab,cd,cea,abc,bcd},
StringContainsQ[SymbolName[#2],SymbolName[#1]]&
]


{ab, cd, cea}

Update to new form of $S$

DeleteDuplicates[
{a b, c d, c e a, a b c, b c d},
Denominator[#2/#1]==1&
]


{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] The sink vertex is what you are after

GraphComputationSinkVertexList[g]


{a c e, c d, a b}

This

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