powerset

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

早过忘川 提交于 2019-11-28 12:31:43
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 the library "sets", with the command 2^as.set(c(1,2,3,4)) , which yields the output {{}, {1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}} . However, this uses a recursive algorithm, which is rather slow. Here's the algorithm I came up with. It's non-recursive, so it's much faster than some of the other solutions out there (and ~100x faster on my machine than the

Generate a powerset of a set without keeping a stack in Erlang or Ruby

↘锁芯ラ 提交于 2019-11-28 11:38:08
I would like to generate a powerset of a rather big set (about 30-50 elements) and I know that it takes 2^n to store the powerset. Is it possible to generate one subset at a time? I.e. generate a powerset of a set with iterations, saving each generated subset to disk/database, removing it from the stack/memory and only then continuing to generate other subsets? Unfortunately I have failed to modify Erlang and Ruby examples to my needs. Edit: Added the enumerator (as @Jörg W Mittag) if no block is given. class Array def powerset return to_enum(:powerset) unless block_given? 1.upto(self.size) do

Generate all “unique” subsets of a set (not a powerset)

天涯浪子 提交于 2019-11-28 03:53:19
问题 Let's say we have a Set S which contains a few subsets: - [a,b,c] - [a,b] - [c] - [d,e,f] - [d,f] - [e] Let's also say that S contains six unique elements: a, b, c, d, e and f . How can we find all possible subsets of S that contain each of the unique elements of S exactly once? The result of the function/method should be something like that: [[a,b,c], [d,e,f]]; [[a,b,c], [d,f], [e]]; [[a,b], [c], [d,e,f]]; [[a,b], [c], [d,f], [e]]. Is there any best practice or any standard way to achieve

Find n-th set of a powerset

被刻印的时光 ゝ 提交于 2019-11-27 21:40:31
I'm trying to find the n-th set in a powerset. By n-th I mean that the powerset is generated in the following order -- first by the size, and then, lexicographically --, and so, the indices of the sets in the powerset of [a, b, c] is: 0 - [] 1 - [a] 2 - [b] 3 - [c] 4 - [a, b] 5 - [a, c] 6 - [b, c] 7 - [a, b, c] While looking for a solution, all I could find was an algorithm to return the n-th permutation of a list of elements -- for example, here . Context : I'm trying to retrieve the entire powerset of a vector V of elements, but I need to do this with one set at a time. Requirements : I can

Implementation of Permutation, Combinations and PowerSet in C++ [duplicate]

[亡魂溺海] 提交于 2019-11-27 16:32:36
This question already has an answer here: combination and permutation in C++ 4 answers I am looking for the implementation of Permutation, Combination and PowerSet using C+++ Using STL: Permutation : using std::next_permutation template <typename T> void Permutation(std::vector<T> v) { std::sort(v.begin(), v.end()); do { std::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " ")); std::cout << std::endl; } while (std::next_permutation(v.begin(), v.end())); } Combination: template <typename T> void Combination(const std::vector<T>& v, std::size_t count) { assert(count <= v.size());

Memory efficient power set algorithm

混江龙づ霸主 提交于 2019-11-27 14:49:33
Trying to calculate all the subsets ( power set ) of the 9-letter string 'ABCDEFGHI'. Using standard recursive methods, my machine hits out of memory (1GB) error before completing. I have no more physical memory. How can this be done better? Language is no issue and results sent to the standard output is fine as well - it does not need to be held all in memory before outputting. There is a trivial bijective mapping from the power set of X = {A,B,C,D,E,F,G,H,I} to the set of numbers between 0 and 2^|X| = 2^9: Ø maps to 000000000 (base 2) {A} maps to 100000000 (base 2) {B} maps to 010000000

How to generate the power set of a set in Scala

穿精又带淫゛_ 提交于 2019-11-27 11:17:39
问题 I have a Set of items of some type and want to generate its power set. I searched the web and couldn't find any Scala code that adresses this specific task. This is what I came up with. It allows you to restrict the cardinality of the sets produced by the length parameter. def power[T](set: Set[T], length: Int) = { var res = Set[Set[T]]() res ++= set.map(Set(_)) for (i <- 1 until length) res = res.map(x => set.map(x + _)).flatten res } This will not include the empty set. To accomplish this

Python: powerset of a given set with generators [duplicate]

依然范特西╮ 提交于 2019-11-27 09:18:40
This question already has an answer here: Powersets in Python using itertools 2 answers I am trying to build a list of subsets of a given set in Python with generators . Say I have set([1, 2, 3]) as input, I should have [set([1, 2, 3]), set([2, 3]), set([1, 3]), set([3]), set([1, 2]), set([2]), set([1]), set([])] as output. How can I achieve this? The fastest way is by using itertools, especially chain and combinations: >>> from itertools import chain, combinations >>> i = set([1, 2, 3]) >>> for z in chain.from_iterable(combinations(i, r) for r in range(len(i)+1)): print z () (1,) (2,) (3,) (1

What algorithm can calculate the power set of a given set?

落花浮王杯 提交于 2019-11-27 08:02:36
I would like to efficiently generate a unique list of combinations of numbers based on a starting list of numbers. example start list = [1,2,3,4,5] but the algorithm should work for [1,2,3...n] result = [1],[2],[3],[4],[5] [1,2],[1,3],[1,4],[1,5] [1,2,3],[1,2,4],[1,2,5] [1,3,4],[1,3,5],[1,4,5] [2,3],[2,4],[2,5] [2,3,4],[2,3,5] [3,4],[3,5] [3,4,5] [4,5] Note. I don't want duplicate combinations, although I could live with them, eg in the above example I don't really need the combination [1,3,2] because it already present as [1,2,3] hobodave There is a name for what you're asking. It's called

Generate a powerset of a set without keeping a stack in Erlang or Ruby

佐手、 提交于 2019-11-27 06:22:13
问题 I would like to generate a powerset of a rather big set (about 30-50 elements) and I know that it takes 2^n to store the powerset. Is it possible to generate one subset at a time? I.e. generate a powerset of a set with iterations, saving each generated subset to disk/database, removing it from the stack/memory and only then continuing to generate other subsets? Unfortunately I have failed to modify Erlang and Ruby examples to my needs. 回答1: Edit: Added the enumerator (as @Jörg W Mittag) if no