Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views3 pages

Backtracking

Backtracking is a problem-solving technique that explores all possible solutions by trying different possibilities and abandoning those that fail to meet constraints, useful in problems like the 8 Queens and Graph Coloring. The 8 Queens Problem involves placing 8 queens on a chessboard without them threatening each other, while the Graph Coloring Problem requires coloring graph vertices such that no two adjacent vertices share the same color. Both problems have exponential time complexity due to the need to explore multiple configurations and backtrack when encountering invalid placements.

Uploaded by

anandraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Backtracking

Backtracking is a problem-solving technique that explores all possible solutions by trying different possibilities and abandoning those that fail to meet constraints, useful in problems like the 8 Queens and Graph Coloring. The 8 Queens Problem involves placing 8 queens on a chessboard without them threatening each other, while the Graph Coloring Problem requires coloring graph vertices such that no two adjacent vertices share the same color. Both problems have exponential time complexity due to the need to explore multiple configurations and backtrack when encountering invalid placements.

Uploaded by

anandraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Backtracking

Backtracking is a problem-solving technique that systematically explores all possible solutions to


a problem by trying different possibilities and then abandoning (or "backtracking") those that fail
to meet the problem's constraints. It's particularly useful in constraint satisfaction problems like
the 8 Queens Problem and Graph Coloring Problem. Let's explore how backtracking works for
each of these problems.

1. 8 Queens Problem

The 8 Queens Problem is a classical problem in


computer science where you are asked to place 8
queens on a chessboard in such a way that no two
queens threaten each other. In chess, a queen can move
horizontally, vertically, or diagonally, so no two queens
can share the same row, column, or diagonal.

Backtracking Solution for the 8 Queens Problem:

 Goal: Place 8 queens on an 8x8 chessboard so that no two queens attack each other.
 Steps:
1. Start by placing a queen in the first row. There are 8 possible columns where the
queen can be placed.
2. Move to the next row and try placing a queen in one of the columns. Ensure that
the new queen does not share a column or diagonal with any previously placed
queens.
3. If you can place a queen in a row such that no two queens attack each other, move
to the next row and repeat the process.
4. If placing a queen violates the constraints (i.e., two queens threaten each other),
backtrack: remove the queen from the previous row and try the next possible
position in that row.
5. Continue this process until all 8 queens are placed on the board or you have
explored all possibilities.
6. Once 8 queens are successfully placed without conflicts, a solution is found.
7. If no solution can be found in a given configuration, backtrack to previous rows
and try other positions.

Algorithm:

 Initialize the board (an 8x8 grid).


 Use a recursive function that places a queen in each row one by one.
 For each row, try all columns, but check if the column, diagonal, or anti-diagonal is
already threatened by a previously placed queen.
 If all rows are successfully filled, return the solution.
 If you encounter an invalid configuration, backtrack and try different placements.

Example: For the first queen, we start with the first row, and for each row, we place a queen at a
column that doesn't violate the constraints (no same column, row, or diagonal). If we reach a
dead-end, we backtrack and change the position of the queen in the previous row.
2. Graph Coloring Problem

The Graph Coloring Problem involves coloring the


vertices of a graph such that no two adjacent vertices
have the same color. The goal is to assign the least
number of colors to the graph, satisfying this constraint.

Backtracking Solution for Graph Coloring:

 Goal: Assign colors to the vertices of a graph such that adjacent vertices don’t share the
same color, using the minimum number of colors.
 Steps:
1. Start with the first vertex and assign it the first color.
2. Move to the next vertex and attempt to assign a color that does not match any of
its adjacent vertices' colors.
3. If a vertex can be assigned a valid color, move to the next vertex.
4. If a vertex cannot be assigned a color (because all colors are taken by adjacent
vertices), backtrack to the previous vertex and try a different color for it.
5. Continue the process until all vertices are colored or backtrack if no solution is
possible.

Algorithm:

1. Start by coloring the first vertex with the first color.


2. For each subsequent vertex, attempt to color it with the smallest color number that
doesn’t conflict with its adjacent vertices.
3. If no valid color can be assigned to a vertex (because all valid colors are used by its
neighbors), backtrack to the previous vertex and try a different color.
4. Continue until all vertices are colored or until a solution is impossible.
5. If you run out of color options or backtrack to the first vertex, it means no valid coloring
exists for the given constraints with the current number of colors. You can then try with
more colors.

Example: For a simple graph with 4 vertices, the algorithm would start by coloring vertex 1.
Then it will move to vertex 2 and try to color it with a color that is different from vertex 1. This
continues, and if a conflict occurs, the algorithm backtracks to the previous vertex and tries
another color.

Common Steps in Backtracking:

1. Choose a Decision: Select a decision (place a queen, assign a color).


2. Explore the Decision: Explore all possible subsequent choices (place the queen in all
available columns, assign each color to a vertex).
3. Check Constraints: Ensure that the current choice satisfies all the constraints (no two
queens in the same row/column/diagonal, no adjacent vertices with the same color).
4. Backtrack if Necessary: If a conflict arises, backtrack to the last decision point and try
another choice.

Backtracking Process for Both Problems:


 In 8 Queens: You backtrack when you cannot place a queen in a row without violating
constraints. You undo the last queen placement and try another position.
 In Graph Coloring: If no valid color can be assigned to a vertex due to adjacent color
constraints, you backtrack and try a different coloring for the previous vertex.

Time Complexity:

 The time complexity for both problems is generally exponential, as you are essentially
trying multiple configurations of placements or colorings and backtracking when you
encounter invalid ones.
o 8 Queens Problem: Worst-case time complexity is O(N!)O(N!)O(N!) since you
have to place queens in each row, and each row has NNN possible positions.
o Graph Coloring Problem: The worst-case time complexity is also exponential,
dependent on the number of vertices and the number of colors.

Conclusion:

Backtracking is an efficient technique for solving constraint satisfaction problems like the 8
Queens Problem and Graph Coloring Problem because it allows you to explore multiple
configurations systematically while eliminating invalid configurations early on. This method
ensures that you don't waste time checking solutions that clearly violate the problem’s
constraints.

You might also like