Mod 5 (Aad)
Mod 5 (Aad)
Mod 5 (Aad)
MODULE-5
INTRODUCTION TO COMPLEXITY THEORY
Tractable and Intractable Problems, Complexity Classes – P, NP, NP- Hard and
NP-Complete Classes- NP Completeness proof of Clique Problem and Vertex
Cover Problem- Approximation algorithms- Bin Packing, Graph Coloring.
Randomized Algorithms (Definitions of Monte Carlo and Las Vegas algorithms),
Randomized version of Quick Sort algorithm with analysis.
1
CST 306 AAD MOD V
Most intractable problems have an algorithm that provides a solution, and that
algorithm is the brute-force search.
This algorithm, however, does not provide an efficient solution and is, therefore,
not feasible for computation with anything more than the smallest input.
Examples
Towers of Hanoi: we can prove that any algorithm that solves this problem must
have a worst-case running time that is at least 2n − 1.
* List all permutations (all possible orderings) of n numbers.
2
CST 306 AAD MOD V
P CLASS
The P in the P class stands for Polynomial Time. It is the collection of decision
problems (problems with a “yes” or “no” answer) that can be solved by a
deterministic machine in polynomial time.
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.
Features:
1. The solution to P problems is easy to find.
2. P is often a class of computational problems that are solvable and tractable.
Tractable means that the problems can be solved in theory as well as in
practice. But the problems that can be solved in theory but not in practice are
known as intractable.
This class contains many natural problems like:
1. Calculating the greatest common divisor.
2. Finding a maximum matching.
3. Decision versions of linear programming.
NP CLASS
The NP in NP class stands for Non-deterministic Polynomial Time. It is the
collection of decision problems that can be solved by a non-deterministic machine
in polynomial time.
NP are problems that we have yet to find efficient algorithms in Polynomial
Time for, but given a solution we can verify that solution in polynomial time. It
has not been proved if these problems can be solved in polynomial time, or if they
would require super polynomial time
The class NP consists of those problems that are “verifiable” in polynomial time.
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. For example, in the Hamiltonian cycle problem, given a directed graph
G =(V,E) a certificate would be a sequence (v1,v2,….vn) of n vertices. We could
easily check in polynomial time that (vi,vi+1) ϵ E for i=1,2,….n-1 and that (vn,v1)
ϵE
The complexity class NP is the class of languages that can be verified by a
polynomial- time algorithm. A language L belongs to NP if and only if there
exist a two-input polynomial-time algorithm A and a constant c such that
Features:
1. The solutions of the NP class are hard to find since they are being solved by a
non-deterministic machine but the solutions are easy to verify.
2. Problems of NP can be verified by a Turing machine in polynomial time.
3
CST 306 AAD MOD V
Example:
Let us consider an example to better understand the NP class. Suppose there is a
company having a total of 1000 employees having unique employee IDs. Assume
that there are 200 rooms available for them. A selection of 200 employees must
be paired together, but the CEO of the company has the data of some employees
who can’t work in the same room due to some personal reasons.
This is an example of an NP problem. Since it is easy to check if the given choice
of 200 employees proposed by a coworker is satisfactory or not i.e. no pair taken
from the coworker list appears on the list given by the CEO. But generating such
a list from scratch seems to be so hard as to be completely impractical.
It indicates that if someone can provide us with the solution to the problem, we
can find the correct and incorrect pair in polynomial time. Thus for the NP class
problem, the answer is possible, which can be calculated in polynomial time.
This class contains many problems that one would like to be able to solve
effectively:
1. Boolean Satisfiability Problem (SAT).
2. Hamiltonian Path Problem.
3. Graph coloring.
Co-NP CLASS
Co-NP stands for the complement of NP Class. It means if the answer to a
problem in Co-NP is No, then there is proof that can be checked in polynomial
time.
Features:
1. If a problem X is in NP, then its complement X’ is also is in CoNP.
2. For an NP and CoNP problem, there is no need to verify all the answers at
once in polynomial time, there is a need to verify only one particular answer
“yes” or “no” in polynomial time for a problem to be in NP or CoNP.
Some example problems for C0-NP are:
1. To check prime number.
2. Integer Factorization.
NP-HARD CLASS
An NP-hard problem is at least as hard as the hardest problem in NP and it is
the class of the problems such that every problem in NP reduces to NP-hard.
We say that a decision problem Pi is NP-hard if every problem in NP is
polynomial time reducible to Pi. It means that Pi is ‘as hard as’ all the problems
in NP. If Pi can be solved in polynomial-time, then so can all problems in NP.
Features:
1. All NP-hard problems are not in NP.
2. It takes a long time to check them. This means if a solution for an NP-hard
problem is given then it takes a long time to check whether it is right or not.
4
CST 306 AAD MOD V
NP-COMPLETE CLASS
A problem is NP-complete if it is both NP and NP-hard. NP-complete problems
are the hard problems in NP.
We say that a decision problem Pi is NP-complete if it is NP-hard and it is also
in the class NP itself. NP-complete problems are set of problems that have been
proved to be in NP. That is, a nondeterministic solution is quite trivial, and yet
no polynomial time algorithm has yet been developed. If any of the problems can
be solved in polynomial time on a deterministic machine, then all the problems
can be solved in NP(Cook's Theorem). NP-complete problems form a set of
problems that may or may not be intractable.
Features:
1. NP-complete problems are special as any problem in NP class can be
transformed or reduced into NP-complete problems in polynomial time.
2. If one could solve an NP-complete problem in polynomial time, then one could
also solve any NP problem in polynomial time.
Some example problems include:
1. 0/1 Knapsack.
2. Hamiltonian Cycle.
3. Satisfiability.
4. Vertex cover.
5
CST 306 AAD MOD V
6
CST 306 AAD MOD V
7
CST 306 AAD MOD V
8
CST 306 AAD MOD V
9
CST 306 AAD MOD V
Thus, the graph G (V, E) is constructed such that – V = { <a, i> | a belongs to
Ci } and E = { ( <a, i>, <b, j> ) | i is not equal to j ; b is not equal to a’ }
Consider the subgraph of G with the vertices <x2, 1>; <x1’, 2>; <x3, 3>. It
forms a clique of size 3 (Depicted by dotted line in above figure) .
Corresponding to this, for the assignment – <x1, x2, x3> = <0, 1, 1> F
evaluates to true. Therefore, if we have k clauses in our satisfiability
expression, we get a max clique of size k and for the corresponding
assignment of values, the satisfiability expression evaluates to true. Hence,
for a particular instance, the satisfiability problem is reduced to the clique
decision problem. Therefore, the Clique Decision Problem is NP-Hard.
The Clique Decision Problem is NP and NP-Hard. Therefore, the Clique
decision problem is NP-Complete.
10
CST 306 AAD MOD V
Clique ≤ρ 3CNF
Proof: - As you know that a function of K clause, there must exist a Clique
of size k. It means that P variables which are from the different clauses
can assign the same value (say it is 1). By using these values of all the
variables of the CLIQUES, you can make the value of each clause in the
function is equal to 1
(1+1+0)(1+0+0)(1+0+1)
Clique ϵ NP:-
Proof: - As you know very well, you can get the Clique through 3CNF and to
convert the decision-based NP problem into 3CNF you have to first convert
into SAT and SAT comes from NP. So, concluded that CLIQUE belongs to
NP.
11
CST 306 AAD MOD V
Proof of NPC:-
12
CST 306 AAD MOD V
13
CST 306 AAD MOD V
14
CST 306 AAD MOD V
15
CST 306 AAD MOD V
APPROXIMATION ALGORITHMS
An approximation algorithm for a problem is a polynomial-time algorithm that,
when given input I, outputs an element of FS(I). A feasible solution(FS) is an
object of the right type but not necessarily an optimal one. FS(I) is the set of
feasible solutions for I.
Approximation algorithms are a method to solve NP complete optimization
problems, which are fast(polynomially bounded) algorithms that are not
guaranteed to give the best solutions but will give one that is closest to the
optimal.
BIN PACKING
How to pack or store objects of various sizes and shapes with a minimum of
wasted space.
Let S = (s1, …, sn), where 0 < si <= 1 for 1 <= i <= n. pack s1, …, sn into as
few bin as possible, where each bin has capacity one
Optimal solution for Bin Packing, considering all ways to partition S into n
or fewer subsets. There are more than (n/2)n/2 possible partitions.
Find a packing in unit-sized bins that minimizes the number of bins used.
16
CST 306 AAD MOD V
17
CST 306 AAD MOD V
Algorithm:
GRAPH COLORING
Graph coloring is the procedure of assignment of colors to each vertex of a graph
G such that no adjacent vertices get same color. The objective is to minimize the
number of colors while coloring a graph. The smallest number of colors required
18
CST 306 AAD MOD V
to color a graph G is called its chromatic number of that graph. Graph coloring
problem is a NP Complete problem.
Vertex coloring is the most common graph coloring problem. The problem is,
given m colors, find a way of coloring the vertices of a graph such that no two
adjacent vertices are colored using same color. The other graph coloring
problems like Edge Coloring (No vertex is incident to two edges of same color)
and Face Coloring (Geographical Map Coloring) can be transformed into vertex
coloring.
19
CST 306 AAD MOD V
Algorithm:
Input : G(V,E) an undirected graph where V={ v1,v2,v3,……………..,vn}
Output: A coloring of G
SeqColor(V,E)
int c,i;
for(i=1; i<=n; i++)
for(c=1;c<=n;c++)
if no vertex adjacent to Vi has color C
color Vi with C
break; // exit for C
// continue for C
// continue for i
20
CST 306 AAD MOD V
V is assigned color 3.
c) G with H(v) and V removed. ∆(G)=2<√13
Sequential coloring
21
CST 306 AAD MOD V
22
CST 306 AAD MOD V
23
CST 306 AAD MOD V
24