Power set: Difference between revisions
Content added Content deleted
m (→Non-recursive version: Fix syntax) |
|||
Line 3,193: | Line 3,193: | ||
This method is much faster than a recursive method, though the speed is still O(2^n). |
This method is much faster than a recursive method, though the speed is still O(2^n). |
||
<lang R>powerset |
<lang R>powerset <- function(set){ |
||
ps |
ps <- list() |
||
ps[[1]] |
ps[[1]] <- numeric() #Start with the empty set. |
||
for(element in set){ #For each element in the set, take all subsets |
for(element in set){ #For each element in the set, take all subsets |
||
temp |
temp <- vector(mode="list",length=length(ps)) #currently in "ps" and create new subsets (in "temp") |
||
for(subset in 1:length(ps)){ #by adding "element" to each of them. |
for(subset in 1:length(ps)){ #by adding "element" to each of them. |
||
temp[[subset]] = c(ps[[subset]],element) |
temp[[subset]] = c(ps[[subset]],element) |
||
} |
} |
||
ps |
ps <- c(ps,temp) #Add the additional subsets ("temp") to "ps". |
||
} |
} |
||
ps |
|||
} |
} |
||