0% found this document useful (0 votes)
9 views

Lecture 10 - NP - Complete Problems

Uploaded by

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

Lecture 10 - NP - Complete Problems

Uploaded by

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

DESIGN AND

ANALYSIS OF
ALGORITHMS

Chapter # 10 :
NP- Complete Problems

Dr. Muhammad Akhlaq


Assistant Professor, University of Hafr Al-Batin (UHB), College of Computer Science and Engineering (CCSE)
Hafr Al-Batin 39524, Saudi Arabia. Email: akhlaq[at]uhb.edu.sa

Slides adopted from M. Alsuwaiyel, Algorithms: Design Techniques and Analysis


the textbook: (revised edition), World Scientific Publishing Co Pte Ltd, 2016.
NP-Complete Problems
2

 Reading Material: Chapter 10 … Sections 1, 2, 3,


and 4 only.

Complexity Theory is concerned with efficient


computation.
Computational complexity of a problem is the
computational complexity of the most efficient algorithm
to solve that problem (not the cost of a particular
OUTLINE
3

• Introduction / Basics

1. Introduction to NP- Completeness

2. The Class P

3. The Class NP

4. NP-Complete Problems
Introduction
4
 Polynomial problems: A problem is said to be
polynomial if there exists an algorithm that solves the
problem in time T(n)=O(nk), where k is a constant.
 Exponential problems: A problem is said to be
exponential if no polynomial-time algorithm can be
developed for it. ( i.e., T(n)=O(an) ).
 In-tractable problems: are
the one which are non-
computable. There exist no
efficient algorithms to solve
them. Usually brute-force
search is used.
 Examples: Traveling Salesman
Problem, Subset Sum, Bin Packing,
Job Shop Scheduling, Satisfiability...
Cont…
5
 Deterministic algorithms: An algorithm A is deterministic if,
when presented with an instance of the problem Q, it has only
one choice in each step throughout its execution. Thus if A is
run again and again on the same instance, its output never
changes.
 Non-deterministic algorithms: An algorithm that defines
multiple ways of processing the same input, without any
specification of which one will be taken (e.g., randomised
algorithms). They can be converted into a deterministic
algorithm, possibly with exponential slow down.
 E.g.,: Primality testing problem: given a natural number larger than one,
determine whether it is prime.
Solution: A nondeterministic algorithm for this problem is the following.
 Pick any integer k such that 2 ≤ k ≤ √(n).
 If k is a divisor of n, stop with answer no; otherwise stop with don't know.
Cont…
6
 Complexity Classes
 The class P is the class of problems that can be solved in
time that is polynomial in the size of the input, n.
The problems in P are considered “tractable”.
 The class NP is the class of problems with solutions that
can be verified in polynomial time.
 How to prove that a problem is in NP ?
1. Show that the problem is in P, or
2. Write a program solving the problem using the steps that runs in
polynomial time, or
3. Write 2 polynomial programs that: (1) generate a possible
solution S and (2) tests if S is a solution to the problem.
 NP-Complete problems: the problems having the common
property that if one of them is solvable in polynomial time,
then all the others are solvable in polynomial time.
Cont…
7

Example of an NP problem:
The Hamiltonian Cycle (HC) problem

 Input: A graph G
 Question: Does G have a Hamiltonian Cycle?
Hamiltonian cycle is a
cycle in graph that visits
each vertex exactly once.

begin The solution


/* The following for-loop is the guessing stage*/
for i=1 to n size of HC is
X[i] := choose(i); O(n).
/* Next is the verification stage */
for i=1 to n
for j=i+1 to n  the time of the
if X[i] = X[j] then return(no); //visiting twice
for i=1 to n-1
verification stage
if (X[i],X[i+1]) is not an edge then return(no); is O(n2).
if (X[n],X[1]) is not an edge then return(no);
return(yes);
Therefore, HC
end is NP.
Cont…
8

 P, NP, NPC...how are they related?


 Any problem in P is also in NP, since if a problem is
in P then we can solve it in polynomial-time without
even being given a certificate.
So P  NP.
 By definition, NPC  NP

 is NP  P ?
• It is an open problem.
• we think P ≠ NP, but no one has proven it one way
or the other (despite enormous effort).
• It is known that if P ≠ NP, there must exist NP
problems that neither are in P nor are NPC.
1. NP-Complete Problems
9

 Problems in Computer Science are classified into:

 Tractable: There exists a polynomial time algorithm


that solves the problem
 O(nk)

 In-tractable: Unlikely for a polynomial time algorithm


solution to exist
 NP-Complete Problems
Decision Problems vs. Optimization Problems
10

 Decision Problem: Yes/no answer

 Optimization Problem: Maximization or minimization of a


certain quantity

 When studying NP-Completeness, it is easier to deal with


decision problems than optimization problems…So, restrict
attention to decision problems.
 If we have an efficient algorithm that solves a decision problem,
then it can easily be modified to solve its corresponding
optimization problem.
Example-1: Element Uniqueness Problem
11

 Decision Problem: Element Uniqueness


 Input: A sequence of integers S
 Question: Are there two elements in S that are equal?

 Optimization Problem: Element Count


 Input: A sequence of integers S
 Output: An element in S of highest frequency

 What is the algorithm to solve this problem?


 Sort and then check if there are any consecutive equal elements
 How much does it cost? ……. O(n log n)
Example-2: Coloring a Graph
12

• Decision Problem: Coloring


– Input: G=(V,E) undirected graph and k, k > 0.
– Question: Is G k-colorable?

• Optimization Problem: Chromatic Number


– Input: G=(V,E) undirected graph
– Output: The chromatic number of G,(G)
• i.e. the minimum number (G) of colors needed to color a
graph in such a way that no two adjacent vertices have the
same color.
Example-3: Cliques
13

 Given an undirected graph G = (V,E), a clique is a


subset of vertices V, each pair of which is connected by
an edge in E.
 Definition: A clique of size k in graph G, for some +ve
integer k, is a complete subgraph of G with k vertices.
 Decision Problem
 Input: G
 Question: Yes or No
 Optimization Problem
 Input: G
 Output: k that is the maximum clique size.
From Decision To Optimization
14

 For a given problem, assume we were able to find


a solution to the decision problem in polynomial
time. Can we find a solution to the optimization
problem in polynomial time also? ……… Yes

 Example:
If we have an algorithm A that solves the decision problem for
graph coloring, we can find the chromatic number of a graph
G using binary search and Algorithm A as a subroutine.
Hence, the chromatic number of G can be found using only
O(log n) calls to algorithm A.
Deterministic Algorithms
15

 Definition: Let A be an algorithm to solve problem.


A is called deterministic if, when presented with an
instance of the problem , it has only one choice in
each step throughout its execution.
 If we run A again and again, is there a possibility that the
output may change? ………… No

 What type of algorithms did we have so far? … many


2. The Class P
16

 Definition:
The class of decision problems P consists of those
whose yes/no solution can be obtained using a
deterministic algorithm that runs in polynomial
time of steps, i.e. O(nk), where k is a non-negative
integer and n is the input size.
Examples
17

 Sorting: Given n integers, are they sorted in non-


decreasing order?
 Set Disjointness: Given two sets of integers, are
they disjoint?
 Shortest path: …
 2-coloring:
 Theorem: A graph G is 2-colorable if and only if G is
bipartite
Closure Under Complementation
18

 A class C of problems is closed under complementation if


for any problem   C the complement of  is also in C.
 Theorem:
The class P is closed under complementation.
 Example: The complement of the 2-coloring problem can be stated as
follows. Given a graph G, is it not 2-colorable? Let us call this problem not-
2-color. We can show that it is in P as follows. Since 2-coloring is in P, there
is a deterministic algorithm A which when presented with a 2-colorable graph
halts and answers yes, and when presented with a graph that is not 2-
colorable halts and answers no. We can simply design a deterministic
algorithm for the problem not-2-color by simply interchanging the yes and no
answers in Algorithm A.
Non-Deterministic Algorithms
19

 A non-deterministic algorithm A on input x consists of


two phases:
1. Guessing: An arbitrary “string of characters y” is
generated in polynomial time. It may
 Correspond to a solution
 Not correspond to a solution
 Not be in proper format of a solution
 Differ from one run to another

2. Verification: A deterministic algorithm verifies


 The generated “string of characters y” is in proper format
 Whether y is a solution

in polynomial time
Cont…
20
 Definition: Let A be a nondeterministic algorithm for a
problem . We say that A accepts an instance I of  if
and only if on input I, there exists a guess that leads to
a yes answer.
 Does it mean that if an algorithm A on a given input I
leads to an answer of no for a certain guess, that it does
not accept it?
 What is the running time of a non-deterministic
algorithm?
 It is simply the sum of the two running times: the one for
the guessing phase, and that for the verification phase. So
it is O(ni) + O(nj) = O(nk), for k≥0 … polynomial time.
3. The Class NP
21

 Definition:
The class of decision problems NP consists of those
decision problems for which there exists a
nondeterministic algorithm that runs in
polynomial time.

 Example: Show that the coloring problem belongs to the


class of NP problems.
 Asguessing and verification based algorithm can be designed
easily, the coloring problem belongs to the class of NP problems.
P and NP Problems
22

 What is the difference between P problems and NP


Problems?
We can decide/solve problems in P using
deterministic algorithms that run in polynomial time
We can check/verify the solution of NP problems in
polynomial time using a deterministic algorithm

 What is the set relationship between the classes P


and NP? …………. P  NP
3. NP-Complete Problems
23

 Definition:
Let  and ’ be two decision problems. We say that
’ reduces to  in polynomial time, denoted by
’poly , if there exists a deterministic algorithm A
that behaves as follows:
When A is presented with an instance I’ of problem ’,
it transforms it into an instance I of problem  in
polynomial time such that the answer to I’ is yes if and
only if the answer to I is yes.

( That is,  NP, and ’ NP, ’poly . )


NP-Complete and NP-Hard
24
 Definition: A decision problem  is said to be NP-
complete if
  NP
 ’ NP, ’poly .

 Definition: A decision problem  is said to be NP-hard if


 ’ NP, ’poly .

 What is the difference between an NP-complete problem 


and an NP-hard problem ’ ?
  must be in the class NP whereas ’ may not be in NP
0
Conjunctive Normal Forms (CNF)
25

 Definition: A clause is the disjunction of literals,


where a literal is a boolean variable or its negation
 E.g., x1  x2  x3  x4

 Definition: A boolean formula f is said to be in


conjunctive normal form (CNF) if it is the conjunction
of clauses.
 E.g., (x1  x2)  (x1  x5)  (x2  x3  x4  x6)

 Definition: A boolean formula f is said to be


satisfiable if there is a truth assignment to its variables
that makes it true.
The Satisfiability Problem
26

 Input: A CNF boolean formula f.


 Question: Is f satisfiable?

 Theorem: Satisfiability is NP-Complete


 Satisfiability is the first problem to be proven as
NP-Complete
 The proof includes reducing every problem in NP to
Satisfiability in polynomial time.
Transitivity of poly
27

 Theorem: Let , ’, and ’’ be three decision


problems such that  poly ’ and ’poly ’’. Then
poly ’’ … (i.e., the reducibility relation poly is
transitive)
Proof: (not required)

 Corollary: If , ’ NP such that ’ poly  and ’


 NP-complete, then   NP-complete
Proving NP-Completeness
28
Vertex-Cover: A set of
vertices that includes at least

SAT
one endpoint of every edge of
the graph.

3-CNF-SAT

Clique Hamiltonian Cycle

Vertex-Cover Traveling Salesman

Subset-Sum SUBSET-SUM problem: It involves determining


whether or not a subset from a list of integers can
sum to a target value.
Example: If S = [1, 2, 3, 4] and target= 7, there are
Example: (Proving NP-Completeness)
29
 Show that the traveling salesman problem is NP-complete,
assuming that the Hamiltonian cycle problem is NP-
complete.
 Proof:
1. Show that traveling salesman is in NP: This is very simple, since a
nondeterministic algorithm can start by guessing a sequence of cities, and
then verifies that this sequence is a tour. If this is the case, it then
continues to see if the length of the tour is at most k.
2. Show that Hamiltonian cycle can be reduced to traveling salesman in
polynomial time ( HCpoly TS ). Here is how:
 Let G=(V, E) be any arbitrary instance of Hamiltonian cycle.
 Construct a weighted graph G’=(V, E’) and a bound k such that G has a
Hamiltonian cycle if and only if G’ has a tour of total length at most k.
 Assign a length to each edge in E’: 1 if edge, n if not … where n = |V|.
 Assign k = n … where n = |V|.
 It is clear from the construction that G has a Hamiltonian
cycle if and only if G’ has a tour of length exactly n.
Comparison between P, NP, NPC,
30
NPH

You might also like