{
  "id": "two-sat",
  "title": "2-SAT",
  "lang": "en",
  "source_lang": "en",
  "summary": "2-SAT is the problem of deciding whether a Boolean formula with at most two literals per clause has a satisfying assignment; it can be solved in linear time.",
  "category": "computer-science",
  "subcategory": "algorithms-theory",
  "type": "concept",
  "aliases": [
    "2-satisfiability",
    "2-CNF satisfiability",
    "bijunctive satisfiability"
  ],
  "generated_at": "2026-11-04",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/two-sat/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/two-sat/",
      "json": "https://aiwiki.page/zh/two-sat.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "2-SAT is a restricted form of the Boolean satisfiability problem in which each clause contains at most two literals. A literal is a Boolean variable or its negation, and a satisfying assignment gives every variable a truth value that makes the entire formula true. Unlike unrestricted Boolean satisfiability, 2-SAT has a linear-time solution: its clauses can be represented as implications, and satisfiability can be determined by examining cycles in a directed graph. It is an important example in computational complexity of how a small restriction on the form of an input can change a problem’s difficulty."
    },
    {
      "heading": "Definition and notation",
      "text": "A formula is in conjunctive normal form (CNF) if it is a conjunction of clauses, each of which is a disjunction of literals. A 2-CNF formula has the form\n\n\\[F=\\bigwedge_{i=1}^{m}(a_i\\lor b_i),\\]\n\nwhere each \\(a_i\\) and \\(b_i\\) is a literal drawn from variables \\(x_1,\\ldots,x_n\\). The operators \\(\\land\\), \\(\\lor\\), and \\(\\neg\\) mean AND, OR, and NOT, respectively.\n\nFor example,\n\n\\[(x\\lor y)\\land(\\neg x\\lor z)\\land(\\neg y\\lor\\neg z)\\]\n\nis a 2-CNF formula. The assignment \\(x=\\mathrm{true}\\), \\(y=\\mathrm{false}\\), and \\(z=\\mathrm{true}\\) satisfies all three clauses.\n\nDefinitions commonly allow clauses with at most two literals. If exactly two literal positions are required, a unit clause \\(a\\) can be written as \\(a\\lor a\\). An empty clause is always false, so a formula containing one is immediately unsatisfiable. A formula with no clauses is true under every assignment.\n\nThe decision version asks only whether a satisfying assignment exists. A constructive version also returns one. Both can be solved in linear time.\n\nThe term “2-SAT” refers to the number of literals per clause, not to the total number of variables or the number of values a variable can take."
    },
    {
      "heading": "Implication graph",
      "text": "The central observation is the logical equivalence\n\n\\[(a\\lor b) \\equiv (\\neg a\\Rightarrow b) \\land (\\neg b\\Rightarrow a).\\]\n\nIf \\(a\\) is false, \\(b\\) must be true; if \\(b\\) is false, \\(a\\) must be true.\n\nThe implication graph is a directed graph with one vertex for each literal \\(x_i\\) and \\(\\neg x_i\\). Each clause \\(a\\lor b\\) contributes two directed edges:\n\n\\[\\neg a\\longrightarrow b, \\qquad \\neg b\\longrightarrow a.\\]\n\nThus, a formula with \\(n\\) variables and \\(m\\) two-literal clauses produces \\(2n\\) vertices and at most \\(2m\\) edges before duplicate removal.\n\nA path from a literal \\(p\\) to a literal \\(q\\) means that setting \\(p\\) true forces \\(q\\) true through successive implications. Every edge also has a paired contrapositive edge: if \\(p\\to q\\) appears, so does \\(\\neg q\\to\\neg p\\).\n\nA unit clause \\(a\\) can be represented by the edge \\(\\neg a\\to a\\). A tautological clause such as \\(x\\lor\\neg x\\) imposes no restriction."
    },
    {
      "heading": "Satisfiability criterion",
      "text": "A strongly connected component (SCC) is a maximal set of vertices in which every vertex can reach every other vertex by a directed path.\n\nThe fundamental criterion is:\n\nA 2-CNF formula is satisfiable if and only if no variable and its negation belong to the same strongly connected component of its implication graph.\n\nEquivalently,\n\n\\[F\\text{ is satisfiable} \\quad\\Longleftrightarrow\\quad \\forall i,\\ \\operatorname{SCC}(x_i)\\ne \\operatorname{SCC}(\\neg x_i).\\]\n\nWhy a contradictory component implies unsatisfiability\n\nIf \\(x\\) and \\(\\neg x\\) lie in the same component, there are paths\n\n\\[x\\leadsto\\neg x \\qquad\\text{and}\\qquad \\neg x\\leadsto x.\\]\n\nSetting \\(x\\) true forces \\(\\neg x\\) true. Setting \\(x\\) false makes \\(\\neg x\\) true, which forces \\(x\\) true. Neither choice is consistent.\n\nFor example,\n\n\\[(x\\lor y) \\land(x\\lor\\neg y) \\land(\\neg x\\lor y) \\land(\\neg x\\lor\\neg y)\\]\n\nis unsatisfiable. The first two clauses require \\(x\\) to be true, while the last two require it to be false.\n\nWhy the absence of contradictory components is sufficient\n\nContracting every SCC to a single vertex produces the condensation graph, which is a directed acyclic graph. Complementing all literals in one component produces another component, with the directions of corresponding implications reversed. When the formula passes the criterion, these complementary components are distinct.\n\nTake a topological ordering of the condensation graph, numbered so that every edge between distinct components goes from a smaller number to a larger number. Let \\(r(\\ell)\\) be the number assigned to the component containing literal \\(\\ell\\). Set\n\n\\[x_i=\\mathrm{true} \\quad\\Longleftrightarrow\\quad r(x_i)>r(\\neg x_i).\\]\n\nThis assignment satisfies every implication. To see why, suppose an edge \\(p\\to q\\) had a true source and a false destination. Its paired edge is \\(\\neg q\\to\\neg p\\), so the ordering would require\n\n\\[r(\\neg p)<r(p)\\le r(q)<r(\\neg q)\\le r(\\neg p),\\]\n\nan impossibility. Hence no clause can have both its literals false."
    },
    {
      "heading": "Linear-time algorithm",
      "text": "The standard algorithm proceeds as follows:\n\n- Construct the implication graph.\n- Compute its strongly connected components.\n- Reject the formula if any variable shares a component with its negation.\n- Otherwise, obtain a satisfying assignment from an appropriate ordering of the components.\n\nSCCs can be computed by Tarjan’s algorithm or Kosaraju’s algorithm, both based on depth-first search. With adjacency-list storage, the overall time complexity and space complexity are\n\n\\[O(n+m).\\]\n\nImplementations must distinguish a topological order from the incidental numbers assigned by an SCC routine. Some routines number components in reverse topological order; consequently, the comparison used to extract truth values depends on the numbering convention.\n\nA classic linear-time algorithm for formulas with two literals per clause was published by Bengt Aspvall, Michael Plass, and Robert Tarjan in 1979."
    },
    {
      "heading": "Expressing constraints",
      "text": "Many constraints involving two Boolean choices can be translated directly into 2-CNF.\n\nConstraint | 2-CNF encoding\nIf \\(a\\), then \\(b\\) | \\(\\neg a\\lor b\\)\nAt least one of \\(a,b\\) | \\(a\\lor b\\)\nAt most one of \\(a,b\\) | \\(\\neg a\\lor\\neg b\\)\nExactly one of \\(a,b\\) | \\((a\\lor b)\\land(\\neg a\\lor\\neg b)\\)\n\\(a\\) and \\(b\\) have equal values | \\((\\neg a\\lor b)\\land(a\\lor\\neg b)\\)\n\\(a\\) and \\(b\\) have different values | \\((a\\lor b)\\land(\\neg a\\lor\\neg b)\\)\n\nMore generally, forbidding one particular joint assignment to two Boolean variables requires just one clause. For example, forbidding \\(x=\\mathrm{true}\\) together with \\(y=\\mathrm{false}\\) gives \\(\\neg x\\lor y\\). Any Boolean relation involving at most two variables can therefore be expressed by excluding its forbidden assignments.\n\nApplications\n\nThis encoding makes 2-SAT useful for binary-choice feasibility problems:\n\n- Configuration: options may imply other options or be mutually incompatible.\n- Restricted scheduling: each item has two possible placements, and incompatible pairs of placements are forbidden.\n- Geometric labeling: each label has two candidate positions, and overlapping pairs cannot both be selected.\n- Program analysis and verification: some systems of Boolean implications can be solved as 2-SAT instances within broader formal verification procedures.\n\nThe restriction is essential. A scheduling or configuration problem is not automatically a 2-SAT problem merely because it contains Boolean decisions; all required constraints must admit a suitable encoding."
    },
    {
      "heading": "Complexity and related problems",
      "text": "Comparison with general SAT\n\nUnrestricted SAT and 3-SAT are NP-complete, whereas 2-SAT is solvable in polynomial time and, more specifically, linear time in its explicit input size. Allowing a third literal per clause therefore crosses an important complexity boundary.\n\nThis does not mean every clause of length three can be converted into an equivalent 2-SAT construction by introducing auxiliary variables. A general polynomial-time reduction of 3-SAT to 2-SAT would imply a positive resolution of the P versus NP problem.\n\nAt a finer level, 2-SAT is complete for NL, the class of decision problems solvable with nondeterministic logarithmic workspace, under logarithmic-space many-one reductions. This classification concerns memory-limited computation and is compatible with its linear-time algorithms.\n\nCounting satisfying assignments\n\n#2-SAT asks how many satisfying assignments a 2-CNF formula has. It is #P-complete, despite the tractability of deciding whether at least one assignment exists.\n\nThe distinction can be seen through independent sets in an undirected graph. Introduce a variable \\(x_v\\) for each vertex and, for every edge \\(\\{u,v\\}\\), add\n\n\\[\\neg x_u\\lor\\neg x_v.\\]\n\nSatisfying assignments correspond exactly to vertex subsets containing no adjacent pair. Counting those subsets is a hard counting problem, even though the formula is always satisfiable by setting every variable false.\n\nMaximization and additional requirements\n\nMAX-2-SAT asks for an assignment satisfying as many clauses as possible. Unlike ordinary 2-SAT, it is NP-hard.\n\nAdditional requirements can likewise destroy tractability. For example, maximizing the number of true variables in the independent-set encoding is exactly the maximum independent set problem. The linear-time satisfiability algorithm therefore does not automatically extend to optimization over satisfying assignments."
    },
    {
      "heading": "Structural limitations",
      "text": "2-CNF formulas describe a special family of Boolean relations, often called bijunctive relations. Their satisfying assignments are closed under coordinatewise majority: taking three satisfying assignments and choosing, for each variable, the value occurring in at least two of them yields another satisfying assignment.\n\nFor a two-literal clause, if the majority assignment made both literals false, each literal would be false in at least two of the three original assignments. Those two sets of assignments must overlap, so at least one original assignment would also falsify both literals—a contradiction.\n\nThis property explains an expressive limitation. The relation\n\n\\[a\\lor b\\lor c\\]\n\nis not majority-closed: assignments \\(100\\), \\(010\\), and \\(001\\) satisfy it, but their coordinatewise majority, \\(000\\), does not. Consequently, this three-way “at least one” relation cannot be represented exactly by a 2-CNF formula, even with existentially quantified auxiliary variables. Existentially eliminating variables preserves majority closure."
    }
  ],
  "links": [
    "boolean-satisfiability-problem",
    "computational-complexity",
    "conjunctive-normal-form",
    "directed-graph",
    "strongly-connected-component",
    "directed-acyclic-graph",
    "algorithm",
    "kosarajus-algorithm",
    "depth-first-search",
    "time-complexity",
    "space-complexity",
    "formal-verification",
    "np-completeness",
    "p-versus-np-problem",
    "np-hardness",
    "mathematical-optimization"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}