0

i cant understand how permute function works.Every time i lost to find what actually permute function do and why it's called backtracking????

void swap (char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
void permute(char *a, int i, int n) 
{
   int j; 
   if (i == n)
     printf("%s\n", a);
   else
   {
        for (j = i; j <= n; j++)
       {
          swap((a+i), (a+j));
          permute(a, i+1, n);
          swap((a+i), (a+j)); //backtrack
       }
   }
}
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Atif
  • 210
  • 2
  • 11
  • 2
    it recursively prints all the permutations of an array. it does so by changing the array. the 'backtracking' changes it back – sp2danny Jul 10 '14 at 06:10
  • Did you google for [recursion](http://imgur.com/ikPW2Ec)? – Pradhan Jul 10 '14 at 06:20
  • 1
    While [this SO answer](http://stackoverflow.com/questions/9552295/using-recursion-and-backtracking-to-generate-all-possible-combinations?rq=1) deals with all possible combinations, the basic principle is the same. – Pradhan Jul 10 '14 at 06:23
  • "backtracking" is normally used when you can think of the ways to find a solution as describing a tree... if the algorithm has exhausted possible "solutions" (here permutations) down one part of the tree, it backtracks by going up to the parent node of the exhausted subtree then down a previously sibling branch, exploring new possible solutions. – Tony Delroy Jul 10 '14 at 07:03

2 Answers2

1

This particular implementation works from the observation the set of all permutations of a sequence {a, b, c, d, e, ...} can be described as follows:

all sequences that start with 'a': { a, ... }, all sequences that start with 'b' :{ b, ...}, etc. where all sequences that start with 'a' ({a, x, y, z, ... }} are just an 'a' followed by all possible sequences of { x, y, z, ...} ( and that is the the recursive part).

Backtracking refers to the fact that the algorithm starts from a given partial solution ( {a, x, y, z}) then "goes into" solving the yet unsolved part ({x, y, z}) and then goes back to the situation where the previously given situation is not a given anymore but will change (going from {a, x, y, z} to { b, ...}, { c, ...})

dhavenith
  • 2,028
  • 13
  • 14
0
//Swapping two characters
void swap (char *x, char *y)
{

    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

void permute(char *a, int i, int n) 
{

    int j; 
    if (i == n)
    //This is an edge case where (i==j==n) and hence no swap needed (recursion terminates).
    printf("%s\n", a);
    else
    {
        for (j = i; j <= n; j++)
     {
          swap((a+i), (a+j));
          permute(a, i+1, n);
          /*Basically recursion terminated for first set of swapping 
          and then get back the original array by backtracking to apply second set of swapping. 
          Then iterating this procedure for all sets of swapping will 
          give all the permutations of the given set of characters*/
          swap((a+i), (a+j));
       }
   }
}
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
gmfreak
  • 399
  • 4
  • 12