Guest
Sign in to keep your graphs and progress.

Practice book
Graph Theory Through Problems300 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.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
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
Select an algorithm and generate steps to begin visualization
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.
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.
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.
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 valueTarjan 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.
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.
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.
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.
The SCC method is the standard way to solve 2-SAT. Other approaches exist for nearby problems.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Brute force over all assignments | Tiny 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 |
Read the full article: Strongly Connected Components: Tarjan & Kosaraju
Read the full article: Cycles in Graphs: Cycle Detection Explained
Related algorithms: Tarjan's SCC Algorithm, Kosaraju's SCC Algorithm, Topological Sort, Cycle Detection, Depth-First Search