I understand that this is a "vague" question, but I completed a data structures class at my college and still do not understand how recursive backtracking works. I don't understand the "magic" of recursion and struggle to write recursive backtracking algorithms. Like say I give an example here that is a a template for recursive backtracking:
void findSolutions(n, other params) {
if (found a solution) {
solutionsFound++;
displaySolution();
if (solutionsFound >= solutionTarget) {
System.exit(0);
return;
}
}
for (val = first to last) {
if (isValid(val, n)) {
applyValue(val, n);
findSolutions(n + 1, other params);
removeValue(val, n);
}
}
}
I don't understand how this actually works. And I'm kind of tired of having someone tell me that I just have to believe it does. I'd really like an in depth explanation of how recursion actually works and how to write it. I believe that recursion is an important concept to understand for technical interviews and such and I really want to be able to write code when I need to solve problems like these. Any tips would be greatly appreciated for explaining this as well as good resources to look at and study as well.