0% found this document useful (0 votes)
12 views51 pages

NP Complete

Uploaded by

Dhiman Paul
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)
12 views51 pages

NP Complete

Uploaded by

Dhiman Paul
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/ 51

Design and Analysis of Algorithm

School of Computer Engineering


KALINGA INSTITUTE OF INDUSTRIAL
TECHNOLOGY

NP Completeness

Biswajit Sahoo,
KIIT
Course
Contents
2

Sr # Major and Detailed Coverage Hrs


Area
1 NP Completeness 4

 Defination of P, NP, NP Complete,


NP Hard
 3-CNF Satisfiability Problem
 Clique Decision Problem
 Hamiltonian Cycle
 TSP
 Sum of Subset
Graph Coloring
Biswajit Sahoo, KIIT
Contents of Discussion
3

 Intractabe Problem
 Nondeterministic Algorithm
 P and NP Defination
 Optimization & Decision Problem
 Verification of Problem
 Reducibility
 NP Complete & NP Hard Defination
 Cook’s Theorem
 Examples of NP Complete
 Clique Decision Problem
 Hamiltonian Cycle, TSP
 Sum of Subset, Graph Coloring
Biswajit Sahoo, KIIT
Tractability
4

Tractable problems: Intractable problems:

Polynomial time Super Polynomial time

O(n2), O(n3), O(1),O(nlg n) O(2n), O(n 2n), O(nn), O(n!)

Ex:- O(n3); for n=100 Ex:- O(2n); for n=100


Number of steps = 10,00,000 Number of steps ≈90,....,00

Biswajit Sahoo, KIIT


Prove
Tractability
5

Tractable problems: Intractable problems:

Polynomial time Super Polynomial time

O(n2), O(n3), O(1),O(nlg n) O(2n), O(n 2n), O(nn), O(n!)

O(n100) O(n 2n)


High order Polynomial (for n=10, small input)

Biswajit Sahoo, KIIT


Disprove
Tractability
6

Tractable problems: Intractable problems:

Polynomial time algorithms Super Polynomial time


are Tractable Normally algorithms are
Intractable in General

Not applicable for: Not applicable for:


Higher order Polynomial Small inputs

Biswajit Sahoo, KIIT


Polynomial Time Nondeterministic
7 Algorithm

int Search(a, n, key)


{ Running Time
i=choice(1 : n) //O(1) : Nondeterministic

if(key==a[i]) //1
return(i); //1
else
return(-1); //1
} Total Running time=O(1)

Assume time required for choice(1 : n) is


O(1).
Biswajit Sahoo, KIIT
P and NP
8

 P is set of problems that can be solved in


polynomial time in a deterministic machine

 NP (nondeterministic polynomial time) is


the set of problems that can be solved in
polynomial time by a nondeterministic
machine
A non-deterministic computer is a computer
that magically “guesses” a solution, then has
to verify that it is correct.
Is P = NP ?
Biswajit Sahoo, KIIT
P and NP
9

NP

Today nondeterministic, Tomorrow may be deterministic


Biswajit Sahoo, KIIT
Optimization and Decision Problems
10

Optimization Problem: Maximize profit or Minimize


Loss
Decision Problem: Answers are in Boolean (Yes/No)

Optn: Find MCST of the graph, G.


Decn: Is there any MCST exist in G with cost
less than k?

Optn: Find TSP (optimal) of the graph, G.


Decn: Is there any TSP exist in G with cost less
than k?

n
Biswajit Sahoo, KIIT
Optimization and Decision Problems
11

In fact, from the point of view of polynomial-


time solvability, there is not a significant
difference between the optimization (maximize
or minimize) version of the problem and the
decision version (decide, yes or no).

Given a method to solve the optimization


version, we automatically solve the decision
version as well.

Biswajit Sahoo, KIIT


Optimization and Decision Problems
12

Solution to decision problems takes a fraction


of time more than solution to optimization
problems.(condn check).

NP completeness is proved directly w.r.t.


decision problems. However, same
computational complexity will also be
applicable to the optimization problems.

Biswajit Sahoo, KIIT


Verification of Decision Problems
13

Ex:- TSP, Formula Satisfiability, 0/1


Knapsack...

Given a solution (guess) to the problem, we


neeed to verify it in the problem

NP problems are Verifiable in polynomial


time in deterministic machine

Biswajit Sahoo, KIIT


Verification of Decision TSP in P
14

3 4

5 8

1, 5, 3, 4, 8, 6, 9, 2, 7, 1 6
Source 1 Verified in P

9
7
2
Biswajit Sahoo, KIIT
Reducibility
15

The crux of NP-Completeness is


Reducibility

Informally, a problem L can be reduced to


another problem Q if any instance of L can be
“easily rephrased” as an instance of Q, the
solution to which provides a solution to the
instance of L

Intuitively: If L reduces to Q, L is “no harder


to solve” than Q
Biswajit Sahoo, KIIT
Reducibility Examples
16

Given an equation ax2 + bx + c = 0, find


roots of the equation.
Question: 5x + 6 = 0;
P
0.x2 + 5x + 6
=0
Question: Find the value of (452 + 462)
Apply Pithagoras Theorem: P
Draw a right angle triangle with p=45, b=46, Measure h

Biswajit Sahoo, KIIT


Reducibility Examples
17

X: Given n integers, is the largest integer > 0 ?


Y: Given n Boolean variables, is there at least one TRUE?
X -1 -49 -4 5 1 0 -6 8 -20
Y F F F T T F F T F
Transform X to Y by yi = T if xi > 0, yi = F if xi <= 0

Time required Time required to + Time required


=
to test X reduce X to Y to test Y
Now, X is Polynomial-time Reducible to Y,
So, we denote this X p Y
Reducibility relation
18

Yes for A
Yes
α Poplynomial β
Algorithm for B
Reduction No
No for A
Decision Algorithm for A

A and B are two decision Problems


If decision algorithm for B is polynomial so does A
A is no harder than B
If A hard ( e.g. NPC) so does B
Biswajit Sahoo, KIIT
Transitive property of Reducibility
19

X, Y and Z are three problems;

X is Polynomial-time Reducible to Y, X p Y and


Y is Polynomial-time Reducible to Z, Y p Z

Now, X p Z; X is Polynomial-time Reducible to Z

Biswajit Sahoo, KIIT


Computational Relationship
20

NP-Complete problems are computationaly related:

 If any one NP-Complete problem can be solved in


polynomial time…
 …then every NP-Complete problem can be solved in
polynomial time…
 …and in fact every problem in NP can be solved in
polynomial time (which would show P = NP)

Biswajit Sahoo, KIIT


P & NP Defination
21

P = problems that can be solved in polynomial time


(quick solution)

NP = problems for which a solution can be verified in


polynomial time (quick verification)

Unknown whether P = NP ? (most suspect not)

Biswajit Sahoo, KIIT


NP Complete & NP Hard Defination
22

NP-Complete NP-Hard

Problem L is NP Complete, if Problem L is NP Hard if


 L  NP and
 R p L  R  NP R p L  R  NP

Problem L is NP Complete, if Problem L is NP Hard if


 L  NP and
 R p L for any R  NPC R p L for any R  NPC

Biswajit Sahoo, KIIT


NP-Complete Problems
23

Formula SAT

3Coloring 3-CNF SAT Clique Decision

Sum of Subset Hamiltonian Cycle

TSP

Biswajit Sahoo, KIIT


Maximum Clique Problem
24

Max clique problem: A complete subgraph of a graph is a


clique.

Number of edges in a complete graph, G=(V,E) is


|E|= |V|x|V-1|/2

The maximal clique problem is to determine the size of a


largest clique in G.

Biswajit Sahoo, KIIT


Maximum Clique Problem
25

1 2 1 2

Clique of size 2

4 3 4 3

Biswajit Sahoo, KIIT


Maximum Clique Problem
26

1 2 2

4 3 4 3
Clique of size 3
4 3
5

Biswajit Sahoo, KIIT


Maximum Clique Problem
27

4 2

6 5

Biswajit Sahoo, KIIT


Maximum Clique Problem
28

4 2

3 Clique of size 4

6 5

Biswajit Sahoo, KIIT


Clique Decision Problem (CDP)
29

Is there a clique of size 3 in the graph?

Clique Decision Problem (CDC) is in NP-Complete?


i. CDC  NP and
ii. 3-CNF SAT p CDC

if the formula is satisfiable then the graph has a Clique of


size 3.

Biswajit Sahoo, KIIT


Verification of CDP
30

CDC  NP 1

4 2

3 Clique of size 3 (1, 2, 4)


Verified in polynomial time

6 5

Biswajit Sahoo, KIIT


Clique Decision Problem (CDP)
31

3-CNF SAT p CDP

φ = (V x2 V ) Λ (x1 V V x3) Λ (x1 V x2 V ) p Gφ


C1C2 C3

The formula is satisfiable iff the graph Gφ has a Clique of


size 3.

Biswajit Sahoo, KIIT


φ = (V x2 V ) Λ (x1 V V x3) Λ (x1 V x2 V )
C1C2 C3

X1 x2 X3

x3 X1

X2 X2

x1 x3
V = { <a, i> | a ∈ Ci }
Biswajit Sahoo, KIIT E= ( <a, i><b, j>) | b ≠ , i ≠ j
φ = (V x2 V ) Λ (x1 V V x3) Λ (x1 V x2 V )
C1C2 C3

X1 x2 X3

x3 X1

X2 X2

x1 x3
V = { <a, i> | a ∈ Ci }
Biswajit Sahoo, KIIT E= ( <a, i><b, j>) | b ≠ , i ≠ j
φ = (V x2 V ) Λ (x1 V V x3) Λ (x1 V x2 V )
C1C2 C3

X1 x2 X3

x3 X1

X2 X2

x1 x3
V = { <a, i> | a ∈ Ci }
Biswajit Sahoo, KIIT E= ( <a, i><b, j>) | b ≠ , i ≠ j
φ = (V x2 V ) Λ (x1 V V x3) Λ (x1 V x2 V )
C1C2 C3

X1 x2 X3

x3 X1

X2 X2

x1 x3
V = { <a, i> | a ∈ Ci }
Biswajit Sahoo, KIIT E= ( <a, i><b, j>) | b ≠ , i ≠ j
φ = (V x2 V ) Λ (x1 V V x3) Λ (x1 V x2 V )
C1C2 C3

X1 x2 X3

x3 X1

X2 X2

x1 x3
V = { <a, i> | a ∈ Ci }
Biswajit Sahoo, KIIT E= ( <a, i><b, j>) | b ≠ , i ≠ j
φ = (V x2 V ) Λ (x1 V V x3) Λ (x1 V x2 V )
C1C2 C3

Assignment:
X1 x2 X3
x1=0, x2=1, x3=1

x3 X1

X2 X2

x1 x3

Biswajit Sahoo, KIIT


φ = (V x2 V ) Λ (x1 V V x3) Λ (x1 V x2 V )
C1C2 C3

Assignment:
X1 x2 X3
x1=0, x2=1, x3=1
TRUE
x3 X1

X2 X2

x1 x3

Biswajit Sahoo, KIIT


φ = (V x2 V ) Λ (x1 V V x3) Λ (x1 V x2 V )
C1C2 C3
Assignment:
x1=0, x2=1, x3=1
X1 x2 X3 Satisfiable
Clique of size 3
x3 X1

X2 X2

x1 x3

Biswajit Sahoo, KIIT


φ = (V x2 V ) Λ (x1 V V x3) Λ (x1 V x2 V )
C1C2 C3
Assignment:
x1=0, x2=1, x3=1
X1 x2 X3 Satisfiable
Clique of size 3
x3 X1

X2 X2

x1 x3

Biswajit Sahoo, KIIT


φ = (V x2 V ) Λ (x1 V V x3) Λ (x1 V x2 V )
C1C2 C3
Assignment:
x1=1, x2=0, x3=1
X1 x2 X3 FALSE
Clique of size 3 ?
x3 X1

X2 X2

x1 x3

Biswajit Sahoo, KIIT


φ = (V x2 V ) Λ (x1 V V x3) Λ (x1 V x2 V )
C1C2 C3
Assignment:
x1=1, x2=0, x3=1
X1 x2 X3 Not Satisfiable
Clique of size 3 ?
x3 X1

X2 X2

x1 x3

Biswajit Sahoo, KIIT


φ = (V x2 V ) Λ (x1 V V x3) Λ (x1 V x2 V )
C1C2 C3
Assignment:
x1=1, x2=0, x3=1
X1 x2 X3 Not Satisfiable
Clique of size 3 ?
x3 X1

X2 X2

x1 x3

Biswajit Sahoo, KIIT


φ = (V x2 V ) Λ (x1 V V x3) Λ (x1 V x2 V )
C1C2 C3
Assignment:
x1=1, x2=0, x3=1
X1 x2 X3 Not Satisfiable
No Clique of size 3
x3 X1

X2 X2

x1 x3

Biswajit Sahoo, KIIT


Travelling Salesperson Problem (TSP)
45

Consider a salesman who must visit n cities


labeled v1, v2,..., vn.
The salesman starts in city v1, his home, and
wants to find a tour—an order in which to visit
all the other cities and return home. His goal
is to find a tour that causes him to travel as
little total distance as possible.

Decision Travelling Salesman Problem: Given


a set of distances on n cities, and a bound D,
is there a tour of length at most D?
Biswajit Sahoo, KIIT
Travelling Salesperson Problem (TSP)
46

TSP is in NP-Complete ?
i. TSP  NP
ii. Hamiltonian Cycle p TSP

if the graph (G) has a Hamiltonian Cycle then


the graph (G’) must have a TSP

TSP: Does the graph have a TSP whose cost is


k?

Biswajit Sahoo, KIIT


Verification of decision TSP
47

TSP  NP
3 4

5 8

1, 5, 3, 4, 8, 6, 9, 2, 7, 1 6
Source 1 Verified in P

9
7
2
Biswajit Sahoo, KIIT
Reducing to Travelling Salesperson Problem (TSP)
48

Hamiltonian Cycle p TSP


G G’

Cost matrix of G is reduced to cost matrix of G’


cost(i, j) = 1 if an edge is there between i to j else 0

The reduction can be done in P i. e. O(n2), considering n


number of vertices

Biswajit Sahoo, KIIT


TSP of cost k?
49 Hamiltonian Cycle?

Hamiltonian Cycle pTSP


3 4
add cost 1
5 8

1, 5, 3, 4, 8, 6, 9, 2, 7, 1 6
Source 1

9
7
2
All edges are present, Hamiltonian cycle exists
Biswajit Sahoo, KIIT TSP exists with cost k
TSP of cost k?
50 Hamiltonian Cycle?

Hamiltonian Cycle pTSP


3 4
X No Edge(add cost 0)
add cost 1
5 8

1, 5, 3, 4, 8, 6, 9, 2, 7, 1 6
Source 1

9
7
2
No Hamiltonian cycle exists
Biswajit Sahoo, KIIT No TSP exist with cost k
51

Biswajit Sahoo, KIIT

You might also like