In this article we are focusing on Introduction to Backtracking (with implementation), in which we have covered advantages and disadvantages as well as use cases of backtracking.
Table of contents
- What is Backtracking?
- When to apply the backtracking algorithm?
- Advantages of Backtracking
- Disadvantages of Backtracking
- Popular use cases of Backtracking
What is Backtracking?
As the term suggests, Backtracking refers to removing the present solution to a problem if it is inadequate or unsuitable, going back (Backtracking), and trying a different approach. It looks for every possible solution to a problem. In essence, it accomplishes this by seeking out every solution that may be applied to a given problem and selecting the most effective one.
Backtracking uses the concept of recursion to go through every possible solution.
Let’s use an example to try to comprehend this concept.
Finding the right route through a maze from one place to another is one of the most frequent problem statements that can be solved through Backtracking.
In actuality, how do we address this issue?
- We try to move in a particular direction after choosing a route.
- If we come to a point where we cannot proceed in the direction of the goal.
- We turn around (backtrack) and try to attempt another route.
Backtracking should be distinct from the brute-force method. In the brute-force method, we look at every possibility until the very last one, at which point we determine whether the result is what we wanted. Backtracking involves stopping an option’s exploration when it becomes clear that it won’t result in a workable solution. Backtracking allows us to reject a choice without going all the way to the conclusion, which saves us a lot of time.
Backtracking is frequently quicker than the brute force method since it avoids the need to generate and evaluate every potential solution. Instead, it can disqualify a lot of applicants with just one test.
Also read: All You Need to Know About Algorithms and Data Structures
Also read: Data Structures and Algorithms Online Courses & Certifications
Must explore: Free Data Structures and Algorithms Courses Online
When to apply the backtracking algorithm?
If there is a constraint on the conclusion or if you want to identify every possible solution to a problem, you can utilize Backtracking. It can also be utilized to resolve various optimization issues when we eliminate a potential answer as soon as we realize it does not exist.
You would think that recursion and Backtracking are similar, but there is a difference. In recursion, the function calls stop when they satisfy the base case. In Backtracking, recursion is used to check and find all possible combinations until and unless we get the best solution for that problem. Both recursion and Backtracking call their functions repeatedly.
Must explore: Introduction To Backtracking Algorithm
Advantages of Backtracking
- Backtracking has a brute-force nature; due to this reason, it can solve maximum problems.
- Backtracking problems are very intuitive to code.
- The step-by-step representation of the backtracking solution is straightforward to understand.
- You can easily debug backtracking code.
- The backtracking code contains less LOC. Most backtracking codes are generally a few lines of recursive function code.
Disadvantages of Backtracking
- When compared to other options, it is quite slow.
- Depending on your data, it is possible to use Backtracking to conduct a thorough search that ultimately yields no results that meet your search criteria.
- Backtracking is a recursive algorithm with a high computational cost that uses a lot of memory and the CPU.
- Due to the usage of recursion and stack storage for function information, there is a high space complexity.
- The branch-and-bound algorithm may be used if you require an efficient algorithm that can handle a huge volume of data and find every feasible solution while using fewer computational resources in a shorter amount of time.
Popular use cases of Backtracking
Let’s start with an introductory problem statement that uses the concept of backtracking to come to the solution so that you get a basic understanding.
Problem Statement: Given N digits, print all the numbers formed by only digits 1 and 2. You have to print the numbers in increasing order.
Input 1:
N = 1
Output 1:
1,2
Input 2:
N = 2
Output 2:
11, 12, 21, 22
Input 3:
N = 3
Output 3:
111, 112, 121, 122, 211, 212, 221, 222
Idea:
Let’s say I have 4 places to fill, i.e, N = 4, so how can you fill the first position?
The answer is 2 since we have only two digits in options 1 and 2.
If filling 4 positions is a problem, then filling 3 positions is a subproblem, so we can quickly get an idea of recursion here.
Let’s make a dry run for N=3. You have to fill 3 positions here.
The idea is to try both the digits at each index (0th, 1st, and 2nd), and as soon as your control reaches the 3rd index, you can simply backtrack.
Implementation:
Parameters to function: N, arr[N], index
where,
N = input
arr = to store elements
index = to keep track of the index (as soon as index = 3, print your number and backtrack)
Number of the function call: 2 (at every index, you have two possibilities, 1 and 2)
Let’s look at the pseudocode now:
class Main{ public static void printAll(int[] arr, int n, int index){ if(index == n){ print(arr); System.out.println(); return; } arr[index] = 1; //first put '1' in 0th index printAll(arr, n, index + 1); //function call for next index arr[index] = 2; //after all the outputs statring with 1, try '2' in 0th index printAll(arr, n, index + 1); //function call for next index } public static void main(String args[]){ int n = 3; //read n int[] arr = new int[n]; printAll(arr, n, 0); } public static void print(int[] arr){ for(int i=0;i<arr.length;i++){ System.out.print(arr[i] + " "); } }}
Output:
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
2 2 2
Conclusion
Backtracking is a general algorithmic technique that is often used in data structures to solve problems recursively by trying out various possible solutions and then undoing them if they do not lead to a solution.If you liked this article then please like it and share with your friends.
Download this article as PDF to read offline
Download as PDFThis is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio
Comments