Algorithm to calculate power set (all possible subsets) of a set in R

后端 未结 4 1490
故里飘歌
故里飘歌 2020-12-11 05:52

I couldn\'t find an answer to this anywhere, so here\'s my solution.

The question is: how can you calculate a power set in R?

It is possible to do this with

4条回答
  •  一个人的身影
    2020-12-11 06:17

    Here's another simple approach that seems to perform reasonably well for small sets. There are obvious memory issues with this method as the data cardinality increases.

    getPowSet <- function(set) {     
      n <- length(set)
      keepBool <- sapply(2^(1:n - 1), function(k) 
        rep(c(FALSE, TRUE), each=k, times=(2^n / (2*k))))
      lapply(1:2^n, function(j) set[keepBool[j, ]])
    }
    

    Speed comparison for n=10:

    microbenchmark(powerset(LETTERS[1:10]), f(LETTERS[1:10]), getPowSet(LETTERS[1:10]))
    
    Unit: milliseconds
                         expr      min       lq     mean   median       uq      max neval
      powerset(LETTERS[1:10]) 2.466167 2.551928 2.656964 2.581211 2.637358 3.676877   100
             f(LETTERS[1:10]) 2.923339 3.029928 3.115222 3.104413 3.175931 4.033136   100
     getPowSet(LETTERS[1:10]) 2.415290 2.490522 2.574131 2.547115 2.617198 3.664040   100
    

    But then for n=15 the original function seems to perform better:

    microbenchmark(powerset(LETTERS[1:15]), f(LETTERS[1:15]), getPowSet(LETTERS[1:15]))
    
    Unit: milliseconds
                         expr       min        lq      mean    median       uq      max neval
      powerset(LETTERS[1:15])  81.48276  88.50272  94.88927  91.61366  94.8262 174.0636   100
             f(LETTERS[1:15]) 110.86208 118.08736 124.38501 122.35872 126.7830 189.3131   100
     getPowSet(LETTERS[1:15])  86.16286  93.32314  98.14936  96.85443 100.6075 159.1030   100
    

提交回复
热议问题