How to generate unique permutation

Let's talk about algorithms!

Moderator: Board moderators

Post Reply
PMNOX
New poster
Posts: 49
Joined: Wed Feb 13, 2002 2:00 am
Location: Poland
Contact:

Post by PMNOX »

Hi
Why don't you use stl buildin next_permutation libiary?
for example
http://www.sgi.com/tech/stl/next_permutation.html
rakeb
New poster
Posts: 42
Joined: Fri Aug 30, 2002 2:51 pm
Location: France

Post by rakeb »

u can also use the next permutation algorithm given in discrecte math book of kenneth h. rosen ( 4th edition ) at page 298 to find the next permutation


http://www.mhhe.com/math/advmath/rosen/
Rakeb
Shahab
New poster
Posts: 24
Joined: Sun Nov 10, 2002 2:17 pm

Post by Shahab »

As you know the if we have a(k) number of the kth element, and we have n elements, the number of permutations is like this:

C(m,a(1))*C(m-a(1),a(2))*C(m-a(1)-a(2),a(3))*...

where m represents the sum of a(i)s, and C(n,k)=(n!/(k!*(n-k)!))

If we number the permutations, using the above formula, we can simply generate them without repeating them.

for example, when we want to produce the permutations of AAABCC, we have

0 -> AAABCC 1 -> AAACBC 2 -> AAACCB
3 -> AABACC 4 -> AACABC 5 -> AACACB
6 -> AABCAC 7 -> AACBAC 8 -> AACCAB
9 -> AABCCA 10-> AACBCA 11-> AACCBA
12-> ABAACC 13-> ACAABC 14-> ACAACB
15-> ABACAC 16-> ACABAC 17-> ACACAB
18-> ABACCA 19-> ACABCA 20-> ACACBA
21-> ABCAAC 22-> ACBAAC 23-> ACCAAB
24-> ABCACA 25-> ACBACA 26-> ACCABA
27-> ABCCAA 28-> ACBCAA 29-> ACCBAA

the next 30 permutations are like them already, if you look at the rows, you will see that in every row the places of As are the same. and therefore, for solving this problem, you should find the places of the first element, and then, solve the problem for the remaining places and the remaining elements. in this manner, your problem is reduced to placing k elements in the n places that is easy to solve.
Post Reply

Return to “Algorithms”