0% found this document useful (0 votes)
3 views

Backtracking Programming

The document discusses the concept of backtracking as a problem-solving technique, highlighting its advantages and disadvantages, as well as its applications in various computational problems like the N-Queens problem. It details the implementation of the N-Queens problem using backtracking, providing a step-by-step execution and showcasing two distinct solutions. The document emphasizes the time and space complexity associated with backtracking algorithms.

Uploaded by

Yash Shekhawat
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)
3 views

Backtracking Programming

The document discusses the concept of backtracking as a problem-solving technique, highlighting its advantages and disadvantages, as well as its applications in various computational problems like the N-Queens problem. It details the implementation of the N-Queens problem using backtracking, providing a step-by-step execution and showcasing two distinct solutions. The document emphasizes the time and space complexity associated with backtracking algorithms.

Uploaded by

Yash Shekhawat
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
You are on page 1/ 6

Smt.

Chandibai Himathmal Mansukhani College


Computer Science Departments
Design and Analysis of Algorithms
SEM-II (2024-2025)
Backtracking Programming: Concept, Advantages & Disadvantages,
Applications, Implementation Using Problems like N-Queen Problem

Concept of Backtracking

Backtracking is a problem-solving technique used to find all or some solutions to computational


problems that incrementally build candidates for the solutions, and discard those candidates
("backtrack") as soon as they determine that the candidate cannot lead to a valid solution.

In simpler terms, backtracking is a way of systematically searching for a solution by trying out
different possibilities and backtracking (going back) when a solution is not possible. It is often
referred to as a depth-first search technique in solving problems.

The key principle of backtracking is:

• Try to build the solution step-by-step.


• If a step leads to a dead-end or an invalid solution, backtrack and try a different path.

Backtracking is commonly used to solve problems involving:

• Combinatorics
• Permutations
• Combinations
• Puzzles and games (e.g., Sudoku, N-Queens problem)

Advantages of Backtracking

1. Simplicity: Backtracking is often easy to conceptualize and implement, as it closely


follows a depth-first search approach.
2. Exhaustive Search: It explores all possible solutions to a problem and guarantees that
the solution, if it exists, will be found.
3. Flexibility: It can be adapted to various types of problems (combinatorial, optimization,
constraint satisfaction).
4. Optimality: In some cases, backtracking can be used to find an optimal solution (e.g., in
puzzles or pathfinding problems).

Disadvantages of Backtracking

1. Inefficiency: In problems with large solution spaces, backtracking can be highly


inefficient, as it explores all possibilities, even if many are not relevant.

Notes Prepared By Ms. Razia Khan


Smt. Chandibai Himathmal Mansukhani College
Computer Science Departments
Design and Analysis of Algorithms
SEM-II (2024-2025)
2. Exponential Time Complexity: Backtracking can often lead to exponential time
complexity (O(2^n), O(n!), etc.), making it impractical for large inputs.
3. Memory Overhead: For some problems, backtracking requires storing a large number of
states or paths, which may lead to high memory usage.

Applications of Backtracking

Backtracking is a powerful technique used to solve many types of problems. Some common
applications include:

1. N-Queens Problem: The classic problem of placing N queens on an N×N chessboard


such that no two queens threaten each other.
2. Sudoku Solver: Filling a partially completed Sudoku grid with numbers that follow the
rules of the game.
3. Knapsack Problem: Finding the optimal subset of items to put in a knapsack to
maximize profit while respecting weight limits.
4. Permutations and Combinations: Generating all possible permutations or combinations
of a given set.
5. Graph Coloring: Coloring vertices of a graph such that no two adjacent vertices have the
same color.
6. Maze Solving: Finding a path through a maze by exploring all possible routes.

Implementation: N-Queens Problem Using Backtracking

Problem Statement:

The N-Queens problem is a classic example of backtracking. It requires placing N queens on an


N×N chessboard such that no two queens attack each other. A queen can attack another queen if
they share the same row, column, or diagonal.

Task: Find all solutions to the N-Queens problem for a given N.

Solution Strategy Using Backtracking:

1. Initialization: Create an empty N×N chessboard.

Notes Prepared By Ms. Razia Khan


Smt. Chandibai Himathmal Mansukhani College
Computer Science Departments
Design and Analysis of Algorithms
SEM-II (2024-2025)
2. Recursion: Start placing queens one by one in each row. For each row:
o Try placing a queen in each column.
o Check if placing the queen results in any conflicts (i.e., check if it's safe to place it
in that position).
o If no conflicts occur, move to the next row and repeat the process.
o If a conflict arises, backtrack by removing the queen and try placing it in a
different column.
3. Base Case: When all queens are placed successfully, print the solution.

The safe check ensures that the new queen does not share the same column or diagonal with any
previously placed queens.

Time Complexity and Space Complexity:

• Time Complexity: The worst-case time complexity of backtracking for the N-Queens
problem is O(N!) because for each row, we attempt placing queens in all columns, and in
the worst case, we might try every possible arrangement of queens.
• Space Complexity: The space complexity is O(N) for the recursion stack and the space
used by the board.

Problem:

We need to place 4 queens on a 4x4 chessboard such that no two queens can attack each other.
The solution involves placing queens such that they do not share the same row, column, or
diagonal.

Step-by-Step Execution of the Backtracking Algorithm:

1. Start with an empty 4x4 board:

. . . .
. . . .
. . . .
. . . .

2. Place the first queen in the first row:


o Try placing the queen in the first column (0,0).
o The board becomes:

Notes Prepared By Ms. Razia Khan


Smt. Chandibai Himathmal Mansukhani College
Computer Science Departments
Design and Analysis of Algorithms
SEM-II (2024-2025)
Q . . .
. . . .
. . . .
. . . .

3. Move to the second row:


o Try placing the queen in the first column (1,0). This is not safe because it shares
the same column with the queen in row 0.
o Try placing the queen in the second column (1,1). This is not safe because it
shares the diagonal with the queen in (0,0).
o Try placing the queen in the third column (1,2). This is safe.
o The board becomes:

Q . . .
. . Q .
. . . .
. . . .

4. Move to the third row:


o Try placing the queen in the first column (2,0). This is not safe because it shares
the diagonal with the queen in (0,0).
o Try placing the queen in the second column (2,1). This is not safe because it
shares the column with the queen in (1,1).
o Try placing the queen in the third column (2,2). This is not safe because it shares
the diagonal with the queen in (1,2).
o Try placing the queen in the fourth column (2,3). This is safe.
o The board becomes:

Q . . .
. . Q .
. . . Q
. . . .

5. Move to the fourth row:


o Try placing the queen in the first column (3,0). This is not safe because it shares
the column with the queen in (0,0).
o Try placing the queen in the second column (3,1). This is safe.
o The board becomes:

Q . . .
. . Q .
. . . Q
. Q . .

Notes Prepared By Ms. Razia Khan


Smt. Chandibai Himathmal Mansukhani College
Computer Science Departments
Design and Analysis of Algorithms
SEM-II (2024-2025)
6. Solution Found: All four queens are placed safely. The solution is:

Q . . .
. . Q .
. . . Q
. Q . .

7. Backtrack: Now that we've found a solution, backtrack to explore other possible
arrangements.
8. Exploring Other Columns: We explore different columns for the queens in each row,
repeating the process until all possible solutions are found.

Second Solution:

After backtracking and exploring other possibilities, we find another valid arrangement:

. Q . .
Q . . .
. . . Q
. . Q .

Conclusion:

For N = 4, there are 2 distinct solutions to the N-Queens problem. These solutions are:

1. Solution 1:

Q . . .
. . Q .
. . . Q
. Q . .

2. Solution 2:

. Q . .
Q . . .
. . . Q
. . Q .

Both solutions ensure that no two queens threaten each other in terms of rows, columns, or
diagonals.

Notes Prepared By Ms. Razia Khan


Smt. Chandibai Himathmal Mansukhani College
Computer Science Departments
Design and Analysis of Algorithms
SEM-II (2024-2025)

Notes Prepared By Ms. Razia Khan

You might also like