Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming I understand your question I believe this is straightforward for fairly small sets.</p> <p>I will use <em>Mathematica</em> code for illustration, but the concept is universal.</p> <p>I generate <code>10</code> random subsets of length <code>4</code>, from the set {1 .. 8}:</p> <pre><code>ss = Subsets[Range@8, {4}] ~RandomSample~ 10 </code></pre> <blockquote> <pre><code>{{1, 3, 4, 6}, {2, 6, 7, 8}, {3, 5, 6, 7}, {2, 4, 6, 7}, {1, 4, 5, 8}, {2, 4, 6, 8}, {1, 2, 3, 8}, {1, 6, 7, 8}, {1, 2, 4, 7}, {1, 2, 5, 7}} </code></pre> </blockquote> <p>I convert these to a binary array of the presence of each number in each subset:</p> <pre><code>a = Normal@SparseArray[Join @@ MapIndexed[Tuples[{##}] &amp;, ss] -&gt; 1]; Grid[a] </code></pre> <p><img src="https://i.stack.imgur.com/aXmiz.png" alt="Mathematica graphics"></p> <p>That is ten columns for ten subsets, and eight rows for elements {1 .. 8}.</p> <p>Now generate all possible target subsets (size <code>3</code>):</p> <pre><code>keys = Subsets[Union @@ ss, {3}]; </code></pre> <p>Take a "key" and extract those rows from the array and do a BitAnd operation (return <code>1</code> iff all columns equal <code>1</code>), then count the number of ones. For example, for key <code>{1, 6, 8}</code> we have:</p> <pre><code>a[[{1, 6, 8}]] </code></pre> <p><img src="https://i.stack.imgur.com/b03J5.png" alt="Mathematica graphics"></p> <p>After BitAnd:</p> <p><img src="https://i.stack.imgur.com/2PJYZ.png" alt="Mathematica graphics"></p> <p>Do this for each key:</p> <pre><code>counts = Tr[BitAnd @@ a[[#]]] &amp; /@ keys; </code></pre> <p>Then find the position(s) of the maximum element of that list, and extract the corresponding parts of <code>keys</code>:</p> <pre><code>keys ~Extract~ Position[counts, Max@counts] </code></pre> <blockquote> <pre><code>{{1, 2, 7}, {2, 4, 6}, {2, 4, 7}, {2, 6, 7}, {2, 6, 8}, {6, 7, 8}} </code></pre> </blockquote> <p>With adequate memory this process works quickly for a larger set. Starting with 50,000 randomly selected subsets of length <code>7</code> from {1 .. 30}:</p> <pre><code>ss = Subsets[Range@30, {7}] ~RandomSample~ 50000; </code></pre> <p>The maximum sub-subsets of length <code>4</code> are calculated in about nine seconds:</p> <pre><code>AbsoluteTiming[ a = Normal@SparseArray[Join @@ MapIndexed[Tuples[{##}] &amp;, ss] -&gt; 1]; keys = Subsets[Union @@ ss, {4}]; counts = Tr[BitAnd @@ a[[#]]] &amp; /@ keys; keys~Extract~Position[counts, Max@counts] ] </code></pre> <blockquote> <pre><code> {8.8205045, {{2, 3, 4, 20}, {7, 10, 15, 18}, {7, 13, 16, 26}, {11, 21, 26, 28}}} </code></pre> </blockquote> <p>I should add that <em>Mathematica</em> is a high level language and these operations are on generic objects, therefore if this is done truly at the binary level this should be much faster, and more memory efficient.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload