JOIN
Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
[ 1 2 ]    NEXT >
Iterating Over All Permutations of an Array | Reply
Name
Iterating Over All Permutations of an Array

Problem
You have an array of objects supporting comparison, and you want to iterate through all possible permutations of these objects.

Solution
Assuming that quite natural order of permutations is lexicographic order, you should be able to do just three things:
1) Generate the first permutation;
2) Get the next permutation from a given one;
3) Recognize the last permutation.

And then you can use this simple algorithm (in pseudo-code):
array a[n];
generate_first_permutation(a);
while (1) {
	// Do something with the array.
	if (is_last_permutation(a)) break;
 	get_next_permutation(a);
}

Of course, as soon as the first permutation is just a lexicographically first, we can use for generating it well-known quick sort algorithm. Even more, in C++ STL there is nice function bool next_permutation (BidirectionalIterator first, BidirectionalIterator last) (and rather like this, the function named prev_permutation, if you want to iterate in reverse order). If the given permutation is not last, this function generates the next permutation and returns 1, else it generates the first permutation and returns 0. (Note that this function works correctly even if there are equal elements in the array.) Using all these, we can easily write our program in C++:
#include <vector>
#include <algorithm>
using namespace std;
class Permutations {
public:
	int howMany (vector<int> a) {
		sort(a.begin(),a.end());
		int ans=0;
		do {
			++ans;
		} while (next_permutation(a.begin(),a.end()));
		return ans;
	}
};

Done it, we can enjoy the life. But this is still a problem for coders who aren’t experiencing C++ language and thus have no such a great tool as STL. While sorting arrays is very common procedure for which we refer to other recipes, generating next permutation is not as well. So we should show how to write such a function manually. This code represents Java function bool my_next_permutation (int []), whose arguments are an array and number of permutating elements in it, and this function’s action copies C++ STL function’s one:
public boolean my_next_permutation(int[] a) {
	int n=a.length;
	int i,j,k,temp;
	i=n-2;
	while (i>=0 && a[i]>=a[i+1]) --i;
	if (i<0) {
		for (j=0,k=n-1; j<k; j++,k--) {
			temp=a[j];
			a[j]=a[k];
			a[k]=temp;
		}
		return false;
	}
	j=n-1;
	while (a[i]>=a[j]) --j;
	temp=a[i];
	a[i]=a[j];
	a[j]=temp;
	for (j=i+1,k=n-1; j<k; j++,k--) {
		temp=a[j];
		a[j]=a[k];
		a[k]=temp;
	}
	return true;
}
Re: Iterating Over All Permutations of an Array (response to post by Ferlon) | Reply
Good recipe. I think it would help the reader's understanding if the discussion gave some visual examples of sequences and their next permutations.
Re: Iterating Over All Permutations of an Array (response to post by dimkadimon) | Reply
You are right, maybe it would. I spent some time drawing the picture but couldn't insert it into the post :-(( I'll do it, if somebody explain to me how to use <img> tag in this forum.
Re: Iterating Over All Permutations of an Array (response to post by Ferlon) | Reply
You don't really need a picture. It would suffice to have your discussion relate to some examples of sequences. I was thinking something like this: http://wordaligned.org/articles/next-permutation

By the way, not all coders have access to reverse() method. Here is my Java implementation, which you are free to use:
//elements in a have to be sorted in ascending order
//changes the elements to achieve the next permutation
//returns false if there are no more permutations
public static boolean nextPermutation(int[] a)
{
	int N=a.length;
	int i=N-2;
	for (; i>=0; i--)
		if (a[i]<a[i+1])
			break;
	if (i<0) return false;
	
	for (int j=N-1; j>=i; j--)
	{
		if (a[j]>a[i])
		{
			int temp=a[i];
			a[i]=a[j];
			a[j]=temp;
			break;		
		}
	}
	for (int j=i+1; j<(N+i+1)/2; j++)		//reverse from a[i+1] to a[N-1]
	{
		int temp=a[j];
		a[j]=a[N+i-j];
		a[N+i-j]=temp;
	}
	return true;
}
Re: Iterating Over All Permutations of an Array (response to post by dimkadimon) | Reply
Thanks for advice. I've edited the post.
Re: Iterating Over All Permutations of an Array (response to post by Ferlon) | Reply
Why does my_next_permutation get exactly lexicographically next permutation from a given one? Let’s examine it intently. To generate the next permutation we should find such an element ai (with maximal possible i) that can be replaced by such an element aj that ai< aj and i<j (with minimal possible aj under these constraints), then swap ai and aj and reorder all elements with indices more than i non-decreasing.>


I know that this works, but I am having a hard time understanding why this works. In other words, why does this procedure give us the next lexicographical permutation, rather than some other permutation?

Also you can mention that next_permutation (at least the C implementation) ignores repeated permutations. So if you have repeated elements then 0,0,1,1 will go to 0,1,0,1 (instead of 0,0,1,1 again). This is a very useful feature that allows us to solve problems with long permutations (eg. length 20). For example see this solution, which did not time out on the challenge case.
Re: Iterating Over All Permutations of an Array (response to post by dimkadimon) | Reply
1) For answer your question, crusial words in this paragraph are maximal and minimal. It generates exactly the next permutation (and not arbitrary lexicographically greater than given one) because of this.

2) I mention it :-) Just re-read attentively.
Re: Iterating Over All Permutations of an Array (response to post by Ferlon) | Reply
2) Ah I see now :)
Re: Iterating Over All Permutations of an Array (response to post by Ferlon) | Reply
Re: Iterating Over All Permutations of an Array (response to post by Ferlon) | Reply
As the instance of Nickolas I've supplemented "Discussion" section with new details and had to split initial post into two parts because of size limit.
Re: Iterating Over All Permutations of an Array (response to post by Ferlon) | Reply

So if there are no equal elements in the array, time complexity of whole algorithm is O(n!*n).


That is not the whole truth. The next permutation algorithm always inspects two last elements, but the third one is inspected once per 2 times, the fourth one is inspected once per 6 times, the fifth one once per 24 times, etc. Thus, the time complexity is actually

O(n!+n!+n!/2+n!/6+n!/24+n!/120+...) = O(e*n!) = O(n!)

There is no swap function in Java, right? (Otherwise my_next_permutation should use it)
Re: Iterating Over All Permutations of an Array (response to post by Eryx) | Reply
Yes, it has been pointed already by it4.kp, but I think it isn't necessary to give the best asymptotical estimate in this case.

I don't know about swap function: even coding this program in Java was rather hard for me. But as there are nobody who claims that there is one, I suppose there isn't :)
Re: Iterating Over All Permutations of an Array (response to post by Ferlon) | Reply
I think a short mention that next_permutation takes a constant amount of time on average would be more useful than the current simple analysis, because:

  • everyone at this point should be able to do this simple calculation themselves, so it is not interesting (especially that it is naive),
  • even if there is no direct practical importance in this case, I think it would be good for general educational purpose to note that some algorithms work much faster than one could get by just counting the nesting of loops,
  • actually, what really matters in this case in TopCoder is not the time generating all permutations, but time of doing some stuff (X) for all of them. To do this, you obviously have to do the X thing N! times. So even if N is 8 and the X thing is something very complex, the "try all permutations" algorithm does not work.
Re: Iterating Over All Permutations of an Array (response to post by Ferlon) | Reply
As for the problem example, you can look at TheLuckyString. And this is Java solution where we do exactly what we say in previous paragraph – generate all permutations straightforward and check them for required property (no two adjacent elements are equal):
import java.util.*;
public class TheLuckyString {
public boolean my_next_permutation(int[] a) {
	int n=a.length;
	int i,j,k,temp;
	i=n-2;
	while (i>=0 && a[i]>=a[i+1]) --i;
	if (i<0) {
		for (j=0,k=n-1; j<k; j++,k--) {
			temp=a[j];
			a[j]=a[k];
			a[k]=temp;
		}
		return false;
	}
	j=n-1;
	while (a[i]>=a[j]) --j;
	temp=a[i];
	a[i]=a[j];
	a[j]=temp;
	for (j=i+1,k=n-1; j<k; j++,k--) {
		temp=a[j];
		a[j]=a[k];
		a[k]=temp;
	}
	return true;
}
public int count (String s) {
		int a[]=new int[s.length()];
		int i;
		for (i=0; i<s.length(); ++i) a[i]=s.charAt(i);
		Arrays.sort(a);
		int ans=0;
		do {
			for (i=0; i+1<a.length; ++i) if (a[i]==a[i+1]) break;
			if (i+1==a.length) ans++;
		} while (my_next_permutation(a));
		return ans;
	}
};

And this is more compact C++ solution, in which we will iterate from the last permutation to the first one – just to show that there are no major differences:
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class TheLuckyString {
public:
	int count (string a) {
		int i;
		int ans=0;
		sort(a.begin(),a.end());
		reverse(a.begin(),a.end());
		do {
			for (i=0; i<int(a.size())-1; ++i) if (a[i]==a[i+1]) break;
			if (i==int(a.size())-1) ++ans;
		} while (prev_permutation(a.begin(),a.end()));
		return ans;
	}
};

Another example may be MatrixGame. To solve it, we should search over all permutations of columns: got next permutation of columns, we sort obtained rows lexicographically and compare the whole matrix with current answer. There are two thin details: first, we should not so take a fancy of next_permutation function as using it for generating all permutations of rows also, because it will result in total (8!)2 possibilities, what is too much to go into time limit; second, we cannot generate the next permutation of columns by generating next permutation of every row of the matrix simultaneously, since for distinct rows there may be distinct numbers of permutations of elements (because of equal elements in the same row) and even there are two rows with the same numbers of permutations, they may have distinct lexicographically numbers of initial permutations, and will not change equally under effect of next_permutation – so we should create a vector indicating current permutation of columns, and rearrange elements in the matrix after getting every next permutation of that vector’s elements. This is the solution:
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class MatrixGame {
vector<string> ans;
vector<string> now;
public:
	vector<string> getMinimal (vector<string> a) {
		int i,j;
		vector<int> v;
		ans=now=a;
		for (i=0; i<a[0].size(); ++i) v.push_back(i);
		do {
			for (i=0; i<a.size(); ++i)
				for (j=0; j<a[i].size(); ++j)
					now[i][j]=a[i][v[j]];
			sort(now.begin(),now.end());
			if (now<ans) ans=now;
		} while	(next_permutation(v.begin(),v.end()));
		return ans;
	}
};

And the last example will be TheMoviesLevelTwoDivTwo. Here we should iterate over all possible permutations of movies and punctually simulate John’s scare level during watching them. As every movie has two parameters – length and scary moment – we can join these two ints into pair<int,int> and permutate such pairs, or also create an array of indices.
Re: Iterating Over All Permutations of an Array (response to post by Eryx) | Reply
OK, as you insist, I added this estimation to the recipe (what made the post too big, so I splitted it).
[ 1 2 ]    NEXT >

RSS