How to get all possible permutations

Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
Reply

Join Date: May 2009
Posts: 6
Reputation: drdaco is an unknown quantity at this point 
Solved Threads: 0
drdaco drdaco is offline Offline
Newbie Poster

How to get all possible permutations

 
0
  #1
Jul 7th, 2009
This is dumb and I've been banging my head against it for the better part of the day, so perhaps someone here could help me see the light I keep missing.

Let's say I have a string of 0/1's (or heads of tails coins, or yes or nos etc)

For any string length, I need to come up with all the possible permutations these 0/1s can have but none are degenerate. Ex.

say string length = 3

so I can have
0 0 0
1 0 0
0 1 0
0 0 1
1 1 0
1 0 1
0 1 1
1 1 1

the point is that 1 0 0 and 0 0 1 are each unique.

the best I've come up with is to use the degenerates (000, 100, 110, 111) and then use something like next_permutation() to generate the rest. Obviously 000,111 I can skip the procedure.

so that for string of 4:
0000
1000-->next_permutation()
1100-->next_permutation()
1110-->next_permutation()
1111

but I'm wondering if this is the fastest I can do this because I plan to use really LONG strings to generate my permutations...perhaps 1000 chars, so I need something scalable to multiple cores/cpus.

I welcome your ideas
Last edited by drdaco; Jul 7th, 2009 at 6:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: How to get all possible permutations

 
0
  #2
Jul 8th, 2009
> perhaps 1000 chars, so I need something scalable to multiple cores/cpus.
Try doing the maths first.
Each character you add to the length of the string DOUBLES the amount of time it takes.

If you were blindingly efficient in your code, and could permute every 1nS (a handful of instructions on the fastest processors), you could do a string of 30 characters.

The next 30 chars would take about 30 YEARS to get through.

61 chars -> 60 years
62 chars -> 120 years (are you still interested?)

Or each char DOUBLES the number of machines you need to solve the problem. There isn't enough sand to make enough processors to make any significant dent in this problem.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

Re: How to get all possible permutations

 
0
  #3
Jul 8th, 2009
So you essentially want a truthtable.
The dimension of this truthtable is n*2^n, where n is the number of coin throws.
I once did this program, and I have to agree with you,
it was annoyingly difficult to come up with,
and I never had a beautifull solution.
I think I had 2-loops and 2 extra counters.
the trick is to start from the column1 and then fill up with 1's untill you reach halfway, and then zeroes.
in column2 you would fill up with 1's quaterdown, zeroes till halfdown, 1' till 3quaterdown and 0's the rest
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 5
Reputation: Deque is an unknown quantity at this point 
Solved Threads: 0
Deque's Avatar
Deque Deque is offline Offline
Newbie Poster

Re: How to get all possible permutations

 
0
  #4
Jul 8th, 2009
You can use binaries. Start with 000...0, then shift your binary left and add 1 and so on. So you get e.g.:
0000
0001 (shift left --> 0010 plus 1 --> 0011)
0011
0111
1111
You only have to convert this to Strings.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 50
Reputation: sloan31 is an unknown quantity at this point 
Solved Threads: 3
sloan31 sloan31 is offline Offline
Junior Poster in Training

Re: How to get all possible permutations

 
0
  #5
Jul 8th, 2009
I recently did something similar to help me solve a knapsack problem. You may be interested in my java method here which can take a string of 0s and 1s and generate the next subset value. So basically, just pass it the initial string of all 0s(the empty subset) and use this method in a loop to generate all possible permutations.

  1.  
  2. public static String nextSubset(String subset) {
  3. //Step 1 (initialization)
  4. StringBuffer buffer = new StringBuffer(subset);
  5. int k = buffer.length();
  6.  
  7. // Step 2 (look for rightmost 0)
  8. while((k >= 1) && (buffer.charAt(k - 1) == '1') ) {
  9. k--;
  10. }
  11.  
  12. // Step 3 (if there is a zero, form the next string)
  13. if(k >= 1) {
  14. buffer.setCharAt(k - 1, '1');
  15.  
  16. for(int j = k + 1; j <= buffer.length(); j++) {
  17. buffer.setCharAt(j - 1 , '0');
  18. }
  19. return buffer.toString(); }
  20. else
  21. return buffer.toString();
  22.  
  23.  
  24. }
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 6
Reputation: drdaco is an unknown quantity at this point 
Solved Threads: 0
drdaco drdaco is offline Offline
Newbie Poster

Re: How to get all possible permutations

 
0
  #6
Jul 8th, 2009
thanks everyone, those are good ideas. I had another idea too to make it scalable, and that is to generate just a smaller subset of permutations and then combine to make a bigger set:
a[i] b[j] c[k]
000 | 000 | 000
001 |\ 001 |\ 001
011 |/ 011 |/ 011
111 | 111 | 111

now my algorithm is:
loop over i,j,k to make it easier
000 000 000 --> a[0]+b[0]+c[0]

etc

The binary idea sounds really clean though, can you give me an example? Is there a library I can use or is it staring me in the face this morning? lol
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 6
Reputation: drdaco is an unknown quantity at this point 
Solved Threads: 0
drdaco drdaco is offline Offline
Newbie Poster

Re: How to get all possible permutations

 
0
  #7
Jul 8th, 2009
thanks sloan! I was indeed reading the knapsack problem yesterday!! I'll give this a try today
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 5
Reputation: Deque is an unknown quantity at this point 
Solved Threads: 0
Deque's Avatar
Deque Deque is offline Offline
Newbie Poster

Re: How to get all possible permutations

 
0
  #8
Jul 8th, 2009
This may help you, if you want to use Java:
http://www.sap-img.com/java/java-bit...-operators.htm
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 6
Reputation: drdaco is an unknown quantity at this point 
Solved Threads: 0
drdaco drdaco is offline Offline
Newbie Poster

Re: How to get all possible permutations

 
0
  #9
Jul 8th, 2009
thanks. I'll have to find equivalent libraries in C.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 1
Reputation: aliannahfth is an unknown quantity at this point 
Solved Threads: 0
aliannahfth aliannahfth is offline Offline
Newbie Poster

Re: How to get all possible permutations

 
0
  #10
Jul 15th, 2009
Thanks for your informations
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC