About Blog Projects Art Gallery

Sudoku Solver

A backtracking algorithm implementation with conflict tracking and optimization strategies.

Ready to solve
0
Recursions
0
Backtracks
0ms
Time

Sample Puzzles:

Algorithm Details

Backtracking with Conflict Tracking: This solver uses an optimized backtracking algorithm that maintains conflict matrices for rows, columns, and 3x3 squares to quickly determine valid moves.

Key Features:

Performance Metrics:

Bit Masking Optimization

Advanced Performance Technique: The current implementation uses MRV (Minimum Remaining Values) heuristic but can be significantly optimized by adding bit masking, a technique that uses individual bits in integers to represent data more efficiently.

Current vs Optimized Approach:

Current Implementation (MRV + Boolean Arrays):

  • Uses MRV: Always chooses cell with fewest valid options
  • Uses boolean arrays: rowConflict[row][value]
  • Uses boolean arrays: colConflict[col][value]
  • Uses boolean arrays: squareConflict[square][value]
  • Requires 3 separate array lookups per validity check

Optimized Implementation (MRV + Bit Masking):

  • Uses MRV: Always chooses cell with fewest valid options
  • Uses integer arrays: rowMasks[row] (16-bit)
  • Uses integer arrays: colMasks[col] (16-bit)
  • Uses integer arrays: squareMasks[square] (16-bit)
  • Single bitwise operation combines all constraints

MRV (Minimum Remaining Values) Heuristic:

The findBestBlank() function implements the MRV heuristic, which is a crucial optimization for constraint satisfaction problems:

Bit Representation:

Each number 1-9 is represented by a single bit in a 16-bit integer:

Number 1: 0000000000000001 (bit 0)
Number 2: 0000000000000010 (bit 1)
Number 3: 0000000000000100 (bit 2)
Number 4: 0000000000001000 (bit 3)
Number 5: 0000000000010000 (bit 4)
Number 6: 0000000000100000 (bit 5)
Number 7: 0000000001000000 (bit 6)
Number 8: 0000000010000000 (bit 7)
Number 9: 0000000100000000 (bit 8)

Optimized findBestBlank() Function (MRV + Bit Masking):

findBestBlank() {
    let minOptions = 10;
    let bestRow = -1;
    let bestCol = -1;

    for (let i = 0; i < this.boardSize; i++) {
        for (let j = 0; j < this.boardSize; j++) {
            if (!this.isBlank(i, j)) continue;

            // COMBINE ALL CONSTRAINTS USING BITWISE OR
            const square = this.getSquareNumber(i, j);
            const usedMask = this.rowMasks[i] | this.colMasks[j] | this.squareMasks[square];
            
            // COUNT UNUSED NUMBERS (UNSET BITS) - MRV HEURISTIC
            let options = 0;
            for (let val = 1; val <= 9; val++) {
                const valMask = 1 << (val - 1); // Convert number to bit mask
                if (!(usedMask & valMask)) {    // If bit is NOT set, number is valid
                    options++;
                }
            }

            // MRV: Choose cell with minimum remaining values
            if (options < minOptions) {
                minOptions = options;
                bestRow = i;
                bestCol = j;
                if (minOptions === 1) return [bestRow, bestCol]; // Early termination
            }
        }
    }

    return [bestRow, bestCol];
}

Key Bit Operations:

Performance Benefits:

Speed Improvement

  • 2-3x faster validity checking
  • 1.5-2x faster option counting for MRV
  • 20-40% overall improvement for complex puzzles

Memory Efficiency

  • Instead of 270 booleans, uses 432 bits total
  • More compact memory representation
  • Better cache efficiency

CPU Optimization

  • Bitwise operations are fastest CPU operations
  • Modern CPUs have dedicated bit manipulation hardware
  • Better instruction-level parallelism

Real Example:

If row 0 has numbers [1,3,5], column 0 has numbers [2,4], and square 0 has numbers [7,9]:

rowMasks[0]: 00000010101 (bits 0,2,4 set for numbers 1,3,5)
colMasks[0]: 00000001010 (bits 1,3 set for numbers 2,4)
squareMasks[0]: 10000010000 (bits 6,8 set for numbers 7,9)
usedMask: 10000011111 (OR of all three)
Valid numbers: 6,8 (bits 5,7 are unset)
Options count: 2 (MRV would prioritize this cell if it has fewest options)