powerset

Algorithm to print all combination of letters of the given string in lexicographical order

别说谁变了你拦得住时间么 提交于 2019-12-01 10:43:13
问题 I tried to create the code to generate all possible combination of the given string in the lexicographical order: The code that I wrote is: void get(char *n) { int l=strlen(n); sort(n,n+l); int k=0,m,i,j,z; while(k<l) { m=k; for(i=k;i<l;i++) { for(j=k;j<=i;j++) cout<<n[j]; cout<<"\n"; } for(z=m+2;z<l;z++) cout<<n[m]<<n[z]<<"\n"; k++; } } int main() { char n[100]; cin>>n; get(n); return 0; } Suppose the string is : abcde My code is not generating combinations like: abd abe The output I am

Generating the power set of a list

◇◆丶佛笑我妖孽 提交于 2019-11-30 23:32:45
I have to write a brute-force implementation of the knapsack problem. Here's the pseudocode: computeMaxProfit(weight_capacity) max_profit = 0 S = {} // Each element of S is a weight-profit pair. while true if the sum of the weights in S <= weight_capacity if the sum of the profits in S > max_profit update max_profit if S contains all items // Then there is no next subset to generate return max generate the next subset S While the algorithm is fairly easy to implement, I haven't the slightest idea how to generate the power set of S, and to feed the subsets of the power set into each iteration

Generate powerset lazily

十年热恋 提交于 2019-11-30 22:59:42
I want to calculate powerset of a set. Because I don't need the whole powerset at a time, it's better to generate it lazily. For example: powerset (set ["a"; "b"; "c"]) = seq { set []; set ["a"]; set ["b"]; set ["c"]; set ["a"; "b"]; set ["a"; "c"]; set ["b"; "c"]; set ["a";"b"; "c"]; } Since the result is a sequence, I prefer it in the above order. How can I do it in an idomatic way in F#? EDIT: This is what I'm going to use (based on BLUEPIXY's answer): let powerset s = let rec loop n l = seq { match n, l with | 0, _ -> yield [] | _, [] -> () | n, x::xs -> yield! Seq.map (fun l -> x::l)

next_permutation for combinations or subsets in powerset

不想你离开。 提交于 2019-11-30 07:34:30
问题 Is there some equivalent library or function that will give me the next combination of a set of values like next_permutation in does for me? 回答1: Combinations: from Mark Nelson's article on the same topic we have next_combination http://marknelson.us/2002/03/01/next-permutation Permutations: from STL we have std::next_permutation template <typename Iterator> inline bool next_combination(const Iterator first, Iterator k, const Iterator last) { if ((first == last) || (first == k) || (last == k)

Printing all possible subsets of a list

元气小坏坏 提交于 2019-11-30 02:24:50
I have a List of elements (1, 2, 3), and I need to get the superset (powerset) of that list (without repeating elements). So basically I need to create a List of Lists that looks like: {1} {2} {3} {1, 2} {1, 3} {2, 3} {1, 2, 3} What is the best (simplicity > efficiency in this case, the list won't be huge) way to implement this? Preferably in Java, but a solution in any language would be useful. Use bitmasks: int allMasks = (1 << N); for (int i = 1; i < allMasks; i++) { for (int j = 0; j < N; j++) if ((i & (1 << j)) > 0) //The j-th element is used System.out.print((j + 1) + " "); System.out

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

南楼画角 提交于 2019-11-29 10:33:12
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 that? I would be grateful for a Pseudo-code, Ruby or Erlang example. It sounds like what you are looking

next_permutation for combinations or subsets in powerset

我的未来我决定 提交于 2019-11-29 04:38:09
Is there some equivalent library or function that will give me the next combination of a set of values like next_permutation in does for me? Combinations: from Mark Nelson's article on the same topic we have next_combination http://marknelson.us/2002/03/01/next-permutation Permutations: from STL we have std::next_permutation template <typename Iterator> inline bool next_combination(const Iterator first, Iterator k, const Iterator last) { if ((first == last) || (first == k) || (last == k)) return false; Iterator itr1 = first; Iterator itr2 = last; ++itr1; if (last == itr1) return false; itr1 =

Printing all possible subsets of a list

巧了我就是萌 提交于 2019-11-29 00:04:57
问题 I have a List of elements (1, 2, 3), and I need to get the superset (powerset) of that list (without repeating elements). So basically I need to create a List of Lists that looks like: {1} {2} {3} {1, 2} {1, 3} {2, 3} {1, 2, 3} What is the best (simplicity > efficiency in this case, the list won't be huge) way to implement this? Preferably in Java, but a solution in any language would be useful. 回答1: Use bitmasks: int allMasks = (1 << N); for (int i = 1; i < allMasks; i++) { for (int j = 0; j

How to generate a power set of a given set?

南笙酒味 提交于 2019-11-28 18:49:20
I am studying for an interview and I stumbled upon this question online under the "Math" category. Generate power set of given set: int A[] = {1,2,3,4,5}; int N = 5; int Total = 1 << N; for ( int i = 0; i < Total; i++ ) { for ( int j = 0; j < N; j++) { if ( (i >> j) & 1 ) cout << A[j]; } cout <<endl; } Please I do not want an explicit answer. I just want clarifications and hints on how to approach this problem. I checked power set algorithm on google and I still do not understand how to address this problem. Also, could someone reiterate what the question is asking for. Thank you. Power set of

How to generate the power set of a set in Scala

杀马特。学长 韩版系。学妹 提交于 2019-11-28 18:22:25
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 you would have to change the last line of the method simply to res + Set() Any suggestions how this can