Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This returns one set of a powerset at a time. It is based on python code <a href="http://groups.google.com/group/comp.lang.python/browse_thread/thread/d9211cd6c65e1d3a/" rel="noreferrer">here</a>. It works for powersets of over 32 elements. If you need fewer than 32, you can change long to int. It is pretty fast -- faster than my previous algorithm and faster than (my modified to use yield return version of) P Daddy's code.</p> <pre><code>static class PowerSet4&lt;T&gt; { static public IEnumerable&lt;IList&lt;T&gt;&gt; powerset(T[] currentGroupList) { int count = currentGroupList.Length; Dictionary&lt;long, T&gt; powerToIndex = new Dictionary&lt;long, T&gt;(); long mask = 1L; for (int i = 0; i &lt; count; i++) { powerToIndex[mask] = currentGroupList[i]; mask &lt;&lt;= 1; } Dictionary&lt;long, T&gt; result = new Dictionary&lt;long, T&gt;(); yield return result.Values.ToArray(); long max = 1L &lt;&lt; count; for (long i = 1L; i &lt; max; i++) { long key = i &amp; -i; if (result.ContainsKey(key)) result.Remove(key); else result[key] = powerToIndex[key]; yield return result.Values.ToArray(); } } } </code></pre> <p>You can download all the fastest versions I have tested <a href="http://www.iwebthereforeiam.com/files/TestYieldReturn.zip" rel="noreferrer">here</a>.</p> <p>I really think that using yield return is the change that makes calculating large powersets possible. Allocating large amounts of memory upfront increases runtime dramatically and causes algorithms to fail for lack of memory very early on. Original Poster should figure out how many sets of a powerset he needs at once. Holding all of them is not really an option with >24 elements.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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