What is DIMACS format?
DIMACS CNF is the standard text format for representing Boolean satisfiability problems. It’s named after the Center for Discrete Mathematics and Theoretical Computer Science (DIMACS), which established format standards for SAT competitions.Almost all SAT solvers, including MiniSat, support DIMACS format as their primary input method. This makes it easy to share SAT problems and compare solver performance.
Format structure
A DIMACS CNF file consists of:- Comments (optional): Lines starting with
c - Problem line (required): Specifies format, variable count, and clause count
- Clauses: Space-separated literal integers, each clause ending with
0
Basic example
example.cnf
Explanation
Explanation
- Line 1-2: Comments describing the problem
- Line 4: Problem line declaring CNF format, 3 variables, 3 clauses
- Line 5: First clause: (x1 ∨ x2 ∨ ¬x3)
- Line 6: Second clause: (¬x1 ∨ x3)
- Line 7: Third clause: (x2 ∨ ¬x3)
Problem line
The problem line has the format:p: Indicates problem linecnf: Format type (Conjunctive Normal Form)<variables>: Number of variables (integer)<clauses>: Number of clauses (integer)
Example
The problem line helps the solver pre-allocate memory efficiently. However, MiniSat doesn’t strictly require the counts to be exact—it can handle more variables or clauses than declared.
Variables and literals
Variable numbering
Variables are numbered starting from 1 (not 0):Literal representation
- Positive integer represents a positive literal:
5means x₅ - Negative integer represents a negative literal:
-5means ¬x₅ - Zero marks the end of a clause:
0
Writing clauses
Each clause is written as a sequence of space-separated integers ending with 0:Multi-line clauses
Clauses can span multiple lines (newlines are treated as spaces):Comments
Comments start withc and extend to the end of the line:
Comments can appear anywhere in the file, even between clauses. They’re completely ignored by the parser.
Complete example
Here’s a complete DIMACS file for a simple SAT problem:pigeonhole-3-2.cnf
Parsing in MiniSat
MiniSat provides functions to parse DIMACS files:Dimacs.h
MiniSat supports reading both plain text and gzip-compressed DIMACS files. Compressed files are automatically detected and decompressed during parsing.
Parser behavior
Variable creation
Variable creation
Variables are created automatically as they appear in clauses:
Dimacs.h
Literal conversion
Literal conversion
DIMACS literals (1-based with sign) are converted to MiniSat literals:
Dimacs.h
Clause addition
Clause addition
Each clause is added immediately after parsing:If adding a clause makes the formula unsatisfiable, parsing continues but the solver is marked as conflicting.
Header validation
Header validation
MiniSat can optionally check that the actual number of clauses matches the declared count:
Writing DIMACS files
MiniSat can also export problems to DIMACS format:Common patterns
At-most-one constraints
Express “at most one of these variables is true”:At-least-one constraints
Express “at least one of these variables is true”:Exactly-one constraints
Combine at-least-one and at-most-one:Implications
Express “x1 implies x2” (x1 → x2):Equivalences
Express “x1 is equivalent to x2” (x1 ↔ x2):File extensions
Common file extensions for DIMACS files:.cnf- Standard DIMACS CNF file.cnf.gz- Gzip-compressed DIMACS file.dimacs- Alternative extension
MiniSat automatically handles gzip-compressed files, so you can work with compressed problems without manual decompression.
Troubleshooting
Parse error: Unexpected character
Parse error: Unexpected character
Cause: Invalid character in file, usually from incorrect encoding or binary corruption.Solution: Ensure file is plain text (ASCII or UTF-8) without binary data.
Variable number out of range
Variable number out of range
Cause: Using variable 0 or negative variable numbers in literals.Solution: Variables must be positive integers starting from 1. Use negative for negation:
-5, not ~5.Missing clause terminator
Missing clause terminator
Cause: Clause doesn’t end with 0.Solution: Every clause must end with a zero:
Header mismatch warning
Header mismatch warning
Cause: Actual clause count doesn’t match declared count in problem line.Solution: Update the problem line or use non-strict parsing. MiniSat can handle mismatches unless strict mode is enabled.
Tools and utilities
CNF generators
Tools to convert other formats to CNF:
- Boolean circuit to CNF
- SMT to CNF
- Constraint problems to CNF
Problem libraries
Public SAT problem collections:
- SATLIB benchmark suite
- SAT Competition problems
- Industrial verification problems
Visualization
Tools to visualize CNF formulas:
- Clause-variable incidence graphs
- Variable interaction graphs
Preprocessing
CNF simplification tools:
- Remove tautologies
- Subsumption elimination
- Variable elimination
Next steps
SAT solving
Learn how MiniSat solves problems in DIMACS format
Clauses
Understand how DIMACS clauses are represented internally
Quickstart
Try solving your first DIMACS problem
API reference
Explore the solver API and parsing functions