Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 67

NP hard and NP Complete problems

Basic Concepts

1
NP –HARD AND NP – COMPLETE PROBLEMS
Basic Concepts
NP –HARD AND NP – COMPLETE PROBLEMS
Basic Concepts

Decision Problems
To keep things simple, we will mainly concern ourselves with
decision problems. These problems only require a single bit output:
``yes'' and ``no''.

How would you solve the following decision problems?

-Is this directed graph acyclic?


-Is there a spanning tree of this undirected graph with total
weight less than w?
NP –HARD AND NP – COMPLETE PROBLEMS
Basic Concepts

• The computing times of algorithms fall into


two groups.

• Group1– consists of problems whose solutions


are bounded by the polynomial of small degree.

Example – Binary search o(log n) ,


sorting O(n log n),
matrix multiplication 0(n 2.81).
4
NP –HARD AND NP – COMPLETE PROBLEMS
Basic Concepts

• Group2 – contains problems whose best known algorithms are


non polynomial.

• Example –Traveling salesperson problem 0(n22n), knapsack


problem 0(2n/2) etc.

• There are two classes of non polynomial time problems


1) NP- hard
2) NP-complete

• A problem which is NP complete will have the property that it


can be solved in polynomial time iff all other NP – complete
problems can also be solved in polynomial time.
5
NP –HARD AND NP – COMPLETE PROBLEMS
Basic Concepts

The class NP (meaning non-deterministic polynomial time) is the


set of problems that might appear in a puzzle magazine: ``Nice
puzzle.''

What makes these problems special is that they might be hard to


solve, but a short answer can always be printed in the back, and it
is easy to see that the answer is
correct once you see it.
NP –HARD AND NP – COMPLETE PROBLEMS
Basic Concepts

• Another way of thinking of NP is it is the set of problems that


can solved efficiently by a really good guesser.

• The guesser essentially picks the accepting certificate out of the


air (Non-deterministic Polynomial time). It can then convince
itself that it is correct using a polynomial
time algorithm. (Like a right-brain, left-brain sort of thing.)

• Clearly this isn't a practically useful characterization: how


could we build such a machine?
NP –HARD AND NP – COMPLETE PROBLEMS
Basic Concepts

NP-hardness

As we will see, some problems are at least as hard to solve as any


problem in NP. We call such problems NP-hard.

How might we argue that problem X is at least as hard (to within


a polynomial factor) as problem Y?

If X is at least as hard as Y, how would we expect an algorithm


that is able to solve X to behave?
NP –HARD AND NP – COMPLETE PROBLEMS
Basic Concepts

• If an NP-hard problem can be solved in polynomial


time, then all NP-complete problems can be solved
in polynomial time.

• All NP-complete problems are NP-hard, but all NP-


hard problems are not NP-complete.

• The class of NP-hard problems is very rich in the


sense that it contain many problems from a wide
variety of disciplines.
10
• P: the class of problems which can be solved by a
deterministic polynomial algorithm.

• NP : the class of decision problem which can be solved by a


non-deterministic polynomial algorithm.

• NP-hard: the class of problems to which every NP problem


reduces.

• NP-complete (NPC): the class of problems which are NP-


hard and belong to NP.
8- 11
NP-Completeness
• How would you define NP-Complete?
• They are the “hardest” problems in NP

NP-Complete

P
NP
DETERMINISTIC and NONDETERMINISTIC ALGORITHMS

• Algorithms with the property that the result of every


operation is uniquely defined are termed
deterministic.

• Such algorithms agree with the way programs are


executed on a computer.

• In a theoretical framework, we can allow algorithms to


contain operations whose outcome is not uniquely
defined but is limited to a specified set of possibilities.
12
DETERMINISTIC and NON-DETERMINISTIC ALGORITHMS

• The machine executing such operations are allowed to


choose any one of these outcomes subject to a
termination condition.

• This leads to the concept of non deterministic algorithms.

• To specify such algorithms in SPARKS, we introduce three


statements
i) choice (s) ……… arbitrarily chooses one of
the elements of the set S.
ii) failure …. Signals an unsuccessful
completion.
iii) Success : Signals a successful
DETERMINISTIC and NON-DETERMINISTIC ALGORITHMS

• Whenever there is a set of choices that leads to a successful


completion then one such set of choices is always made and the
algorithm terminates.

• A nondeterministic algorithm terminates unsuccessfully if


and only if there exists no set of choices leading to a successful
signal.

• A machine capable of executing a non deterministic algorithm is


called a non deterministic machine.

• While non-deterministic machines do not exist in practice they


will provide strong intuitive reason to conclude that certain
problems cannot be solved by fast deterministic algorithms.
Nondeterministic algorithms
• A nondeterminstic algorithm consists of
phase 1: guessing
phase 2: checking

• If the checking stage of a nondeterministic algorithm


is of polynomial time-complexity, then this algorithm is
called an NP (nondeterministic polynomial) algorithm.
• NP problems : (must be decision problems)
searching, MST sorting
satisfiability problem (SAT)
traveling salesperson problem (TSP)
Example of a non deterministic algorithm
Deterministic and Nondeterministic algorithms
• Many optimization problems can be recast into decision problems
with the property that the decision problem can be solved in
polynomial time iff the corresponding optimization problem can .
EXAMPLE : [0/1 knapsack] :
• The decision is to determine if there is a 0/1 assignment of values to

• It is easy to obtain polynomial time non deterministic algorithms for


many problems that can be deterministically solved by a systematic
search of a solution space of exponential size.
P, NP, NP-Hard, NP- Complete
SATISFIABILITY
• Let x1,x2,x3….xn denotes Boolean variables.
_
• Let xi denotes the negation of xi.

• A literal is either a variable or its negation.

• A formula in the prepositional calculus is an expression that


can be constructed using literals and the operators and ‘Ʌ’
or ‘V’
• A clause is a formula with at least one positive literal.

• The satisfiability problem is to determine if a formula is


true for some assignment of truth values to the variables
SATISFIABILITY

• It is easy to obtain a polynomial time non determination


algorithm that terminates successfully if and only if a given
prepositional formula E(x1,x2……xn) is satiable.

• Such an algorithm could proceed by simply choosing (non-


deterministically) one of the 2n possible assignments of
truth values to (x1,x2…xn) and verify that E(x1,x2…xn) is true
for that assignment.

20
The satisfiability problem
 
The Satisfiability problem
- The logical formula:
(X1 V X2 V X3 ) Ʌ V X2 V X3 )Ʌ(X1 V X3 )
Clauses C1 ɅC2 Ʌ C3
conjunctive normal form (CNF)

• If there is at least one assignment which satisfies a


formula, then we say that this formula is satisfiable;
otherwise, it is unsatisfiable.
NP-HARD GRAPH AND SCHEDULING PROBLEMS
NP-HARD GRAPH AND SCHEDULING PROBLEMS
Order of transformations for various hard problems

23
NP-HARD GRAPH PROBLEMS
Clique Decision Problem(CDP)
• a clique is a complete sub-graph of the given graph.
• Complete sub-graph means, all the vertices of this sub-graph is connected to all other
vertices of this sub-graph.
• The Max-Clique problem is the computational problem of finding maximum clique of the
graph.
NP-HARD GRAPH PROBLEMS
Clique Decision Problem(CDP)

1. CNF Satisfiability ≤ρ Clique


is NP- Hard
2. Solution of Clique is verifiable in
polynomial time then it is NP problem.

If (1) and (2) are possible for any


problem we can say that the problem is
NP-Complete.
NP-HARD GRAPH PROBLEMS
Clique Decision Problem(CDP) Clauses C1 ɅC2 Ʌ C3
1. CNF Satisfiability ≤ρ Clique conjunctive normal form (CNF)
is NP- Hard
NP-HARD GRAPH PROBLEMS
Clique Decision Problem(CDP)
1. CNF Satisfiability ≤ρ Clique
is NP- Hard

 Consider F= (X1 V X2 V X3 ) Ʌ ( V V )

 Graph G=(V,E) , V={ < X1, 1 > ,< X2, 1 >, < X3, 1 >,< ,2>,< ,2>,< ,2>}
NP-HARD GRAPH PROBLEMS
Clique Decision Problem(CDP)
1. CNF Satisfiability ≤ρ Clique
is NP- Hard

 Consider F= (X1 V X2 V X3 ) Ʌ ( V V )

 Graph G=(V,E) , V={ < X1, 1 > ,< X2, 1 >, < X3, 1 >,< ,2>,< ,2>,< ,2>}

G has a clique of size k=2


Hence CDP is NP Hard. Proved (1)
Solution of CDP can be verified in
polynomial time. Proved (2) CDP is NP Complete
NP-HARD GRAPH PROBLEMS
Node Cover Decision Problem
NP-HARD GRAPH
Node Cover Decision Problem
NP-HARD SCHEDULING PROBLEMS

Scheduling identical processors


• Let Pi 1≤i≤m be identical processors or machines Pi.
• Let Ji 1≤i≤n be n jobs.
• Jobs Ji requires ti processing time.
• A schedule S is an assignment of jobs to processors.
• For each job Ji ,S specifies the time intervals and the
processor(s) on which this job is to be processed.
• A job cannot be processed by more than one
processor at any given time
• Let fi be the time at which the processing of job Ji is
completed.
Scheduling identical processors

• Schedule S is a nonpreemptive schedule if and only if each


job Ji is processed continuously from start to finish on the
same processor
• In a preemptive schedule each job need not be processed
continuously to completion on one Processor.
Scheduling identical processors
Obtaining minimum weighted mean finish time and
minimum finish time nonpreemptive schedules is NP-hard.
Scheduling identical processors
Scheduling identical processors
Scheduling identical processors
NP-HARD CODE GENERATION PROBLEMS
• Compiler
• The translation clearly depends on the particular assembly
language (and hence machine)being used.
• Let us consider a very simple machine model .We call this model
machine A. This machine has only one register called the
accumulator.
• All arithmetic operators like +, -, *, and / has to be
performed in this register.
• Always in an expression the left hand operand should be in
the accumulator.
• For the machine ‘A’ the relevant assembly language
instructions are :
NP-HARD CODE GENERATION PROBLEMS
Consider the expression (a+b)/(c+d)

A translation of an expression E
into the machine or assembly
language of a given machine is
optimal if and only if it has a
minimum number of
instructions
NP-HARD CODE GENERATION PROBLEMS
NP-HARD CODE GENERATION PROBLEMS
(a+b)/(c*d)

• Can we Evaluate E without any STORES?


• What is the minimum number of registers needed to evaluate
E without any STORES?

This Problem is NP Hard


NP-HARD CODE GENERATION PROBLEMS
Code Generation with Common Subexpressions

When arithmetic expressions have common subexpressions they can be represented by a


directed acyclic graph (dag).
Internal Nodes are
operators A shared node is a node with
more than one parent

A leaf dag is a dag in


which all shared nodes
are leaves

A leaf is a node with out-


A level-one node is a degree zero.
node both of whose A level-one dag is a
children are leaves. dag in which all
shared nodes are
level-one nodes.
NP-HARD CODE GENERATION PROBLEMS
Code Generation with Common Subexpressions

The minimum number of registers needed to evaluate this dag without any STOREs is two.

Generating optimal code without or with minimum number of stores is NP-hard.


NP-HARD CODE GENERATION PROBLEMS
Code Generation with Common Subexpressions

The dag A consists of


three kinds of nodes:
leaf nodes chain nodes
and tree nodes.
All chain and tree nodes
are internal nodes
representing
commutative operators
(for example,+)
NP-HARD CODE GENERATION PROBLEMS
If ‘d’ is the
out degree
of a node,
then
number
nodes in the
chain will be
equal to Chain Nodes
d+1.
v1,v2,…vd+1

v1 tail node of the


chain
vd+1 head node of
the chain,
contains to leaf
nodes vL , vR
NP-HARD CODE GENERATION PROBLEMS
root

Tree Nodes

Leaf Nodes
NP-HARD CODE GENERATION PROBLEMS

n-1 tree
nodes
NP-HARD CODE GENERATION PROBLEMS

r4 value
should be
stored in
memory . So
that r3 can
LOAD it.

R1
NP-HARD CODE GENERATION PROBLEMS

R1 R2
NP-HARD CODE GENERATION PROBLEMS

R3 R1 R2
NP-HARD CODE GENERATION PROBLEMS

R4 R2
NP-HARD CODE GENERATION PROBLEMS

R5 R4 R2
NP-HARD CODE GENERATION PROBLEMS

R6 R4 R2
NP-HARD CODE GENERATION PROBLEMS

R7

R2
NP-HARD CODE GENERATION PROBLEMS

R7

R2 R8
NP-HARD CODE GENERATION PROBLEMS

R7
R9
R2
NP-HARD CODE GENERATION PROBLEMS

R7
R10
R2
NP-HARD CODE GENERATION PROBLEMS
t2 value
should be
stored in
memory . So
that s1 can
LOAD it.

R7
R10
R2 R11
NP-HARD CODE GENERATION PROBLEMS

R7
R10
R12
R2
NP-HARD CODE GENERATION PROBLEMS

R13
R7

R2
NP-HARD CODE GENERATION PROBLEMS

R13
R7
R1
4
NP-HARD CODE GENERATION PROBLEMS

R15 R13
R7
R1
4
NP-HARD CODE GENERATION PROBLEMS

R16 R13
R7
R1
4
NP-HARD CODE GENERATION PROBLEMS

R17
R13
R7
R1
4
NP-HARD CODE GENERATION PROBLEMS

R13
R7 R18
NP-HARD CODE GENERATION PROBLEMS

R19 R13
NP-HARD CODE GENERATION PROBLEMS
t2 value
should be
stored in
memory . So
that s1 can
LOAD it.

t2 value
should be
stored in
memory . So
R19 R13 that s1 can
LOAD it.
NP-HARD CODE GENERATION PROBLEMS

Generating optimal code for a parallel assignment statement


is NP-hard,

- however if the expressions are restricted to be simple


variables, then optimal code can be generated in
polynomial time.

You might also like