Skip to content

Commit

Permalink
The Nquuens Algorithm with Explaination and Lesser Complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Sairam-K26 committed Mar 17, 2023
1 parent 798d57c commit 4054557
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 43 deletions.
45 changes: 2 additions & 43 deletions backtracking/n-queens/Python/8queen.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'''from __future__ import print_function
from __future__ import print_function
board = []
size = 8

Expand All @@ -24,47 +24,6 @@ def placequeen(row):
board.remove((row,col))

if __name__ == "__main__":
placequeen(1)'''
placequeen(1)

#The complexity for above is o(N!)

def solve_n_queens(n):
if n < 1:
return 0

res = []
dfs(n, 0, 0, 0, 0, [], res)
return len(res)

def dfs(n, row, cols, left_diagonal, right_diagonal, path, res):
if row == n:
res.append(path)
return

# Generate a bit mask for valid positions in the row
valid_positions = ((1 << n) - 1) & (~(cols | left_diagonal | right_diagonal))

while valid_positions:
# Get the position of the least significant bit
pos = valid_positions & (-valid_positions)

# Remove the least significant bit from the mask
valid_positions &= valid_positions - 1

# Calculate the column index of the queen
col = bin(pos - 1).count('1')

# Generate the next row state
new_cols = cols | pos
new_left_diagonal = (left_diagonal | pos) << 1
new_right_diagonal = (right_diagonal | pos) >> 1

# Recurse on the next row
dfs(n, row+1, new_cols, new_left_diagonal, new_right_diagonal, path+[col], res)

if __name__ == "__main__":
n = int(input("Enter the size of the board: "))
count = solve_n_queens(n)
print("Number of valid solutions for {}-queens problem: {}".format(n, count))

# The complexity for my code is : time complexity of O(2^n) and a space complexity of O(n)
40 changes: 40 additions & 0 deletions backtracking/n-queens/Python/Sairam-K26 NQueens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def solve_n_queens(n):
if n < 1:
return 0

res = []
dfs(n, 0, 0, 0, 0, [], res)
return len(res)

def dfs(n, row, cols, left_diagonal, right_diagonal, path, res):
if row == n:
res.append(path)
return

# Generate a bit mask for valid positions in the row
valid_positions = ((1 << n) - 1) & (~(cols | left_diagonal | right_diagonal))

while valid_positions:
# Get the position of the least significant bit
pos = valid_positions & (-valid_positions)

# Remove the least significant bit from the mask
valid_positions &= valid_positions - 1

# Calculate the column index of the queen
col = bin(pos - 1).count('1')

# Generate the next row state
new_cols = cols | pos
new_left_diagonal = (left_diagonal | pos) << 1
new_right_diagonal = (right_diagonal | pos) >> 1

# Recurse on the next row
dfs(n, row+1, new_cols, new_left_diagonal, new_right_diagonal, path+[col], res)

if __name__ == "__main__":
n = int(input("Enter the size of the board: "))
count = solve_n_queens(n)
print("Number of valid solutions for {}-queens problem: {}".format(n, count))

# The complexity for my code is : time complexity of O(2^n) and a space complexity of O(n)
21 changes: 21 additions & 0 deletions backtracking/n-queens/Python/Sairam-K26.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The N-Queens problem is a classic puzzle where the goal is to place N chess queens on an NxN chessboard so that no two queens attack each other. In other words, no two queens can share the same row, column, or diagonal.

Here's an example of how we can visualize the N-Queens problem in a game format with a 4x4 board and 4 queens:
a b c d
1 Q . . .
2 . . Q .
3 . Q . .
4 . . . Q

Each square on the board represents a position where a queen can be placed. In this example, we have placed four queens on the board so that no two queens share the same row, column, or diagonal.

Now, let's say you have a specific input, such as a 5x5 board and 5 queens. Here's how we can visualize the game with this input:
a b c d e
1 . . Q . .
2 Q . . . .
3 . . . . Q
4 . Q . . .
5 . . . Q .



0 comments on commit 4054557

Please # to comment.