0% found this document useful (0 votes)
2 views4 pages

Practical Assignment LP-II - 4

The document outlines an assignment for third-year computer engineering students focused on solving the N-Queens problem using backtracking and branch and bound techniques. It details prerequisites, objectives, outcomes, relevant theories, algorithms, and a comparison between backtracking and branch and bound methods. The assignment aims to enhance understanding of constraint satisfaction problems and algorithmic approaches to optimization.

Uploaded by

kanadeshubhu04
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)
2 views4 pages

Practical Assignment LP-II - 4

The document outlines an assignment for third-year computer engineering students focused on solving the N-Queens problem using backtracking and branch and bound techniques. It details prerequisites, objectives, outcomes, relevant theories, algorithms, and a comparison between backtracking and branch and bound methods. The assignment aims to enhance understanding of constraint satisfaction problems and algorithmic approaches to optimization.

Uploaded by

kanadeshubhu04
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/ 4

Laboratory Practice II Third Year Computer Engineering

Answers Coding Viva Timely Total Dated Sign of


Efficiency Completion Subject Teacher
5 5 5 5 20

Date of Performance:....................................Date of Completion:..............................................

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

N-Queens problem Algorithm


1) Start in the leftmost column
2) If all queens are placed return true
3) Try all rows in the current column.
Do the following for every tried
row.
a) If the queen can be placed safely in this row then mark this [row, column] as part of
the solution and recursively check if placing the queen here leads to a solution.
b) If placing the queen in [row, column] leads to a solution then return true.
c) If placing queen doesn't lead to a solution then unmark this [row, column]
(Backtrack) and go to step (a) to try other rows.
4) If all rows have been tried and nothing worked, return false to trigger backtracking.

Branch and Bound


Branch and bound is an algorithm design paradigm which is generally used for solving
combinatorial optimization problems. These problems are typically exponential in terms of
time complexity and may require exploring all possible permutations in the worst case. The
Branch and Bound Algorithm technique solves these problems relatively quickly.

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

Parameter Backtracking Branch and Bound

Approach Backtracking is used to find all Branch-and-Bound is used to solve


possible solutions available to a optimization problems. When it
problem. When it realizes that it has realizes that it already has a better
made a bad choice, it undoes the last optimal solution that the
choice by backing it up. It searches pre-solution leads to, it abandons
the state space tree until it has found that pre-solution. It completely
a solution for the problem. searches the state space tree to
get an optimal solution.

Traversal Backtracking traverses the Branch-and-Bound traverse


state space tree by DFS(Depth the tree in any manner, DFS or
First Search) manner. BFS.

Function Backtracking involves a feasibility Branch-and-Bound involves


function. a bounding function.

Problems Backtracking is used for solving Branch-and-Bound is used for


Decision Problems. solving the optimization Problem.

Searching In backtracking, the state space tree In Branch-and-Bound as the


is searched until the solution is optimum solution may be present
obtained. anywhere in the state space tree,
so the tree needs to be searched
completely.

Efficiency Backtracking is more efficient. Branch-and-Bound is less efficient.

Applications Useful in solving N-Queen Problem, Useful in solving Knapsack


Sum of subset. Problem, Traveling Salesman
Problem.

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.

You might also like