Backtracking
ALGORITHMS
Backtracking is a problem-solving algorithmic technique that
involves finding a solution incrementally by trying different options
and undoing them if they lead to a dead end.
The following is a general outline of how a backtracking algorithm
works:
1. Choose an initial solution.
2. Explore all possible extensions of the current solution.
3. If an extension leads to a solution, return that solution.
4. If an extension does not lead to a solution, backtrack to the
previous solution and try a different extension.
5. Repeat steps 2-4 until all possible solutions have been explored.
When to Use a Backtracking Algorithm?
Backtracking algorithms are best used to solve problems that have
the following characteristics:
There are multiple possible solutions to the problem.
The problem can be broken down into smaller subproblems.
The subproblems can be solved independently.
Problem : Rat in the Maze
You are given a N×NN×N maze, represented by a 2D array, where:
Each cell is either open (represented by 1) or blocked (represented by 0).
The rat starts at the top-left corner of the maze (i.e., the cell at (0, 0)).
The rat needs to reach the bottom-right corner of the maze (i.e., the cell at (N-1, N-1)).
The rat can move in four possible directions:
Left (x, y-1) ,Right (x, y+1), Up (x-1, y) ,Down (x+1, y)
Objective: Find a path from the start (0, 0) to the destination (N-1, N-1) such that the rat only
moves through open cells (cells with value 1). If a path exists, print one possible path,
otherwise indicate that no path exists.
Problem : Rat in the Maze
input : input :
N=4 N=4
maze = [ maze = [
[1, 0, 0, 0], [1, 0, 0, 0],
[1, 1, 0, 1], [1, 1, 1, 1],
[0, 1, 0, 0], [0, 1, 0, 0],
[1, 1, 1, 1] [1, 1, 0, 1]
] ]
output : True output : False
Problem : The Knight’s tour problem
You are given an N×NN×N chessboard and a knight positioned at the starting
coordinates (x, y). The knight has to move to every square on the board exactly
once. A knight in chess moves in an L-shape: two squares in one direction and then
one square perpendicular to that direction.
Objective
Determine if there is a way for the knight to visit every square on the board exactly
once starting from the given initial position. If such a tour exists, print one possible
sequence of moves. Otherwise, indicate that no tour exists.
Input
1. An integer NN representing the size of the chessboard.
2. Two integers xx and yy representing the starting position of the knight.
Output
1. If a tour exists, output the sequence of moves in the form of an N×NN×N 2D
array, where the moves are numbered from 1 to N2N2.
2. If no tour exists, output an appropriate message (e.g., "No tour exists").
Problem : The Knight’s tour problem
Input:
N=5
start_x = 0
start_y = 0
Output (one possible solution):
[
[1, 14, 19, 10, 3],
[18, 9, 2, 13, 20],
[15, 6, 25, 4, 11],
[8, 17, 12, 5, 22],
[23, 16, 7, 24, 21]
]
Problem : N-Queen problem
You are given an integer NN. The task is to place NN queens on an N×NN×N
chessboard so that no two queens attack each other.
Objective
Find all possible ways to place NN queens on the chessboard, or if that's not
required, find at least one valid configuration.
Input
1. An integer NN representing the size of the chessboard and the number of
queens.
Output
1. A list of all possible solutions, where each solution is represented by a 2D array
of size N×NN×N with 1s representing queens and 0s representing empty spaces.
2. If no solution exists (which happens if N<4N<4 for N≠1N=1), the output should
indicate that no solutions exist.
Problem : N-Queen problem
Input : N = 4
Output
[
[0, 0, 1, 0],
[1, 0, 0, 0],
[0, 0, 0, 1],
[0, 1, 0, 0]
],
Problem : Permutations of a String
A permutation also called an “arrangement number” or “order,” is a rearrangement of
the elements of an ordered list S into a one-to-one correspondence with S itself. A
string of length N has N! permutations.
Input: S = “ABC”
Output: “ABC”, “ACB”, “BAC”, “BCA”, “CBA”, “CAB”
Problem : Sudoku Solver
Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to
9) to the empty cells so that every row, column, and subgrid of size 3×3 contains
exactly one instance of the digits from 1 to 9.
board = [
[5, 3, 0, 0, 7, 0, 0, 0, 0],
[6, 0, 0, 1, 9, 5, 0, 0, 0],
[0, 9, 8, 0, 0, 0, 0, 6, 0],
[8, 0, 0, 0, 6, 0, 0, 0, 3],
[4, 0, 0, 8, 0, 3, 0, 0, 1],
[7, 0, 0, 0, 2, 0, 0, 0, 6],
[0, 6, 0, 0, 0, 0, 2, 8, 0],
[0, 0, 0, 4, 1, 9, 0, 0, 5],
[0, 0, 0, 0, 8, 0, 0, 7, 9]
]
Problem : Remove Invalid Parentheses
An expression will be given which can contain open and close parentheses and
optionally some characters, No other operator will be there in string. We need to
remove minimum number of parentheses to make the input string valid. If more than
one valid output are possible removing same number of parentheses then print all
such output.
Examples:
Input : str = “()())()” -
Output : ()()() (())()
There are two possible solutions
"()()()" and "(())()"
Input : str = (v)())()
Output : (v)()() (v())()