473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

All Permutations - non-recursive

The other code snipplets I found were either recursive or too complex.
I therefore developed a simple, fast and yet non-recursive method;
thats useful especially when working on the graphics card with CUDA as recursion is not possible there.

Expand|Select|Wrap|Line Numbers
  1. std::string default = "12345";
  2.  
  3. int perm=1, digits=default.size();
  4. for (int i=1;i<=digits;perm*=i++); // perm = !default.size()
  5. for (int a=0;a<perm;a++)
  6. {
  7.     std::string avail=default; // initialize string
  8.  
  9.     // b counts the columns
  10.     for (int b=digits,div=perm;b>0; b--) 
  11.     {
  12.         // compute the number of repetitions for one character in the actual column
  13.         div/=b;
  14.         //compute the index of the character in the string 
  15.         int index = (a/div)%b;       
  16.         printf("%c", avail[index] );
  17.         //remove the used character
  18.         avail.erase(index,1) ;
  19.     }
  20.     printf("\n");
  21. }
  22. printf("permutations:%d\n",perm);
(c) Sven Forstmann
Apr 8 '10 #1
6 7822
donbock
2,426 Expert 2GB
Do you have a question?

It looks like your first loop computes factorial(digits) in an int variable. A 16-bit int can hold at most 8!; a 32-bit int can hold at most 12! Are these limits acceptable for how you intend to use this program?
Apr 8 '10 #2
jkmyoung
2,057 Expert 2GB
div is not defined, and if this is meant to be an insight, there should be comments in the code.
Apr 8 '10 #3
@donbock: To get more digits, you have to use a 64 bit integer or write an extension for 128 and more bits, as it is done for public key encryption e.g.

At the moment I dont have any plans for the program - it was just written for experimental purpose.

The algorithm is based on the following property. Lets say you have 6 characters in the string that you want to permute. Then you get 6! = 720 combinations. In the first column of your results, you get 720/6=120 lines of each character. This means if your index counts from 0 to 719, then the first character to be removed is computed as remove_index = index/120.

For the next column, you have 5 numbers left. This means, that 120 = 5 * 24 lines of the same character - and so on. Its not based on a random thought as you might think.

You get the following list:
column0: remove_index = (a/120)%5;
column1: remove_index = (a/24)%4;
column2: remove_index = (a/6)%3;
column3: remove_index = (a/2)%2;
column4: remove_index = (a/1)%1;

Do you know if this is written in a paper somewhere?
Apr 8 '10 #4
@ jkmyoung:
div is defined in the beginning of the for loop.
for (int b=digits,div=perm;b>0; b--)

>>there should be comments in the code.
done
Apr 8 '10 #5
jkmyoung
2,057 Expert 2GB
I meant the type has not been defined. If you're going to post this code, at least post fully compiling code. This algorithm doesn't deal with duplicates then?

It is interesting in that the combined index a is like a number with multiple bases:
(From the right) the first digit is base 1, the second digit is base 2, the third digit is base 3,
(5*4*3*2), (4*3*2), (3*2), 2, 1
24, 6, 2, 1
etc...

So the index values are 43210
Which means pick the 5th char of the 5 characters, (since 4 is the index of the 5th element)
then pick the 4th char of the remaining 4
then pick the 3rd of the remaing 3
then pick the 2nd of the remaining 2
then pick the last one.

So if we pick a random number less than 120, say 75, and convert it into the multiple bases we get:
3011
Which means pick the 4th 4, leaving us with 1235
pick the 1st, 1, leaving us with 235
pick the 2nd, 3, leaving us with 25
pick the 2nd, 5, leaving us with 2

41352 is the 76th number printed by this program.
===
Criticisms:
copying the string and then truncating over and over may affect efficiency.
===
I think this problem is slightly more useful: http://uva.onlinejudge.org/index.php...lem&problem=82
If you come up with a solution to this problem, you can easily modify the program, loop and print all iterations. (Plus, you can make smart optimizations.).

Dealing with the duplicates makes it a little more complicated, so don't know if that's what you need.
=====
Apr 9 '10 #6
>>I meant the type has not been defined.

It should be defined - its same as defining multiple ints with int a=1,b=2,c=2; Isn't it?

>>I think this problem is slightly more useful: http://uva.onlinejudg..

Hm.. looks to me like they want the next_permutation function of STL - or am I wrong? (link to the STL source)

>>Criticisms: copying the string and then truncating over and over may affect efficiency.

Yes, thats a bit slow. You can make it faster but then its in non-lexigraphic order: replace avail.erase(index,1) ; by avail[index]=avail[b-1];. The algorithm is not the fastest to get all permutations at once - but its very efficient if you want the n'th permutation in particular.
Apr 10 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: John Trunek | last post by:
I have a set of X items, but want permutations of length Y (X > Y). I am aware of the permutation functions in <algorithm>, but I don't believe this will do what I want. Is there a way, either...
2
by: Alex Vinokur | last post by:
Does STL contain algorithms which generate (enable to generate) exponents, permutations, arrangements, and combinations for any numbers and words? -- Alex Vinokur mailto:alexvn@connect.to...
2
by: Gunnar G | last post by:
Hello. Given vector<double> x; where I have no knowledge of the size of x at compile time, how can I write code that will give me all the possible permutations of the elements in the vector x? ...
3
by: D. Stimits | last post by:
I've found a number of basic references for PL/PGSQL, but am looking for something more complete. First question, is there available a *complete* reference for PL/PGSQL? I'm using PostgreSQL...
11
by: Girish Sahani | last post by:
Hi guys, I want to generate all permutations of a string. I've managed to generate all cyclic permutations. Please help :) def permute(string): l= l.append(string) string1 = '' for i in...
20
by: anurag | last post by:
hey can anyone help me in writing a code in c (function) that prints all permutations of a string.please help
7
by: Christian Meesters | last post by:
Hi, I'd like to hack a function which returns all possible permutations as lists (or tuples) of two from a given list. So far, I came up with this solution, but it turned out to be too slow for...
5
by: Shraddha | last post by:
Suppose we are having 3 variables...a,b,c And we want to print the permutations of these variables...Such as...abc,acb,bca...all 6 of them... But we are not supposed to do it mannually... I...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
82
by: Bill Cunningham | last post by:
I don't know if I'll need pointers for this or not. I wants numbers 10^16. Like a credit card 16 digits of possible 10 numbers, so I guess that would be 10^16. So I have int num ; These are of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.