I am looking at the backtracking algorithm for computing permutations, and although I can understand what is happening, I cannot understand how it solves the problem for any value of h elements.
How are proofs that algorithm is correct made. Can one show that by swapping, one gets all possible arrangements ? Is there any mathematical book about recursion ?
Can anyone explain to me a proof that leads to the backtracking algorithm ?
Here is the algorithm in C++ using a string to play scrabble.
To run an example of all permutations call permute ("abc" 0 3)
void permute(string& a, int i, int h)
{
if (i == h)
cout << a << endl;
else {
for (int j = i; j < h; j++) {
swap(a[i], a[j]); // Swapping done
permute(a, i + 1, l); // Recursion called
swap(a[i], a[j]); // backtrack
}}}
When i=h this will generate
abc acb bac bca cab cba
Now, let's do permute ("abcd" 0 4). Now things really become complicated. Although I have done the example, understood the backtracking procedure, and that one gets all possible permutation of letters, I find difficulty starting on a proof of correctness.
permute ("abcd" 0 4). I understand the backtracking procedure. Am done with examples, but have problem starting about an inductive proof. That it works for all cases of general lengthh. – Veak Jul 31 '23 at 21:37iindicates the depth level. When the deepest level is reached, we spit out a one of the4!possibilities. – Veak Jul 31 '23 at 21:51ionwards. – Veak Jul 31 '23 at 23:10h, it also works correctly for words of lengthh+1. This involves showing that the algorithm generates all permutations for a word of lengthh+1and does not produce duplicates or miss any valid permutations. – Veak Aug 01 '23 at 04:47