Sub topic 15
Reductions in Practice: Encoding Problems for SAT Solvers, LP, and Max Flow
This chapter assumes you know many-one reductions, the direction rule (algorithms move forward from B to A when A reduces to B), and polynomial-time reductions. It also assumes you can read Python and know basic graph terms.
Earlier chapters used reductions to prove problems hard. Practitioners use them more often to solve problems. You take your messy problem A, translate it into a problem B that already has a fast, well-tested solver, run the solver, and translate the answer back. Because A reduces to B, B's algorithm transfers to A.
The Encode–Solve–Decode Pipeline
Practical use adds a decode step that decision-only reductions don't need. You usually want a schedule or an assignment, so the encoding must let you read a solution of A from a solution of B.
Three Common Targets
| Target | Good for | Watch out for |
|---|---|---|
| SAT | Constraint puzzles, configuration, scheduling with hard rules, verification | Encoding size; "at most one" constraints can cost quadratically many clauses |
| Linear programming (LP) | Resource allocation, blending, transport costs | LP solutions can be fractional; requiring integers gives integer programming, which is NP-hard |
| Maximum flow | Matching, assignment, disjoint paths, project selection | Only fits problems whose constraints are capacities and conservation |
SAT is NP-complete, so reducing to it doesn't promise speed. Modern CDCL solvers still handle many industrial instances with millions of clauses. LP and max flow can be solved in polynomial time, so a correct reduction to either one gives you a polynomial-time algorithm.
Worked Example: Assigning Workers to Tasks with Max Flow
Each worker can do certain tasks. Each worker takes at most one task, and each task needs at most one worker. You want the largest number of assignments. This is maximum bipartite matching, and it reduces to max flow. Add a source s with a capacity-1 edge to every worker, a capacity-1 edge from every worker to each task they can do, and a capacity-1 edge from every task to a sink t. In a unit network like this, an integral maximum flow picks out a maximum matching edge by edge.
from collections import deque, defaultdict
def max_flow(cap, s, t):
flow = 0
while True:
parent = {s: None}
q = deque([s])
while q and t not in parent:
u = q.popleft()
for v, c in list(cap[u].items()):
if c > 0 and v not in parent:
parent[v] = u
q.append(v)
if t not in parent:
return flow
bottleneck, v = float("inf"), t
while parent[v] is not None:
u = parent[v]
bottleneck = min(bottleneck, cap[u][v])
v = u
v = t
while parent[v] is not None:
u = parent[v]
cap[u][v] -= bottleneck
cap[v][u] = cap[v].get(u, 0) + bottleneck
v = u
flow += bottleneck
def max_matching(workers, tasks, can_do):
cap = defaultdict(dict)
for w in workers:
cap["s"][("W", w)] = 1
for task in tasks:
cap[("T", task)]["t"] = 1
for w, task in can_do:
cap[("W", w)][("T", task)] = 1
size = max_flow(cap, "s", "t")
# Decode: a used worker->task edge has capacity 0 left
pairs = [(w, task) for w, task in can_do
if cap[("W", w)][("T", task)] == 0]
return size, pairs
workers = ["ann", "bob", "cy"]
tasks = ["a", "b"]
can_do = [("ann", "a"), ("bob", "a"), ("cy", "b")]
print(max_matching(workers, tasks, can_do)) # (2, [('ann', 'a'), ('cy', 'b')])The decoder reads the matching from the edges that got saturated. Edmonds–Karp always finds integral flows when every capacity is an integer, so each worker edge carries either 0 or 1.
Encoding for SAT
A SAT encoding gives each yes/no choice its own Boolean variable and turns each rule into a clause. To say "exactly one of x1, x2, x3", you need one clause for "at least one", (x1 ∨ x2 ∨ x3), and one clause for each pair for "at most one". Tools like PySAT take these clauses as lists of signed integers, which is the DIMACS-style format used in the earlier 3-SAT chapter.
Exercise
In the matching network, what goes wrong if the edge from s to each worker has capacity 2 instead of 1?
Show answer
Each worker could then send 2 units of flow, so one worker might be assigned two tasks. The flow would no longer correspond to a matching, and the reduction would overcount.
Exercise
Write CNF clauses saying "at most one of x1, x2, x3 is true."
Show answer
(¬x1 ∨ ¬x2), (¬x1 ∨ ¬x3), (¬x2 ∨ ¬x3). Each clause forbids one pair from being true together.