I have some data like this:
data =
{{4, 18, 32}, {4, 23, 42}, {5, 17, 29}, {5, 18, 31}, {11, 22, 33},
{11, 23, 35}, {13, 18, 23}, {13, 23, 33}, {13, 31, 49}, {15, 22, 29},
{15, 23, 31}, {15, 32, 49}, {16, 25, 34}, {16, 29, 42}, {17, 25, 33},
{17, 33, 49}, {22, 32, 42}, {25, 31, 37}, {29, 32, 35}, {31, 34, 37},
{32, 37, 42}, {35, 42, 49}, {4, 13, 22, 31}, {5, 15, 25, 35}, {11, 13, 15, 17},
{13, 25, 37, 49}, {15, 16, 17, 18}, {25, 29, 33, 37}, {4, 11, 18, 25, 32},
{29, 31, 33, 35, 37}, {31, 32, 33, 34, 35}, {5, 11, 17, 23, 29, 35}};
Some list is the subset of other list, for example, {5, 17, 29}
is the subset of {5, 11, 17, 23, 29, 35}
, so I want to delete {5, 17, 29}
. My code is:
DeleteDuplicates[SortBy[data, -Length@# &], Length@#1 != Length@#2 && SubsetQ[#1, #2] &]
But it's too slow when the length of data greater than 1000. How to delete subsets quickly?
In my problem, all numbers are always integers and positive. The lists of data actually are arithmetic sequence.
Edit
[m_golberg]: I have deleted my answer because it fails on this more difficult data set:
test =
{{26, 4, 4, 7}, {12, 3, 36, 6, 33}, {0, 29, 6, 22, 27}, {34, 24}, {24, 36},
{29, 29, 2, 33, 24}, {35, 20, 41, 2, 39}, {19, 1}, {20, 40}, {3}, {18}, {18, 10},
{4}, {32, 8}, {6, 34, 39, 30, 29}, {41}, {20, 38, 15, 18, 14},
{19, 10, 8, 18, 0}, {24, 5, 36}, {40, 2}, {2, 40}, {38, 0}, {10},
{24, 5, 36, 42}};
I suggest that anyone submitting an answer verify it can handle test
, for which the answer should be
{{26, 4, 4, 7}, {12, 3, 36, 6, 33}, {0, 29, 6, 22, 27}, {34, 24}, {24, 5, 36, 42}, {29, 29, 2, 33, 24}, {35, 20, 41, 2, 39}, {19, 1}, {20, 40}, {20, 38, 15, 18, 14}, {19, 10, 8, 18, 0}, {32, 8}, {6, 34, 39,30, 29}, {40, 2}, {38, 0}}
or since this question is regarding the sublists as sets, perhaps it would be best to validate using the canonical (fully-sorted) version.
{{0, 38}, {1, 19}, {2, 40}, {8, 32}, {20, 40}, {24, 34}, {4, 4, 7, 26}, {5, 24, 36, 42}, {0, 6, 22, 27, 29}, {0, 8, 10, 18, 19}, {2, 20, 35, 39, 41}, {2, 24, 29, 29, 33}, {3, 6, 12, 33, 36}, {6, 29, 30, 34, 39}, {14, 15, 18, 20, 38}}
I hope this is useful to anyone working on this surprisingly (to me) difficult problem.
Complement[data, Cases[Subsets[data, {2}], x_List /; (MemberQ[x, Union @@ x]) :> First[x]]]
any faster? $\endgroup$Union[]
with an appropriateSameTest
setting? $\endgroup$