Np-Hard and Np-Complete Problems
Np-Hard and Np-Complete Problems
PROBLEMS
Dr K Shahu Chatrapati
Deterministic Algorithms
• A deterministic algorithm is an algorithm which,
given a particular input, will always produce the
same output, with the underlying machine always
passing through the same sequence of states.
• Formally, a deterministic algorithm computes a
mathematical function; a function has a unique
value for any input in its domain, and the
algorithm is a process that produces this particular
value as output.
Nondeterministic Algorithms
• A nondeterministic algorithm is an algorithm
that, even for the same input, can exhibit
different behaviors on different runs, as
opposed to a deterministic algorithm.
• The nondeterministic algorithms are often
used to find an approximation to a solution,
when the exact solution would be too costly
to obtain using a deterministic one.
Nondeterministic Search
10
Nondeterministic Clique Algorithm
Nondeterministic Knapsack’s Algorithm
Nondeterministic Satisfiability
Algorithmic vs Problem Complexity
• The algorithmic complexity of a computation is
some measure of how difficult is to perform the
computation (i.e., specific to an algorithm)
• The complexity of a computational problem or
task is the complexity of the algorithm with the
lowest order of growth of complexity for solving
that problem or performing that task.
– e.g. the problem of searching an ordered list has at
most O(log n) time complexity.
• Computational Complexity: deals with classifying
problems by how hard they are.
14
Class of “P” Problems
• Class P consists of (decision) problems that are
solvable in polynomial time
• Polynomial-time algorithms
– Worst-case running time is O(nk), for some constant k
• Examples of polynomial time:
– O(n2), O(n3), O(1), O(n lg n)
• Examples of non-polynomial time:
– O(2n), O(nn), O(n!)
15
Tractable/Intractable Problems
• Problems in P are also called tractable
• Problems not in P are intractable or unsolvable
– Can be solved in reasonable time only for small
inputs
– Or, can not be solved at all
16
Examples of Intractable Problems
17
Intractable Problems
• Can be classified into various categories based
on their degree of difficulty, e.g.,
– NP
– NP-complete
– NP-hard
• Let’s define NP algorithms and NP problems …
18
Nondeterministic and NP Algorithms
in P is also in NP:
21
NP-Completeness (informally)
P NP-complete
• NP-complete problems are
NP
defined as the hardest
problems in NP
• Most practical problems turn out to be either P
or NP-complete.
• Study NP-complete problems …
22
Reductions
• Reduction is a way of saying that one problem is
“easier” than another.
• We say that problem A is easier than problem B,
(i.e., we write “A B”)
if we can solve A using the algorithm that solves B.
• Idea: transform the inputs of A to inputs of B
yes
yes
f Problem B no
no
Problem A
23
Polynomial Reductions
(1) B NP NP
25
Implications of Reduction
yes
yes
f Problem B no
no
Problem A
- If A p B and B P, then A P
- if A p B and A P, then B P
26
Proving Polynomial Time
yes
yes
Polynomial time
f algorithm to decide B no
no
Polynomial time algorithm to decide A
27
Proving NP-Completeness In Practice
29
Revisit “Is P = NP?”
P NP-complete
NP
30
P & NP-Complete Problems
• Shortest simple path
– Given a graph G = (V, E) find a shortest path from a source
to all other vertices
– Polynomial solution: O(VE)
31
P & NP-Complete Problems
• Euler tour
– G = (V, E) a connected, directed graph find a cycle that
traverses each edge of G exactly once (may visit a vertex
multiple times)
– Polynomial solution O(E)
• Hamiltonian cycle
– G = (V, E) a connected, directed graph find a cycle that
visits each vertex of G exactly once
– NP-complete
32
NP-naming convention
• NP-complete - means
problems that are
'complete' in NP, i.e.
the most difficult to
solve in NP
• NP-hard - stands for
'at least' as hard as NP
(but not necessarily in
NP);
33
Examples NP-complete and
NP-hard problems
NP-complete
NP-hard
34
Why should we care?
• Knowing that they are hard lets you stop beating
your head against a wall trying to solve them…
– Use a heuristic: come up with a method for solving a
reasonable fraction of the common cases.
– Solve approximately: come up with a solution that
you can prove that is close to right.
– Use an exponential time solution: if you really have
to solve the problem exactly and stop worrying about
finding a better solution.
35
COOK’S Theorem
• Cook’s theorem states that the Boolean satisfiability
problem is NP-complete.
• That is, any problem in NP can be reduced in polynomial
time to the problem of determining whether a Boolean
formula is satisfiable.
• An important consequence of this theorem is that if there
exists a deterministic polynomial time algorithm for solving
Boolean satisfiability, then every NP problem can be solved
by a deterministic polynomial time algorithm.
• The question of whether such an algorithm for Boolean
satisfiability exists is thus equivalent to the P vs. NP
problem, which is widely considered the most important
unsolved problem in theoretical computer science.