0% found this document useful (0 votes)
4 views49 pages

Lecture 71 72 - P NP NP Hard NP Complete

The document discusses the classifications of computational problems, specifically P, NP, NP-Hard, and NP-Complete, along with their definitions and examples. It explains the differences between decision and optimization problems, and how problems can be reduced to one another to determine their complexity. The document also highlights the significance of the P vs NP question and provides insights into specific NP-Complete problems like Circuit Satisfiability and the Traveling Salesman Problem.

Uploaded by

git.sumitjoshi
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)
4 views49 pages

Lecture 71 72 - P NP NP Hard NP Complete

The document discusses the classifications of computational problems, specifically P, NP, NP-Hard, and NP-Complete, along with their definitions and examples. It explains the differences between decision and optimization problems, and how problems can be reduced to one another to determine their complexity. The document also highlights the significance of the P vs NP question and provides insights into specific NP-Complete problems like Circuit Satisfiability and the Traveling Salesman Problem.

Uploaded by

git.sumitjoshi
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/ 49

P, NP, NP-Hard & NP-

complete
Problems
Lecture – 71-72

1
Objective
• P, NP, NP-Hards and NP-
Complete
• Solving 3-CNF Sat problem
• Discussion of Gate Questions

2
Types of
• Trackable Problems
• Intrackable
• Decision
• Optimization

Trackable : Problems that can be


solvable in a reasonable(polynomial)
time.
Intrackable : Some problems are
intractable, as they grow large, we
are unable to solve them in
3
reasonable time.
Tractability
• What constitutes reasonable time?
– Standard working definition: polynomial
time on an input of size n the worst-case
running time is O(nk) for some constant k
– O(n2), O(n3), O(1), O(n lg n), O(2n), O(nn),
O(n!)
– Polynomial time: O(n2), O(n3), O(1), O(n lg
n)
– Not in polynomial time: O(2n), O(nn), O(n!)
• Are all problems solvable in
polynomial time?
– No(Turing’s “Halting Problem” is not
4
solvable by any computer, no matter
Optimization/Decision
Problems
• Optimization Problems
– An optimization problem is one which asks,
“What is the optimal solution to problem
X?”
– Examples:
• 0-1 Knapsack
• Fractional Knapsack
• Minimum Spanning Tree
• Decision Problems
– An decision problem is one with yes/no
answer
– Examples:
• Does a graph G have a MST of weight 5
W?
Optimization/Decision
•Problems
An optimization problem tries to find
an optimal solution
• A decision problem tries to answer a
yes/no question
• Many problems will have decision and
optimization versions
– Eg: Traveling salesman problem
• optimization: find hamiltonian
cycle of minimum weight
• decision: is there a hamiltonian
cycle of weight k
6
Till now all the algorithm we have
learned are classified and written below
in two categories:

Polynomial Exponential based


based Algorithm Algorithm
• Linear Search • 0/1 Knapsack
• Binary Search • Travelling
• Insertion Sort Salesman
• Merge Sort • Sum of Subset
• Matrix • N-Queen
Multiplication • Graph Coloring
• etc… • Hamiltonian
Cycle
P, NP, NP-Hard, NP-
Complete -
Definitions

8
The Class
P
P: the class of problems that have
polynomial-time deterministic algorithms.
– That is, they are solvable in O(p(n)),
where p(n) is a polynomial on n
– A deterministic algorithm is (essentially)
one that always computes the correct
answer

9
Sample Problems
in P
• Fractional Knapsack
• MST (Minimum Spanning Tree)
• Sorting
• Others ?

10
The class
NP
NP: the class of decision problems that are
solvable in polynomial time on a
nondeterministic machine (or with a
nondeterministic algorithm)
– (A determinstic computer is what we
know)
– A nondeterministic computer is one that
can “guess” the right answer or solution
• Think of a nondeterministic computer as a
parallel machine that can freely spawn an
infinite number of processes
• Thus NP can also be thought of as the class
of problems “whose solutions can be
verified in polynomial time”
• Note that NP stands for
“Nondeterministic Polynomial- 11

time”
Sample Problems in
NP
• Fractional Knapsack
• MST
• Others?
– Traveling Salesman
– Graph Coloring
– Satisfiability (SAT)
• the problem of deciding whether a given
Boolean formula is satisfiable

12
P And NP
Summary
• P = set of problems that can be
solved in polynomial time
– Examples: Fractional Knapsack, …
• NP = set of problems for which a solution
can be verified in polynomial time
– Examples: Fractional Knapsack,…,
TSP, CNF SAT, 3-CNF SAT
• Clearly P NP
• Open question: Does P = NP? Or P ≠
NP ?
NP-
• What does NP-hard mean?
hard
– A lot of times you can solve a problem by reducing
it to a different problem. I can reduce Problem
B to Problem A if, given a solution to Problem A, I
can easily construct a solution to Problem B. (In
this case, "easily" means "in polynomial time.“).
• A problem is NP-hard if all problems in
NP are polynomial time reducible to it,
...
• Ex:- Hamiltonian Cycle (HC)
Every problem in NP is reducible to
HC in polynomial time. Ex:- TSP is
B A
. Example:tolcm(m, n) = m * n /
reducible
13
gcd(m, n),
NP-complete
problems
• A problem is NP-complete if the
problem is both
– NP-hard, and
– NP.
Reductio
n
• A problem R can be reduced to another
problem Q if any instance of R can be
rephrased to an instance of Q, the
solution to which provides a solution to
the instance of R
– This rephrasing is called a transformation
• Intuitively: If R reduces in polynomial
time to Q, R is “no harder to solve”
than Q
• Example: lcm(m, n) = m * n / gcd(m,
n),
lcm(m,n) problem is reduced to
NP-Hard and NP-
Complete
• If R is polynomial-time reducible to
Q, we denote this R Q
• Definition of NP-Hard and NP-
Complete:
– If all problems R NP are polynomial-
time
reducible to Q, then Q is NP-Hard
– We say Q is NP-Complete if Q is NP-
Hard and Q NP
• If R Q and R is NP-Hard, Q is also
NP-Hard
Relationship between P, NP,
NP-Hard and NP-Complete

18
Summar
y
• P is set of problems that can be solved
by a deterministic Turing machine in
Polynomial time.
• NP is set of problems that can be solved
by a Non-deterministic Turing Machine
in Polynomial time. P is subset of NP
(any problem that can be solved by
deterministic machine in polynomial
time can also be solved by non-
deterministic machine in polynomial
time) but P≠NP.
19
Summar
• Some problems
y can be translated
into one another in such a way that
a fast solution to one problem
would automatically give us a fast
solution to the other.
• There are some problems that
every single problem in NP can be
translated into, and a fast solution
to such a problem would
automatically give us a fast solution
to every problem in NP. This group
of problems are known as NP-
Complete. Ex:- Clique Problem

20
Summar
• A problem is yNP-hard if an algorithm
for solving it can be translated into
one for solving any NP- problem
(nondeterministic polynomial time)
problem. NP-hard therefore means
"at least as hard as any NP Problem"
although it might, in fact, be harder.

21
First NP-complete problem
(Circuit Satisfiability )
Problem definition
• Boolean combinational circuit
– Boolean combinational elements,
wired together
– Each element, inputs and outputs
(binary)
– Limit the number of outputs to 1.
– Called logic gates: NOT gate, AND gate,
OR gate.
– true table: giving the outputs for
each setting of inputs
– true assignment: a set of boolean 20

inputs.
First NP-complete
problem (Circuit
Satisfiability
• Circuit satisfying )
problem: given a
boolean combinational circuit
composed of AND, OR, and NOT, is it
stisfiable?
• CIRCUIT-SAT={<C>: C is a
satisfiable boolean circuit}
• Implication: in the area of computer-
aided hardware optimization, if a
subcircuit always produces 0, then the
subcircuit can be replaced by a
simpler subcircuit that omits all gates 23
and just output a 0.
First NP-complete
problem (Circuit
Satisfiability
Two instances )
of circuit satisfiability problems

Figure: Two instances of the circuit-satisfiability problem


a) The assignment (to the input of this circuit causes the output
of the circuit to be 1. The circuit is therefore satisfiable.
b) No assignment to the input of the circuit can cause the
output of the circuit to be 1. The circuit is therefore
24
unsatisfiable.
Solving circuit-satisfiability
problem
• Intuitive solution:
– for each possible assignment,
check whether it generates 1.
– suppose the number of inputs is k,
then the total possible assignments
are 2k.
So the running time is (2k). When
the size of the problem is (k), then
the running time is not polynomial.
25
Example of reduction of CIRCUIT-SAT to SAT

REDUCTION: ) ))=……

26
Conversion to 3
• The resultCNF
is that in each clause has at most
three literals
• Change each clause into conjunctive normal form as
follows:
– Construct a true table, (small, at most 8 by 4)
– Write the disjunctive normal form for all true-
table items evaluating to 0
– Using DeMorgan law to change to CNF.
• The resulting is in CNF but each clause has 3 or less
literals.
• Change 1 or 2-literal clause into 3-literal clause as
follows:
– If a clause has one literal l, change it to
– If a clause has two literals change it

27
Example of a polynomial-time
reduction:
Now reduce the 3CNF-satisfiability problem
to the CLIQUE problem

28
3CNF
formula: claus
literal
e

Each clause has three literals

Language:
3CNF-SAT ={w : w is a satisfiable 3CNF formula}

29
Definition of Clique: In a clique problem each
node is connected to each other nodes of that
graph.
Language:
CLIQUE = {< G, k > : graph G
contains a
A 5-clique in graph
k-clique}
G

30
Transform formula to
graph. Example:

Clause
Create x1
2
x2 x4
Nodes:
x1 x1

3
Clause
Clause

x2
x2
1

x4 x3

x2 x3 x4

Clause 31
4
x1 x2 x4

x1 x1

x2
x2

x4 x3

x2 x3 x4

Add link from a literal t o a literal in every


other clause, except the complement 32
x1 x2 x4

x1 x1

x2
x2

x4 x3

x2 x3 x4

Resulting
Graph
33
x1 = 1
x2 = 0 x4
x1 x2
x3 = 0
x4 = x1 x1
1
x2
x2

x4 x3

x2 x3 x4

The formula is satisfied if and only if


the Graph has a 4-clique
The objective is to find a clique of size 4, End of
where 4 is the number of clauses. Proof
32
CIRCUIT-SAT

The side figure SAT

outline the
structure of the 3-CNF-SAT
NP-
Completeness CLIQUE
SUBSET-
proof by using SUM
reduction VERTEX-COVER

methodology
HAM-CYCLE

TSP
Theorem:
is NP-
If: a. Language A
complete is
b. Language B in NP
c. A is polynomial time reducible
to B

Then: B is NP-complete

36
Corollary CLIQUE is NP-
: complete
Proof:
a. 3CNF-SAT is NP-complete
b. CLIQUE is in NP
c. 3CNF-SAT is polynomial reducible to
CLIQUE
(shown earlier)

Apply previous theorem with


A=3CNF-SAT and
37
B=CLIQUE
Previous Gate
Questions

38
Q. No.
1

39
Q. No.
2

40
Q. No.
3

41
Q. No.
4

42
Q. No.
5

43
Q. No.
6

44
Q. No.
7

45
Q. No.
8

46
Q. No.
10

There exist: search


problem

Explanation: The problem of


finding whether there exist a
Hamiltonian Cycle or not is NP
Hard and NP Complete Both.
Finding a Hamiltonian cycle in a
graph G = (V,E) with V divisible
by 3 is also NP Hard.

47
Q. No.
11

48
Thank
You

49

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy