Click to See Complete Forum and Search --> : Iterating order combinations


ian313665
June 30th, 2009, 11:03 AM
I'm having trouble figuring out how to implement this algorithm. The program I'm working on currently works fine but I need to implement this algorithm to get my data processing optimal.

What I'm trying to do is simply iterate every possible order combination of a given set of objects. So let's say I have 5 tiles (A, B, C, D, E) and they are arranged sequentially. I want to loop over the number of different order combinations that are possible using these 5 tiles (or 'n' tiles in algorithmic form).

So you'd get an output like this:

A, B, C, D, E
B, C, D, E, A
C, D, E, A, B
E, D, C, B, A

... and so on.

Picture the tiles as Enums or Objects within a program and their order position as an index into an array. I'm not interested in duplicating anything (A, A, A, B, B) - it's literally a shuffling of existing tiles so they remain unique.

I've been pulling my hair out on this all day - can anyone solve this?

Thanks.

Zachm
June 30th, 2009, 12:29 PM
See this (http://msdn.microsoft.com/en-us/library/tw245tyk(VS.80).aspx) link,
or this (http://www.codeproject.com/KB/recipes/permute.aspx) one for more details about implementation of permutation generation (should you wish to implement it and not use the STL algorithm).

Regards,
Zachm

ian313665
June 30th, 2009, 02:50 PM
Excellent! Those links proved very useful and I have it all working now :p

Thanks, your post was much appreciated.