Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Fundamentally, the time complexity will always be O(2^n) so you the best you can hope for are constant time improvements. That said, your implementation can be simplified to the following:</p> <pre><code>public static List&lt;int[]&gt; powerSetBit(int[] list) { if (list == null) throw new ArgumentNullException("list may not be null."); if (list.Length &gt; 31) throw new ArgumentOutOfRangeException("list must have 31 or fewer items."); List&lt;int[]&gt; results = new List&lt;int[]&gt;(); int count = 1 &lt;&lt; list.Length; for (int i = 0; i &lt; count; i++) { int subsetSize = 0; for (int j = 0; j &lt; list.Length &amp;&amp; (1 &lt;&lt; j) &lt; count; j++) if ((i &amp; (1 &lt;&lt; j)) &gt; 0) subsetSize++; int[] subset = new int[subsetSize]; for (int j = 0, k = 0; j &lt; list.Length &amp;&amp; (1 &lt;&lt; j) &lt; count; j++) if ((i &amp; (1 &lt;&lt; j)) &gt; 0) subset[k++] = list[j]; results.Add(subset); } return results; } </code></pre> <p>For an input size of about 20 items, the existing implementation takes about 771 milliseconds on average to execute on my machine and the simplified implementation takes about 513 milliseconds to execute on average on my machine.</p> <p>If your goal is to improve the readability of the implementation you can also go with the recursive implementation which is slightly slower (857 milliseconds on average for the example test case).</p> <p>The recursive solution works by observing that the list is an element of the power set and that each power set of the list less one of its elements is also part of the overall power set. To prevent duplicated sets, only the left hand side of the traversal tree is considered through the use of the second argument. </p> <pre><code>static class Program { static void Main() { List&lt;List&lt;int&gt;&gt; lists = powerSet(new List&lt;int&gt;() { 1, 2, 3, 4 }); foreach (List&lt;int&gt; list in lists) Console.WriteLine("{{{0}}}", String.Join(", ", list)); } public static List&lt;List&lt;int&gt;&gt; powerSet(List&lt;int&gt; list) { if (list == null) throw new ArgumentNullException("list may not be null."); return powerSet(list, list.Count); } public static List&lt;List&lt;int&gt;&gt; powerSet(List&lt;int&gt; list, int j) { if (list == null) throw new ArgumentNullException("list may not be null."); if (j &lt; 0) throw new ArgumentOutOfRangeException("j must be not be negative."); List&lt;List&lt;int&gt;&gt; results = new List&lt;List&lt;int&gt;&gt;() { new List&lt;int&gt;(list) }; for (int i = 0; i &lt; j; i++) { int x = list[i]; list.RemoveAt(i); results.AddRange(powerSet(list, i)); list.Insert(i, x); } return results; } } </code></pre>
    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.
    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