次のように、セット (コレクション) のすべての順列を生成したいと思います。
Collection: 1, 2, 3
Permutations: {1, 2, 3}
{1, 3, 2}
{2, 1, 3}
{2, 3, 1}
{3, 1, 2}
{3, 2, 1}
一般に、これは「どのように」という問題ではなく、どのように最も効率的に行うかという問題です。また、すべての順列を生成してそれらを返すのではなく、一度に 1 つの順列のみを生成し、必要な場合にのみ続行します (イテレーターのように-私も試しましたが、少ないことが判明しました)効率的)。
私は多くのアルゴリズムとアプローチをテストし、このコードを思いつきました。これは、私が試したものの中で最も効率的です。
public static bool NextPermutation<T>(T[] elements) where T : IComparable<T>
{
// More efficient to have a variable instead of accessing a property
var count = elements.Length;
// Indicates whether this is the last lexicographic permutation
var done = true;
// Go through the array from last to first
for (var i = count - 1; i > 0; i--)
{
var curr = elements[i];
// Check if the current element is less than the one before it
if (curr.CompareTo(elements[i - 1]) < 0)
{
continue;
}
// An element bigger than the one before it has been found,
// so this isn't the last lexicographic permutation.
done = false;
// Save the previous (bigger) element in a variable for more efficiency.
var prev = elements[i - 1];
// Have a variable to hold the index of the element to swap
// with the previous element (the to-swap element would be
// the smallest element that comes after the previous element
// and is bigger than the previous element), initializing it
// as the current index of the current item (curr).
var currIndex = i;
// Go through the array from the element after the current one to last
for (var j = i + 1; j < count; j++)
{
// Save into variable for more efficiency
var tmp = elements[j];
// Check if tmp suits the "next swap" conditions:
// Smallest, but bigger than the "prev" element
if (tmp.CompareTo(curr) < 0 && tmp.CompareTo(prev) > 0)
{
curr = tmp;
currIndex = j;
}
}
// Swap the "prev" with the new "curr" (the swap-with element)
elements[currIndex] = prev;
elements[i - 1] = curr;
// Reverse the order of the tail, in order to reset it's lexicographic order
for (var j = count - 1; j > i; j--, i++)
{
var tmp = elements[j];
elements[j] = elements[i];
elements[i] = tmp;
}
// Break since we have got the next permutation
// The reason to have all the logic inside the loop is
// to prevent the need of an extra variable indicating "i" when
// the next needed swap is found (moving "i" outside the loop is a
// bad practice, and isn't very readable, so I preferred not doing
// that as well).
break;
}
// Return whether this has been the last lexicographic permutation.
return done;
}
使用法は、要素の配列を送信し、これが最後の辞書順列であるかどうかを示すブール値を取得し、配列を次の順列に変更することです。
使用例:
var arr = new[] {1, 2, 3};
PrintArray(arr);
while (!NextPermutation(arr))
{
PrintArray(arr);
}
問題は、コードの速度に満足していないことです。
サイズ 11 の配列のすべての順列を反復処理するには、約 4 秒かかります。サイズ 11 のセットの可能な順列の量は11!
4000 万近くあるため、印象的と見なすこともできます。
論理的には、サイズ 12 の配列では、 12!
isであるため、約 12 倍の時間11! * 12
がかかり、サイズ 13 の配列では、サイズ 12 でかかった時間の約 13 倍の時間がかかります。
したがって、サイズが 12 以上の配列では、すべての順列を処理するのに非常に長い時間がかかることが容易に理解できます。
そして、どうにかしてその時間を大幅に削減できるという強い予感があります (C# 以外の言語に切り替えることなく - コンパイラの最適化は実際にはかなりうまく最適化され、Assembly で手動で適切に最適化できるとは思えないからです)。
それをより速く行うための他の方法を知っている人はいますか?現在のアルゴリズムを高速化する方法について何か考えはありますか?
そのために外部ライブラリやサービスを使用したくないことに注意してください-コード自体が必要であり、人間が可能な限り効率的であることを望んでいます。