BacktrackingIn this topic, we will learn about the backtracking, which is a very important skill set to solve recursive solutions. Recursive functions are those that calls itself more than once. Consider an example of Palindrome: Initially, the function isPalindrome(S, 0, 8) is called once with the parameters isPalindrome(S, 1, 7). The recursive call isPalindrome(S, 1, 7) is called once with the parameters isPalindrome(S, 2, 6). Backtracking is one of the techniques that can be used to solve the problem. We can write the algorithm using this strategy. It uses the Brute force search to solve the problem, and the brute force search says that for the given problem, we try to make all the possible solutions and pick out the best solution from all the desired solutions. This rule is also followed in dynamic programming, but dynamic programming is used for solving optimization problems. In contrast, backtracking is not used in solving optimization problems. Backtracking is used when we have multiple solutions, and we require all those solutions. Backtracking name itself suggests that we are going back and coming forward; if it satisfies the condition, then return success, else we go back again. It is used to solve a problem in which a sequence of objects is chosen from a specified set so that the sequence satisfies some criteria. When to use a Backtracking algorithm?When we have multiple choices, then we make the decisions from the available choices. In the following cases, we need to use the backtracking algorithm:
How does Backtracking work?Backtracking is a systematic method of trying out various sequences of decisions until you find out that works. Let's understand through an example. We start with a start node. First, we move to node A. Since it is not a feasible solution so we move to the next node, i.e., B. B is also not a feasible solution, and it is a deadend so we backtrack from node B to node A. Suppose another path exists from node A to node C. So, we move from node A to node C. It is also a deadend, so again backtrack from node C to node A. We move from node A to the starting node. Now we will check any other path exists from the starting node. So, we move from start node to the node D. Since it is not a feasible solution so we move from node D to node E. The node E is also not a feasible solution. It is a dead end so we backtrack from node E to node D. Suppose another path exists from node D to node F. So, we move from node D to node F. Since it is not a feasible solution and it's a deadend, we check for another path from node F. Suppose there is another path exists from the node F to node G so move from node F to node G. The node G is a success node. The terms related to the backtracking are:
Many problems can be solved by backtracking strategy, and that problems satisfy complex set of constraints, and these constraints are of two types:
Applications of Backtracking
Difference between the Backtracking and RecursionRecursion is a technique that calls the same function again and again until you reach the base case. Backtracking is an algorithm that finds all the possible solutions and selects the desired solution from the given set of solutions.
Next TopicRecursive Maze Algorithm
