Back to CSP Overview

Variable Ordering Heuristics

Intelligent strategies for choosing which variable to assign next in CSP backtracking

🧠 Why Variable Ordering Matters
The Problem:

In backtracking search, we need to choose which variable to assign next. Poor choices lead to:

  • Deep search trees with many dead ends
  • Wasted computation on impossible branches
  • Exponential explosion in search space
  • Slower convergence to solutions
Smart Heuristics:

Intelligent variable ordering can dramatically improve performance:

  • MRV: Most constrained variable (fewest remaining values)
  • Degree: Most constraining variable (most unassigned neighbors)
  • Combined: Break ties with degree heuristic
  • Result: Fail fast and find solutions quicker!

Example 1: MRV (Minimum Remaining Values) Heuristic

🎯 MRV Strategy: "Choose the Most Constrained Variable"

Core Idea: Select the variable with the fewest remaining values in its domain.

Why It Works: Variables with fewer choices are more likely to fail soon, leading to early pruning of impossible branches.

Fail-Fast Principle: If a variable will fail, it's better to discover this early rather than deep in the search tree.

Understanding "Degree" in the Indicators

Degree: The number of uncolored neighboring regions that a region is connected to.

Example: If Region A is connected to regions B, D, and E, but only B and D are still uncolored, then A's degree = 2.

Interactive MRV Demonstration

Regional Map Coloring: Watch how MRV chooses the most constrained region first

A B C D E F
Initial State Analysis

Pre-colored Regions:

  • Region B = Red (already assigned)
  • Region F = Blue (already assigned)

Remaining Choices:

  • Region C & E: Only 1 color left (Green) → MRV targets!
  • Region A: 2 colors left (Blue, Green)
  • Region D: 3 colors left (Red, Blue, Green)
💡 MRV will choose C or E first (most constrained with only 1 color option)

Degree Values Explained:

  • A (deg: 1): Connected to B (colored), D (uncolored) → deg = 1
  • C (deg: 0): Connected to B, F (both colored) → deg = 0
  • D (deg: 2): Connected to A, E (both uncolored) → deg = 2
  • E (deg: 1): Connected to B, F (colored), D (uncolored) → deg = 1
R
B
G
MRV Progress
0
Current Step
0
Assigned
Current Decision

Click "Start MRV Demo" to begin

MRV Decision Log
Ready to demonstrate MRV heuristic...

Example 2: Degree Heuristic

Example 3: Heuristic Comparison