A Python-based solver for the classic N-Queens problem — implemented using both Breadth-First Search (BFS) and Depth-First Search (DFS).
This project compares the performance of the two algorithms and prints a sample solution on the board.
🎯 Developed as part of an AI fundamentals course by Ziyad Azzaz.
The N-Queens problem asks:
Can you place N queens on an N×N chessboard so that no two queens attack each other?
This project:
- ✅ Explores both BFS and DFS approaches
- ✅ Measures time taken by each algorithm
- ✅ Displays one possible solution on the board
- ✅ Shows how constraint satisfaction works in AI search
- 🔎 BFS Search: Iterative search using queues
- 🧭 DFS with Backtracking: Recursive and memory-efficient
- ⏱️ Performance Timer: Measures execution time of each approach
- 🎨 Board Display: Text-based board output with queen positions
- 💻 CLI Interaction: User inputs
Ndirectly from terminal
| File Name | Description |
|---|---|
n_queens_solver.py |
Main Python script |
README.md |
Project documentation (this file) |
-
Make sure Python is installed (>= 3.6)
-
Clone this repo:
git clone https://github.com/ZiyadAzzaz/n-queens-solver.git
cd n-queens-solver- Run the program:
python3 n_queens_solver.pyWelcome to the N-Queens Solver!
═══════════════════════════════
Enter the number of queens (N): 4
Solving the 4-Queens problem...
──────────────────────────────
Search Results:
• BFS found 2 solutions in 1.85 ms
• DFS found 2 solutions in 0.42 ms
Sample Solution:
┌───┬───┬───┬───┐
│ │ Q │ │ │
├───┼───┼───┼───┤
│ │ │ │ Q │
├───┼───┼───┼───┤
│ Q │ │ │ │
├───┼───┼───┼───┤
│ │ │ Q │ │
└───┴───┴───┴───┘
Performance Comparison:
══════════════════════
│ Algorithm │ Time │
══════════════════════
│ BFS │ 1.85ms│
│ DFS │ 0.42ms│
══════════════════════
Thanks for using the N-Queens Solver!
────────────────────────────────────
Made with ❤️ by Ziyad Azzaz 🧠
(AI Course Project)
- Language: Python
- Modules:
collections,time
This project was built to demonstrate:
- Basic constraint satisfaction
- AI search strategies (DFS vs BFS)
- Recursive and iterative problem solving
- Performance trade-offs between search methods
Ziyad Azzaz
🔗 GitHub: ZiyadAzzaz
This project is open-source under the MIT License.