Back to CSP Lecture

Backtracking Algorithm Demonstration

Step-by-step visualization of backtracking search in CSP solving

Learning Objectives

πŸ” Search Process: Understand systematic exploration of solution space
πŸ”„ Backtracking: Learn when and how to undo choices and try alternatives
⚑ Pruning: See how constraint checking reduces search space efficiently

Example 1: Regional Map Coloring with Backtracking

Watch the backtracking algorithm solve map coloring step by step

Backtracking Visualization

Watch as the algorithm tries colors, backtracks when needed, and finds a valid solution.

A B C D E F
R
B
G
Y
Backtracking Log

Real-time log of algorithm decisions, backtracks, and progress

Ready to start Regional Map BACKTRACKING algorithm - observe backtracking in action!
Algorithm State
SELECT
Region A
ASSIGN
Red
CHECK
Valid?
Statistics
0
Regions Colored
0
Backtracks
0
Total Steps
Algorithm
Backtracking Search:
  1. Select: Choose next uncolored region
  2. Assign: Try first available color
  3. Check: Validate against constraints
  4. Forward: If valid, move to next region
  5. Backtrack: If invalid, undo and try next color
  6. Repeat: Until solution found or no options left

Time: O(d^n) where d=colors, n=regions
Space: O(n) for recursion stack

N-Queens Backtracking Visualization

Watch as the algorithm places queens, detects conflicts, and backtracks when needed.

N-Queens Backtracking Log

Real-time log of queen placement, conflict detection, and backtracking

Ready to start N-Queens BACKTRACKING algorithm - observe backtracking in action!
Algorithm State
SELECT
Row 0
PLACE
Col 0
CHECK
Safe?
Statistics
0
Queens Placed
0
Backtracks
0
Total Steps
N-Queens Algorithm
Backtracking Process:
  1. Select Row: Choose current row to place queen
  2. Try Column: Attempt to place queen in column
  3. Check Safety: Validate no conflicts with existing queens
  4. Forward: If safe, move to next row
  5. Backtrack: If unsafe, try next column or backtrack
  6. Solution: All N queens placed successfully

Constraints: No two queens attack each other
Time: O(N!) worst case, much better with pruning

Impossible Map BACKTRACKING Visualization

This map requires 4 colors but we only provide 3 - watch backtracking fail systematically.

X Y Z W
R
B
G
⚠️ Only 3 colors available for a 4-color map!
Failure Point Backtracking Log

Watch the algorithm systematically explore and exhaust all possibilities

Ready to demonstrate BACKTRACKING failure - no solution possible!
Algorithm State
SELECT
Region X
ASSIGN
Red
CHECK
Valid?
Statistics
0
Regions Colored
0
Backtracks
0
Total Steps
Why This Fails
Impossible Configuration:
  • Complete Graph Kβ‚„: Every region connects to every other region
  • Chromatic Number: Requires exactly 4 different colors
  • Available Colors: Only 3 colors provided (Red, Blue, Green)
  • Mathematical Proof: No valid 3-coloring exists for Kβ‚„

Result: Backtracking will systematically try all 3Β³ = 27 combinations and fail
Learning: See how backtracking handles impossible problems