Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

r - How to compute all power sets with cardinality at most K?

I have set $S$ with the cardinality of $M$. I would like to create all powerset of $S$ with at most cardinality of $K$ where $K le M$. I used $R$ to create powersets, but it does not provide an option to constrain it to the mentioned case. Since size of $S$ is really large (500), for my problem, I just need to compute all subsets with cardinality at most 5. Can someone help me to do this in R?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With |S| = 500 k won't be able to be very large. For k = 0, 1, 2, 3, 4, 5 this is how many subsets there are having size of at most k:

cumsum(sapply(0:5, choose, n = 500))
## [1]            1          501       125251     20833751   2593864876 257838552476

Now turning to the code note that combn(x = S, m = i, simplify = FALSE) gives all subsets of size i so:

#  test data
S <- head(letters, 4)
k <- 2

subsets_k <- do.call("c", lapply(0:k, combn, x = S, simplify = FALSE))

giving all subsets of 0, 1 or k=2 elements:

> subsets_k
[[1]]
character(0)

[[2]]
[1] "a"

[[3]]
[1] "b"

[[4]]
[1] "c"

[[5]]
[1] "d"

[[6]]
[1] "a" "b"

[[7]]
[1] "a" "c"

[[8]]
[1] "a" "d"

[[9]]
[1] "b" "c"

[[10]]
[1] "b" "d"

[[11]]
[1] "c" "d"

or we can represent these as a character vector of comma-separated elements:

sapply(subsets_k, toString)
## [1] ""     "a"    "b"    "c"    "d"    "a, b" "a, c" "a, d" "b, c" "b, d" "c, d"

or directly:

unlist(sapply(0:k, function(i) combn(S, i, FUN = toString)))
## [1] ""     "a"    "b"    "c"    "d"    "a, b" "a, c" "a, d" "b, c" "b, d" "c, d"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...