Algorithm to calculate permutations

251 Views Asked by At

I'm aware of Heap's algorithm to calculate permutations of a given sequence, but what if I wanted to calculate the permutations of a k-elements subset for a given sequence N?

The solution I'm thinking of this time is a backtracking one, but it would need to generate a new sequence of sub-elements each time deleting one and recursively calling the permutation function. This sounds expensive and I would like to know if there's a better solution

1

There are 1 best solutions below

0
On BEST ANSWER
  1. Use an algorithm to generate combinations of size K from the set of N. (Pick any from the SO question: Algorithm to return all combinations of k elements from n).
  2. Using the result, apply Heap's Algorithm to create all permutations of this k-element subset (or another Algorithm to generate all possible permutations of a list).
  3. Generate the next subset of size K and repeat (steps 1 and 2) until all subsets of size K have been enumerated.