SimpleVar - 9 days ago 4x

C# Question

I would like to generate all permutations of a set (a collection), like so:

`Collection: 1, 2, 3`

Permutations: {1, 2, 3}

{1, 3, 2}

{2, 1, 3}

{2, 3, 1}

{3, 1, 2}

{3, 2, 1}

This isn't a question of "how", in general, but more about how most efficiently.

Also, I wouldn't want to generate ALL permutations and return them, but only generating a single permutation, at a time, and continuing only if necessary (much like Iterators - which I've tried as well, but turned out to be less efficient).

I've tested many algorithms and approaches and came up with this code, which is most efficient of those I tried:

`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;

}

It's usage would be sending an array of elements, and getting back a boolean indicating whether this was the last lexicographical permutation or not, as well as having the array altered to the next permutation.

Usage example:

`var arr = new[] {1, 2, 3};`

PrintArray(arr);

while (!NextPermutation(arr))

{

PrintArray(arr);

}

The thing is that I'm not happy with the speed of the code.

Iterating over all permutations of an array of size 11 takes about 4 seconds.

Although it could be considered impressive, since the amount of possible permutations of a set of size 11 is

`11!`

Logically, with an array of size 12 it will take about 12 times more time, since

`12!`

`11! * 12`

So you can easily understand how with an array of size 12 and more, it really takes a very long time to go through all permutations.

And I have a strong hunch that I can somehow cut that time by a lot (without switching to a language other than C# - because compiler optimization really does optimize pretty nicely, and I doubt I could optimize as good, manually, in Assembly).

Does anyone know any other way to get that done faster?

Do you have any idea as to how to make the current algorithm faster?

Note that I don't want to use an external library or service in order to do that - I want to have the code itself and I want it to be as efficient as humanly possible.

Thanks for reading everything! :)

Answer

**Here is the fastest implementation I ended up with:**

```
public class Permutations
{
private readonly Mutex _mutex = new Mutex();
private Action<int[]> _action;
private Action<IntPtr> _actionUnsafe;
private unsafe int* _arr;
private IntPtr _arrIntPtr;
private unsafe int* _last;
private unsafe int* _lastPrev;
private unsafe int* _lastPrevPrev;
public int Size { get; private set; }
public bool IsRunning()
{
return this._mutex.SafeWaitHandle.IsClosed;
}
public bool Permutate(int start, int count, Action<int[]> action, bool async = false)
{
return this.Permutate(start, count, action, null, async);
}
public bool Permutate(int start, int count, Action<IntPtr> actionUnsafe, bool async = false)
{
return this.Permutate(start, count, null, actionUnsafe, async);
}
private unsafe bool Permutate(int start, int count, Action<int[]> action, Action<IntPtr> actionUnsafe, bool async = false)
{
if (!this._mutex.WaitOne(0))
{
return false;
}
var x = (Action)(() =>
{
this._actionUnsafe = actionUnsafe;
this._action = action;
this.Size = count;
this._arr = (int*)Marshal.AllocHGlobal(count * sizeof(int));
this._arrIntPtr = new IntPtr(this._arr);
for (var i = 0; i < count - 3; i++)
{
this._arr[i] = start + i;
}
this._last = this._arr + count - 1;
this._lastPrev = this._last - 1;
this._lastPrevPrev = this._lastPrev - 1;
*this._last = count - 1;
*this._lastPrev = count - 2;
*this._lastPrevPrev = count - 3;
this.Permutate(count, this._arr);
});
if (!async)
{
x();
}
else
{
new Thread(() => x()).Start();
}
return true;
}
private unsafe void Permutate(int size, int* start)
{
if (size == 3)
{
this.DoAction();
Swap(this._last, this._lastPrev);
this.DoAction();
Swap(this._last, this._lastPrevPrev);
this.DoAction();
Swap(this._last, this._lastPrev);
this.DoAction();
Swap(this._last, this._lastPrevPrev);
this.DoAction();
Swap(this._last, this._lastPrev);
this.DoAction();
return;
}
var sizeDec = size - 1;
var startNext = start + 1;
var usedStarters = 0;
for (var i = 0; i < sizeDec; i++)
{
this.Permutate(sizeDec, startNext);
usedStarters |= 1 << *start;
for (var j = startNext; j <= this._last; j++)
{
var mask = 1 << *j;
if ((usedStarters & mask) != mask)
{
Swap(start, j);
break;
}
}
}
this.Permutate(sizeDec, startNext);
if (size == this.Size)
{
this._mutex.ReleaseMutex();
}
}
private unsafe void DoAction()
{
if (this._action == null)
{
if (this._actionUnsafe != null)
{
this._actionUnsafe(this._arrIntPtr);
}
return;
}
var result = new int[this.Size];
fixed (int* pt = result)
{
var limit = pt + this.Size;
var resultPtr = pt;
var arrayPtr = this._arr;
while (resultPtr < limit)
{
*resultPtr = *arrayPtr;
resultPtr++;
arrayPtr++;
}
}
this._action(result);
}
private static unsafe void Swap(int* a, int* b)
{
var tmp = *a;
*a = *b;
*b = tmp;
}
}
```

**Usage and testing performance:**

```
var perms = new Permutations();
var sw1 = Stopwatch.StartNew();
perms.Permutate(0,
11,
(Action<int[]>)null); // Comment this line and...
//PrintArr); // Uncomment this line, to print permutations
sw1.Stop();
Console.WriteLine(sw1.Elapsed);
```

**Printing method:**

```
private static void PrintArr(int[] arr)
{
Console.WriteLine(string.Join(",", arr));
}
```

**Going deeper:**

I did not even think about this for a very long time, so I can only explain my code so much, but here's the general idea:

- Permutations aren't lexicographic - this allows me to practically perform less operations between permutations.
- The implementation is recursive, and when the "view" size is 3, it skips the complex logic and just performs 6 swaps to get the 6 permutations (or sub-permutations, if you will).
- Because the permutations aren't in a lexicographic order, how can I decide which element to bring to the start of the current "view" (sub permutation)? I keep record of elements that were already used as "starters" in the current sub-permutation recursive call and simply search linearly for one that wasn't used in the tail of my array.
- The implementation is for integers only, so to permute over a generic collection of elements you simply use the Permutations class to permute indices instead of your actual collection.
- The Mutex is there just to ensure things don't get screwed when the execution is asynchronous (notice that you can pass an UnsafeAction parameter that will in turn get a pointer to the permuted array. You must not change the order of elements in that array (pointer)! If you want to, you should copy the array to a tmp array or just use the safe action parameter which takes care of that for you - the passed array is already a copy).

**Note:**

I have no idea how good this implementation really is - I haven't touched it in so long. Test and compare to other implementations on your own, and let me know if you have any feedback!

Enjoy.

Source (Stackoverflow)

Comments