โ™› 8-Queens CSP Visualizer

PECST522 โ€” Artificial Intelligence | KTU 2024
๐Ÿ“– How to Play

Objective: Place N queens on the board so that no two queens attack each other (no shared row, column, or diagonal).

Rules: Click any cell to place a queen. Click again to remove it. Red highlighted cells show threatened positions. The conflict counter shows how many queen pairs are attacking each other. Solve it when conflicts = 0.

AI Concept: This is a Constraint Satisfaction Problem (CSP). Variables = queen positions, Domain = row numbers, Constraints = no two queens share a row, column, or diagonal.

๐ŸŽฏ Goal: Place all N queens so that no two queens attack each other โ€” no shared row, column, or diagonal. Conflicts counter shows 0 when you've solved it!
0 conflicts remaining
๐Ÿ“– How to Play

Objective: Watch how backtracking search solves the N-Queens problem automatically.

Rules: The algorithm places one queen per column from left to right. If it can't find a valid row, it backtracks (undoes the last placement and tries the next option). Use "Step" for manual control or "Auto" with the speed slider.

AI Concept: Backtracking = systematic trial and error with early failure detection. It explores the search tree depth-first, pruning branches that violate constraints.

๐Ÿ‘€ Watch for: Red flashes = backtrack (failed placement undone). Counter shows total attempts vs backtracks. Fewer backtracks = more efficient search!
300ms
Assignments: 0
Backtracks: 0
๐Ÿ“– How to Play

Objective: See how Forward Checking improves upon basic backtracking by eliminating impossible values early.

Rules: After each queen placement, future columns have their invalid rows greyed out (domain reduction). If any future column has 0 valid rows left, the algorithm backtracks immediately without trying further.

AI Concept: Forward Checking propagates constraints after each assignment, reducing future domains. This detects failures earlier than basic backtracking โ†’ fewer backtracks needed.

โšก Key insight: Grey squares = values eliminated by Forward Checking. When a column has 0 remaining values โ†’ early backtrack (no need to try). Compare backtrack counts: FC should need far fewer than basic backtracking!
300ms
Assignments: 0
Backtracks: 0
Without FC backtracks: โ€”
๐Ÿ“– How to Play

Objective: See the formal CSP formulation update live as the puzzle is solved.

Rules: This panel shows the mathematical representation: Variables (Xโ‚..Xโ‚™), Domains (1..n), and Constraints. The "Current Assignment" updates as queens are placed in any mode.

AI Concept: Every CSP has 3 components: Variables, Domains, and Constraints. The 8-Queens CSP has n variables (one per column), each with domain {1..n}, and constraints ensuring no attacks.

๐Ÿ“ Reading the CSP: Variables (Xแตข) = one queen per column. Domain = possible rows. Constraints = the "no attack" rules. Current Assignment shows what's been decided so far. This IS how you write CSP answers in exams!

CSP Formulation of N-Queens

Variables

X = {Qโ‚, Qโ‚‚, Qโ‚ƒ, Qโ‚„, Qโ‚…, Qโ‚†, Qโ‚‡, Qโ‚ˆ}

Domains

D(Qแตข) = {1, 2, 3, 4, 5, 6, 7, 8} for all i โˆˆ {1..8}

Constraints

For all i โ‰  j: Qแตข โ‰  Qโฑผ (no two queens in same row) |Qแตข - Qโฑผ| โ‰  |i - j| (no two queens on same diagonal)

Why one variable per column?

Placing exactly one queen per column reduces the problem from choosing 64 squares to choosing 8 row-values. Each variable Qแตข represents the row of the queen in column i.

Constraint Graph

Every pair of variables is constrained โ†’ complete graph Total constraints: C(n,2) ร— 2 (row + both diagonals) For n=8: 56 constraint pairs

Live Assignment (from Mode 2 or 3)

๐Ÿ“– How to Play

Objective: Compare the efficiency of different CSP solving algorithms on the same problem.

Rules: Click "Run All" to execute Backtracking, Forward Checking, and FC+MRV on the current board size. The results show assignments tried, backtracks needed, and time taken.

AI Concept: MRV (Minimum Remaining Values) picks the most constrained variable next, dramatically reducing search. FC+MRV combines domain pruning with smart variable ordering.

๐Ÿ“Š How to interpret: Lower assignments = smarter search. Lower backtracks = better pruning. FC+MRV should beat both plain Backtracking and FC alone. The bar chart makes this visually obvious โ€” shorter bars = more efficient!