0% found this document useful (0 votes)
50 views76 pages

daa-unit-IV

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views76 pages

daa-unit-IV

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 76

Presentation Topic

Design and Analysis of Algorithms

Vidula V. Meshram
[email protected]
Department of Computer Engineering

BRACT’S, Vishwakarma Institute of Information Technology, Pune-48


(An Autonomous Institute affiliated to Savitribai Phule Pune University)
(NBA and NAAC accredited, ISO 9001:2015 certified)
Unit 4 - Intractable Problems and NP-
Completeness
Contents :

1. Time-Space tradeoff
2. Tractable and Non-tractable Problems
3. Polynomial and non-polynomial problems
4. Deterministic and non-deterministic algorithms
5. P-class problems, NP-class of problems
6. Polynomial problem reduction
7. NP complete problems- Vertex cover and 3-SAT
8. NP hard problem - Hamiltonian cycle

Department of Computer Engineering, VIIT, Pune-48


Objective of this session
1. Measure the performance of algorithms on the basis of time and space complexity.
2. To explain different algorithmic strategies for solving given problems.
3. To determine and solve the P and NP class of problem.
4. To learn randomized and approximation algorithm.
5. To understand the concepts of parallel and concurrent programming paradigm.

Learning Outcome/Course Outcome

After completion of the course, student will be able to


1. Analyze algorithms for their time and space complexities in terms of asymptotic performance(Analyze)
2. Implement greedy method or dynamic programming algorithmic strategy for solving given problems.(Apply)
3. Apply backtracking and branch and bound algorithmic strategy for solving given problems.(Apply)
4. Classify the given problem using principle of reducibility.(Analyze)
5. Understand randomized and approximation algorithm.(Understand)
6. Apply concepts of parallel and concurrent programming to given problem.(Apply)

Department of Computer Engineering, VIIT , Pune-48


Unit IV : Intractable Problems and NP-Completeness

Standard Complexity Classes :


Tractable vs Intractable Problems :
• We can distinguish problems between two distinct classes.
Problems which can be solved by a polynomial time algorithm
and problems for which no polynomial time algorithm is known.
• An algorithm for a given problem is said to be a polynomial time
algorithm if its worst case time complexity is O(n k), where k is a
fixed integer and n is size of a problem. For example, Sequential
search : O(n), Binary search : O(logn), Insertion sort : O(n 2),
product of two matrices : O(n3), Quick sort : O(nlogn) etc.

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness

Standard Complexity Classes :


Tractable vs Intractable Problems contd… :
• The set of all problems that can be solved in polynomial amount of time are
called “Tractable Problems”. These problems can be solved in a reasonable
amount of time for even very large amount of input data. Their worst case time
complexity is O(nk).
• The set of all problems that can not be solved in polynomial amount of time are
called “Intractable Problems”. Their worst case time complexity is O(kn).
These problems require huge amount of time for even modest input sizes. For
example, 0-1 Knapsack problem : O(2n), Traveling Salesperson Problem :
O(n22n)

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
Standard Complexity Classes : Tractable vs Intractable
Growth rates of few functions w.r.t. the input size ‘n’ :
Class of f(n) n 10 20 30 40
Problems →

1 1 1 1 1
logn 3.3 4.3 4.9 5.3
n 10 20 30 40
Tractable nlogn 33 86 147 212
Problems 100 400 900 1600
n2
Intractable 2n 1024 1 Million 1 Billion 1 Trillion
Problems (1Kilo)

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness

Standard Complexity Classes :


Deterministic vs Non-deterministic Algorithms :
• Deterministic Machines : Conventional Digital
machines are Deterministic in nature. Serialization
of resource access or sequential execution is the
basic concept used in these machines (Von
Neumann Architecture).
• Non-deterministic Machines : These are
hypothetical machines which can do the jobs in
parallel fashion i.e more than one jobs can be done
in one unit of time.

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
Standard Complexity Classes :
Deterministic vs Non-deterministic Algorithms contd … :
• Deterministic Algorithms : Algorithms in which the
result of any operation is uniquely defined are termed as
Deterministic Algorithms. All algorithms studied so far
are deterministic algorithms. Such algorithms agree with
the way programs are executed on a digital computer i.e.
a deterministic machine.
• Non-deterministic Algorithms : If we remove the
restriction on the outcome of every operation, then
outcomes are not uniquely defined but they are limited to
specified sets of possibilities. There is a termination
condition in such algorithms. Such algorithms are called
as non-deterministic algorithms.
Department of Computer Engineering, VIIT, Pune-48
Unit IV : Intractable Problems and NP-Completeness
Standard Complexity Classes :
Deterministic vs Non-deterministic Algorithms contd … :
Example-1 : Non-deterministic Linear Search Algorithm:
• Using Deterministic algorithm it takes O(n) time
• Using Non-deterministic algorithm it takes O(1) time.

Algorithm non-det-lin-search
1. j = choice (1:n)
2. If A[ j ] = x
{ write ( j ); success( );}
3. write (0); failure( );

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness

Standard Complexity Classes : Det. vs Non-det. Algo contd


Example-2 : Non-deterministic Sorting Algorithm :
• Using Deterministic algorithm it takes O(nlogn) time
• Using Non-deterministic algorithm it takes O(n) time.
Algorithm Non-det-sort (A, n)
// sorts n positive integers. B[ ] is an auxiliary array.
{ for i = 1 to n do // Initialize aux. array B
B[i] = 0;
for i = 1 to n do // Non-det step decides the rank of the
{ j = choice (1, n); // input number in unit time
if B[ j] ≠ 0 then failure ( );
B [ j] = A[i];
}
for i = 1 to n-1 do // verification
if B[i] > B[i+1] then failure( );
write (B[1:n]);
success ( );
} Department of Computer Engineering, VIIT, Pune-48
Unit IV : Intractable Problems and NP-Completeness
Standard Complexity Classes :
Decision Problem & Optimization Problem / Algorithm :
• Decision Problem : Any problem for which answer is
either 0 or 1 is called a decision problem and the
corresponding algorithm is referred as a decision
algorithm. For example : To search a given number
• Optimization Problem : Any problem that involves the
identification of an optimal (either min. or max.) value of
a given cost function is known as a optimization problem
and the corresponding algorithm is referred as an
optimization algorithm. For example, Knapsack problem,
Minimum cost spanning tree.

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
Standard Complexity Classes :
P vs NP class problems :
• P class : The class of decision problems that can be
solved in polynomial time using deterministic algorithms
is called the P class or Deterministic Polynomial
problems.
• NP class : The class of decision problems that can be
solved in polynomial time using non-deterministic
algorithms is called the NP class or Non-deterministic
Polynomial problems.
• Any P class problem can be solved using NP class
algorithm. Therefore P is contained in NP class
• Whether NP is contained in P is unknown.

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
Standard Complexity Classes :
P vs NP class problems contd …:
Examples :

Example P time complexity NP time complexity

Linear Search O(n) O(1)

Sorting O(nlogn) O(n)

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
Standard Complexity Classes :
NP-Complete problems :
• A decision problem D is said to be NP-Complete if
1. It belongs to NP class
2. Every problem in NP class is polynomially
reducible to D
• If one instance of such problem can be solved using a
polynomial algorithm, the complete class of problems
can be solved using a polynomial algorithm
• Examples : Traveling Salesperson Problem : optimal tour,
Printed circuit board problem,
Bin packing problem,
0-1 knapsack problem,
Vertex (node) cover problem
Department of Computer Engineering, VIIT, Pune-48
Unit IV : Intractable Problems and NP-Completeness
Standard Complexity Classes :
NP-Hard problems :
• A problem L is said to be NP Hard problem if and only if
satisfiability α L
• NP hard problems are basically the optimization versions
of the problems in NP complete class
• NP hard problems are not mere yes / no problems. They
are problems wherein we need to find the optimal
solution
• A problem L is NP complete if and only if L is NP hard
and L Є NP

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems : Reduction of problem
Reduction of L1 to L2 :

I of L1 I’ Solution for I
Poly. Time Algorithm Poly. Time
transform for L2 transform

L1α L2 or L1 ≤ p L2

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
Standard Complexity Classes :
NP-Hard problems contd … :
• Commonly believed relationship among P, NP,
NP- complete and NP-hard

• An example of NP-hard problem not in NP-complete :

Halting Problem : To determine for an arbitrary


deterministic algorithm A and an input I whether
algorithm A with input I ever terminates or enters an
infinite loop. This problem is undecidable. No algorithm
exists to solve this problem.

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
Standard Complexity Classes :
Time Complexity of a non-deterministic algorithm:
• The time required by a non-deterministic algorithm on any
given input is the minimum number of steps needed to reach
a successful completion if there exists a sequence of choices
leading to such a completion. In case successful completion
is not possible, then the time required is O(1).
• A non-deterministic algorithm is of complexity O(f(n)) if
for all inputs of size n, n>=n0, that results in a successful
completion, the time required is at most c.f(n) for some
constant c and n0.
Example : 0/1 Knapsack Decision Problem

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
Standard Complexity Classes :
Time Complexity of a non-deterministic algorithm:
Algorithm DKP(p,w,n,m,r,x)
{ W = 0; P = 0;
for i = 1 to n do
{ x[i] = choice (0, 1)
W = W + w[i]*x[i]; P = P + p[i] *x[i];
}
if ((W > m) or (P < r) then failure
else success();
}
Time Complexity of DKP = O(n).

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
Standard Complexity Classes :
Satisfiability :
• Literal : A literal is either a Boolean variable or its negation, Ex. a,⌐ a
• Clause : A clause is a literal or a disjunction of literals or a
conjunction of literals. Ex. (p V ⌐q V r), (p ٨ q ٨ r)
• CNF : Boolean formula is in Conjunctive Normal Form (CNF) iff it is
represented as ٨Ci , where Ci are clauses each represented as V lij
1<= i<= k 1<= j<= n

For example : (x3 V ⌐ x4) ٨(x1V ⌐ x2)


• DNF : Boolean formula is in Disjunctive Normal Form (DNF) iff it is
represented as VCi , where Ci are clauses each represented as ٨ lij
1<= i<= k 1<= j<= n

For example : (x1 ٨ ⌐ x2)V(x3 ٨ ⌐ x4)

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
Standard Complexity Classes :
Satisfiability :
• The Satisfiability Problem is to determine whether a Boolean formula
is true for some assignments of truth values to the variables.
• CNF-Satisfiability is the Satisfiability Problem for CNF formula.
Time Complexity (Deterministic algorithm ) for Satisfiability and
CNF-Satisfiability is O(2n), where ‘n’ is total number of Boolean
variables in the formula.
• It is easy to obtain a polynomial time non-deterministic algorithm that
terminates successfully if and only if a given propositional formula
E(x1,x2, …,xn) is satisfiable. Such an algorithm could proceed by
simply choosing (non-deterministically) one of the (2n) assignments
of truth values to (x1,x2, …,xn) and verifying that E(x1,x2, …,xn) is true
for that assignment.
• Satisfiability is NP-Hard since CIRCUIT-SAT α Satisfiability. . (1)
Department of Computer Engineering, VIIT, Pune-48
Unit IV : Intractable Problems and NP-Completeness
NStandard Complexity Classes :
Non-deterministic algorithm to solve Satisfiability problem
Algorithm Eval(E,n)
// Determine whether the propositional formula E is satisfiable
{ for i = 1 to n do
xi = choice(false, true);
if E(x1,x2, …,xn) then success()
else failure();
}
Analysis : Time Complexity = O(n) i.e. polynomial time. Therefore
“Satisfiability is in NP” ……….. (2)
• From (1) and (2) Satisfiability is NP-Complete.

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems : Reduction of problem
The strategy we adopt to show that a problem L2 is NP-hard is :
1. Pick a problem L1 already known to be NP-hard
2. Show how to obtain (in polynomial deterministic time) an
instance I’ of L2 from any instance I of L1 such that from the
solution of I’ we can determine (in polynomial deterministic
time) the solution to instance I of L1.
3. Conclude from step ( 2 ) that L1α L2.
4. Conclude from step (1) and (3) and the transitivity of α that L2 is
NP-hard.

All NP-hard decision problems that we shall discuss here are NP-
Complete and this can be shown by exhibiting a polynomial time
non-deterministic algorithm for L2
Department of Computer Engineering, VIIT, Pune-48
Unit IV : Intractable Problems and NP-Completeness
NP-Complete Problems : Proof by Reduction
Lemma : If L2 is a problem (language) such that L1 α L2
for some L1 Є NP-Complete (or NPC) then L2 is
NP-Hard. Further if L2 Є NP then L2 Є NPC
The structure of NP-completeness proofs:
First NP-Complete Problem CIRCUIT-SAT

SAT

CNF-SAT

CLIQUE SUBSET-SUM

VERTEX-COVER

HAM-CYCLE

TSP
Department of Computer Engineering, VIIT, Pune-48
Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
To prove : 1) CIRCUIT-SAT α Satisfiability or
2) Satisfiability is NP-complete
Proof :
Consider the following electronic digital circuit.
x1 x5
OR
x2
x6 OR

NOT x8
x6 x9 x10
OR
X3 x4 AND
NOT x4 AND x7

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
To prove : 1) CIRCUIT-SAT α Satisfiability or
2) Satisfiability is NP-complete
Proof contd… :
Reducing circuit satisfiability to formula satisfiability:

F = x10 ٨ (x4 ↔ ¬ x3))


٨ (x5 ↔ (x1 V x2))
٨ ( x6 ↔ ¬ x4 )
٨ ( x7 ↔ x1 ٨ x2 ٨ x4)
٨ ( x8 ↔ (x5V x6))
٨ ( x9 ↔ (x6 V x7))
٨ ( x10 ↔ (x8 ٨ x9 ٨ x7))

Therefore CIRCUIT-SAT α Satisfiability.

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
To prove : 1) CIRCUIT-SAT α Satisfiability or
2) Satisfiability is NP-complete
Proof contd… :
In general Boolean formula will have n variable (literals) and to find
legal values of n variables for Ф to be true, needs to evaluate the
formula for every permutation of n values which is = O(2 n), Thus it
will take super-polynomial time to find all the permutations for which
the formula is true and there is no polynomial time algorithm exists.

We know that CIRCUIT-SAT is NP-Hard and we have reduced


CIRCUIT-SAT to Satisfiability i.e. CIRCUIT-SAT α Satisfiability .
Therefore Satisfiability is NP-Hard. We know that Satisfiability is in
NP. Hence Satisfiability is NP-Complete as well.

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
To prove : 1) Satisfiability α 3-CNF-SAT or
2) 3-CNF Satisfiability is NP-complete
Proof (1) : We divide the proof in three parts. Let Boolean formula,
Ф = ((x1 → x2) V ¬((¬x1 ↔ x3) V x4)) ٨ ¬x2.
a) First step : Construct a binary “parse” tree for the formula Ф.
y1
٨
y2
y3 V y4 ¬ x2
¬

x1 x2 y5 Introduce a variable (y) for
y6 V the output of each internal node
x4

¬ x1 x3

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
• Rewrite the original formula Ф as the AND of root variable and a
conjunction of clauses describing the operation of each node.
Ф’ = y1 ٨ ( y1 ↔ (y2 ٨ ¬ x2))
٨ ( y2 ↔ (y3 V ¬ y4))
٨ ( y3 ↔ (x1 → x2))
٨ ( y4 ↔ ¬ y5)
٨ ( y5 ↔ (y6 V x4))
٨ ( y6 ↔ (¬ x1 ↔ x3))
• The formula Ф’ thus obtained is a conjunction of clauses Ф’ i , each of
which has at most 3 literals
b) Second step : Converts each clause Ф’i into CNF. For this we
construct a truth table for each Ф’i by evaluating all possible
assignments to its variables. Each row of the truth table consists of a
possible assignments of the variables of the clause, together with the
value of clause under of
Department theComputer
assignment. For example,
Engineering, VIIT, Pune-48
Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
• Consider the formula ( y1 ↔ (y2 ٨ ¬ x2))
y1 y2 x2 ( y1 ↔ (y2 ٨ ¬ x2))
-------------------------------------------------
1 1 1 0
1 1 0 1
1 0 1 0
1 0 0 0
0 1 1 1
0 1 0 0
0 0 1 1
0 0 0 1
• For the truth table entries that evaluate to 0 (4 entries in above
table), we build a formula in DNF – an OR of ANDs that is
equivalent to ¬ Ф’i
Department of Computer Engineering, VIIT, Pune-48
Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
• ¬ Ф’1 = (y1 ٨ y2٨ x2) V (y1 ٨ ¬ y2٨ x2) V (y1 ٨ ¬ y2٨ ¬ x2)
V
(¬ y1 ٨ y2٨ ¬ x2)
• Negate ¬ Ф’1 and apply DeMorgans’s laws to get Ф’’1
DeMorgan’s Laws : ¬ (a ٨ b) = ¬a V ¬b and
¬ (a V b) = ¬a ٨ ¬b
Ф’’1 = (¬ y1 V ¬ y2V ¬x2) ٨ (¬ y1 V y2 V ¬ x2) ٨
(¬ y1 V y2 V x2) ٨ (y1 ٨ ¬y2 ٨ x2) = original Ф’1
• At this point we have converted each clause Ф’i of the formula Ф’ into
a CNF formula Ф’’ and thus Ф’ is equivalent to the CNF formula Ф’’
consisting of the conjunction of the Ф’’i . Moreover each clause of Ф’
has at most 3 literals.
c) Third Step : This final step of reduction further transforms the
formula so that each clause has exactly 3 literals. We construct the
final 3-CNFDepartment
formula Ф’’’ from theEngineering,
of Computer clauses of VIIT,
the CNF formula Ф’’ .
Pune-48
Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
• The formula Ф’’’ also uses two auxiliary variables that we shall call
p and q. For each clause Ci of Ф’’ , we include the following clauses
in Ф’’’ :
if Ci has 3 distinct literals, then include Ci as a clause of Ф’’’.
if Ci has 2 distinct literals, then replace
Ci = ( l1 V l2) = ( l1 V l2 V p) ٨ ( l1 V l2 V ¬ p)
 if Ci has 1distinct literals, then replace
Ci = ( l1 ) = (l1 V p V q) ٨ (l1 V p V ¬ q) ٨
(l1 V ¬ p V q) ٨ (l1 V ¬ p V ¬ q)
• Thus 3-CNF formula Ф’’’ is satisfiable if and only if Ф is satisfiable.
• Reduction is computable in polynomial time. Step 1 introduces one
variable in each clause of Ф. Step 2 introduces at most 8 clauses for
each clause of Ф’ (truth table for each clause has at most 8 rows).
Step 3 introduces at most 4 clauses for each clause of Ф’’. Thus the
size of resulting formula Ф’’’ is polynomial in the length of the
Department of Computer Engineering, VIIT, Pune-48
Unit IV : Intractable Problems and NP-Completeness

NP-Hard Problems :
• Thus the instance, I of satisfiability is converted into an instance I’ in 3-CNF form in
polynomial time.
• Therefore we can say that Satisfiability α 3-CNF satisfiability.
Part 2 :
• But we also know that Satisfiability is NP-Hard, therefore we can say that 3-CNF
Satisfiability is also NP-Hard.
• We can verify in polynomial time the certificate of 3-CNF satisfiability. Hence 3-CNF
Satisfiability is NP as well
• Hence 3-CNF Satisfiability is NP-Complete.

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness

NP-Hard Problems :
Clique Decision Problem (CDP) :
• A maximal complete sub-graph of a graph G = (V, E) is a clique.
• The size of the clique is the number of vertices in it.
• Maximum Clique Problem is an optimization problem that has to determine the
size of a largest clique in G.
• The corresponding Decision Problem is to determine whether G has a clique of
size at least k for some given k.
• Let DClique(G, k) be a deterministic decision algorithm for the clique decision
problem. If the number of vertices in G is n, the size of max. clique in G can be
found by making several applications of Dclique. Dclique is used once for each k,
k = n, n-1, n-2, … until the output from Dclique is 1.

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness

NP-Hard Problems :
Clique Decision Problem (CDP) :
• If the time complexity of Dclique is f(n), then the size of max clique can be found
in time ≤ n.f(n). Also if the size of max clique can be determined in time g(n), then the
decision problem can be solved in time g(n).
• Hence max clique problem can be solved in polynomial time iff clique decision problem can
be solved in polynomial time.
• There is no polynomial time deterministic algorithm for this problem. A naive
algorithm for determining whether a graph G = (V. E) with |V| vertices has a
clique of size k is to list all k-subsets of V, and check each one to see whether it
forms a clique. The running time of this algorithm is Ω(k 2C(|V|, k) which is
polynomial if k is constant. However in general, k could be near |V| / 2, in which
case the algorithm runs in super-polynomial time. Indeed an efficient algorithm for the
clique problem is unlikely to exist.

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness

NP-Hard Problems :
Clique Decision Problem (CDP) :
Applications :
• Patterns in telecommunications traffic
• Design of error-correcting codes
• Fault diagnosis on large multiprocessor systems
• Tiling geometry problems (Keller’s conjecture)
• Computer Vision and Pattern Recognition
• Graph theoretic approaches have also found widespread
application in modern Biological Research

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness

NP-Hard Problems :
Clique Decision Problem (CDP) : Non-deterministic algorithm
Algorithm CDP(G, n, k)
// G is an adjacency matrix, |V| = n and clique size is k
// Algorithm finds whether there exists a clique of size ≥ k in a graph G
{ S = Ф; // initially S is empty
for (i=1; i ≤ k; i++)
{ t = Choice (1, n);
if (t Є S) Failure();
S = S U {t} // add t to S
}
// At this point S contains k distinct vertices
for (all pairs (i, j) such that vertices i and j) Є S and i ≠ j)
if ((i, j) is not an edge of G then Failure();
Success();
} // In general if k = (n/2),
Time Complexity is O((n/2)+(n/2)2) = O(n2) = O(m), m = input length.

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness

NP-Hard Problems :
Clique Decision Problem (CDP) :
Theorem : CNF-satisfiability α Clique Decision problem.
Proof :
Let F = ٨ Ci be a propositional formula in CNF. Let xi be the
1<= i<= k

variable in F such that 1 ≤ i≤ n. We can construct a graph G = (V, E), from F such that G has a clique of
size at least k iff F is satisfiable.
For any F, G = (V, E) is defined as follows:
V = {<σ, i> | σ is a literal in clause Ci } and
E = {(<σ, i>, < δ, j> ) | i ≠ j, and σ ≠ δ}
Example : Consider F = (x1 V x2 V x3) ٨ (¬ x1 V ¬ x2 V ¬ x3)
F is true when x1 = true and x2 = false

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness

NP-Hard Problems :
Clique Decision Problem (CDP) :
Theorem : CNF-satisfiability α Clique Decision problem.
Proof :
Let F = ٨ Ci be a propositional formula in CNF. Let xi be the
1<= i<= k

variable in F such that 1 ≤ i≤ n. We can construct a graph G = (V, E), from F such that G has a clique of
size at least k iff F is satisfiable.
For any F, G = (V, E) is defined as follows:
V = {<σ, i> | σ is a literal in clause Ci } and
E = {(<σ, i>, < δ, j> ) | i ≠ j, and σ ≠ δ}
Example : Consider F = (x1 V x2 V x3) ٨ (¬ x1 V ¬ x2 V ¬ x3)
F is true when x1 = true and x2 = false

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
• Consider the formula F
x1 x2 x3 F = (x1 V x2 V x3) ٨ (¬ x1 V ¬ x2 V ¬ x3)
---------------------------------------------------------------------------
1 1 1 0
1 1 0 1
1 0 1 1
1 0 0 1
0 1 1 1
0 1 0 1
0 0 1 1
0 0 0 0
• All six edges of the graph have clique of size 2.

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
Clique Decision Problem (CDP) :
Theorem : CNF-satisfiability α Clique Decision problem.
Proof contd: Graph G:

< x1,1> < ¬ x1, 2>


A D

< x2,1> B E < ¬ x2, 2>

C F
< x3,1> < ¬ x3, 2>

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
Clique Decision Problem (CDP) :
Claim : F is satisfiable if and only if G has a clique of size ≥ k.
Proof :
1. If F is satisfiable, then there is a set of truth values for x i,
1≤i ≤n, such that each clause is true with this assignment.
Thus with this assignment there is at least one literal σ in
each Ci such that σ is true. Let S = {<σ, i> | σ is true in
clause Ci} be a set containing exactly one <σ, i> for each i.
Between any two nodes <σ, i> and < δ, j> in S there is an
edge in G, since i ≠ j, and both σ and δ (σ ≠ δ) have the
value true. Thus S forms a clique in G of size k.

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
Clique Decision Problem (CDP) :
Claim : F is satisfiable if and only if G has a clique of size ≥ k.
Proof contd. :
2. If G has a clique K = (V’, E’) of size k, then let
S ={<σ, i> | <σ, i> Є V‘}. Clearly |S| = k as G has no clique
of size more than k. Furthermore, if S’ ={σ | <σ, i> Є S for
some i}, then S’ cannot contain both a literal δ and its
complement ¬ δ as there is no edge connecting < δ, j> and
< ¬ δ, j> in G. Hence by setting xi = true if xi Є S’ and
xi = false if ¬xi Є S’ and choosing arbitrary truth values for
variables not in S’, we can satisfy all clauses in F.
Hence the proof of claim.

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
Node Cover Decision Problem (NCDP):
A set S, a subset of V, is a node cover for graph G = (V, E) if
and only if all edges in E are incident to at least one vertex
in S. The size |S| of the cover is the number of vertices in S.
Example: Diagram
S = {2, 4} is a node cover of size 2.
S = {1, 3, 5} is a node cover of size 3.

In the node cover decision problem we are given a graph G and


an integer k. We are required to determine whether G has a node
cover of size at most k (i.e. ≤ k)

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
Node Cover Decision Problem (NCDP):
Theorem : The Clique Decision Problem α the Node Cover Decision
Problem or the vertex cover problem is NP-Complete.
Proof :
• Let G = (V, E) and k define an instance of CDP. Assume that |
V| = n.
• We construct a graph G’ such that G’ has a node cover of size at
most n – k if and only if G has a clique of size at least k. Graph
G’ = (V, E’) where E’ = {(u, v) | u, v Є V and (u, v) does not Є
E}. The set G’ is known as complement of G.
• Now let us show that G has a clique of size at least k if and only
if G’ has a node cover of size at most n – k.
• Let K be any clique in G. Since there are no edges in E’
connecting vertices in K, the remaining n - |k| vertices in G’ must
cover all edges in E’.
Department of Computer Engineering, VIIT, Pune-48
Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
Node Cover Decision Problem (NCDP):
Proof contd. :
• Similarly if S is a node cover of G’, then V – S must form a
complete sub-graph in G.
• Since G’ can be obtained from G in polynomial time, CDP can be
solved in polynomial deterministic time if we have a polynomial
time deterministic algorithm for NCDP.
• Hence the Clique Decision Problem α the Node Cover Decision
problem.

We know that CNF-satisfiability α CDP, CDP α NCDP and α is


transitive, it follows that CNF-satisfiability α NCDP. So NCDP is
NP-hard. It is also in NP because we can non-deterministically choose
a subset C of V of size k and verify in polynomial time that C is a
cover of G. So NCDP is NP-complete.
Example / Diagram
Department of Computer Engineering, VIIT, Pune-48
Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
Node Cover Decision Problem (NCDP):
Example : Reducing CLIQUE to VERTEX-COVER
a) An undirected graph G = (V, E) with clique V’ = {u, v, x, y}

u v

w
z

y x

Department of Computer Engineering, VIIT, Pune-48


Unit IV : Intractable Problems and NP-Completeness
NP-Hard Problems :
Node Cover Decision Problem (NCDP):
Example : Reducing CLIQUE to VERTEX-COVER
b) An undirected graph G’ = (V, E’) with node cover V-V’ = {w, z}

u v

w
z

y x

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

 A Hamiltonian cycle in a graph is a cycle that visits each vertex exactly once

 Problem Statement

Given A directed graph G = (V,E)

To Find If the graph contains a Hamiltonian cycle

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

Hamiltonian Cycle Problem is NP-Complete

Hamiltonian Cycle Problem is in NP.


Given a directed graph G=(V,E), and a certificate
containing an ordered list of vertices on a Hamiltonian
Cycle. It can be verified in polynomial time that the list
contains each vertex exactly once and that each
consecutive pair in the ordering is joined by an edge.

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

Hamiltonian Cycle Problem is NP Hard

3-SAT ≤p Hamiltonian Cycle

We begin with an arbitrary instance of 3-


SAT having variables x1,…….,xn and
clauses C1,…….,Ck

We model one by one, the 2n different ways in which


variables can assume assignments, and the
constraints imposed by clauses.

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

To correspond to the 2n truth assignments,


we describe a graph containing 2n different
Hamiltonian cycles.

The graph is constructed as follows:

Construct n paths P1,…….,Pn.


Each Pi consists of nodes vi1,….., vib,

where b = 2k (k being the number of clauses)

Department of Computer Engineering, VIIT, Pune-48


………………. P1

………………. P2

………………. P3

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

Draw edges from vij to vi,j+1

………………. P1

………………. P2

………………. P3

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

Draw edges from vi,j+1 to vi,j

………………. P1

………………. P2

………………. P3

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

For each i = 1,2,……..,n-1, define edges from vi1 to vi+1,1 and to vi+1,b.
Also, define edges from vib to vi+1,1 and vi+1,b

………………. P1

………………. P2

………………. P3

Department of Computer Engineering, VIIT, Pune-48


Add two extra nodes s and t. Define edges from s to v 11 and v1b, from
vn1and vnb to t, and from t to s

………………. P1

………………. P2

………………. P3

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

Observations:
 Any Hamiltonian cycle must use the edge (t,s)
 Each Pi can either be traversed left to right or
right to left. This gives rise to 2 n Hamiltonian
cycles.
 We have therefore modeled the n independent
choices of how to set each variable; if P i is
traversed left to right, xi=1, else xi=0

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

We now add nodes to model the clauses

Consider the clause C = (x1’ν x2 ν x3’)

What is the interpretation of the clause?

The path P1 should be traversed right to left, or P2 should be traversed


left to right, or P3 right to left.

Department of Computer Engineering, VIIT, Pune-48


We add a node that does this

s C

………………. P1

………………. P2

………………. P3

t
Department of Computer Engineering, VIIT, Pune-48
Hamiltonian Cycle Problem

In general
We define a node cj for each clause Cj.
In each path Pi, positions 2j-1 and 2j are reserved for
variables that participate in clause Cj
If Cj contains xi, add edges (vi,2j-1 , cj)and (cj , vi,2j)
If Cj contains xi’, add edges (vi,2j , cj)and (cj, vi,2j-1)

Gadget constructed !

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

Consider an instance of 3-SAT having 4 variables : x1, x2, x3, x4

3 clauses C1 : (x1 v x2 v x3’)


C2 : (x2’v x3 v x4)
C3 : (x1’v x2 v x4’)

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

We reduce the given instance as follows:


n = 4, k = 3 b= 2*3 = 6

Construct 4 paths P1, P2, P3, P4

P1 consists of nodes v1,1, v1,2 ,…….., v1,6

P2 consists of nodes v2,1, v2,2 ,…….., v2,6

P3 consists of nodes v3,1, v3,2 ,…….., v3,6

P4 consists of nodes v4,1, v4,2 ,…….., v4,6

Department of Computer Engineering, VIIT, Pune-48


C1
s

1 2 3 4 5 6 P1

1 2 3 4 5 6 P2

1 2 3 4 5 6 P3

1 2 3 4 5 6 P4

t
C3
C2

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem
Claim: 3-SAT instance is satisfiable if and only if
G has a Hamiltonian cycle
Proof: Part I
Given A satisfying assignment for the 3-SAT instance
If xi = 1, traverse Pi left to right, else right to left.
Since each clause Cj is satisfied by the assignment,
there has to be at least one path Pi that moves in the
right direction to be able to cover node cj. This Pi can be
spliced into the tour there via edges incident on vi,2j-1 and
vi,2j

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

Let us try to verify this with our example

Given A satisfying assignment for 3-SAT, say x1 = 1 x2 = 0 x3 =


1 x4 = 0

Let us check out a corresponding Hamiltonian cycle

Department of Computer Engineering, VIIT, Pune-48


C1
s

1 2 3 4 5 6 P1

1 2 3 4 5 6 P2

1 2 3 4 5 6 P3

1 2 3 4 5 6 P4

t
C3
C2

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

Part II

Given A Hamiltonian cycle in G.

Observe that if the cycle enters a node cj on an edge from vi,2j-1 it


must depart on an edge to vi,2j.

Why?

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

Because otherwise, the tour will not be able to cover this


node while still maintaining the Hamiltonian property

Similarly, if the path enters from vi,2j, it has to depart


immediately to vi,2j-1.

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

However, in some situations it may so happen that the path


enters cj from the first (or last) node of Pi and departs at the
first (or last) node of Pi+1.
In either case the following holds true:

The nodes immediately before and after any c j


in the cycle are joined by an edge in G, say e.
Let us consider the following Hamiltonian cycle
given on our graph

Department of Computer Engineering, VIIT, Pune-48


C1
s

1 2 3 4 5 6 P1

1 2 3 4 5 6 P2

1 2 3 4 5 6 P3

1 2 3 4 5 6 P4

t
C3
C2

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

Obtain a Hamiltonian cycle on the subgraph G – {c1,……ck}


by removing cj and adding ‘e’ as shown below

Department of Computer Engineering, VIIT, Pune-48


C1
s

1 2 3 4 5 6 P1

1 2 3 4 5 6 P2

1 2 3 4 5 6 P3

1 2 3 4 5 6 P4

t
C3
C2

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

We now use this new cycle on the subgraph


to obtain the truth assignments for the
3-SAT instance.

If it traverses Pi left to right, set xi =1, else


set xi = 0.

We therefore get the following assignments:


x1 = 1 x2 = 0 x3 = 0 x4 = 1

Department of Computer Engineering, VIIT, Pune-48


Hamiltonian Cycle Problem

Can we claim that the assignment thus determined would satisfy


all clauses.

YES !

Since the larger cycle visited each clause node cj, at least one Pi
was traversed in the right direction relative to the node cj

Department of Computer Engineering, VIIT, Pune-48


References

 Introduction to Algorithms 3rd Edition by Clifford


Stein, Thomas H. Cormen, Charles E. Leiserson,
Ronald L. Rivest

 Horowitz and Sahani, Fundamentals of


Computer Algorithms

Department of Computer Engineering, VIIT, Pune-48

You might also like