Practical Assignment LP-II - 4
Practical Assignment LP-II - 4
Assignment No: 4
1. Title of Assignment:
Implement a solution for a Constraint Satisfaction Problem using Branch and Bound /
Backtracking for n-queens problem
2. Prerequisite:
Basic knowledge of CSP, Backtracking
3. Objective:
In this experiment, we will be able to do the following:
● Study how to place N queens on board with non attacking mode using backtracking.
● What is CSP Problem
4. Outcome: Successfully able to place N queens on board with non attacking mode using
backtracking.
5. Software and Hardware Requirement:
Open Source C++ Programming tool like G++/GCC, python,java and Ubuntu.
6. Relevant Theory / Literature Survey:
Constraint Satisfaction Problem
CSP means solving a problem under certain constraints or rules
CSP depends on three components
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 set of variables
1
Laboratory Practice II Third Year Computer Engineering
Backtracking Algorithm
Backtracking is an algorithmic-technique for solving problems recursively by trying to build a
solution incrementally, one piece at a time, removing those solutions that fail to satisfy the
constraints of the problem at any point of time (by time, here, is referred to the time
elapsed till reaching any level of the search tree).
N-Queens problem
The N-Queens problem is a puzzle of placing exactly N queens on an NxN chessboard, such
that no two queens can attack each other in that configuration. Thus, no two queens can lie
in the same row,column or diagonal.
The idea is to place queens one by one in different columns, starting from the leftmost
column. When we place a queen in a column, we check for clashes with already placed
queens. In the current column, if we find a row for which there is no clash, we mark this row
and column as part of the solution. If we do not find such a row due to clashes then we
backtrack and return false.
2
Laboratory Practice II Third Year Computer Engineering
The branch and bound solution is somehow different, it generates a partial solution until it
figures that there's no point going deeper as we would ultimately lead to a dead end.
In the backtracking approach, we maintain an 8x8 binary matrix for keeping track of safe
cells (by eliminating the unsafe cells, those that are likely to be attacked) and update it each
time we place a new queen. However, it required O(n^2) time to check the safe cell and
update the queen.
Applying the branch and bound approach : The branch and bound approach suggests that
we create a partial solution and use it to ascertain whether we need to continue in a
particular direction or not.
3
Laboratory Practice II Third Year Computer Engineering
7. Questions:
Q 1: Which are the constraints required to solve the N Queen Problem?
Q 2: Compare Backtracking and branch and bound methods.
Q 3: What do you mean by Constraint satisfaction problem?
8. Conclusion:
In This way we have studied how to solve the CSP problem and how to place N queens on
board with non attacking mode using backtracking.