Introduction to Backtracking Pattern
Backtracking
Backtracking is an algorithmic technique that uses brute-force approach to solve a problem.
Brute-force approach states that for any problem, we should try out all possible solutions and pick up those solutions that satisfy the problem constraints.
In backtracking, we build a solution incrementally and follow the approach that if the current solution can’t lead to a valid solution, abandon it and backtrack (or go back) to try another solution. Because of this, recursion becomes a suitable technique for solving backtracking problems.
Dynamic Programming (DP) uses a similar approach where we try out all possible solutions (using Memoization) to pick up the most optimized solution. DP is used to solve optimization problems; backtracking, on the other hand, is mostly used when multiple valid solutions are possible for a problem.
An Example
Let’s understand backtracking with an example.
Imagine we want to plant, in a straight line, three fruit trees Apple, Orange, and Mango. What are the possible ways these trees can be planted? The only restriction we have is that Apple and Orange trees can’t be placed next to each other.
Following a brute-force approach, we can find all possible combinations in which the three trees can be placed and pick those that fulfill the given constraint.
Let’s try finding all the possible arrangements.
The first tree can be of any kind, Apple, Orange, or Mango. If our first tree is Apple, the second tree can be Orange or Mango. If we place Orange as our second tree, then our constraint is not fulfilled (which was not to place Apple and Orange together). Hence it is not a valid arrangement; therefore, we go back (i.e., backtrack) and place Mango as our second tree.
Now, we can place Orange in the third place; this arrangement fulfills the problem constraint.
The following diagram shows all possible arrangements:
From the above diagram, we can clearly see that the two valid arrangements are: [Apple, Mango, Orange]
and [Orange, Mango, Apple]
.
This approach of evaluating all possible solutions, going back whenever we see that a certain constraint can’t be met, and trying out other possible solutions is called backtracking.
Let’s solve some more problems to develop a deeper understanding of backtracking.
Combination Sum
Top Interview 150 | 39. Combination Sum Design Gurus
Problem Statement
Given an array of distinct positive integers candidates
and a target integer target
, return a list of all unique combinations of candidates where the chosen numbers sum to target
. You may return the combinations in any order.
The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.
Example 1:
1 | Input: candidates = [2, 3, 6, 7], target = 7 |
Example 2:
1 | Input: candidates = [2, 4, 6, 8], target = 10 |
Constraints:
1 <= candidates.length <= 30
2 <= candidates[i] <= 40
- All elements of candidates are distinct.
1 <= target <= 40
Solution
This problem is quite similar to the example discussed in the previous lesson (placing apple, orange, and mango trees).
We can follow the brute-force approach to incrementally build the solution.
If, at any time, we find out that the current solution can’t lead to a valid combination, we will abandon it and backtrack to try the remaining solutions.
Let’s try this approach on the following input: Candidates: [3, 4, 5], Target: 9
Here are the number of steps in our algorithm:
- We will start with an empty set. This also means that the sum of the elements in the set is zero.
- We can try adding all three numbers separately in the set. This will give us three sets: [3], [4], [5].
- Let’s take set [3], since the sum of its elements is less than the Target (=9), we will try adding more numbers to it.
- We can add all three numbers again to generate new sets. We can add ‘3’ again, as each number can be added multiple times.
- After adding all numbers separately to the set [3], we will get the following sets: [3, 3], [3, 4], [3, 5]. We can see, each set has a sum less than the target.
- We can now, repeat the same process as described in step-4.
- For [3, 3]: Adding ‘3’ will give us a valid solution [3, 3, 3] having a sum equal to the target. Adding ‘4’ and ‘5’ will give us a sum which is greater than the target. Therefore, we can stop the search here for this set, as adding any additional number will not produce a correct solution.
- For [3, 4]: We will add ‘4’ or ‘5’ to it, which will give us a sum greater than the target. We will stop our search here for this set.
- For [3, 5]: We can only add ‘5’ to it, which does not produce a valid solution.
- Similar approach can be applied to other sets.
- In the end, we can see that the valid solutions are: [3, 3, 3] and [4, 5].
Code
The basic idea is to start with an empty combination and iterate through the candidates array, adding each candidate to the current combination and recursively calling the function with the updated combination and the remaining target. If the target becomes 0, we add the current combination to the result list. If the target becomes negative, we backtrack and remove the last added candidate from the combination.
The function combinationSum
takes in two parameters, an array of distinct integers candidates
and a target integer target
, and returns a list of all unique combinations of candidates where the chosen numbers sum to target. The function starts by defining the function backtrack(candidates, start, target, comb, res)
. This function takes five parameters, candidates
, start
, target
, comb
, and res
:
candidates
is the array containing candidate elements.start
is the starting index of thecandidates
array.target
is the remaining target.comb
is the current combination.res
is the final result list.
The backtrack
function uses recursion to find the combinations. The base case for the recursion is if the target
is 0. When the target
is 0, that means we have found a valid combination and we append a copy of the current combination to the result list. It then iterates through the candidates array starting from the given index. If the current candidate is greater than the remaining target, it skips the current candidate and move on to the next. If the current candidate is less than the remaining target, it adds the current candidate to the current combination, recursively calls the function with the updated combination and remaining target, and then backtracks by removing the last added candidate from the combination.
1 | class Solution(object): |
Time Complexity
This algorithm has a time complexity of O(N^(T/M + 1)), where is N the total number of elements in the candidates array, T is the target value, and M is the smallest value among the candidates. This is because the execution of the backtracking is similar to a DFS traversal of an n-ary tree. So, the time complexity would be the same as the number of nodes in the n-ary tree. This can be seen in the above diagram.
Each node can call the backtrack
function a maximum of N times, i.e., the total number of candidates. The maximal depth of the n-ary tree would be T/M, where we keep on adding the smallest element to the combination. As we know, the maximal number of nodes in N-ary tree of T/M height would be N^(T/M + 1), hence the time complexity is O(N^(T/M + 1)).
Space Complexity
Ignoring the space needed for the output array, the space complexity will be O(T/M) because at any time, we can pile up to T/M recursive calls of the backtrack
function; this will happen when we keep on adding the smallest element to the combination. As a result, the space overhead of the recursion is O(T/M).
Word Search
Top Interview 150 | 79. Word Search Design Gurus
类似 这道题
Problem Statement
Given an m x n grid of characters board and a string word, return true if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
Example 1:
Input: word=”ABCCED”, board:
1 | { 'A', 'B', 'C', 'E' }, |
Output: true Explanation: The word exists in the board:
-> { ‘A’, ‘B’, ‘C’, ‘E’ },
-> { ‘S’, ‘F’, ‘C’, ‘S’ },
-> { ‘A’, ‘D’, ‘E’, ‘E’ }
Example 2:
Input: word=”SEE”, board:
1 | { 'A', 'B', 'C', 'E' }, |
Output: true Explanation: The word exists in the board:
-> { ‘A’, ‘B’, ‘C’, ‘E’ },
-> { ‘S’, ‘F’, ‘C’, ‘S’ },
-> { ‘A’, ‘D’, ‘E’, ‘E’ }
Constraints:
m == board.length
n = board[i].length
1 <= m, n <= 6
1 <= word.length <= 15
board
andword
consists of only lowercase and uppercase English letters.
Solution
The basic approach to solving the word search problem using backtracking is to start at the first character of the word and check all 8 adjacent cells in the grid to see if any of them match the next character of the word. If a match is found, mark the cell as visited and recursively check the next character of the word in the adjacent cells of the newly visited cell. If the entire word is found, return true. If no match is found, backtrack to the previous cell and try a different path. Repeat this process until the entire grid has been searched or the word is found.
Code
This function takes a 2D list board
and a string word
as input, and returns True
if the word can be found in board and False
otherwise. It uses a helper function dfs
which takes 4 additional parameters: i
and j
are the current coordinates of the cell that is being visited, k
is the index of the current character of the word being matched, and board
and word
are the inputs passed to the main function.
The dfs function uses a helper variable tmp
to store the current value of the cell before it is marked as visited. This is done so that we can backtrack later. It then uses recursion to check if the next character of the word exists in the 4 adjacent cells, and it will mark the cell as visited and move to next index of the word by incrementing k
by 1. If the next character is found, the function returns true
, if not it backtracks to the previous cell, and continues the search in different path. If the entire word is found, the function returns True
, otherwise it returns False
after searching the entire grid.
1 | class Solution: |
Time Complexity
The time complexity of the ‘exist’ function is O(4^n), where is n the length of the word. This is because the function uses a depth-first search (DFS) algorithm to traverse the board, and for each cell in the board, it checks all 4 adjacent cells recursively. The worst-case scenario is when the word is found in the last cell of the board, and in that case, the function will have to check all possible paths from the starting cell to the last cell, which is 4^n possible paths.
Space Complexity
The space complexity of the exist
function is O(n), where n is the length of the word. This is because the function uses a DFS algorithm, and the maximum depth of the recursion tree is n. In other words, the maximum number of function calls that will be stored on the call stack at any point in time is n.
Sudoku Solver
37. Sudoku Solver Design Gurus
Problem Statement
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
- Each of the digits 1-9 must occur exactly once in each row.
- Each of the digits 1-9 must occur exactly once in each column.
- Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
The ‘.’ character indicates empty cells.
Example 1:
Input:
1 | {'5', '3', '.', '.', '7', '.', '.', '.', '.'}, |
Output:
1 | {'5'', '3'', '4'', '6'', '7'', '8'', '9'', '1'', '2''}, |
Explanation: The given output is the only valid Sudoku solution.
Constraints:
board.length == 9
board[i].length == 9
board[i][j]
is a digit or ‘.’.- It is guaranteed that the input board has only one solution.
Solution
Sudoku is a popular puzzle game that involves filling in a 9x9 grid with numbers such that each column, each row, and each of the nine 3x3 sub-grids that compose the grid contains all of the digits from 1 to 9. One way to solve a sudoku puzzle is by using backtracking, which is a form of brute-force search.
Here are the steps to solve a sudoku puzzle using backtracking:
- Start with an empty 9x9 grid and fill in the numbers that are given in the puzzle.
- From left to right and top to bottom, find the first empty cell (denoted by a
.
) in the grid. - Try out every number from 1 to 9 as a possible solution for the empty cell.
- Before filling in a number, check if it is a valid solution by checking if the number is already present in the same row, column, or 3x3 sub-grid.
- If the number is valid, fill it in the cell and recursively call the function to move to the next empty cell.
- If the number is not valid, backtrack to the previous state and try a different number.
- If a valid solution is found, return true. If no solution is found, return false.
- If the function returns true, the grid is filled with a valid solution. Else, the puzzle is unsolvable.
Code
The function solveSudoku
starts by iterating through the board and checking for empty cells represented by ‘.’ character. When an empty cell is found, the function tries every number from 1-9 by using a nested loop. Before filling the cell with a number, the function checks if the number is valid in the current cell by using the helper function isValid
. The function isValid
checks if the same number is already present in the same row, column or 3x3 box. If the number is valid, the cell is filled with the number and the function recursively calls itself to solve the rest of the board.
If the current number doesn’t lead to a solution, the function backtracks by emptying the cell and trying the next number. If all the numbers have been tried and none of them lead to a solution, the function returns false. If the board is completely filled, the function returns true, indicating that a solution has been found.
1 | class Solution: |
Time Complexity
since the board size is fixed, the time complexity is O(1), as there is no variable input.
Though let’s discuss the number of operations needed: (9!)^9
Let’s consider one row where we have 9 cells to fill. There are not more than 9 possibilities for the first number to put, not more than 9×8 for the second one, not more than 9×8×7 for the third one, and so on. In total, that results in not more than (9!) possibilities for just one row; this means not more than (9!)^9 operations in total.
If we compare the brutefore and backtracking algorithms:
- 9^81 = 1966270504755529136180759085269121162831034509442147669273154155379663911968099 for the brute force,
- and (9!)^9 =109110688415571316480344899355894085582848000000000 for the standard backtracking, i.e. the number of operations is reduced in times!
Space Complexity
The board size is fixed, and the space is used to store the board containing 81 cells, hence the time complexity is O(1).
Factor Combinations
leetcode 会员 Design Gurus
Problem Statement
Numbers can be regarded as the product of their factors.
For example, 8 = 2 x 2 x 2 = 2 x 4.
Given an integer n, return all possible combinations of its factors. You may return the answer in any order.
Example 1:
1 | Input: n = 8 |
Example 2:
1 | Input: n = 20 |
Constraints:
- 1 <= n <= 10^7
Solution
We can use backtracking to find all the factors of a given number .
We will start by iterating through all integers from start (2 by default) to the square root of n+1
. If the current integer i
divides n
, we will add i
to the list of current factors curr
and appends this list along with the corresponding factor of n/i
to the list of all factors (result
). Then we can recursively call the function with n/i
as the new input, i
as the new start value, and curr
and result
as the current factors and results. After each recursive call, we have to pop the last element from curr
to backtrack and find other factors.
Code
Here is what our algorithm will look like:
1 | class Solution: |
Time Complexity
We can’t have more than factors of a number n
. This means that getAllFactors
can be called a maximum of times recursively. The for
loop iterates a maximum of . This means that the overall time complexity is or
Space Complexity
Ignoring the space needed for the output array, the space complexity will be as we need to save only one factor while in the recursion, and our recursion tree won’t get bigger than steps.
Split a String Into the Max Number of Unique Substrings
1593. Split a String Into the Max Number of Unique Substrings Design Gurus
Problem Statement
Given a string s
, return the maximum number of unique substrings that the given string can be split into.
You can split string s
into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
1 | Input: s = "aab" |
Example 2:
1 | Input: s = "abcabc" |
Constraints:
1 <= s.length <= 16
s
contains only lower case English letters.
Solution
We can use backtracking to solve this problem.
This solution uses a helper function splitAndCount
which takes three arguments, the input string s
, the current start position and a set set
to keep track of the unique substrings that have been split so far. The maxUniqueSplit
function calls the splitAndCount
function to find the maximum number of unique substrings that the given string can be split into.
The splitAndCount
function starts with a base case where it returns the size of the set when the current start position is equal to the length of the input string. This means that all substrings have been processed and the size of the set represents the maximum number of unique substrings.
The function then uses a for loop to iterate through all possible substrings starting from the current start position. For each substring, it checks if the substring is already in the set. If it is not, the substring is added to the set and the function is recursively called with the new start position being the end of the current substring. This continues until all possible substrings have been processed.
After the recursive call, the substring is removed from the set to backtrack. The function keeps track of the maximum number of unique substrings found so far and returns this maximum count when all substrings have been processed.
Code
Here is the code of our algorithm:
1 | class Solution: |
Time Complexity
We can split any given string of length n
in 2^n
ways. Hence the time complexity will be O(2^n).
Space Complexity
The space complexity will be O(n) as we need to save only one way of splitting the given string while in the recursion, and our recursion tree won’t get bigger than O(n) steps too.