Skip to content
Aug 17 2009 / alecmce

Permutations in AS3

I needed a method to find permutations of elements within an Array, for a little something that I’m working on. I couldn’t find one ‘off the shelf’, and while it’s easy enough to do yourself, it’s easier to use someone else’s. If you’re not very familiar with the concept of permutations, please be sure to read the method header if you intend to use this method.

/**
 * Finds the permutations of an array by recursively permutating sub-arrays
 *
 * WARNING! For n elements there are n! permutations - ie. if you have
 * 6 elements in your array you will find returned an array containing
 * 6*5*4*3*2 = 720 arrays of your elements. 10 elements returns an
 * array containing more than 3.5 million arrays, which is not
 * recommended.
 *
 * @param elements The elements to be permutated
 * @return An array of all of the permutations of the elements in the array.
 */
public function permutate(elements:Array):Array
{
	if (elements.length == 1)
		return [elements[0]];

	var permutations:Array = [];
	var length:int = elements.length;

	var i:int, j:int;
	var tmp:Array;
	var sub:Array;

	i = length;
	while (i--)
	{
		tmp = elements.slice(0, i).concat(elements.slice(i + 1, length));
		sub = permutate(tmp);

		j = sub.length;
		while (j--)
		{
			tmp = [elements[i]].concat(sub[j]);
			permutations.push(tmp);
		}
	}

	return permutations;
}
  • http://twitter.com/alecmce alecmce

    Need permutations in AS3? A code snippet: http://bit.ly/KZxoY
    This comment was originally posted on Twitter

  • Marc Tanenbaum

    Useful, thanks.

    Sometimes, with methods as heuristically dangerous as this, I’m inclined to add a bailout clause. In this case, I might be inclined to allow the method to run only if the n! < someUpperValue. You could then have an override for special testing cases, something like: overrideSafeties:Boolean = false

  • Marc Tanenbaum

    Useful, thanks.

    Sometimes, with methods as heuristically dangerous as this, I’m inclined to add a bailout clause. In this case, I might be inclined to allow the method to run only if the n! < someUpperValue. You could then have an override for special testing cases, something like: overrideSafeties:Boolean = false

  • http://alecmce.com Anonymous

    Hi Marc. Hope it’s of use. I’ll post a “perm 3 from 6″ method just as soon as I need it myself!

    I know what you mean about the bailout clause, and in fact I did escape too dangerous an input. However, I chose to put the check in the method that calls permutate.

    My feeling is that the maths should remain indifferent to pragmatic concerns (much like life ;D) For me, it’s the application implementation that should have to worry about whether it’s going to kill the player!

  • http://alecmce.com alec

    Hi Marc. Hope it’s of use. I’ll post a “perm 3 from 6″ method just as soon as I need it myself!

    I know what you mean about the bailout clause, and in fact I did escape too dangerous an input. However, I chose to put the check in the method that calls permutate.

    My feeling is that the maths should remain indifferent to pragmatic concerns (much like life ;D) For me, it’s the application implementation that should have to worry about whether it’s going to kill the player!

  • Manre

    Thanks, dude

  • Manre

    Thanks, dude

  • Manre

    Thanks, dude