8 Np Completeness

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.

NP Completeness: notes

Tractable and Intractable Problems, Concept of Polynomial Time and Super Polynomial Time Complexity

A problem is tractable if some algorithm solves it in time bounded by a polynomial in the input size $n$, that is $O(n^k)$ for a fixed constant $k$; sorting, searching, and matrix multiplication are tractable. A problem is intractable if every known algorithm needs super-polynomial time, growing faster than any polynomial, such as $O(2^n)$ or $O(n!)$; the Towers of Hanoi and brute-force enumeration of all permutations of $n$ items are examples. Ranked by growth rate: $O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(n^3) < \cdots < O(2^n) < O(n!) < O(n^n)$; everything up to and including any fixed power of $n$ is polynomial, and $O(2^n)$ onward is super-polynomial. Polynomial-time algorithms are informally called "efficient" or "fast", and super-polynomial ones "slow", because the running time gap between them explodes as $n$ grows, even though a polynomial with a very large exponent can be slower in practice than an exponential algorithm for small $n$.

Complexity Classes: P, NP, NP-Hard and NP-Complete

P is the class of decision problems solvable by a deterministic algorithm in polynomial time; P problems are exactly the tractable ones, such as testing whether a number is prime by trial division up to $\sqrt n$ done properly, or computing a shortest path.

NP ("nondeterministic polynomial time") is the class of decision problems for which a proposed solution (a certificate) can be verified in polynomial time, whether or not the solution itself can be found efficiently. Every problem in P is in NP, since a solution can trivially be found and hence verified in polynomial time, but whether $\text{P}=\text{NP}$, whether every efficiently checkable problem is also efficiently solvable, is the most famous open problem in computer science. Boolean satisfiability is the canonical example: guessing a truth assignment and checking whether it satisfies a formula takes only polynomial time, even though no polynomial-time way to find a satisfying assignment is known.

NP-hard describes a problem $X$ to which every problem in NP can be reduced in polynomial time; $X$ is at least as hard as everything in NP, but $X$ itself need not be in NP (it might not even be a decision problem, or might not have polynomial-time-checkable certificates).

NP-complete describes a problem that is both in NP and NP-hard: it is one of the hardest problems inside NP itself. If any single NP-complete problem were solved in polynomial time, every problem in NP would follow, since all of them reduce to it, which is exactly why P versus NP hinges on the NP-complete problems.

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 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:

  1. 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.

  2. 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.

  3. 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.

  4. Heuristic approach: Approximation algorithms (also called heuristic algorithms) use smart strategies to avoid exhaustive search while still producing useful results.

  5. 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:

FeatureExact AlgorithmApproximation Algorithm
Solution qualityOptimalNear-optimal
Time complexityExponential (for NP)Polynomial
Practical useSmall inputs onlyLarge real-world inputs
GuaranteeBest solutionBounded 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.

Open this answer on its own page →

2078 · 5 marks

Explain in brief about the complexity classes P, NP and NP Complete. [5]

Definition: P is the class of decision problems that can be solved by a deterministic algorithm in polynomial time, i.e., in O(n^k) steps for some non-negative integer k, where n is the size of the input. - These problems are considered "fast" or efficiently solvable. - Examples: Sorting, Searchi...

Open this answer on its own page →

2076 · 5 marks

Explain in brief about the classes P, NP, and NP complete with examples. [5]

--- Definition: P is the class of decision problems that can be solved by a deterministic algorithm in polynomial time, i.e., in O(n^k) for some constant k. - These are considered tractable (efficiently solvable) problems. Examples: - Sorting (Merge Sort: O(n log n)) - Searching (Binary Search: O...

Open this answer on its own page →

Approximation Algorithms

2081 · 5 marks

Explain the approximation algorithm for vertex cover of a connected graph with an example. [5]

A Vertex Cover of a graph G = (V, E) is a set of vertices C ⊆ V such that every edge in G is incident to at least one vertex in C. The optimization problem is to find the vertex cover with the fewest vertices (optimal vertex cover C). Since finding the exact minimum vertex cover is NP-hard, we u...

Open this answer on its own page →

2076 · 5 marks

Explain the approximation for solving vertex cover with a suitable example. [5]

Show solved 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:

StepEdge PickedVertices Added to CEdges 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).

Open this answer on its own page →

NP Complete Problems, NP Completeness and Reducibility, Cooks Theorem, Proofs of NP Completeness

2081 · 5 marks

State cooks theorem. Discuss about problem reducibility. [5]

Statement: Cook's theorem states that the Boolean Satisfiability Problem (SAT) is NP-Complete. That is, any problem in NP can be reduced in polynomial time by a deterministic Turing machine to the problem of determining whether a Boolean formula is satisfiable. Formally: SAT is NP-Complete, meani...

Open this answer on its own page →

2080 · 5 marks

Define NP-complete problems with examples. Give brief proof of the statement "SAT is NP-complete". [5]

A problem X is called NP-complete if it satisfies both of the following conditions: 1. X belongs to NP: The problem X can be verified in polynomial time. That is, given a candidate solution (certificate), we can check whether it is correct in polynomial time. 2. X is NP-hard: Every other problem ...

Open this answer on its own page →

2078 · 5 marks

Write short notes on: a. NP Hard Problems and NP Completeness b. Problem Reduction [5]

--- A problem is called NP-Complete if it satisfies two conditions: 1. It belongs to the class NP (the solution can be verified in polynomial time). 2. Every other problem in NP can be reduced to it in polynomial time (it is NP-Hard). NP-Complete problems are the hardest problems in NP. If any si...

Open this answer on its own page →

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]

--- A problem is said to be tractable if it can be solved in polynomial time, i.e., its time complexity is O(n^k) for some constant k. These problems are considered efficiently solvable in practice. Examples: Sorting (O(n log n)), Binary Search (O(log n)), Shortest Path (Dijkstra's O(V^2)) A prob...

Open this answer on its own page →