powerset

Two arrays, where items in array x can be in array y but not vice versa, test all permutations

僤鯓⒐⒋嵵緔 提交于 2019-11-27 05:31:47
A small application that I have written allows a user to add various items to two arrays. Some logic calculates a figure from the contents of each array. Any items in array x can be placed into array y, and back again. Items belonging in array y can never be moved (unless they were moved from array x). The user can move these items around in two lists using a simple javascript ui. To make things simpler, I originally made a naive script which: Moved an item from a to y. Performed some logic using this 'possibility' If the result was less than before, leave x in y. If not, then x remains in x.

How to do a powerset in DrRacket?

人盡茶涼 提交于 2019-11-27 02:06:19
I'm using the beginning language with list abbreviations for DrRacket and want to make a powerset recursively but cannot figure out how to do it. I currently have this much (define (powerset aL) (cond [(empty? aL) (list)] any help would be good. What's in a powerset? A set's subsets! An empty set is any set's subset, so powerset of empty set's not empty. Its (only) element it is an empty set: (define (powerset aL) (cond [(empty? aL) (list empty)] [else As for non-empty sets, there is a choice , for each set's element, whether to be or not to be included in subset which is a member of a

How to find all subsets of a set in JavaScript?

自古美人都是妖i 提交于 2019-11-27 01:34:11
I need to get all possible subsets of an array. Say I have this: [1, 2, 3] How do I get this? [], [1], [2], [1, 2], [2, 3], [1, 3], [1, 2, 3] I am interested in all subsets. For subsets of specific length, refer to the following questions: Finding subsets of size n: 1 , 2 Finding subsets of size > 1: 1 Here is one more very elegant solution with no loops or recursion, only using the map and reduce array native functions. const getAllSubsets = theArray => theArray.reduce( (subsets, value) => subsets.concat( subsets.map(set => [value,...set]) ), [[]] ); console.log(getAllSubsets([1,2,3])); We

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

亡梦爱人 提交于 2019-11-26 22:17:33
问题 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

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

天大地大妈咪最大 提交于 2019-11-26 18:41:33
问题 This question already has answers here : combination and permutation in C++ (4 answers) Closed 5 years ago . I am looking for the implementation of Permutation, Combination and PowerSet using C+++ 回答1: 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())

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

扶醉桌前 提交于 2019-11-26 17:48:50
问题 This question already has answers here : Powersets in Python using itertools (2 answers) Closed 2 years ago . 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? 回答1: The fastest way is by using itertools, especially chain and combinations: >>> from itertools import chain, combinations

Two arrays, where items in array x can be in array y but not vice versa, test all permutations

僤鯓⒐⒋嵵緔 提交于 2019-11-26 11:38:24
问题 A small application that I have written allows a user to add various items to two arrays. Some logic calculates a figure from the contents of each array. Any items in array x can be placed into array y, and back again. Items belonging in array y can never be moved (unless they were moved from array x). The user can move these items around in two lists using a simple javascript ui. To make things simpler, I originally made a naive script which: Moved an item from a to y. Performed some logic

How to do a powerset in DrRacket?

与世无争的帅哥 提交于 2019-11-26 09:55:06
问题 I\'m using the beginning language with list abbreviations for DrRacket and want to make a powerset recursively but cannot figure out how to do it. I currently have this much (define (powerset aL) (cond [(empty? aL) (list)] any help would be good. 回答1: What's in a powerset? A set's subsets! An empty set is any set's subset, so powerset of empty set's not empty. Its (only) element it is an empty set: (define (powerset aL) (cond [(empty? aL) (list empty)] [else As for non-empty sets, there is a

How to find all subsets of a set in JavaScript?

橙三吉。 提交于 2019-11-26 09:09:23
问题 I need to get all possible subsets of an array. Say I have this: [1, 2, 3] How do I get this? [], [1], [2], [1, 2], [2, 3], [1, 3], [1, 2, 3] I am interested in all subsets. For subsets of specific length, refer to the following questions: Finding subsets of size n: 1, 2 Finding subsets of size > 1: 1 回答1: Here is one more very elegant solution with no loops or recursion, only using the map and reduce array native functions. const getAllSubsets = theArray => theArray.reduce( (subsets, value)

Obtaining a powerset of a set in Java

前提是你 提交于 2019-11-26 01:26:50
问题 The powerset of {1, 2, 3} is: {{}, {2}, {3}, {2, 3}, {1, 2}, {1, 3}, {1, 2, 3}, {1}} Let\'s say I have a Set in Java: Set<Integer> mySet = new HashSet<Integer>(); mySet.add(1); mySet.add(2); mySet.add(3); Set<Set<Integer>> powerSet = getPowerset(mySet); How do I write the function getPowerset, with the best possible order of complexity? (I think it might be O(2^n).) 回答1: Yes, it is O(2^n) indeed, since you need to generate, well, 2^n possible combinations. Here's a working implementation,