Generating permutations of a set (most efficiently)
Eu gostaria de gerar todas as permutações de um conjunto (uma coleção), assim:
Collection: 1, 2, 3
Permutations: {1, 2, 3}
{1, 3, 2}
{2, 1, 3}
{2, 3, 1}
{3, 1, 2}
{3, 2, 1}
Esta não é uma questão de "como", em geral, mas mais sobre como mais eficientemente. Além disso, eu não gostaria de gerar TODAS as permutações e retorná-las, mas apenas gerando uma permutação única, de cada vez, e continuando apenas se necessário (muito parecido com Iteradores - que eu também tentei, mas acabou sendo menos eficiente).
Eu testei muitos algoritmos e abordagens e criei este código, que é o mais eficiente daqueles que eu tentei:
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;
}
Seu uso seria o envio de uma matriz de elementos, e receber de volta um booleano indicando se esta foi a última permutação lexicográfica ou não, bem como ter o array alterado para a próxima permutação.
Exemplo de uso:
var arr = new[] {1, 2, 3};
PrintArray(arr);
while (!NextPermutation(arr))
{
PrintArray(arr);
}
O problema é que não estou feliz com a velocidade do código.
Iterar todas as permutações de uma matriz de tamanho 11 leva cerca de 4 segundos. Embora possa ser considerado impressionante, uma vez que a quantidade de permutações possíveis de um conjunto de tamanho 11 é11!
que é quase 40 milhões.
Logicamente, com uma matriz de tamanho 12, levará cerca de 12 vezes mais tempo,12!
é11! * 12
, e com uma matriz de tamanho 13, levará cerca de 13 vezes mais tempo do que o tempo gasto com o tamanho 12 e assim por diante.
Assim, você pode entender facilmente como, com uma matriz de tamanho 12 e mais, leva muito tempo para passar por todas as permutações.
E eu tenho um forte palpite de que eu posso de alguma forma cortar esse tempo muito (sem mudar para um idioma diferente de C # - porque a otimização do compilador realmente otimiza muito bem, e eu duvido que eu poderia otimizar tão bem, manualmente, em Assembly).
Alguém sabe alguma outra maneira de fazer isso mais rápido? Você tem alguma idéia de como tornar o algoritmo atual mais rápido?
Note que eu não quero usar uma biblioteca ou serviço externo para fazer isso - eu quero ter o código em si e eu quero que ele seja tão eficiente quanto humanamente possível.