The document describes implementing a backtracking algorithm to solve the N-Queen problem by placing queens on an NxN chessboard without conflicts. It details the procedure, code implementation, test output, and analyzes the effectiveness and limitations of the backtracking approach.
The document describes implementing a backtracking algorithm to solve the N-Queen problem by placing queens on an NxN chessboard without conflicts. It details the procedure, code implementation, test output, and analyzes the effectiveness and limitations of the backtracking approach.
Department of Computer Science and Engineering (CSE)
Faculty of Sciences and Engineering Semester: (Spring, Year:2024), B.Sc. in CSE (Day)
Lab Report NO 03
Course Title: Artificial Intelligence Lab
Course Code: CSE 316 Section: 212-D5
Lab Experiment Name: Solve N-Queen Problem Using Backtracking Algorithm.
Student Details
Name ID
1. Tahsin Ahmmed 212002063
Lab Experiment Date : 24 - 03 - 2024
Submission Date : 02 - 04 - 2024 Course Teacher’s Name : Sheikh Fazle Rabbi
Lab Report Status
Marks: ………………………………… Signature:..................... Comments:.............................................. Date:.............................. 1. TITLE OF THE LAB REPORT EXPERIMENT Solve N-Queen Problem Using Backtracking Algorithm. 2. OBJECTIVES/AIM : Þ To implement a backtracking algorithm to solve the N-Queen problem. Þ To demonstrate the application of backtracking in finding valid queen placements on an NxN chessboard. Þ To analyze the performance of the algorithm in terms of runtime and memory usage for varying board sizes. Þ To discuss the effectiveness and limitations of the backtracking approach for solving combinatorial problems like the N-Queen problem. Þ To provide insights into potential optimizations or enhancements to improve the algorithm's efficiency. 3. PROCEDURE / ANALYSIS / DESIGN Þ Input: Obtain the number of queens to be placed, n, from the user. Þ Initialize Chessboard: Create an empty n x n chessboard represented as a 2D list (board). Þ Define Helper Functions: • check_column(board, row, column): Check if placing a queen at a given row and column conflicts with existing queens in the same column. • check_diagonal(board, row, column): Check if placing a queen at a given row and column conflicts with existing queens diagonally. Þ Backtracking Algorithm (main_func_n_qween): • If all queens are placed (row == n), return True. • Iterate through each column in the current row. • Check if the column and diagonals are safe for placing the queen. • If safe, mark the position as a queen, recursively call the function for the next row, and backtrack if no solution is found. Þ Print Final Result: Display the final placement of queens on the chessboard after backtracking. Þ Additional Information: Provide information about the Python zip() function used in the code (optional). 4. IMPLEMENTATION 1. n = int(input("Number of queens to place: ")) 2. 3. board = [[0] * n for _ in range(n)] 4. 5. #print(board) 6. #for row in board: 7. #print(row) 8. 9. print(' ', n, ' X ', n, ' Chess Board') 10. 11. 12. def check_column(board, row, column): 13. # print(row,column) 14. 15. for i in range(row, -1, -1): 16. 17. if board[i][column] == 1: 18. # print('next',row,column) 19. '''if board[i][column] == 1, if there's a queen (1) present in the curren 20. column (column) at the row i. If there is a queen in the 21. current column, then it means that placing another queen in the same 22. column at row would result in a conflict.''' 23. 24. return False 25. # print(board) 26. 27. return True 28. def check_diagonal(board, row, column): 29. for i, j in zip(range(row, -1, -1), range(column, -1, -1)): 30. # oop iterates over the diagonally upward-left positions starting from (row, c 31. # print(list(zip(range(row,-1,-1)))) 32. 33. if board[i][j] == 1: 34. return False 35. 36. for i, j in zip(range(row, -1, -1), range(column, n, 1)): 37. 38. if board[i][j] == 1: 39. return False 40. # print(board) 41. 42. return True 43. 44. # back-track 45. def main_func_n_qween(board, row): 46. if row == n: 47. return True 48. for i in range(n): 49. if (check_column(board, row, i) == True and check_diagonal(board, row, i) == 50. board[row][i] = 1 # place a qween there 1 = qween 51. if main_func_n_qween(board, row + 1): 52. return True 53. board[row][i] = 0 54. 55. return False 56. 57. 58. main_func_n_qween(board, 0) 59. # print(board) 60. 61. #final result------------------------------>>>>> 62. 63. print() 64. print('After right placement of qween, we get :') 65. for row in board: 66. print(row) 67. 68. #info about Python zip() Function 69. a = ("John", "Charles", "Mike") 70. b = ("Jenny", "Christy", "Monica") 71. 72. x = zip(a, b) 73. tuple(x)
5. TEST RESULT / OUTPUT
6. ANALYSIS AND DISCUSSION:
The backtracking algorithm offers a powerful strategy for efficiently solving the N-Queen problem, systematically exploring potential queen configurations while backtracking upon encountering conflicts. Its effectiveness lies in its ability to guarantee a valid solution by exhaustively searching through the solution space and pruning branches that violate problem constraints. However, the algorithm's exponential time complexity poses challenges for larger board sizes, limiting its scalability. Despite its limitations, the backtracking approach remains a valuable tool for solving combinatorial problems, providing a solid foundation for further exploration and optimization. By leveraging alternative algorithms or implementing optimizations, such as symmetry breaking or parallelization, researchers can continue to enhance the efficiency and applicability of solutions to the N-Queen problem and similar combinatorial optimization challenges.