0

This is a question that has already appeared a few times (cf Print all the permutations of a string in C) however what i'm having trouble with is a bit different and i'm having an hard time figuring an answer.

I'm currently working on a function that would apply another function on all permutations of an int array. I decided to use backtracking and it resulted in the code below.

void ft_perm(int *mat, int size, int i) // Initially called with i = 0
{
    int j;

    j = i;
    if (i == size)
    {
        f(mat, ...) // any function that applies itself on mat
    }
    else
    {
        while (j >= 0)
        {
            ft_swap(mat, i, j); // function that swaps i and j
            ft_perm(mat, size, i + 1);
            ft_swap(mat, i, j); // backtrack
            j--;
        }
    }
}

However, the order in which the function is applied is not exactly what i'd need and i can't figure out how to make it work correctly. I would need 'f' to be applied in an order where the smallest number is always at the leftmost.

For example, using a 3 number array, the current function will apply 'f' on 012->021->210->102->120 while what i'd need would be to apply 'f' on 012->021->102->201->120->210.

But that's specific, in a nutshell, my question is "Is it possible to manipulate the permutation order using backtracking ?"

Thanks for your help and well, sorry for the long and wobbly question.

Ps : Thanks for the edit !

  • 1
    I think it'll help you: http://stackoverflow.com/questions/29928236/print-all-permutation-in-lexicographic-order – Edgar Rokjān Feb 12 '16 at 19:02
  • Thanks ! I'm currently reading trough the answer but the thought process is very probably the same one i should use. I'll try that right now ! – Luci Dyevan Feb 12 '16 at 19:09
  • Ive been searching for two days but i really cant figure it out... My case is pretty unusual. Is there anybody that has an idea ? – Luci Dyevan Feb 15 '16 at 13:54

0 Answers0