UNPKG

dancing-links

Version:

Fastest JS solver for exact cover problems using Dancing Links

30 lines (29 loc) 1.33 kB
/** * Knuth's Dancing Links Implementation * * Implements Knuth's Algorithm X using Dancing Links technique for solving * exact cover problems. Uses Struct-of-Arrays architecture with typed arrays. * * Reference: https://arxiv.org/pdf/cs/0011047.pdf * Based on: https://github.com/shreevatsa/knuth-literate-programs/blob/master/programs/dance.pdf * * ARCHITECTURE: * - Struct-of-Arrays data layout using Int32Array for navigation fields * - Index-based references with NULL_INDEX (-1) for null pointers * - Pre-allocated storage based on constraint matrix analysis * - State machine pattern to avoid recursion and enable goto-like control flow * * ALGORITHM OPTIMIZATIONS: * - Early termination for impossible constraints (columns with 0 options) * - Unit propagation for forced moves (columns with 1 option) * - Pre-calculated pointers to reduce data dependencies */ import { Result } from '../types/interfaces.js'; import { SearchContext } from './problem-builder.js'; /** * Execute Dancing Links search using SearchContext for resumable state * * The context preserves algorithm state between calls, enabling generator-style * iteration without modifying the core algorithm to use generators directly. */ export declare function search<T>(context: SearchContext<T>, numSolutions: number): Result<T>[][];