P, NP & Approximation algorithms
P, NP & Approximation algorithms
&
Approximation algorithms
P, NP, NP-Hard and NP-Complete Problems:
Based on Time Complexity:
1. Polynomial Time Algorithms: (Time complexity Less)
Example: Linear search-𝑂(𝑛)
Bubble sort-𝑂(𝑛 )
Merge sort-𝑂(𝑛𝑙𝑜𝑔𝑛)
2. Non-polynomial or Exponential Algorithms: (Time complexity is High)
Example: Travelling Salesman Problem-𝑂(𝑛² ∗ 2 )
Knapsack Problem -𝑂(2 / )
Based on Result:
1. Decision problems:
• Result is yes or no {0,1}
2. Optimization problems
• Result is a number representing an objective value
P, NP, NP-Hard and NP-Complete Problems:
Comparison:
P, NP, NP-Hard and NP-Complete Problems:
• Problems that yield solution (Algorithms that gives answer) in
Polynomial time are classified as “P Problems”
(Tractable/Deterministic Problems)
• Problems which can be solved in polynomial time, which take time
like O(n), O(n^2), O(n^3), O(log n), O(n*log n)….
• Eg: finding maximum element in an array or to check whether a string is
palindrome or not. so, there are many problems which can be solved in
polynomial time.
Konigsberg Bridge Problem :
• Pass over all 7 bridges exactly once!
• Solution: (Euler's analysis) Convert this
Problem into Graph theory.
• Land is denoted by vertices and bridges by
edges. For Graph to be traversable,
All vertices of Graph must be of EVEN degree
OR Exactly two vertices are of ODD, and rest
must be EVEN!
As all vertices of this graph are ODD degree,
solution does not exist (Proved) and this itself is
a solution for this problem!
Every Problem in the word have a Solution too.
In graph theory, an Eulerian trail (or Eulerian path) is a trail in a finite graph that visits every edge exactly once (allowing
for revisiting vertices). Similarly, an Eulerian circuit or Eulerian cycle is an Eulerian trail that starts and ends on the same
vertex.
P, NP, NP-Hard and NP-Complete Problems:
P Class Problems:
• P is set of problems that can be solved (deterministic) in Polynomial Time
Example: Linear Search, Binary search
NP Class Problem:
• NP is set of problems that can be solved in exponential (non-
deterministic Polynomial-NP) Time
• But these kind of problem can be verified in Polynomial Time
NP- Problems :
• Problems that take exponential time to yield solutions, these solutions can be
verified by Non-deterministic Polynomial time are classified as NP-Problems
(Non-deterministic/Non-tractable Problems).
• NP- Non-deterministic Polynomial time solving. Problem which can't be
solved in polynomial time but are checkable in polynomial time means that
given a solution of a problem , we can check that whether the solution is
correct or not in polynomial time.
• Example: TSP, Graph coloring, Hamiltonian cycle etc.…
• NP is set of decision problems that can be solved by a Non-deterministic
Turing Machine. P is subset of NP.
• Informally, NP is set of decision problems which can be solved by a
polynomial time via a “Lucky Algorithm or Guess Algorithm”, a magical
algorithm that always makes a right guess among the given set of choices
NP problems :
• Graph theory has these fascinating(annoying?) pairs of problems
– Shortest path algorithms?
– Longest path is NP complete (we’ll define NP complete later)
– Eulerian tours (visit every vertex but cover every edge only once,
even degree etc). Solvable in polynomial time!
– Hamiltonian tours (visit every vertex, no vertices can be
repeated). NP complete
Hamiltonian cycles :
• Determining whether a directed graph has a Hamiltonian cycle
does not have a polynomial time algorithm (yet!)
• However, if someone was to give you a sequence of vertices,
determining whether that sequence forms a Hamiltonian cycle
can be done in polynomial time
• Therefore, Hamiltonian cycles are in NP
SAT :
• A Boolean formula is satisfiable if there exists
some assignment of the values 0 and 1 to its variables that causes it
to evaluate
to 1.
• CNF – Conjunctive Normal Form. ANDing of clauses of ORs
2-CNF SAT :
• Each or operation has two arguments that are either variables or
negation of variables
• The problem in 2 CNF SAT is to find true/false(0 or 1) assignments
to the variables to make the entire formula true.
(x y)(y z)(xz)(z y)
x
y
x
y
z
z
Satisfiability via path finding :
• If there is a path from
• And if there is a path from
• Then FAIL!
• How to find paths in graphs?
– DFS/BFS and modifications thereof
3 CNF SAT (3 SAT) :
• Not so easy anymore.
• Implication graph cannot be constructed
• No known polytime algorithm
• Is it NP?
– If someone gives you a solution how long does it take to verify it?
– Make one pass through the formula and check
• This is an NP problem
P, NP, NP-Hard and NP-Complete Problems:
NP-Class:
NP Class • Solved in Non-polynomial Time
• Verified in Polynomial Time
• Intractable Problem
P Class
P-Class:
• Solved in Polynomial Time
• Verified in Polynomial Time
• Tractable Problem
• CLIQUE problem
– A clique is a subgraph of a graph such that all the vertices in this
subgraph are connected with each other that is the subgraph is a
complete graph.
– The Maximal Clique Problem is to find the maximum sized clique of a
given graph G, that is a complete graph which is a subgraph of G and
contains the maximum number of vertices.
P, NP, NP-Hard and NP-Complete Problems:
An example reduction :
• Even if the vertices come from different clauses, do not connect if it results in incompatibility. No variable should be
connected to its not.
┐x3 x3 x3
P, NP, NP-Hard and NP-Complete Problems:
Vertex cover problem :
• A vertex cover of an undirected graph 𝐺 = (𝑉, 𝐸) is a subset of vertices such that
every edge is incident to at least one of the vertices
NP Class
Reduce NP-Hard
Class
P Class
• Feasible Solution A subset C ⊆ V such that at least one vertex of every edge of
G belongs C.
• Value The value of the solution is the size of the cover, |C|, and the goal is to
minimize it.
Approximation Algorithm:
Vertex Cover:
Example-
Approximation Algorithm:
Vertex Cover:
Greedy algorithm 1 :
1. C ← ∅
3. C = C ∪ {u, v}
6. return C
Approximation Algorithm:
Vertex Cover:
Example- algorithm 1 :
Approximation Algorithm:
Vertex Cover:
Performance analysis of algorithm 1:
1. The edges picked by the algorithm is a maximal matching (say M), hence C is a vertex cover.
4. Hence C ∗ contains at least one of the end points of each edge in M, implies |C ∗ | ≥ |M|.
• The order in which he visits cities does not matter and he visits each city
during his trip and finishes where he was at first.
• The salesman wants to keep the travel cost he travels as low as possible
Approximation Algorithm:
Reducing TSP to graph problem:
Feasible Solution Find a cycle that visits each vertex exactly once.
Value The value of the solution is the sum of the weights associated with edges
in the cycle, and the goal is to find a cycle whose total weight is minimum.
Approximation Algorithm:
Approximation Algorithm for EUCLIDEAN TSP:
Approximation Algorithm:
Approximation Algorithm for EUCLIDEAN TSP (Example):
Approximation Algorithm:
Approximation ratio: