What is Backtracking?
Backtracking can be defined as a general algorithmic technique that considers searching every possible combination in order to solve a computational problem.
What is Backtracking Algorithm?
Backtracking is an algorithmic technique for solving problems recursively by trying to build a solution incrementally, one piece at a time, removing those solutions that fail to satisfy the constraints of the problem at any point of time (by time, here, is referred to the time elapsed till reaching any level of the search tree).
Types of Backtracking Algorithm
There are three types of problems in backtracking –
- Decision Problem – In this, we search for a feasible solution.
- Optimization Problem – In this, we search for the best solution.
- Enumeration Problem – In this, we find all feasible solutions.
When can be Backtracking Algorithm used?
For example, consider the SudoKo solving Problem, we try filling digits one by one. Whenever we find that current digit cannot lead to a solution, we remove it (backtrack) and try next digit. This is better than naive approach (generating all possible combinations of digits and then trying every combination one by one) as it drops a set of permutations whenever it backtracks.
- Backtracking | Set 1 (The Knight’s tour problem)
- Backtracking | Set 2 (Rat in a Maze)
- Backtracking | Set 3 (N Queen Problem)
- Backtracking | Set 4 (Subset Sum)
- Backtracking | Set 5 (m Coloring Problem)
- Backtracking | Set 6 (Hamiltonian Cycle)
- Backtracking | Set 7 (Sudoku)
- Backtracking | Set 8 (Solving Cryptarithmetic Puzzles)
- Backtracking | Set 9 (Magnet Puzzle)
- N Queen in O(n) space
- Boggle | Set 2 (Using Trie)
- Remove Invalid Parentheses
- Prime numbers after prime P with sum S
- Rat in a Maze with multiple steps or jump allowed
- A backtracking approach to generate n bit Gray Codes
- C++ program for Solving Cryptarithmetic Puzzles
- Write a program to print all permutations of a given string
- Print all possible paths from top left to bottom right of a mXn matrix
- Tug of War
- 8 queen problem
- Combinational Sum
- Backtracking to find all subsets
- Power Set in Lexicographic order
- Check if a given string is sum-string
- Fill 8 numbers in grid with given conditions
- Word Break Problem using Backtracking
- Minimize number of unique characters in string
- Count all possible paths between two vertices
- Partition of a set into K subsets with equal sum
- Warnsdorff’s algorithm for Knight’s tour problem
- Longest Possible Route in a Matrix with Hurdles
- Match a pattern and String without using regular expressions
- Fill two instances of all numbers from 1 to n in a specific way
- Find all distinct subsets of a given set
- Find shortest safe route in a path with landmines
- Find paths from corner cell to middle cell in maze
- Find if there is a path of more than k length from a source
- Find Maximum number possible by doing at-most K swaps
- Print all palindromic partitions of a string
- Printing all solutions in N-Queen Problem
- Print all paths from a given source to a destination
- Print all possible strings that can be made by placing spaces| Set-1
- Print all possible strings that can be made by placing spaces| Set-2
- Print all longest common sub-sequences in lexicographical order
- Smallest expression to represent a number using single digit
- Given an array A and a number x, check for pair in A with sum as x
- Combinations where every element appears twice and distance between appearances is equal to the value
- Top 20 Backtracking Algorithm Interview Questions
- ‘Practice Problems’ on Backtracking
- ‘Quiz’ on Backtracking
- ‘Videos’ on Backtracking
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.