| | |
How to get all possible permutations
Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
![]() |
•
•
Join Date: May 2009
Posts: 6
Reputation:
Solved Threads: 0
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
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.
> 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.
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.
•
•
Join Date: Aug 2008
Posts: 149
Reputation:
Solved Threads: 8
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
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
•
•
Join Date: Apr 2008
Posts: 50
Reputation:
Solved Threads: 3
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.
java Syntax (Toggle Plain Text)
public static String nextSubset(String subset) { //Step 1 (initialization) StringBuffer buffer = new StringBuffer(subset); int k = buffer.length(); // Step 2 (look for rightmost 0) while((k >= 1) && (buffer.charAt(k - 1) == '1') ) { k--; } // Step 3 (if there is a zero, form the next string) if(k >= 1) { buffer.setCharAt(k - 1, '1'); for(int j = k + 1; j <= buffer.length(); j++) { buffer.setCharAt(j - 1 , '0'); } return buffer.toString(); } else return buffer.toString(); }
•
•
Join Date: May 2009
Posts: 6
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- permutations of STRING Vs First n natural numbers (C++)
- How to generate a list of permutations for a given binary number (Java)
- write a program which take a string give it permutations and combination (C++)
- write a program which take a string give it permutations and combination (C++)
- Permutations of an N element array (C++)
Other Threads in the Computer Science Forum
- Previous Thread: Interviewing a Computer Science Graduate
- Next Thread: Rename multiple files, changing spacing
| Thread Tools | Search this Thread |
ai algorithm algorithms amazon assignment assignmenthelp assignments battery bigbrother binary bittorrent bletchleypark blogging bomb business cern codebreaker compiler computer computers computerscience computertrackingsoftware connect conversion csc data dataanalysis dataintepretation development dfa dissertation dissertations dissertationthesis dissertationtopic ebook employment energy extensions floatingpoint foreclosure foreclosuresoftware gadgets geeks givemetehcodez government graphics hardware history homeowners homeworkassignment homeworkhelp humor ibm idea ideas internet ipod itcontracts jobs kindle laser laws linkbait lsmeans mainframes marketing mining mobileapplication msaccess nano networking news os p2p piracy piratebay principles programming rasterizer sam-being-cute sas science security sex simulation software spying sql stephenfry study supercomputer supercomputing sweden technology textfield two'scompliment uk virus warehouse ww2






