2

Consider the following list of numbers: 0, 1, 2, 3

I am trying to find all permutations of the list of length 2, 3 and 4.

i.e.

(0, 1)
(0, 2)
(0, 3)
(1, 0)
(1, 2)
(1, 3)
(2, 0)
(2, 1)
(2, 3)
(3, 0)
(3, 1)
(3, 2)
(0, 1, 2)
(0, 1, 3)
(0, 2, 1)
(0, 2, 3)
(0, 3, 1)
(0, 3, 2)
(1, 0, 2)
(1, 0, 3)
(1, 2, 0)
(1, 2, 3)
(1, 3, 0)
(1, 3, 2)
(2, 0, 1)
(2, 0, 3)
(2, 1, 0)
(2, 1, 3)
(2, 3, 0)
(2, 3, 1)
(3, 0, 1)
(3, 0, 2)
(3, 1, 0)
(3, 1, 2)
(3, 2, 0)
(3, 2, 1)
(0, 1, 2, 3)
(0, 1, 3, 2)
(0, 2, 1, 3)
(0, 2, 3, 1)
(0, 3, 1, 2)
(0, 3, 2, 1)
(1, 0, 2, 3)
(1, 0, 3, 2)
(1, 2, 0, 3)
(1, 2, 3, 0)
(1, 3, 0, 2)
(1, 3, 2, 0)
(2, 0, 1, 3)
(2, 0, 3, 1)
(2, 1, 0, 3)
(2, 1, 3, 0)
(2, 3, 0, 1)
(2, 3, 1, 0)
(3, 0, 1, 2)
(3, 0, 2, 1)
(3, 1, 0, 2)
(3, 1, 2, 0)
(3, 2, 0, 1)
(3, 2, 1, 0)

I need to implement this in C, but all the algorithms I have found [1,2] only give the permutations with length equal to the length of the number list, i.e. only the results from (0, 1, 2, 3) in the block above. Reducing the length from 4 to 3 gives only the permutations for the list 0, 1, 2.

I can currently achieve what I want in Python using itertools.permutation, as shown below.

import itertools

MaxN = 4

for Length in range(2, MaxN + 1):
    for perm in itertools.permutations(Indices, Length):
        print perm

Any advice on how to implement this in C would be greatly appreciated.

[1] http://rosettacode.org/wiki/Permutations#C

[2] http://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/

medgoode
  • 53
  • 7

4 Answers4

2

You can do this with a slight modification to [2]:

/* Function to print permutations of string
   This function takes three parameters:
   1. String
   2. Starting index of the string
   3. Ending index of the string.
   4. Length of permutation */
void permute(char *a, int i, int n, int m) 
{
   int j; 
   if (i == m)
   {
       char temp = *(a+i);
       *(a+i) = '\0';
       printf("%s\n", a);
       *(a+i) = temp;
   }
   else
   {
       for (j = i; j <= n; j++)
       {
          swap((a+i), (a+j));
          permute(a, i+1, n, m);
          swap((a+i), (a+j)); //backtrack
       }
   }
} 

Would be used like this:

char a[] = "0123";  
for (int i = 2; i <= 4; i++)
    permute(a, 0, 3, i);

Which gives the same result as your Python implementation.

Taylor Brandstetter
  • 3,268
  • 12
  • 23
1

You can use a combination generating algorithm that will spit out the sequence of C(n, k) combinations for increasing k in an outer loop, and apply the P(k, k) permutation algorithm to produce the sequence of permutations for each combination.

c = empty_combination();
for (k = 0; k <= n; ++k) {
    do {
        c = next_combination(c, sequence, k);
        p = empty_permutation();
        do {
            p = next_permutation(p, c);
            print_permutation(p);
        } while (! is_empty_permutation(p));
    } while (! is_empty_combination(c));
}
Community
  • 1
  • 1
jxh
  • 64,506
  • 7
  • 96
  • 165
0

It is quite simple to do it in C++ . Using the (next_permutation()) method.

http://www.cplusplus.com/reference/algorithm/next_permutation/

0

Here is an iterative method for generating all the subsets!
total number of subsets is 2^n - 1
so it generates the subsets of all length.
if you want the subset of particular length just add a counter and check if you are printing of specific length.

main(){
int a[]={0,1,2,3};
int n=pow(2,4) - 1;
for(int i=0;i<=n;i++){
    int p = i;
    int l=0;
    while(p){
        if(p%2==1)
            printf("%d",a[l]);
        p>>=1;
        l++;
    }
    printf("\n");
}
}
akash
  • 1,653
  • 5
  • 22
  • 38