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

N Queens Backtracking Algorithm Report

The document provides a comprehensive overview of the backtracking algorithm applied to the N-Queens problem, detailing its implementation, performance evaluation, and visualization using Python. It discusses the objectives, literature review, and the recursive nature of the backtracking approach, along with performance metrics for various values of N. The findings indicate that while backtracking is effective for small to moderate N, it becomes impractical for larger values due to exponential time complexity.

Uploaded by

majoriske24
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 views12 pages

N Queens Backtracking Algorithm Report

The document provides a comprehensive overview of the backtracking algorithm applied to the N-Queens problem, detailing its implementation, performance evaluation, and visualization using Python. It discusses the objectives, literature review, and the recursive nature of the backtracking approach, along with performance metrics for various values of N. The findings indicate that while backtracking is effective for small to moderate N, it becomes impractical for larger values due to exponential time complexity.

Uploaded by

majoriske24
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/ 12

DEDAN KIMATHI UNIVRSITY OF TECHNOLOGY

A COMPREHENSIVE OVERVIEW OF
BACKTRACKING ALGORITHM FOR N-QUEENS
PROBLEM

CONTRIBUTORS(GROUP 5)
Maina Anderson C025-01-0841/2022
Simon Kanyari C025-1328/2012
Kelvin Maina C025-01-2566/2022
Yego Joy C025-01-0848/2022
Telvin Marikio C025-01-0590/2020
Hillary Buluku C025-01-2181/2022
Benedict Musyoki C025-01-0834/2022
Contents
CHAPTER ONE..............................................................................................................................................3
1.0 Introduction.......................................................................................................................................3
1.1 N Queen Problem using Backtracking................................................................................................4
1.2 Objectives..........................................................................................................................................4
CHAPTER 2...................................................................................................................................................5
2.0 Literature Review...............................................................................................................................5
2.1 Backtracking Approach......................................................................................................................5
CHAPTER 3...................................................................................................................................................6
3.0 Implementation details......................................................................................................................6
3.1 Code in python (with comments).....................................................................................................6
CHAPTER 4...................................................................................................................................................8
4.0 Evaluation of Performance.................................................................................................................8
4.1 Performance Metrics for Different Values of N..................................................................................8
4. 1 Conclusion and Future Work...........................................................................................................10
4.2 Source Code Repository...................................................................................................................10
4.3 Visualization.....................................................................................................................................10
4.3.0 Tkinter code for visualization....................................................................................................10
4.3.1 Tkinter output...........................................................................................................................12
CHAPTER ONE

1.0 Introduction

1.0.1 What is N-Queen problem?

The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens
attack each other.
For example, the following is a solution for the 4 Queen problem.

For example, the following is a solution for the 4 Queen problem.

The expected output is a matrix with '1's for blocks containing queens and '0' for empty spaces.
For example, consider the output matrix for the above 4-Queen solution.
1.0.2 N Queen Problem using Backtracking

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.

1.2 Objectives

 To create a backtracking-based solution for the N-Queens problem.


 To evaluate the solution's performance for various N numbers.
 To view the board and put the answer into practice using Python.
 To record the performance indicators and findings.
CHAPTER 2

2.0 Literature Review

Because of the N-Queens problem's computational complexity and wide range of applications,
computer science has researched it in great detail. This problem has been solved using a variety
of techniques, including constraint programming, genetic algorithms, and backtracking.
Backtracking is among the easiest and most natural techniques among them.

2.1 Backtracking Approach

A general algorithm for solving all (or some) computational problems, particularly constraint
satisfaction problems, is backtracking. The algorithm creates candidates to the solutions step-by-
step, discarding each one as soon as it finds that it is not likely to lead to a workable solution.
CHAPTER 3

3.0 Implementation details

The following actions are involved in the implementation:


Initialization: Make a N×N board with a starting point of 0.
Function for Backtracking: Create a recursive function that calls itself to place queens in
succeeding columns after attempting to place a queen in a column.
Make sure the location is safe (that is, that no other queen can assault it) before putting a queen
there.
Base Case: Return true if every queen is successfully put.
Backtracking: Take out the queen (backtrack) and try the next place if putting one does not result
in a solution.

3.1 Code in python (with comments)


Get the official code(S) from this github repo
https://github.com/ andersonmaina/DSA-Bcktracking-for-N-Queens.git

Output,
where N=8
00000010
00001000
00000001
01000000
00010000
00000100
00100000
CHAPTER 4

4.0 Evaluation of Performance

The number of recursive calls performed and the time it takes to discover a solution can be used to
examine the backtracking algorithm's performance. Since the algorithm attempts every conceivable
configuration, the worst-case time complexity of the backtracking solution for the N-Queens issue is
O(N!).

4.1 Performance Metrics for Different Values of N

N=4
Solution:
0010
1000
0001
0100
Time: Very quick (milliseconds)
Recursive Calls: Few

N=8
Solution: (one of many solutions)
00000010
00001000
00000001
01000000
00010000
00000100
00100000
Time: Moderate (seconds)
Recursive Calls: More
N = 12
Solution: (One of many possible solutions)
000000100000
000000000100
000100000000
000000000010
000010000000
000000010000
000001000000
Time: Noticeable delay (seconds to minutes)
Recursive Calls: Many

N = 15
Solution: (One of many possible solutions)
000000000100000
000010000000000
000000000000010
000001000000000
000000001000000
000000100000000
000000000001000
Time: Longer delay (minutes to tens of minutes)
Recursive Calls: Substantial
4. 1 Conclusion and Future Work

The backtracking approach is effective for solving the N-Queens problem for small to moderate
values of N. However, as N increases, the time complexity and the number of recursive calls
increase exponentially, making it impractical for very large N. Future work could explore more
advanced algorithms such as genetic algorithms or constraint programming to handle larger
values of N more efficiently.

4.2 Source Code Repository

The source code can be found https://github.com/ andersonmaina/DSA-Bcktracking-for-N-


Queens.git with clear instructions for running the code and detailed comments explaining key
sections.

4.3 Visualization

4.3.0 Tkinter code for visualization

import tkinter as tk

class NQueensVisualizer:
def __init__(self, N):
self.N = N
self.window = tk.Tk()
self.window.title(f"{N}-Queens Problem")
self.canvas = tk.Canvas(self.window, width=600, height=600)
self.canvas.pack()
self.board = [[0] * N for _ in range(N)]
self.cell_size = 600 // N
self.solve_n_queens()

def draw_board(self):
self.canvas.delete("all")
for i in range(self.N):
for j in range(self.N):
x0 = j * self.cell_size
y0 = i * self.cell_size
x1 = x0 + self.cell_size
y1 = y0 + self.cell_size
color = "white" if (i + j) % 2 == 0 else "gray"
self.canvas.create_rectangle(x0, y0, x1, y1, fill=color)
if self.board[i][j] == 1:
self.canvas.create_oval(x0 + 5, y0 + 5, x1 - 5, y1 -
5, fill="black")

def is_safe(self, row, col):


for i in range(col):
if self.board[row][i] == 1:
return False
for i, j in zip(range(row, -1, -1), range(col, -1, -1)):
if self.board[i][j] == 1:
return False
for i, j in zip(range(row, self.N, 1), range(col, -1, -1)):
if self.board[i][j] == 1:
return False
return True

def solve_n_queens_util(self, col):


if col >= self.N:
return True
for i in range(self.N):
if self.is_safe(i, col):
self.board[i][col] = 1
self.draw_board()
self.window.update_idletasks()
if self.solve_n_queens_util(col + 1):
return True
self.board[i][col] = 0
self.draw_board()
self.window.update_idletasks()
return False

def solve_n_queens(self):
if not self.solve_n_queens_util(0):
print("Solution does not exist")
else:
print("Solution found")

if __name__ == "__main__":
N = 8 # Change this value for different N
visualizer = NQueensVisualizer(N)
visualizer.window.mainloop()
4.3.1 Tkinter output

You might also like