ai-pract4
ai-pract4
AIM: To use Branch & Bound and Backtracking to solve CSP & n-queens or graph coloring problem.
Theory:
Constraint Satisfaction Problems:
The objective of every problem-solving technique is one, i.e., to find a solution to reach the goal. Although,
in adversarial search and local search, there were no constraints on the agents while solving the problems and
reaching to its solutions. By the name, it is understood that constraint satisfaction means solving a problem under
certain constraints or rules.
Constraint satisfaction is a technique where a problem is solved when its values satisfy certain constraints
or rules of the problem. Such type of technique leads to a deeper understanding of the problem structure as well as
its complexity.
Constraint satisfaction depends on three components, namely:
X: It is a set of variables.
D: It is a set of domains where the variables reside. There is a specific domain for each variable. C: It is a set of
constraints which are followed by the set of variables.
In constraint satisfaction, domains are the spaces where the variables reside, following the problem specific
constraints. These are the three main elements of a constraint satisfaction technique. The constraint value consists of
a pair of {scope, rel}. The scope is a tuple of variables which participate in the constraint and rel is a relation which
includes a list of values which the variables can take to satisfy the constraints of the problem.
CSP Problems:
Constraint satisfaction includes those problems which contains some constraints while solving the problem. CSP includes the
following problems:
Graph Coloring: The problem where the constraint is that no adjacent sides can have the same color.
n-queen problem: In n-queen problem, the constraint is that no queen should be placed either diagonally, in the same row or
column. The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each
other.
a) If the queen can be placed safely in this row then mark this [row,
Number 1 is automatic because of the way we store the solution. For number 2, 3 and 4, we can perform
updates in O(1) time. The idea is to keep three Boolean arrays that tell us which rows and which diagonals are
occupied.
Let’s do some pre-processing first. Let’s create two N x N matrix one for / diagonal and other one for \
diagonal. Let’s call them slashCode and backslashCode respectively. The trick is to fill them in such a way that
two queens sharing a same /-diagonal will have the same value in
matrix slashCode, and if they share same \-diagonal, they will have the same value in
backslashCode matrix.
For an N x N matrix, fill slashCode and backslashCode matrix using below formula – slashCode[row]
[col] = row + col
backslashCode[row][col] = row – col + (N-1)
The ‘N – 1’ in the backslash code is there to ensure that the codes are never negative because we
will be using the codes as indices in an array.
Now before we place queen i on row j, we first check whether row j is used (use an array to store
row info). Then we check whether slash code ( j + i ) or backslash code ( j – i + 7 ) are used (keep two
arrays that will tell us which diagonals are occupied). If yes, then we have to try a different location for
queen i. If not, then we mark the row and the two diagonals as used and recurse on queen i + 1. After the
recursive call returns and before we try another position for queen i, we need to reset the row, slash code
and backslash code as unused again
Conclusion: Thus we have studied how to solve n-queen (Constraint Satisfaction Problem) using
Backtracking and Branch & Bound