Generating permutations of a set (most efficiently)
Chciałbym wygenerować wszystkie permutacje zestawu (kolekcji), w ten sposób:
Collection: 1, 2, 3
Permutations: {1, 2, 3}
{1, 3, 2}
{2, 1, 3}
{2, 3, 1}
{3, 1, 2}
{3, 2, 1}
To nie jest kwestia „jak”, ogólnie, ale więcej o tym, jak najbardziej efektywnie. Nie chciałbym też generować WSZYSTKICH permutacji i zwracać ich, ale generować pojedynczą permutację naraz i kontynuować tylko wtedy, gdy jest to konieczne (podobnie jak Iteratory - które również próbowałem, ale okazały się mniej wydajny).
Przetestowałem wiele algorytmów i podejść i wymyśliłem ten kod, który jest najbardziej skuteczny spośród tych, których próbowałem:
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;
}
Jego użycie polegałoby na wysyłaniu tablicy elementów i odzyskiwaniu wartości logicznej wskazującej, czy była to ostatnia permutacja leksykograficzna, czy nie, a także zmiana tablicy na następną permutację.
Przykład użycia:
var arr = new[] {1, 2, 3};
PrintArray(arr);
while (!NextPermutation(arr))
{
PrintArray(arr);
}
Rzecz w tym, że nie jestem zadowolony z szybkości kodu.
Iterowanie wszystkich permutacji tablicy o rozmiarze 11 trwa około 4 sekund. Chociaż można to uznać za imponujące, ponieważ ilość możliwych permutacji zestawu o rozmiarze 11 jest11!
który wynosi prawie 40 milionów.
Logicznie rzecz biorąc, przy tablicy o rozmiarze 12 zajmie to około 12 razy więcej czasu12!
jest11! * 12
a przy tablicy o rozmiarze 13 zajmie to około 13 razy więcej czasu niż w przypadku rozmiaru 12 i tak dalej.
Dzięki temu możesz łatwo zrozumieć, w jaki sposób za pomocą macierzy o rozmiarze 12 i więcej, naprawdę zajmuje bardzo dużo czasu, aby przejść przez wszystkie permutacje.
Mam silne przeczucie, że mogę jakoś skrócić ten czas o wiele (bez przełączania na język inny niż C # - ponieważ optymalizacja kompilatora naprawdę bardzo dobrze się optymalizuje i wątpię, żebym mógł zoptymalizować tak dobrze, ręcznie, w złożeniu).
Czy ktoś zna inny sposób na szybsze wykonanie tego zadania? Czy masz jakiś pomysł, jak przyspieszyć działanie obecnego algorytmu?
Zauważ, że nie chcę korzystać z zewnętrznej biblioteki lub usługi, aby to zrobić - chcę mieć sam kod i chcę, aby był tak wydajny, jak to tylko możliwe.