DEV Community

ANISHA SWAIN | The UI Girl
ANISHA SWAIN | The UI Girl

Posted on • Originally published at theuigirl.hashnode.dev on

Concept Visualisation for N-Queen Problem

VISUALIZATION-1

Try to place N numbers of queens on an N x N chessboard so that none of the queens attacks each other.

Lets play a simple puzzle game. Your task is to place two more Queens in of the given chessboard such that, there will no Queen attacking the others.

Solved it? Pretty simple, wasnt it? The above problem already had 3 queens placed. Will you be able to place all the Queens from a start?

Try to place N numbers of queens on an N x N chessboard so that none of the queens attacks each other vertically, horizontally or diagonally. With every solution, you will be allowed to play the next level with the incremented number of N. Lets start with a 4x4 board. Give it a try.

Are you stuck at a point and it is killing your time? But a computer can solve such puzzles faster. So how does it do that? Lets try to figure it out.

Lets dive deep into it

The problem stated above is otherwise known as the N Queen Problem, which is a useful test problem for search algorithms. That is why, we might consider trying different search algorithms like BFS, DFS, GA(Genetic Algorithms), Backtracking etc for searching its solution. So lets dive deep into each of these to figure out the best problem-solving approach for this particular problem.

Talking about BFS first, in this approach queens will be placed in every position in each column to check whether it is a solution. Lets see how it works in an intuitive manner.

As you can see most of the arrangements are not even be close to being valid. Dont you think this will lead to unnecessary wastage of time and will increase the time complexity exponentially? Well, try solving it with another intuitive visualization.

So thats it for this article. I hope you have checked out the visualization and if you liked it then do not forget to tell us your thoughts in the comment section below.

If you want to connect with me, here I am at Twitter or Instagram

Announcement: we will be ding d3.js tutorials soon so stay tuned.

Follow our community LinkedIn group, Facebook Page and Twitter for more such articles and posts and meet like-minded people to collaborate.

Top comments (0)