You are on page 1of 16

Graduate Studies Program

Term: Fall 2022/2023

Computing

Lecture 9
NP-Complete
(Draft)

This lecture notes have been compiled from different resources,


I’d like to thank all authors who make them available to use.

1
Lecture Outline

• Optimization & Decision Problems


• Deterministic polynomial time algorithms (P Class)
• Complexity of P Class
• Polynomial Time Algorithms (Examples)
• Exponential Time Algorithms (EXP)
• Nondeterministic Polynomial time algorithms (NP Class)
• CNF-Satisfiability Problem
• NP-Hard Class
• NP-Complete Class

2
Optimization & Decision Problems

• Decision problems
– Given an input and a question regarding a problem, determine if
the answer is yes or no
• Optimization problems
– Find a solution with the “best” value
• Optimization problems can be cast as decision problems that are
easier to study
– E.g.: Shortest path: G = unweighted directed graph
• Find a path between u and v that uses the fewest edges
• Does a path exist from u to v consisting of at most k edges?

3
Class of “P” Problems

4
Complexity Class P

• Deterministic in nature
• Solved by conventional computers in polynomial time

– O(1) Constant
– O(log n) Sub-linear
– O(n) Linear
– O(n log n) Nearly Linear
– O(n2) Quadratic
– O(n3) Cubic
Deterministic Polynomial Time Algorithms (P class)

• Two classes are defined:


– Deterministic polynomial time algorithms ( P class)
– Nondeterministic polynomial time algorithms (NP class)
• P class algorithms:
– each step is clear to us in P time
• NP class algorithms:
– May some steps are clear in P time and may some
steps are not clear to us or in EXP time
• P is a subset of NP ………………
Polynomial Time Algorithms (P)

• P time algorithms
– Linear Search - O(n)
– Binary Search - O(log n)
– Bubble Sort - O(n2)
– Merge Sort - O(n log n)
– Matrix Multiplication – O(n3)
– Huffman Coding - O(nlogn)
– Single Source Shortest Path – O(ve)
– Min-Spanning Tree - O(elogv)
• As a research work more fast algorithms are required
• P is a subset of NP
Exponential Time Algorithms (EXP)

• EXP time algorithms


– 0/1 Knapsack - O(2n)
– TSP - O(2n)
– Hamiltonian Circuit - O(2n)
– Graph Coloring - O(2n)

• 2n is very time consuming,


• So, as a research work P time is required for EXP algorithms
• No solution was found in P time
Nondeterministic Polynomial Time Algorithms (NP)

• When we are enable to write a deterministic P time algorithms for


EXP time algorithms, then:
– Writing Nondeterministic algorithms (NP) in P time by
leaving blanks for nondeterministic steps till some body finds the
solution in P time, then we just fill the blank.
– Showing the relationship between the EXP algorithms, so, if
one is solved, then all other related problems will be solved.
• By writing NP algorithms or Showing the relationship between them,
a framework is done to preserve the research work by classifying
the problems into NP-Hard and NP-Complete.
Relating the EXP-time Problems

Terminologies
literal: (negated or not) Boolean variable
negation: not ()
Examples: x, x conjunction: and ()
clause: several literals connected with  disjunction: or ()

Example: (xyz)

CNF (Conjunctive Normal Form): several clauses connected with 

Example: (x y)(xyz)

3CNF: a CNF formula with three literals in each clause.

Example: (xyz)(xyz)

10
Relating the EXP-time Problems

• If we enable to solve the EXP algorithms in P time, at least we try to


relate them, by finding the relationship between them.
• To relate them we need a base problem
– CNF-Satisfiability can be taken as a base problem
• Satisfiability problem: given a logical expression CNF, find an
assignment of values (F, T) to variables xi that causes CNF be
evaluated to True
3-CNF = (x1   x2  x3 )  ( x1  x2   x3 )
xi = {x1,x2,x3}
• CFN is a special case of SAT
• xi is in “Conjuctive Normal Form” (CNF)
– “AND” of expressions (i.e., clauses)
– Each clause contains only “OR”s of the variables and their
complements
• Satisfiability means find out for what xi values this formula is
evaluated to True.
CNF-Satisfiability (Sat)

• To find the solution of CNF-Sat:


3-CNF = (x1   x2  x3 ) AND ( x1  x2   x3 )
xi = {x1,x2,x3}
• We should try all the possible Boolean values of
• xi = {000, 001,010,011,100,101,110,111}.
xi -> 23 -> 2n
So, that means Sat requires EXP time.
• Now we need to relate EXP time algorithm by taking Sat as base
problem.
• The solution of Sat can be represented by State Space Tree, where
the path from the root to the leaves gives solution, so the tree shows
all 8 possible values of xi = {000, 001,010,011,100,101,110,111}.
• So, we need to show EXP Problems that is similar or related to Sat,
if we solve any one of them in P time, then all other EXP Problems
can be solved in P time.
CNF-Satisfiability (Sat)

• Let us take 0/1 Knapsack problem as an example:


– let profit(p) = {5,6,3},
– weight(w)={3,5,8},
– n=3, maximum size(m)=10,
– then the solution will be xi={0/1,0/1,0/1} -> 23 -> 2n,
– and this the same solution of Sat
• So, the state space tree of this problem is the same for Sat and 0/1 Knapsack
• All the EXP time algorithms are classified as Hard problems (sine they are time
consuming problems)
• Since Sat is taken as base, it is classified as NP-Hard Problem
• Then, we have to show the hard problems if they are related to the base
problem or solved.
• So, we show the relationship.
NP-Hard: Reduction

• How to show the relationship?


– The procedure is: Reduction
• Sat reduces (converted) in P time into a Problem (L):
Sat α L ( conversion in P time)
• If for example Sat α 0/1 Knapsack ( in P time),
– we take an instance of Sat I1
– and converted into instance of 0/1 Knapsack I2 in P time,
– if an algorithm can solve I2 in P time,
– then the same algorithm can solve Sat and vise versa.
• It is clear that 0/1 Knapsack is reducible (similar) by Sat in P, then it is
also classified as NP-Hard,
• So, any problem can be reducible by Sat, it becomes NP-Hard
instead of hard problem.
NP-Complete (NPC)

• Since:
– Sat may reduces (converted) in P time into a Problem (L):
Sat α L (in P)
– As Sat is taken as base problem, it is NP-Hard Problem
– L is reducible by Sat in p, it becomes NP-Hard
• If there is Nondeterministic algorithm (NP) for an NP-Hard problem,
then such a problem reaches NP-Complete class.
• Nondeterministic algorithm is exist (proofed) for Sat, so, Sat is NP-Hard
and NP-Complete.
• If any NP-Hard problem has NP time algorithm, then that problem
becomes NP-Complete (NPC).
• NPC problems are the hardest NP problems
• So far, no polynomial time algorithms are found for any of NPC
problems.
• However, “P is equal to NP or not” is currently unknown
Why we study NPC?

• One of the most important reasons is:


– If you see a problem is NPC, you can stop from
spending time and energy to develop a fast
polynomial-time algorithm to solve it.
• NP-Hard and NP-Complete Diagram

You might also like