NP-Complete & NP-Hard
1
Introduction
Saw efficient POLYNOMIAL TIME algorithms for finding a
shortest path in a graph, a graph’s minimum spanning tree, etc.
They are intelligent ways of reducing the search space of solutions!
A graph with n vertices can have upto nn-2 spanning trees (complete
graph) [Kirchhoff’s Theorem], and a typical graph has an exponential
number of paths from s to t (complete: (n-2)!).
So exhaustive search would be of exponential (or worse) complexity
2
But for certain problems, the most efficient solution invented till
now is still of exponential complexity, may not much simpler
than exhaustive search.
Satisfiability (SAT) AND OR
Either find an assignment such that the result is TRUE or
report that it does not exist.
3-SAT (problem with max 3 literals in a clause, 1&2-SATs are Polynomial) 3
n variables in the expression means 2n possibilities, and an
exhaustive search through them to perform the task on all clauses.
A search problem must have the property that any proposed
solution S to an instance I can be quickly checked for correctness
Input size 4
Travelling Salesman Problem (TSP)
Seeing TSP as a search problem. (slightly different!)
Check whether:
A
Exhaustive search would be worse than exponential! Our DP TSP
algorithm was also exponential!
5
Non-deterministic* polynomial time
We denote the class of all search problems by NP
Most experts think so, but not proven.
* Turing machine 6
Reduction: Polynomial time Polynomial time
The converse may not be true!
If A is hard then B is also hard, if B is efficient so is A.
A problem is NP-hard if all search problems in NP reduce to it. If a NP-
hard problem is in NP (or is a search problem), then it is NP-complete.
If even one NP-complete problem is in P, then P = NP. Note: P ⊆ NP 7
Largest subset of graph vertices
with no edge between them
(Set Cover)
(Knapsack)
Largest vertex subset that forms a
complete graph (all edges present)
8
Why only P, NP?
Subset to Superset? P then NP?
EXPTIME - The set of all decision problems that are solvable by a
deterministic Turing machine in exponential time.
NEXPTIME - The set of decision problems that can be solved by a non-
deterministic Turing machine in exponential time.
PSPACE - The set of all decision problems that can be solved by a
Turing machine using a polynomial amount of space.
EXPSPACE - The set of all decision problems solvable by a Turing
machine in an exponential amount of space.
We also have L, NL, NL-hard, NL-complete… NL ⊆ P
9
Branch & Bound for Travelling Salesman
Problem:
Starting point
Cost (lower bound): Cost of completed partial tour + cost of completing the
partial tour (at least) 10
Lower bound on the cost of Sum
completing the partial tour:
Example:
TSP sol.
11
Approximation
for TSP Assume complete graph and that the distances (edge length)
satisfy triangle inequality.
If we remove an edge from the TSP solution, it will
be a spanning tree!
We cannot go through the same node (city) twice
Lets consider MST and drop the requirement that we cannot visit the same city twice.
So we can traverse through the MST edges at most twice to visit all places and return.
So cost is at most 2 times that of MST or at most 2 times the optimal TSP cost.
When 𝑂𝑂𝑂𝑂𝑂𝑂. 𝑇𝑇𝑇𝑇𝑇𝑇 𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐 = 𝑀𝑀𝑀𝑀𝑀𝑀 𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐
Now invoking the ‘no 2 visit’ clause, traversing directly from one place to
another without going through the MST edges twice can only reduce cost by
triangular inequality! Remove duplicate visits by direct path! Polynomial time! 12