Design and Analysis of Algorithms · Unit 8 · 5 hrs
NP Completeness
Exam-focused notes for NP Completeness (Design and Analysis of Algorithms, CSC325): what the TU syllabus asks and how it has actually been tested, with 9 solved past questions from this unit.
NP Completeness in short
This closing unit asks which problems can be solved efficiently at all. It sets out the P versus NP framework, proves that three well known problems are NP-complete by the reduction technique that underlies every such proof, and shows how approximation algorithms give provably good, if not optimal, answers to two of them in polynomial time.
What this unit covers
- Tractable and Intractable Problems, Concept of Polynomial Time and Super Polynomial Time Complexity
- Complexity Classes: P, NP, NP-Hard and NP-Complete
- NP Complete Problems, NP Completeness and Reducibility, Cooks Theorem, Proofs of NP Completeness (CNF-SAT, Vertex Cover and Subset Sum)
- Approximation Algorithms: Concept, Vertex Cover Problem, Subset Sum Problem
Complexity Classes
2082 · 5 marks
Define class P and NP problem. Why do we need approximation algorithms? Justify. [5]
Show solved answerHide answer
Class P and NP Problems, and Approximation Algorithms
Class P Problems
Class P (Polynomial time) is the class of decision problems that can be solved by a deterministic algorithm in polynomial time, i.e., in O(n^k) time for some constant k, where n is the size of the input.
- These are considered "efficiently solvable" problems.
- Examples: Sorting, Shortest path (Dijkstra's), GCD computation, Matrix multiplication.
Class NP Problems
Class NP (Non-deterministic Polynomial time) is the class of decision problems for which a given solution (certificate) can be verified in polynomial time by a deterministic algorithm, even though finding the solution may take exponential time.
- Every problem in P is also in NP (P ⊆ NP), but whether P = NP is still an unsolved open question.
- Examples: Travelling Salesman Problem (TSP), 0/1 Knapsack, N-Queens, Subset Sum, Graph Coloring.
Key Distinction:
- P: Can be solved in polynomial time.
- NP: Solution can be verified in polynomial time.
Why Do We Need Approximation Algorithms? (Justification):
"An approximate algorithm is a way of approach NP-completeness for the optimization problem. This technique does not guarantee the best solution. The goal of an approximation algorithm is to come as close as possible to the optimum value in a reasonable amount of time which is at the most polynomial time."
Justification:
-
NP-complete problems are intractable: Many real-world optimization problems (TSP, Knapsack, Graph Coloring) are NP-complete. Exact algorithms for these problems require exponential time O(2^n), which is computationally infeasible for large inputs.
-
Polynomial time is needed in practice: Waiting exponential time for an exact solution is not practical in real-world applications. Approximation algorithms provide solutions in polynomial time.
-
Near-optimal solutions are acceptable: In many practical scenarios, a solution that is close to optimal (say within 10% of the best) is good enough. Approximation algorithms guarantee such near-optimal solutions.
-
Heuristic approach: Approximation algorithms (also called heuristic algorithms) use smart strategies to avoid exhaustive search while still producing useful results.
-
Guaranteed approximation ratio: A good approximation algorithm provides a provable bound on how far the solution is from the optimal, giving confidence in the result.
Summary Table:
| Feature | Exact Algorithm | Approximation Algorithm |
|---|---|---|
| Solution quality | Optimal | Near-optimal |
| Time complexity | Exponential (for NP) | Polynomial |
| Practical use | Small inputs only | Large real-world inputs |
| Guarantee | Best solution | Bounded error ratio |
Conclusion: Since NP-complete problems cannot be solved exactly in polynomial time (unless P = NP), approximation algorithms are essential to obtain good-enough solutions efficiently in real-world applications.
2078 · 5 marks
Explain in brief about the complexity classes P, NP and NP Complete. [5]
2076 · 5 marks
Explain in brief about the classes P, NP, and NP complete with examples. [5]
Approximation Algorithms
2081 · 5 marks
Explain the approximation algorithm for vertex cover of a connected graph with an example. [5]
2076 · 5 marks
Explain the approximation for solving vertex cover with a suitable example. [5]
Show solved answerHide answer
Approximation Algorithm for Vertex Cover
Definition
A vertex cover of an undirected graph G = (V, E) is a subset V' ⊆ V such that for every edge (u, v) ∈ E, at least one of u or v belongs to V'.
The optimization problem is to find the vertex cover with the fewest vertices. The approximation problem is to find a vertex cover with few vertices (not necessarily minimum, but within a guaranteed bound).
Approximation Algorithm (APPROX-VERTEX-COVER)
APPROX-VERTEX-COVER(G):
1. C = {} (empty set - will hold the vertex cover)
2. E' = E[G] (copy of all edges)
3. while E' is not empty:
a. Pick any edge (u, v) from E'
b. Add both u and v to C
C = C ∪ {u, v}
c. Remove from E' every edge incident to u or v
4. return C
Key Properties
- Time Complexity: O(V + E)
- Approximation Ratio: This algorithm is a 2-approximation, meaning:
|C| ≤ 2 × |C*|
where C* is the optimal (minimum) vertex cover.
Reason: Each edge picked in step 3(a) belongs to an optimal cover (at least one endpoint must be in C*). Since both endpoints are added, we add at most twice the optimal number of vertices.
Example
Consider the following graph:
Vertices: {1, 2, 3, 4, 5, 6, 7}
Edges: {(1,2), (1,3), (2,4), (3,4), (4,5), (4,6), (5,7)}
Step-by-step execution:
| Step | Edge Picked | Vertices Added to C | Edges Removed |
|---|---|---|---|
| 1 | (1, 2) | {1, 2} | (1,2), (1,3), (2,4) |
| 2 | (3, 4) | {1, 2, 3, 4} | (3,4), (4,5), (4,6) |
| 3 | (5, 7) | {1, 2, 3, 4, 5, 7} | (5,7) |
Result: C = {1, 2, 3, 4, 5, 7} → size = 6
Optimal cover: C* = {1, 4, 5} or {2, 3, 4, 7} → size = 3 or 4
Here |C| = 6 ≤ 2 × 3 = 6, which satisfies the 2-approximation guarantee.
Conclusion
The approximation algorithm for vertex cover guarantees a solution no worse than twice the optimal. It is efficient and practical because finding the exact minimum vertex cover is NP-hard, while this approximation runs in polynomial time O(V + E).
NP Complete Problems, NP Completeness and Reducibility, Cooks Theorem, Proofs of NP Completeness
2081 · 5 marks
State cooks theorem. Discuss about problem reducibility. [5]
2080 · 5 marks
Define NP-complete problems with examples. Give brief proof of the statement "SAT is NP-complete". [5]
2078 · 5 marks
Write short notes on: a. NP Hard Problems and NP Completeness b. Problem Reduction [5]
Tractable and Intractable Problems, Concept of Polynomial Time and Super Polynomial Time Complexity
2079 · 5 marks
Define tractable and intractable problem. Illustrate vertex cover problem with an example. [5]
Make Unit 8 stick
Practice CSC325 with flashcards & quizzes