Determining Backtracking Problems
Generally every constraint satisfaction problem can be solved using backtracking but, Is it optimal to use backtracking every time? Turns out NO, there are a vast number of problem that can be solved using Greedy or Dynamic programming in logarithmic or polynomial time complexity which is far better than exponential complexity of Backtracking. However many problems still exists that can only be solved using Backtracking.
To understand whether a problem is Backtracking based or not, let us take a simple problem:
Problem: Imagine you have 3 closed boxes, among which 2 are empty and 1 has a gold coin. Your task is to get the gold coin.Why dynamic programming fails to solve this question: Does opening or closing one box has any effect on the other box? Turns out NO, each and every box is independent of each other and opening/closing state of one box can not determine the transition for other boxes. Hence DP fails.
Why greedy fails to solve this question: Greedy algorithm chooses a local maxima in order to get global maxima, but in this problem each and every box has equal probability of having a gold coin i.e 1/3 hence there is no criteria to make a greedy choice.
Why Backtracking works: As discussed already, backtracking algorithm is simply brute forcing each and every choice, hence we can one by one choose every box to find the gold coin, If a box is found empty we can close it back which acts as a Backtracking step.
Technically, for backtracking problems:
- The algorithm builds a solution by exploring all possible paths created by the choices in the problem, this solution begins with an empty set S={}
- Each choice creates a new sub-tree ‘s’ which we add into are set.
- Now there exist two cases:
- S+s is valid set
- S+s is not valid set
- In case the set is valid then we further make choices and repeat the process until a solution is found, otherwise we backtrack our decision of including ‘s’ and explore other paths until a solution is found or all the possible paths are exhausted.
Introduction to Backtracking – Data Structure and Algorithm Tutorials
Backtracking is like trying different paths, and when you hit a dead end, you backtrack to the last choice and try a different route. In this article, we’ll explore the basics of backtracking, how it works, and how it can help solve all sorts of challenging problems. It’s like a method for finding the right way through a complex choices.
Table of Content
- What is Backtracking?
- Types of Backtracking Problems
- How does Backtracking works?
- Determining Backtracking Problems
- Pseudocode for Backtracking
- Complexity Analysis of Backtracking
- How Backtracking is different from Recursion?
- Applications of Backtracking
- Must Do Backtracking Problems
Contact Us