Backtracking is a general algorithm for finding all (or some) solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons each partial candidate c (“backtracks”) as soon as it determines that c cannot possibly be completed to a valid solution.
Backtracking can be applied only for problems that admit the concept of a “partial candidate solution” and a relatively quick test of whether it can be completed to a valid solution. Backtracking is often much faster than brute force enumeration of all candidates since it can eliminate a large number of candidates with a single test.
In this post, we have listed out common problems that can be solved using the backtracking technique:
- Print all possible solutions to N–Queens problemHard
- Print all possible Knight’s tours on a chessboardHard
- Find the shortest path in a mazeMedium
- Find the longest possible route in a matrixMedium
- Find the path from source to destination in a matrix that satisfies given constraintsMedium
- Find the total number of unique paths in a maze from source to destinationMedium
- Find all combinations of elements satisfying given constraintsMedium
- K–Partition Problem | Printing all partitionsHard
- Magnet PuzzleHard
- Print all k–colorable configurations of a graph (Vertex coloring of a graph)Medium
- Find the minimum number possible by doing at-most
kswapsMedium - Find all permutations of a string – C++, Java, PythonHard
- Determine whether a string matches with a given patternHard
- Find all paths from the first cell to the last cell of a matrixMedium
- Print all shortest routes in a rectangular gridMedium
- Find all distinct combinations of a given length with repetition allowedMedium
- Print all combinations of numbers from 1 to
nhaving sumnMedium - Print all triplets in an array with a sum less than or equal to a given numberMedium
- Find all possible combinations by replacing given digits with characters of the corresponding listHard
- Find all combinations of non-overlapping substrings of a stringMedium
- Find all n-digit numbers with equal sum of digits at even and odd indicesHard
- Find all occurrences of the given string in a character matrixHard
- Print all distinct subsets of a given setHard
- Find all binary strings that can be formed from a wildcard patternMedium
- Find ways to calculate a target from elements of the specified arrayMedium
- Find all Possible Topological Orderings of a DAGHard
- Print all Hamiltonian paths present in a graphHard
- Find the path between given vertices in a directed graphEasy
- Generate a list of possible words from a character matrixHard
- Print all paths from the root to leaf nodes of a binary treeEasy
- Print all paths from leaf to root node of a binary treeMedium
- Generate the power set of a given setMedium
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)