aaronstacy
aaronstacy

Reputation: 6428

Efficiently calculate next permutation of length k from n choices

I need to efficiently calculate the next permutation of length k from n choices. Wikipedia lists a great algorithm for computing the next permutation of length n from n choices.

The best thing I can come up with is using that algorithm (or the Steinhaus–Johnson–Trotter algorithm), and then just only considering the first k items of the list, and iterating again whenever the changes are all above that position.

Constraints:

Non-constraints:

Upvotes: 5

Views: 1296

Answers (1)

rici
rici

Reputation: 241761

You can break this problem down into two parts:

1) Find all subsets of size k from a set of size n.

2) For each such subset, find all permutations of a subset of size k.

The referenced Wikipedia article provides an algorithm for part 2, so I won't repeat it here. The algorithm for part 1 is pretty similar. For simplicity, I'll describe it for "find all subsets of size k of the integers [0...n-1].

1) Start with the subset [0...k-1]

2) To get the next subset, given a subset S:

2a) Find the smallest j such that j ∈ S ∧ j+1 ∉ S. If j == n-1, there is no next subset; we're done.

2b) The elements less than j form a sequence i...j-1 (since if any of those values were missing, j wouldn't be minimal). If i is not 0, replace these elements with i-i...j-i-1. Replace element j with element j+1.

Upvotes: 1

Related Questions