Unit 5
Unit 5
NP-hard
If a polynomial time algorithm exists for any of these problems, all problems in NP would be
polynomial time solvable. These problems are called NP-complete. The phenomenon of NP-
completeness is important for both theoretical and practical reasons.
Definition of NP-Completeness
B is in NP
If a language satisfies the second property, but not necessarily the first one, the language B is
known as NP-Hard. Informally, a search problem B is NP-Hard if there exists some NP-Complete
problem A that Turing reduces to B.
The problem in NP-Hard cannot be solved in polynomial time, until P = NP. If a problem is
proved to be NPC, there is no need to waste time on trying to find an efficient algorithm for it.
Instead, we can focus on design approximation algorithm.
NP-Complete Problems
Following are some NP-Complete problems, for which no polynomial time algorithm is known.
NP-Hard Problems
Set Cover
Vertex Cover
TSP is NP-Complete
The traveling salesman problem consists of a salesman and a set of cities. The salesman has to
visit each one of the cities starting from a certain one and returning to the same city. The
challenge of the problem is that the traveling salesman wants to minimize the total length of
the trip
Proof
To prove TSP is NP-Complete, first we have to prove that TSP belongs to NP. In TSP, we find a
tour and check that the tour contains each vertex once. Then the total cost of the edges of the
tour is calculated. Finally, we check if the cost is minimum. This can be completed in polynomial
time. Thus TSP belongs to NP.
Secondly, we have to prove that TSP is NP-hard. To prove this, one way is to show that
Hamiltonian cycle ≤p TSP (as we know that the Hamiltonian cycle problem is NPcomplete).
Hence, an instance of TSP is constructed. We create the complete graph G' = (V, E'), where
E′={(i,j):i,j∈Vandi≠j
t(i,j)={01if(i,j)∈Eotherwise
Now, suppose that a Hamiltonian cycle h exists in G. It is clear that the cost of each edge in h is
0 in G' as each edge belongs to E. Therefore, h has a cost of 0 in G'. Thus, if graph G has a
Hamiltonian cycle, then graph G' has a tour of 0 cost.
Conversely, we assume that G' has a tour h' of cost at most 0. The cost of edges in E' are 0 and 1
by definition. Hence, each edge must have a cost of 0 as the cost of h' is 0. We therefore
conclude that h' contains only edges in E.
We have thus proven that G has a Hamiltonian cycle, if and only if G' has a tour of cost at most
0. TSP is NP-complete.
Polynomial Time:
What do you mean by polynomial time?
(definition) Definition: When the execution time of a computation, m(n), is no more than a
polynomial function of the problem size, n. More formally m(n) = O(n k) where k is a constant.
Polynomial-Time Algorithms
For example, most algorithms on arrays can use the array size, n, as the input size. To find the
largest element in an array requires a single pass through the array, so the algorithm for doing
this is O(n), or linear time.
Sorting algorithms usually require either O(n log n) or O(n2) time. Bubble sort takes linear time
in the best case, but O(n2) time in the average and worst cases. Heapsort takes O(n log n) time in
all cases. Quicksort takes O(n log n) time on average, but O(n2) time in the worst case.
The base of the logarithms is irrelevant, since the difference is a constant factor, which
we ignore; and
Although n log n is not, strictly speaking, a polynomial, the size of n log n is bounded by
n2, which is a polynomial.
Probably all the programming tasks you are familiar with have polynomial-time solutions. This
is not because all practical problems have polynomial-time solutions. Rather, it is because your
courses and your day-to-day work have avoided problems for which there is no known practical
solution.
NP-Completeness:
We have been writing about efficient algorithms to solve complex problems, like shortest
path, Euler graph, minimum spanning tree, etc. Those were all success stories of
algorithm designers. In this post, failure stories of computer science are discussed.
Can all computational problems be solved by a computer? There are computational
problems that can not be solved by algorithms even with unlimited time. For example
Turing Halting problem (Given a program and an input, whether the program will
eventually halt when run with that input, or will run forever). Alan Turing proved that a
general algorithm to solve the halting problem for all possible program-input pairs cannot
exist. A key part of the proof is, Turing machine was used as a mathematical definition of
a computer and program (Halting Problem).
Status of NP Complete problems is another failure story, NP complete problems are
problems whose status is unknown. No polynomial time algorithm has yet been
discovered for any NP complete problem, nor has anybody yet been able to prove that no
polynomial-time algorithm exists for any of them. The interesting part is, if any one of
the NP complete problems can be solved in polynomial time, then all of them can be
solved.
NP-Completeness Proofs:
For example, simple sort algorithms run approximately in time kN^2/2, where N is the number of
elements being sorted, and k is a proportionality constant that is CPU-dependent and specific to
this problem. Multiplication of NxN matrices runs approximately in time kN^3, where we are
talking about a different k for this different problem. If you want to get picky, there are other terms
in that polynomial, but they don’t matter as much as the n^3 term.
17. Define Non-Polynomial time or exponential time.
Non-deterministic polynomial time (NP) is actually a marker used to point to a set of problems and bounds
of the capability of certain types of computing. NP refers to the set of problems that can be solved in
polynomial time by a non-deterministic Turing machine
18. What is Decision Based Problem? Give example.
Decision problem is a computational problem that can be posed as a yes–no question of the input values.
An example of a decision problem is deciding by means of an algorithm whether a given natural number is
prime. Another is the problem “given two numbers x and y, does x evenly divide y?” The answer is either
‘yes’ or ‘no’ depending upon the values of x and y. A method for solving a decision problem, given in the
form of an algorithm, is called a decision procedure for that problem
19. Define NP-hard class: -
In computational complexity theory, NP-hardness (non-deterministic polynomial-time hardness) is the
defining property of a class of problems that are informally "at least as hard as the hardest problems in NP".
A simple example of an NP-hard problem is the subset sum problem.
20. Define NP-complete class: -
In computational complexity theory, a problem is NP-complete when: it is a problem for which the
correctness of each solution can be verified quickly (namely, in polynomial time) and a brute-force search
algorithm can find a solution by trying all possible solutions.
21. Define optimization problem. Give example.
An optimization problem consists to find the best solution among all possible ones. For example, in the Bin
Packing Problem (BPP) the aim is to find the right number of boxes of a given size to store a set of objects
of given sizes; optimization involves, for example, finding the smallest number of boxes.
22. What is 2CNF SAT?
The “2” in this name stands for the number of literals per clause, and “CNF” stands for conjunctive normal
form, a type of Boolean expression in the form of a conjunction of disjunctions.
Each clause in a 2-CNF formula is logically equivalent to an implication from one variable or negated
variable to the other.
Part – B
1. How will you prove that the problem is NP-Complete? Explain in detail.
NP-complete problem, any of a class of computational problems for which no efficient solution algorithm
has been found. Many significant computer-science problems belong to this class—e.g., the traveling
salesman problem, satisfiability problems, and graph-covering problems. Hamiltonian circuit, bin packing,
partition problems are NP complete problems.
There must be some first NP-Complete problem proved by the definition of NP-Complete problems. SAT
(Boolean satisfiability problem) is the first NP-Complete problem proved by Cook (See CLRS book for
proof).
It is always useful to know about NP-Completeness even for engineers. Suppose you are asked to write an
efficient algorithm to solve an extremely important problem for your company. After a lot of thinking, you
can only come up exponential time approach which is impractical. If you don’t know about NP-
Completeness, you can only say that I could not come up with an efficient algorithm. If you know about
NP-Completeness and prove that the problem is NP-complete, you can proudly say that the polynomial-
time solution is unlikely to exist. If there is a polynomial-time solution possible, then that solution solves a
big problem of computer science many scientists have been trying for years.
NP-complete problems are the hardest problems in the NP set. A decision problem L is NP-
complete if:
1) L is in NP (Any given solution for NP-complete problems can be verified quickly, but there is
no efficient known solution).
2) Every problem in NP is reducible to L in polynomial time (Reduction is defined below).
A problem is NP-Hard if it follows property 2 mentioned above, and doesn’t need to follow
property 1. Therefore, the NP-Complete set is also a subset of the NP-Hard set.
From the definition of NP-complete, it appears impossible to prove that a problem L is NP-
Complete. By definition, it requires us to that show every problem in NP is polynomial time
reducible to L. Fortunately, there is an alternate way to prove it. The idea is to take a known
NP-Complete problem and reduce it to L. If a polynomial-time reduction is possible, we can prove
that L is NP-Complete by transitivity of reduction (If an NP-Complete problem is reducible to L
in polynomial time, then all problems are reducible to L in polynomial time).
2. Discuss about any NP completeness problem with example.[JAN2022] (refer notes)
NP-complete problem, any of a class of computational problems for which no efficient
solution algorithm has been found. Many significant computer-science problems belong to this class—e.g.,
the traveling salesman problem, satisfiability problems, and graph-covering problems.
Traveling salesman problem, an optimization problem in graph theory in which the nodes (cities) of a graph
are connected by directed edges (routes), where the weight of an edge indicates the distance between two
cities. The problem is to find a path that visits each city once, returns to the starting city, and minimizes the
distance traveled. The only known general solution algorithm that guarantees the shortest path requires a
solution time that grows exponentially with the problem size (i.e., the number of cities). This is an example
of an NP-complete problem (from nonpolynomial), for which no known efficient (i.e., polynomial
time) algorithm exists.
Part – C
Medium -NP
Using the diagram, we assume that {P} and {NP} are not the same set, or, in other words, we assume that
{P} not equal to {NP}. This is our apparently-true, but yet-unproven assertion. Of course, another
interesting aspect of this diagram is that we’ve got some overlap between \mathcal{NP} and
\mathcal{NP}\text{-}Hard. We call NP-Complete when the problem belongs to both of these sets.
Alright, so, we’ve mapped {P}, {NP}, NP Complete and NP-Hard to “easy”, “medium”, “hard” and
“hardest”.
Polynomial Algorithms
The first set of problems are polynomial algorithms that we can solve in polynomial time, like logarithmic,
linear or quadratic time. If an algorithm is polynomial, we can formally define its time complexity as:
T(n) = O(C * n^{k}) where C > 0 and k > 0 where C and k are constants and n is input size. In general, for
polynomial-time algorithms \textbf{\textit{k}} is expected to be less than \textbf{\textit{n}}. Many
algorithms complete in polynomial time:
All basic mathematical operations; addition, subtraction, division, multiplication
Testing for primacy (Priority)
Hashtable lookup, string operations, sorting problems
Shortest Path Algorithms; Djikstra, Bellman-Ford, Floyd-Warshall
Linear and Binary Search Algorithms for a given set of numbers
NP Algorithms
The second set of problems cannot be solved in polynomial time. However, they can be verified (or
certified) in polynomial time. We expect these algorithms to have an exponential complexity, which we’ll
define as: T(n) = O(C_1 * k^{C_2 * n}) where C_1 > 0, C_2 > 0 and k > 0 where C_1, C_2 and k are
constants and n is the input size. T(n) is a function of exponential-time when at least C_1=1 and C_2=1. As
a result, we get O(k^n). For example, we’ll see complexities like O(n^n), O(2^n), O(2^{0.000001*n}) in
this set of problems. There are several algorithms that fit this description. Among them are:
Traveling Salesman
Knapsack, and
Graph Coloring
Curiously, what they have in common, aside from being in NP, is that each can be reduced into the other
in polynomial time. These facts together place them in NP-Complete. The major and primary work of NP-
Completeness belongs to Karp. And his 21, NP-Complete problems are fundamental to this theoretical
computer science topics. These works are founded on the Cook-Levin theorem and prove that the
Satisfiability (SAT) problem is NP-Complete:
NP-Hard Algorithms
Our last set of problems contains the hardest, most complex problems in computer science. They are not
only hard to solve but are hard to verify as well. In fact, some of these problems aren’t even decidable.
Among the hardest computer science problems are:
K-means Clustering
Traveling Salesman Problem, and
Graph Coloring
These algorithms have a property similar to ones in NP-Complete – they can all be reduced to any problem
in NP. Because of that, these are in NP-Hard and are at least as hard as any other problem in {NP}. A
problem can be both in NP and NP-Hard, which is another aspect of being NP-Complete.
This characteristic has led to a debate about whether or not Traveling Salesman is indeed NP-Complete.
Since {NP} and NP-Complete problems can be verified in polynomial time, proving that an algorithm
cannot be verified in polynomial time is also sufficient for placing the algorithm in NP-Hard.