AA Unit 5 Notes
AA Unit 5 Notes
AA Unit 5 Notes
ADVANCED ALGORITHMS
UNIT V
5.1. NP Completeness
Almost all the algorithms we have studied thus far have been polynomial -time
algorithms: on inputs of size n, their worst-case running time is O(nk) for some constant k.
You might wonder whether all problems can be solved in polynomial time. The answer is
no.
Generally, we think of problems that are solvable by polynomial-time algorithms as
being tractable, or easy, and problems that require superpolynomial time as being
intractable, or hard.
The subject of this chapter, however, is an interesting class of problems, called the
“NP-complete” problems, whose status is unknown. No polynomial-time algorithm has yet
been discovered for an NP-complete problem, nor has anyone yet been able to prove that
no polynomial-time algorithm can exist for any one of them.
Several NP-complete problems are particularly tantalizing because they seem on the
surface to be similar to problems that we know how to solve in polynomial time. In each of
the following pairs of problems, one is solvable in polynomial time and the other is NP-
complete, but the difference between problems appears to be slight:
Shortest vs. longest simple paths
Euler tour vs. hamiltonian cycle
2-CNF satisfiability vs. 3-CNF satisfiability
The class P consists of those problems that are solvable in polynomial time. More
specifically, they are problems that can be solved in time O(nk) for some constant k, where
n is the size of the input to the problem. Most of the problems examined so far are in P.
The class NP consists of those problems that are “verifiable” in polynomial time.
What do we mean by a problem being verifiable? If we were somehow given a “certificate”
of a solution, then we could verify that the certificate is correct in time polynomial in the
size of the input to the problem.
Any problem in P is also in NP, since if a problem is in P then we can solve it in
polynomial time without even being supplied a certificate.
Verification algorithms
We define a verification algorithm as being a two-argument algorithm A, where
one argument is an ordinary input string x and the other is a binary string y called a
certificate. A two-argument algorithm A verifies an input string x if there exists a
certificate y such that A(x, y) = 1.
NP-completeness
Polynomial-time reductions provide a formal means for showing that one problem
is at least as hard as another, to within a polynomial-time factor.
A language L ⊆{0, 1}* is NP-complete if
1. L NP, and
2. L’ ≤P L for every L1 NP.
If a language L satisfies property 2, but not necessarily property 1, we say that L is
NP-hard. We also define NPC to be the class of NP-complete languages.
Circuit satisfiability
Boolean combinational circuits are built from boolean combinational el ements that
are interconnected by wires. A boolean combinational element is any circuit element
that has a constant number of boolean inputs and outputs and that performs a well-defined
function. Boolean values are drawn from the set {0, 1}, where 0 represents FALSE and 1
represents TRUE.
The boolean combinational elements that we use in the circuit -satisfiability problem
compute simple boolean functions, and they are known as logic gates. Following figure
shows the three basic logic gates that we use in the circuit-satisfiability problem: the NOT
gate (or inverter), the AND gate, and the OR gate.
We can describe the operation of each gate, and of any boolean combinational
element, by a truth table, shown under each gate in Figure.
Figure (a) is an undirected graph G= (V, E) with clique V’ = {u, v, x, y}. (b) The graph
G produced by the reduction algorithm that has vertex cover V – V’ = {w, z}.
The vertex-cover problem is to find a vertex cover of minimum size in a given
graph.
For example, the graph in following figure (b) has a vertex cover {w, z} of size 2.
As a language, we define
VERTEX-COVER = {<G, k> : graph G has a vertex cover of size k}.
The vertex-cover problem is NP-complete.
In many practical situations, the least costly way to go from a place u to a place w is
to go directly, with no intermediate steps. Put another way, cutting out an intermediate
stop never increases the cost. We formalize this notion by saying that the cost function c
satisfies the triangle inequality if, for all vertices u, v, w V, c(u, w) ≤ c(u, v) + c(v, w).
APPROX-TSP-TOUR(G, c)
1 select a vertex r G.V to be a “root” vertex
2 compute a minimum spanning tree T for G from root r using MST-PRIM(G,c,r)
3 let H be a list of vertices, ordered according to when they are first visited in a
preorder tree walk of T
4 return the hamiltonian cycle H
The following figure illustrates the operation of APPROX-TSP-TOUR. Part (a) of the
figure shows a complete undirected graph, and part (b) shows the minimum spanning tree
T grown from root vertex a by MST-PRIM. Part (c) shows how a preorder walk of T visits
the vertices, and part (d) displays the corresponding tour, which is the tour returned by
APPROX-TSP-TOUR. Part (e) displays an optimal tour, which is about 23% shorter.
APPROX-TSP-TOUR is a polynomial-time 2-approximation algorithm for the
traveling-salesman problem with the triangle inequality.