DAA Unit 5 Part 2
DAA Unit 5 Part 2
P(Polynomial Time)
• Problems in the class P can be solved by a deterministic algorithm in polynomial time.
This means there exists an algorithm that can find a solution efficiently (in time
proportional to a polynomial of the input size, nk, where k is a constant).
• The P in the P class stands for Polynomial Time. It is the collection of decision
problems(problems with a “yes” or “no” answer) that can be solved by a deterministic
machine in polynomial time.
• Key Idea: Problems that are "easy" or "efficiently solvable" on a standard computer.
Examples:
• Sorting numbers (e.g., merge sort, O(nlogn)).
• Finding the shortest path in a graph (e.g., Dijkstra's algorithm).
• Multiplying two matrices.
Features:
• The solution to P problems is easy to find.
• P is often a class of computational problems that are solvable and tractable. Tractable
means that the problems can be solved in theory as well as in practice.
NP (Nondeterministic Polynomial Time)
• Problems in the class NP have solutions that can be verified in polynomial time by a
deterministic algorithm. However, finding the solution may not necessarily be efficient.
The NP in NP class stands for Non-deterministic Polynomial Time. It is the collection of
decision problems that can be solved by a non-deterministic machine in polynomial time.
• Key Idea: Problems for which verifying a solution is "easy," but finding it may be "hard."
Examples:
• The Travelling Salesman Problem (TSP): Given cities and distances between them, find
the shortest route visiting all cities exactly once.
• Verifying a given solution (e.g., total route length) is quick.
• Finding the shortest route is challenging.
• The Subset Sum Problem: Does a subset of numbers add up to a target value?
• Features:
• The solutions of the NP class are hard to find since they are being solved by a non-
deterministic machine but the solutions are easy to verify.
• Problems of NP can be verified by a Turing machine in polynomial time.
NP-Hard Problem
• An NP-Hard problem is a type of computational problem that is at least as hard as the
hardest problems in NP. However, an NP-Hard problem does not necessarily belong
to NP, meaning:
• It might not have a verifiable solution in polynomial time.
• Solving any NP-Hard problem efficiently (in polynomial time) would imply that every
problem in NP can also be solved efficiently.
Characteristics of NP-Hard Problems
• At least as hard as NP problems:
• Every NP problem can be reduced to an NP-Hard problem in polynomial time.
• May not belong to NP:
• NP problems require solutions to be verifiable in polynomial time. NP-Hard problems
may not meet this criterion.
• No guaranteed polynomial-time solution:
• There is no known polynomial-time algorithm for NP-Hard problems unless P=NPP =
NPP=NP.
NP-Hard Problem
Examples of NP-Hard Problems
1. Traveling Salesman Problem (TSP)
• Description: Given a list of cities and the distances between them, find the shortest
possible route that visits each city exactly once and returns to the origin city.
• Why NP-Hard:
• Finding the shortest route is computationally difficult.
• Even approximate solutions are computationally intensive for large instances.
Algorithm
• Step 1 − Select any random edge from the input graph and mark all the edges that are
incident on the vertices corresponding to the selected edge.
• Step 2 − Add the vertices of the arbitrary edge to an output set.
• Step 3 − Repeat Step 1 on the remaining unmarked edges of the graph and add the
vertices chosen to the output until there’s no edge left unmarked.
• Step 4 − The final output set achieved would be the near-optimal vertex cover.
Approximation Algorithms
Psuedocode
APPROX-VERTEX_COVER (G: Graph)
c←{}
E’ ← E[G] while E’ is not empty do
Let (u, v) be an arbitrary edge of E’
c ← c U {u, v}
Remove from E’ every edge incident on either u or v
return c
Approximation Algorithms