How to Generate Distinct Combinations

Let's talk about algorithms!

Moderator: Board moderators

Post Reply
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

if you use C++,try to use function next_perm() (or something near that name - I don't remember it correct) ....

If you want try other way - read about permutations from Knuth .... in this book is given algorithm , which do this :)) but be very carefull - generating all distinct permutations can take a very very long time .... when I write such function long time ago, I found that for 11 different characters all permutations my computer compute 1 hour (with printing to screen) .... :(

Best luck
Dominik Michniewski
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

If you want only combinations like:
AAABC, AAACB, BAAAC, CAAAB, BCAAA, CBAAA and nothing other - try to permute unique symbols, and repetitions add only for output - in this case permute only string ABC :))

I don't know - it's your problem ?
If not, try to tell me more about it :)))

Dominik
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
asar786
New poster
Posts: 1
Joined: Tue Feb 11, 2003 9:16 am
Location: Karachi
Contact:

Post by asar786 »

let say i have symbols
A,B,C,D,E,F (N=6)
i hav to find unique sets of order K. such that
repeatation of symbol is also required
but a set in whole can not be repeated by means of its permutation
let say K=3

for AAA...AFF

AAA ACA* AEA*
AAB ACB* AEB*
AAC ACC AEC*
AAD ACD AED*
AAE ACE AEE
AAF ACF AEF
ABA* ADA* AFA*
ABB ADB* AFB*
ABC ADC* AFC*
ABD ADD AFD*
ABE ADE AFE*
ABF ADF AFF

the starred entries are not required because they r repeating

ABA is same as AAB,BAA,

how to drop those.
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

try to generate all possible permutations, which have that property that val(a_i) is less or equal than val(a_j) foreach i,j

try to use BFS
in first step insert to queue letters A,B,C,D,E,F ....

qlen - length of queue
qact - act element in queue

Code: Select all

MAX = 6; /* size of {A,B,C,D,E,F} set */
while(qact < qlen)
{
   q = queue[qlen];
   if(LENGTH(q) >= K) break; /* in positions K..qlen are permutations ... */
   for(i=0;i<MAX;i++) if(UNIQUE_IN_QUEUE(q + set(i))
   {
      INSERT_TO_QUEUE(q + set(i));
      qlen++;
   }
   qact++;
}
UNIQUE_IN_QUEUE could be realized as you wish - linear search, binary search, lookup table
INSERT_TO_QUEUE could be realized the same - it's very important to insert data to queue by it's length....

I think that time complexity is O(N^2) where N is number of combinations ...

I hope, that should be work :)

Regards
Dominik
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Abednego
A great helper
Posts: 281
Joined: Tue Sep 10, 2002 5:14 am
Location: Mountain View, CA, USA
Contact:

Post by Abednego »

Look here:
http://ttc.stefan-pochmann.de/
under Tutorials->Sets,Subsets
junjieliang
Experienced poster
Posts: 169
Joined: Wed Oct 31, 2001 2:00 am
Location: Singapore

Post by junjieliang »

Maybe the way he used "val(a_i) <= val(a_j) for each i,j" looks intimidating, but in short he's saying that your sequences should be in non-descending order. Therefore, print ABC only and ignore that other 5 since they are not in non-descending order. To make sure you don't get AAAB 2 or 3 times, you would have to keep track to make sure you don't permute the same character twice.

I think your problem can be solved by recursion... if I'm free I'll send you a code through PM.
kpras
New poster
Posts: 2
Joined: Thu Jun 12, 2003 2:15 pm
Location: IIT KANPUR, INDIA
Contact:

Generating all permutations

Post by kpras »

Hi
This can be done using cyclisation and recursion ; Lets take a word
say code. You think of this word as placed as a circular array,
key for recursion is that all the words starting with c are : c added
in the start to all the permutations of the string "ode". similarly for
"ode" now. ...
Going back to stack of recusion all words starting wih o are ...
If some letters of alphabet are repeated in the word ,then we
end up getting repetitions think a bit how to handle it
bye
anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post by anupam »

hello all,
i want to know abt pochmen.
he was a great helper.
can any1 give me info of pochmen.
--
"Everything should be made simple, but not always simpler"
Moni
Experienced poster
Posts: 202
Joined: Fri Mar 22, 2002 2:00 am
Location: Chittagong. CSE - CUET
Contact:

Post by Moni »

Yeah! Hope soon he will come back from his TOP CODER journey ;)
ImageWe are all in a circular way, no advances, only moving and moving!
Post Reply

Return to “Algorithms”