0% found this document useful (0 votes)
145 views8 pages

Backtracking Algorithm Overview and Examples

1. The document discusses backtracking, an algorithmic technique for solving problems by systematically exploring all possible decisions and discarding those that lead to unfeasible solutions. 2. It provides steps for backtracking algorithms, examples of problems that can be solved using backtracking like maze solving and Sudoku, and applications like artificial intelligence and constraint satisfaction problems. 3. A key example discussed is the N-queens problem, where backtracking is used to place N queens on a chessboard so that no two queens attack each other, by systematically trying all possible placements while avoiding infeasible solutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views8 pages

Backtracking Algorithm Overview and Examples

1. The document discusses backtracking, an algorithmic technique for solving problems by systematically exploring all possible decisions and discarding those that lead to unfeasible solutions. 2. It provides steps for backtracking algorithms, examples of problems that can be solved using backtracking like maze solving and Sudoku, and applications like artificial intelligence and constraint satisfaction problems. 3. A key example discussed is the N-queens problem, where backtracking is used to place N queens on a chessboard so that no two queens attack each other, by systematically trying all possible placements while avoiding infeasible solutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Rajshahi Science and Technology University (RSTU),

Natore.

Algorithms

CSE-213

Course Teacher:

Md Shahidul Islam Shabuz

Lecturer, Department of CSE, RSTU.

Personal Website: [Link]


BACKTRACKING

Backtracking is an algorithmic technique for solving problems by systematically exploring all


possible decisions and discarding those that lead to unfeasible solutions. It is a recursive
algorithm that builds a solution incrementally, one decision at a time, while constantly checking
for constraints and backtracking if necessary.

Steps in Backtracking

1. Make a decision: Choose one of the available options.


2. Check for constraints: Determine if the chosen decision violates any constraints.
3. If the decision is feasible: Continue building the solution by making another decision.
4. If the decision is not feasible: Backtrack to the previous decision point and try a different
option.
5. Repeat steps 1 to 4 until a solution is found or all possibilities have been exhausted.

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

• Artificial intelligence: Backtracking is a fundamental technique in artificial intelligence,


used in problem solving, planning, and decision making.
• Constraint satisfaction problems: Backtracking is commonly used to solve constraint
satisfaction problems, where the goal is to find a solution that satisfies a set of
constraints.
• Graph algorithms: Backtracking is used in various graph algorithms, such as finding
shortest paths, finding Hamiltonian cycles, and finding minimum spanning trees.

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:

▪ Start with an empty chessboard.


▪ Place the first queen on any empty square.
▪ Recursively place the remaining queens, one at a time, on empty squares that are not
attacked by any of the previously placed queens.
▪ If all N queens can be placed successfully, then the solution has been found.
▪ If no empty square can be found that is not attacked by any of the previously placed
queens, then backtrack to the previous decision point and try a different placement for
the previous queen.
▪ Repeat steps 3-5 until a solution is found or all possibilities have been exhausted.
N - Queens problem is to place n - queens in such a manner on an n x n chessboard that no
queens attack each other by being in the same row, column or diagonal.

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:

1. Write down basic of the problem


2. Gives a appropriate examples
3. Explanations how to solve the problems
4. Application of the problems
5. Advantages and Disadvantages

N.B: You can follow according to N-QUEENS problems above.

Send the pdf of your assignment mshahidul@[Link]

Deadline: Softcopy or pdf send to Email: 23 November 2023 at 11.59 PM

Hardcopy: 24 November 2023 during office times.


References:

[Link]

Common questions

Powered by AI

Backtracking is considered inefficient for problems with a large number of possible solutions due to its high time and space complexity, as it exhaustively explores all possibilities. It is also not ideal for optimization problems as it does not focus on finding the most optimal solution. In such cases, alternative algorithms like dynamic programming and greedy algorithms are preferable, as they are designed to efficiently solve such optimization problems by using techniques like memorization or making local optimal choices .

Dead ends in a backtracking algorithm are handled by retracing steps to the previous decision point and exploring alternative solutions. If a chosen path leads to a situation where no further progress can be made without violating constraints, the algorithm backtracks to the last valid decision point where different options are unexplored. By altering earlier decisions and testing new paths, the method ensures all potential solutions are evaluated, allowing the algorithm to recover from dead-end paths and theoretically avoid missing any feasible solutions .

In Sudoku puzzles, backtracking places numbers in empty cells while ensuring each number fits within rows, columns, and boxes constraints. It attempts each possible number in sequence, validating constraints before proceeding. In the N-Queens problem, backtracking involves placing queens on a board such that no two queens attack each other, focusing only on row, column, and diagonal constraints. While both problems use the same underlying technique of making decisions, checking constraints, and backtracking as needed, Sudoku requires checking multiple domains simultaneously within each subgrid, adding complexity compared to the linear constraints in N-Queens .

The N-Queens problem is solved through backtracking by recursively placing queens one by one on the board. Starting with an empty chessboard, the algorithm places the first queen on any empty square. It then places each subsequent queen on squares not attacked by the previously placed queens. If a position leads to a situation where no safe placement is possible for a queen, it backtracks to the previous queen's position and tries a different placement. This systematic checking ensures all configurations are attempted until a solution is found. Dead ends are handled by backtracking and exploring alternate paths, ensuring exhaustive exploration .

The four-tuple (2, 4, 1, 3) represents a solution to the 4-queens problem where each number in the tuple corresponds to the column placement of a queen, with the index of the tuple representing the row. Here, queen 'q1' is placed in column 2 on the first row, 'q2' in column 4 on the second row, 'q3' in column 1 on the third row, and 'q4' in column 3 on the fourth row. This arrangement ensures no two queens attack each other, fulfilling the requirements of the N-Queens problem .

When applied to large-scale problems like the N-Queens problem with high values of N, backtracking becomes computationally expensive due to its exhaustive nature, which results in high time complexity as it explores all configurations. The space complexity also increases because of the many recursion calls required to explore potential paths. Furthermore, the absence of heuristics means the algorithm will not prioritize more promising paths, making it inefficient for large problem sizes where faster convergence to a solution is critical. Alternative techniques like constraint programming or heuristic-based search might be more effective for large N .

Backtracking ensures finding all solutions to problems like the N-Queens problem by exploring all possible configurations systematically. It does so by making a decision, checking for constraints, and continuing if the decision is feasible, otherwise backtracking to try different possibilities. The algorithm covers all paths until a solution is found or paths are exhausted. The main advantages of backtracking include its versatility, as no heuristics are required, making it applicable to a wide variety of problems. It is also systematic and exhaustive, which means it considers every possible solution, providing a thorough search .

The backtracking method is generally not efficient for solving optimization problems because it exhaustively explores all possible solutions without prioritizing pathways that lead to optimal solutions. Its focus is to find any feasible solution rather than the most optimal one. Hence, it can be time-consuming and inefficient for large optimization problems. Methods like dynamic programming, which uses memorization to avoid redundant calculations, or greedy algorithms, which make locally optimal choices to find global optima, are better suited for efficiently solving optimization problems .

In artificial intelligence, backtracking is used for problem-solving, planning, and decision-making tasks. It is fundamental in AI as it provides a method for systematically exploring possible solutions to complex problems. In graph algorithms, backtracking is applied in finding shortest paths, Hamiltonian cycles, and minimum spanning trees. These applications leverage backtracking's ability to exhaustively explore nodes and paths to find feasible solutions to graph-related problems .

Backtracking is highly suitable for constraint satisfaction problems (CSPs) as it systematically explores variable assignments one by one, checking constraints at each step. Its advantage lies in its systematic approach, ensuring all possible configurations are considered to find configurations that meet all constraints. Unlike heuristics-based methods, backtracking is exhaustive, guaranteeing that if a solution exists, it will be found. However, for large CSPs, backtracking might be inefficient compared to methods like constraint propagation or local search, which can significantly reduce the search space using smart heuristics .

You might also like