BACKTRACKING Algorithms
BACKTRACKING Algorithms
Natore.
Algorithms
CSE-213
Course Teacher:
Steps in Backtracking
Advantages of Backtracking
• Systematic and exhaustive: Backtracking ensures that all possible solutions are
considered, making it a reliable approach for finding all feasible solutions.
• No need for heuristics: Backtracking does not require any heuristics or problem-specific
knowledge, making it a versatile algorithm for a variety of problems.
• Easy to implement: Backtracking is relatively simple to implement, especially in
recursive programming languages.
Disadvantages of Backtracking
• Time and space complexity: Backtracking can be inefficient for problems with a large
number of possible solutions, as it explores all possibilities systematically. This can lead
to high time and space complexity.
• Ineffective for optimization problems: Backtracking is primarily used for finding
feasible solutions, not for finding optimal solutions. For optimization problems, other
algorithms like dynamic programming or greedy algorithms are more appropriate.
Examples of Backtracking
• Maze solving: Backtracking can be used to find a path from the starting point to the goal
in a maze by systematically exploring all possible paths while avoiding walls.
• Sudoku: Backtracking can be used to solve Sudoku puzzles by filling in the empty cells
with numbers while satisfying the constraints of the puzzle.
• N-queens problem: Backtracking can be used to place N queens on an N x N chessboard
such that no two queens attack each other.
Applications of Backtracking
N QUEENS Problem
The N-queens problem is a classic problem in computer science, where the goal is to place N
queens on an N x N chessboard such that no two queens attack each other. Queens attack each
other if they are in the same row, column, or diagonal.
There are many ways to solve the N-queens problem, but one of the most common is
backtracking. Backtracking is an algorithm that systematically explores all possible solutions
to a problem, one decision at a time. In the case of the N-queens problem, the decisions are
where to place the queens on the chessboard.
Here is a step-by-step description of how to solve the N-queens problem using backtracking:
It can be seen that for n =1, the problem has a trivial solution, and no solution exists for n =2
and n =3. So first we will consider the 4 queens problem and then generate it to n - queens
problem.
Given a 4 x 4 chessboard and number the rows and column of the chessboard 1 through 4.
Since, we have to place 4 queens such as q1 q2 q3 and q4 on the chessboard, such that no two
queens attack each other. In such a conditional each queen must be placed on a different row,
i.e., we put queen "i" on row "i.".
Now, we place queen q1 in the very first acceptable position (1, 1). Next, we put queen q2 so
that both these queens do not attack each other. We find that if we place q2 in column 1 and 2,
then the dead end is encountered. Thus the first acceptable position for q2 in column 3, i.e. (2,
3) but then no position is left for placing queen 'q3' safely. So we backtrack one step and place
the queen 'q2' in (2, 4), the next best possible solution. Then we obtain the position for placing
'q3' which is (3, 2). But later this position also leads to a dead end, and no place is found where
'q4' can be placed safely. Then we have to backtrack till 'q1' and place it to (1, 2) and then all
other queens are placed safely by moving q2 to (2, 4), q3 to (3, 1) and q4 to (4, 3). That is, we
get the solution (2, 4, 1, 3). This is one possible solution for the 4-queens problem. For another
possible solution, the whole method is repeated for all partial solutions. The other solutions for
4 - queens problems is (3, 1, 4, 2) i.e.
The implicit tree for 4 - queen problem for a solution (2, 4, 1, 3) is as follows:
Fig shows the complete state space for 4 - queens problem. But we can use backtracking method
to generate the necessary node and stop if the next node violates the rule, i.e., if two queens are
attacking.
4 - Queens solution space with nodes numbered in DFS
It can be seen that all the solutions to the 4 queens problem can be represented as 4 - tuples (x1,
x2, x3, x4) where xi represents the column on which queen "qi" is placed.
Assignment/Home Work
Sub: Algorithms
Sub Code: 213
Marks: 15
1. Sum of Subsets Problem
2. Graph Coloring
3. Hamiltonian Cycles
Follow the following instructions:
https://www.javatpoint.com/n-queens-problems