Encoding matters
Encoding Matters: When measuring running time, we measure the number of state transitions in a Turing machine. Given that a Turing machine can process only one symbol at a time, the encoding of the input matters.
We can find problems where changing the encoding changes the time-complexity bound drastically. Two relevant examples are Weak NP-Hard Decision Problem and Strong NP-Hard Decision Problem.
Knapsack’s dependence on encoding
Consider the Knapsack problem with running time . How does relate to the input? What even is the input?
Let
be a string. The input length is
Here, if changes the length of the input string.
Binary encoding
Encode the integer values , , and in binary. The dynamic programming algorithm of Knapsack then runs over
Here, is purely structural. It is the count of tuples encoded in the input string; it is not encoded explicitly. Thus, it is encoding-independent.
However, is part of the encoded string because it is encoded in binary. We still need to explore all state combinations. We cannot simply skip elements of the state space.
Unary encoding
Encode the integer values in unary. Unary means that the length of the encoding is constrained linearly by the integer’s magnitude:
The dynamic programming algorithm of Knapsack then runs over
Again, is purely structural; it is not part of the input string. The inner loop now iterates over ‘s magnitude. We can imagine iterating over a unary-encoded string space equivalent to appending an element to a linear list:
This has running time .
Why not encode with a larger base?
Logarithms of different bases are equivalent in complexity theory:
By definition, if a problem is NP-hard, every problem in NP can be reduced to . Assume there exists at least one weakly NP-hard problem. Why is not every problem weakly NP-hard?
Complexity is always defined with respect to a fixed encoding. We cannot compare complexity relations for different encodings without fixing how their instances are represented.
Why not translate to an easier encoding?
Suppose the binary input contains . To convert all bits into unary, the Turing machine must physically produce symbols on its tape to encode .
Producing those symbols already costs
(lower bound; exponential) running time. Therefore, we cannot simply skip the complexity caused by the encoding.
Reduction from Subset Sum to Knapsack
The reduction is a Karp reduction from Subset Sum to Knapsack:
The two problems
Subset Sum: The input is a multiset of non-negative integers and a target integer . The output question is
Knapsack: The input is a multiset of integer pairs , where is the weight and is the profit; an integer for the maximum weight; and an integer for the minimum profit. The output question is
Construction
Let be an instance of Subset Sum. Construct an instance of Knapsack. Set and . The Knapsack condition becomes
For every , append to :
The constructed Knapsack instance is equivalent to the Subset Sum instance because each selected item contributes the same amount to the weight and the profit:
Given that Subset Sum is weakly NP-hard, Knapsack is weakly NP-hard.
Dynamic programming for Knapsack
The algorithm is dynamic programming for Knapsack. Sort the items arbitrarily and label them by an integer .
Let denote whether profit is achievable using the first items and a weight bound .
Base case
Given that all profits are non-negative, profit is always achievable. Set
for all and all .
Progression
In the progression, there are two possible actions to choose from: take and skip.
If item is skipped, it is achievable only if all previous items achieve the condition with the capacity constraints and . This is obtained from . Therefore,
If item is taken, the state is achievable using items and constraints and if one of two cases is true. First, it is achievable even without , which is the same scenario as skipping it. Second, it is achievable without and with ‘s weight and profit . Item can be taken only when and .
Together, we arrive at the formula
Islands of Intractability
Within NP-complete problems, there are tractable subproblems.
Example: , where 3-SAT is efficiently solvable and SAT is NP-complete.
Horn and Krom formulas
A Horn formula is a conjunction of Horn clauses. A Horn clause is a disjunctive clause with at most one positive literal. For example,
is a Horn clause.
A unit clause is a clause that consists of only one literal. Unit propagation uses unit clauses to force values on literals and simplify the formula.
For example, consider
The recorded unit-propagation steps are
The handwritten notes also record . This last assignment does not follow from the displayed clauses alone: the clause would force only if were already known. It should therefore be checked against the original lecture example.
A Krom formula is a formula in which every clause has at most two literals. For example,
Solving Krom formulas is tractable because every disjunction of two literals can be represented as two implications:
\begin{array}{r@{\;}c@{\;}l@{\quad}c@{\quad}l@{\;}c@{\;}l@{\quad}c@{\quad}l@{\;}c@{\;}l} a & \lor & \neg b & \iff & (b & \Rightarrow & a) & \land & (\neg a & \Rightarrow & \neg b), \\ a & \lor & b & \iff & (\neg a & \Rightarrow & b) & \land & (\neg b & \Rightarrow & a), \\ \neg a & \lor & \neg b & \iff & (a & \Rightarrow & \neg b) & \land & (b & \Rightarrow & \neg a). \end{array}Unit clauses can be represented in the same way:
Consequently, we can represent Krom formulas as an implication graph. For each clause, add the two equivalent implication edges.
For example,
becomes
The graph has six singleton strongly connected components. There is no circularity, so the formula is satisfiable.
As a contrasting example,
becomes
Here, the strongly connected components include
The component containing both and is non-singleton. A variable and its negation belonging to the same strongly connected component makes the Krom formula unsatisfiable.
Tree-like formulas
The incidence graph of a formula is a bipartite graph whose vertex set consists of all variables and clauses in the formula. A variable is incident to a clause if and only if its literal occurs in that clause.
Formulas whose incidence graphs are acyclic can be solved in polynomial time.
Consider the formula
Its incidence graph is a tree. Squares represent variables and circles represent clauses.
Removing satisfied leaves
The tree can be simplified from its leaves upwards. The leaf clause contains only , so it forces
The first stage highlights the leaf clause and its parent:
Remove the satisfied leaf pair . The remaining tree is:
The variable is a leaf in . Choosing satisfies without branching through , which has more children. Remove and :
The leaf variable occurs in . Choosing satisfies , so remove and . The final clause is
It can be made true by choosing a satisfying literal: , , or .
Conflict-driven clause learning
Conflict-driven clause learning (CDCL) follows the cycle
Consider the clauses
Assume the decisions
Unit propagation proceeds as follows:
The last clause is false, so the decisions have produced a conflict. The implication graph records the propagation history as it is built.
After , , and
The decisions and force through and through . Together, and force through .
Adding and
The decision and the propagated value force through . The same value reduces to the unit clause , giving .
The conflict and the learned clause
Finally, and reduce to the empty clause. The graph now exposes the complete causal chain.
Tracing the conflict backwards reaches the decisions , , and . CDCL learns that these three decisions cannot hold simultaneously:
The solver adds and retries after backtracking. The learned clause prevents the same conflicting decision combination from being explored again.