learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

300 Exercises with Hints, Worked Algorithms and Complete Solutions

By Mohammed Islam Hadjoudj

Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

2-SAT Solver

Either-or rules, solved in linear time

Turns each two-literal clause into two implications, finds the strongly connected components, and reads a satisfying assignment off their order

Time: O(n + m)
Space: O(n + m)
Use Case: Scheduling with either-or rules, map labelling, configuration checks
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About 2-SAT (Implication Graph and SCC)

2-SAT asks whether a boolean formula made of clauses with two literals each, such as (A ∨ B) ∧ (¬A ∨ C), can be made true by some choice of true and false for its variables. General satisfiability (SAT) is NP-complete, but this special case with two literals per clause is solvable in linear time. The trick is to see each clause as two implications: (a ∨ b) fails only when both are false, so if a is false then b must be true, and if b is false then a must be true. Drawn as arrows between literals, the whole formula becomes a directed graph, the implication graph, and the question becomes a question about its cycles.

How it works

Every variable x gets two vertices, x and ¬x. For each clause (a ∨ b) the algorithm adds the edges ¬a → b and ¬b → a. It then finds the strongly connected components, here with Tarjan's algorithm. Literals in one component imply each other, so they must all be true or all false. If some x and ¬x fall in the same component, x would force ¬x and ¬x would force x, and the formula is unsatisfiable. Otherwise a satisfying assignment exists, and the order of the components gives one: Tarjan finds components sinks first, and taking them in that order, every component whose literals are not yet decided is made true (and its negations false). Aspvall, Plass and Tarjan published this method in 1979. It runs in O(n + m) for n variables and m clauses. The visualizer draws the literals as vertices, colours the components and marks each literal T or F as it is decided.

Applications

Many practical rules are of the either-or kind that 2-SAT captures. Scheduling: a meeting must be in the morning or the afternoon, two people cannot both take the late shift. Map labelling: each city label goes above or below its point, and overlapping positions exclude each other. Configuration and dependency checks: one of two packages must be installed, two options conflict. 2-SAT also appears inside other algorithms, for instance in some geometric placement problems and in 2-colouring with constraints. In competitive programming it is a classic, and it is a clean example of how strongly connected components answer a question that does not look like a graph question at all.

Pseudocode

Three phases: build the implication graph from the clauses, find its strongly connected components, then read the answer off the component numbers.

TwoSAT(variables, clauses):
    for each variable x: add vertices x and ¬x
    for each clause (a ∨ b):
        add edge ¬a → b
        add edge ¬b → a
    comp = Tarjan(G)        // components numbered sinks first
    for each variable x:
        if comp[x] == comp[¬x]:
            return UNSATISFIABLE
    for each variable x:
        value[x] = (comp[x] < comp[¬x])   // earlier = closer to a sink
    return value

Tarjan numbers the components in reverse topological order, sinks first. Choosing the literal whose component comes earlier is the same as walking the components sinks first and making true every component not yet decided, which is how the visualizer shows it.

Worked example, step by step

Solve the formula this page loads: four variables A, B, C, D and five clauses.

Example graph: The clauses (¬A ∨ C), (¬C ∨ ¬B), (B ∨ A), (¬C ∨ D) and (¬A ∨ ¬D), drawn as 10 implications between the 8 literals. On the left, C, ¬B and A form a triangle of implications, on the right their negations, and D and ¬D sit in between.

  1. Clauses to arrows. Each clause gives two arrows. (¬A ∨ C) gives A → C and ¬C → ¬A. (B ∨ A) gives ¬B → A and ¬A → B. All ten arrows are already drawn, so nothing has to be added.
  2. Components. Tarjan finds four strongly connected components: {¬C, B, ¬A} first (a sink), then {D}, {¬D} and finally {¬B, C, A}. The left and the right triangles are cycles, so each is one component.
  3. Conflict check. A and ¬A are in different components, and so are B, C and D with their negations. The formula is satisfiable.
  4. Sinks first. {¬C, B, ¬A} comes first, so ¬C, B and ¬A become true and C, ¬B, A false. Then {D}: D is true and ¬D false.
  5. Already decided. {¬D} and {¬B, C, A} were decided as the negations of earlier components, so they stay false.

A = F, B = T, C = F, D = T satisfies all five clauses. The picture explains why A must be false: A implies ¬D, ¬D implies ¬C, and ¬C implies ¬A, so A being true would force A to be false.

Complexity, and where it comes from

Time: O(n + m) · Space: O(n + m)

With n variables and m clauses the implication graph has 2n vertices and 2m edges. Building it is O(n + m), Tarjan's algorithm visits every vertex and edge once, O(n + m), and the final pass over the variables is O(n). So the whole algorithm is linear, which is remarkable because 3-SAT, with three literals per clause, is NP-complete.

When to use 2-SAT (Implication Graph and SCC), and when not to

The SCC method is the standard way to solve 2-SAT. Other approaches exist for nearby problems.

AlternativePrefer it whenCost
Brute force over all assignmentsTiny formulas, or to check an answer in tests.O(2^n · m)
Unit propagation (limited backtracking)Also solves 2-SAT: set a variable, propagate, undo on a conflict. Simpler to write inside a general SAT solver.O(n · m)
General SAT solver (CDCL)Clauses with three or more literals, or mixed constraints. No polynomial guarantee.exponential worst case

Common pitfalls

  • Adding only one implication per clause. A clause (a ∨ b) is two implications, ¬a → b and ¬b → a. Leaving out the contrapositive breaks the symmetry of the graph and can produce a wrong assignment. The visualizer adds a missing one as a dashed edge.
  • Choosing the value in the wrong direction. With Tarjan's numbering (sinks first) x is true when comp[x] < comp[¬x]. With Kosaraju's numbering (sources first) the comparison flips. Mixing them up gives assignments that violate clauses.
  • Forgetting clauses with one literal. A unit clause (x) is the 2-clause (x ∨ x), which gives the single edge ¬x → x. It forces x to be true and must not be dropped.

Frequently asked questions

Why is 2-SAT easy when 3-SAT is NP-complete?
A two-literal clause turns into implications between single literals, and chains of implications are paths in a graph, which can be searched in linear time. A three-literal clause says "if a is false then b or c is true", which is not an implication between two literals, so the graph trick does not work.
When is a 2-SAT formula unsatisfiable?
Exactly when some variable x and its negation ¬x are in the same strongly connected component: then x implies ¬x and ¬x implies x, a contradiction whatever value x takes.
How is the satisfying assignment chosen?
Each variable takes the value of the literal whose component comes first in reverse topological order. This never makes a true literal imply a false one, so every clause is satisfied.
Can 2-SAT find the assignment with the most true variables?
No. Maximising the number of satisfied clauses (MAX-2-SAT) or of true variables is NP-hard in general. 2-SAT only decides satisfiability and finds some satisfying assignment.

Related algorithms: Tarjan's SCC Algorithm, Kosaraju's SCC Algorithm, Topological Sort, Cycle Detection, Depth-First Search

An example graph: you can change it, or start blank.

Zoom Controls

30%
Nodes: 8
Edges: 10