Самый простой ответ
для (int i = 0; i
На практике обычно O (n!) Алгоритмы - это те, которые работают, пробуя все различные перестановки списка, то есть все различные способы, которыми вы можете переупорядочить список. Одним из примеров является поиск самой короткой линии, проходящей через все точки на карте, которая называется проблемой коммивояжера. Вам нужно попробовать все различные способы, чтобы пройти все пункты, и это будет O (n!).
IEnumerable<List<int>> nextPermutation(List<int> nodesLeft)
{
if (nodesLeft.Count == 0)
{
yield return new List<int>();
}
else
{
for (int i = 0; i < nodesLeft.Count; i++)
{
List<int> newNodesLeft = new List<int>(nodesLeft);
newNodesLeft.removeAt(i);
foreach (List<int> subPermutation in nextPermutation(newNodesLeft)
{
subPermutation.add(nodesLeft[i]);
yield return subPermutation;
}
}
}
}
void main()
{
foreach (List<int> permutation in nextPermutation(new List<int>(new int[]{1,2,3,4,5}))) {
//every permutation of [1,2,3,4,5] will be generated here
//this will take O(n!) to complete, where n is the number of nodes given (5 in this case)
}
}