Jump to content

Power set: Difference between revisions

→‎Non-recursive version: improved the descriptions of my algorithm
(→‎{{header|R}}: added non-recursive method that is 100x faster)
(→‎Non-recursive version: improved the descriptions of my algorithm)
Line 1,704:
</lang>
 
Even though theThis method is notmuch faster than a recursive method, though the speed of the algorithm is still O(n^2^n).
 
The code is optimized to reduce the creation of new lists, which is a slow step. (You algorithm is a couple lines shorter without "temp", but takes ~4x as long).
 
<lang R>powerset = function(set){
Line 1,723 ⟶ 1,721:
powerset(1:4)
</lang>
 
The list "temp" is a compromise between the speed costs of doing arithmetic and of creating new lists (since R lists are immutable, appending to a list means actually creating a new list object). Thus, "temp" collects new subsets that are later added to the power set. This improves the speed by 4x compared to extending the list "ps" at every step.
 
===Recursive version===
Cookies help us deliver our services. By using our services, you agree to our use of cookies.