Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N Queen Problem README.md added #150

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions code/code/backtracking/src/n_queen/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
# cosmos
Your personal library of every algorithm and data structure code that you will ever encounter
# The N-Queens Problem

The N-Queens problem is one of the most common examples to describe the backtracking algorithmic approach. It is an extension of the traditional 8 Queens Problem and is stated as follows:

_“Given an NxN chessboard, place N queens on it such that none of them are in a position to attack another (i.e. no two queens are in the same row, column, or diagonal).”_

This problem can be solved using a naïve approach, where we first check if all configurations of arrangement have been exhausted. If not, we generate the next possible configuration and check if it is a valid arrangement. If a valid arrangement is found, it is returned and the program exits. This is a poor solution because as the size of n increases, the number of possible permutations increases by a huge order. The growth rate of the output set or the complexity in simple terms is huge and entirely unpredictable, even for relatively smaller values of n. We must look for a more efficient solution.

The backtracking algorithm to solve the N-Queens problem is as follows:
1. Start with the left-most column on the chessboard
2. If all queens have been placed, return true
3. Now try other rows in the current column. For each row:
1. If (queen can be placed safely) then
1. Mark row
2. If (placing queen in [row,col] leads to solution) then
1. Return true
3. Else
1. Unmark this row-col pair
1. Backtrack to step i. to try other rows
4. If all rows have been tried and nothing works, return false