| shmokarami (20) | |
Hi, I want to write an algorithm, in which it needs to find all subset of k elements from the set {1,2,...,n} elements, for k=1,...,p (p is defined by user),I have no idea to write code which efficient in speed and memory, Can anybody help me? | |
|
Last edited on
|
|
| JLBorges (1756) | |
1. Generate each permutation of the n elements2. For each permutation, pick the first k elementsstd::next_permutation()http://en.cppreference.com/w/cpp/algorithm/next_permutation | |
|
|
|
| mik2718 (301) | |
|
Thats going to generate loads of duplicates and is very inefficient (imagine n=100 and k=2) I'm not sure what the best way is but I suggest more research | |
|
|
|
| JLBorges (1756) | |
|
> Thats going to generate loads of duplicates Yes, duplicate sets. > and is very inefficient True. A lexicographic-ordering algorithm or the grey-code algorithm would be faster. | |
|
Last edited on
|
|
| JLBorges (1756) | |||
Using inverted Gray codes:
| |||
|
|
|||
| kev82 (323) | |
|
Am I missing something obvious here? Just loop a large enough bit size integer from 1 to 2^n Calculating the parity can be a bit slow. If this is an issue then precalculate a lookup table for the parity of an 8 bit number and then split the integer into 8 bit numbers. With carful thought, and small p, you can optimize to skip a lot of integers. | |
|
Last edited on
|
|
| shmokarami (20) | |
| I thank you so much. | |
|
|
|
| JLBorges (1756) | |
|
> Just loop a large enough bit size integer from 1 to 2^n inclusive. Yes. Standard integral types will work for n <= std::numeric_limits< unsigned long long >::digits. With a lookup table for parity, that would be faster.For larger n, a big integer library would be needed. Don't know if that would be any faster. | |
|
|
|
| ResidentBiscuit (2650) | |
|
OP, give this a read. http://www8.cs.umu.se/kurser/TDBAfl/VT06/algorithms/BOOK/BOOK4/NODE152.HTM | |
|
|
|
| Duoas (6752) | |
|
As this is a fairly obvious homework assignment, I'm not so sure the OP has received the help he needs. I would also warn him against turning in anything like JLBorges's code. Even though he made it really simple it is still beyond anything the teacher will expect you to personally produce. | |
|
|
|
| shmokarami (20) | |
Thanks for your consideration, I need subset of k elements of {1,2,..,n} for my algorithm in "Network Theory", This is only a partial task of the algorithm and in best scenario k=100 and n=1000. that the best way mentioned by JLBorges using Gray codes, seems not work. how can I generate a subset of k elements of {1,2,..,n} randomly?
| |
|
|
|
| JLBorges (1756) | |
|
> how can I generate a subset of k elements of {1,2,..,n} randomly? 1. Generate a random permutation of the n elements. http://en.cppreference.com/w/cpp/algorithm/random_shuffle 2. Select the first k elements | |
|
|
|
| kev82 (323) | |||
If you haven't already, you may want to rethink what you're doing, there are roughly 10^139 size 100 subsets of 1000 elements. | |||
|
|
|||