Algorithms
Complexity Classes
Complexity questions are definition and recognition questions: know P, NP, NP-hard and NP-complete precisely, the direction of a reduction, the consequences of P = NP, and which classic problems are NP-complete versus easy.
GATE CSE complexity classes — decision problems, P, NP, NP-hard, NP-complete, polynomial-time reductions, Cook-Levin, and classic NP-complete problems (SAT, 3-SAT, clique, vertex cover, Hamiltonian cycle) at MCQ depth.
Learning outcomes
- Define P, NP, NP-hard and NP-complete and place a given problem in the correct class
- State the direction of polynomial reductions and the consequences of reducing between problems
- Recognise classic NP-complete problems and their polynomial-time easy variants
- Answer implication questions about P = NP, decidability and NP-hardness
Decision problems, P and NP
- A decision problem has a YES/NO answer. Optimisation problems (shortest tour) are converted to decision form ("is there a tour of length ≤ k?") for classification.
- P is the class of decision problems solvable by a deterministic algorithm in polynomial time (O(n^k) for some constant k), measured in the size of the input encoding.
- NP is the class of decision problems whose YES instances can be VERIFIED in polynomial time given a certificate (witness); equivalently, solvable in polynomial time by a non-deterministic Turing machine. NP does NOT mean "non-polynomial".
- P ⊆ NP, because a polynomial-time solver can verify by ignoring the certificate. Whether P = NP is the major open question; it is believed that P ≠ NP.
- Examples in P: sorting, shortest path, MST, bipartite matching, 2-SAT, linear programming, primality testing (AKS), connectivity, Euler circuit existence.
- co-NP: problems whose NO instances have short certificates (e.g., tautology). P is closed under complement; whether NP = co-NP is open.
| Class | Informal meaning | Example |
|---|---|---|
| P | Solvable in polynomial time | Shortest path, MST, 2-SAT |
| NP | Verifiable in polynomial time | SAT, clique, Hamiltonian cycle |
| NP-hard | At least as hard as every NP problem | TSP optimisation, halting problem |
| NP-complete | In NP and NP-hard | SAT, 3-SAT, vertex cover |
Worked example
Problem. Why is Hamiltonian cycle in NP?
Solution. A certificate is a sequence of vertices; we can check in O(V) time that it visits every vertex once and consecutive vertices are adjacent.
Reductions, NP-hard and NP-complete
- A ≤p B (A polynomially reduces to B) means there is a polynomial-time function f converting every instance x of A into an instance f(x) of B such that x is YES for A iff f(x) is YES for B.
- Consequences: if B ∈ P then A ∈ P; if A is NP-hard then B is NP-hard. A ≤p B means "B is at least as hard as A". Reductions are transitive.
- NP-hard: every problem in NP reduces to it in polynomial time (it need not be in NP and need not even be a decision problem or decidable).
- NP-complete = NP ∩ NP-hard: in NP and NP-hard. These are the hardest problems in NP.
- To prove problem X NP-complete: (1) show X ∈ NP (verifier), and (2) reduce a KNOWN NP-complete problem Y to X (Y ≤p X). The reduction direction is the classic trap.
- Cook-Levin theorem: SAT is NP-complete, the first problem proved so. Then 3-SAT, clique, vertex cover, Hamiltonian cycle and others follow by reductions.
- If any NP-complete problem has a polynomial-time algorithm then P = NP. If any NP-complete problem is proved not to be in P then P ≠ NP.
- Four-step template to prove X NP-complete: (1) show X ∈ NP with a certificate and a polynomial verifier; (2) pick a KNOWN NP-complete problem Y; (3) give a polynomial-time map f from instances of Y to instances of X; (4) prove x is YES for Y iff f(x) is YES for X (both directions).
- The reduction itself must run in polynomial time, so f(x) has size polynomial in |x|. A reduction that takes exponential time proves nothing about hardness.
- GATE uses many-one (Karp) reductions: one instance maps to one instance. Turing (Cook) reductions may call a solver for B several times; Cook-Levin hardness is usually stated with Karp reductions.
- Typical chain: SAT ≤p 3-SAT ≤p Clique ≤p Independent Set ≤p Vertex Cover, and Hamiltonian cycle ≤p TSP (complete graph with weight 1 on original edges, 2 on others; G has a Hamiltonian cycle iff a tour of length ≤ n exists).
Formulas to remember
-
A ≤p B and B ∈ P ⇒ A ∈ P -
A ≤p B and A NP-hard ⇒ B NP-hard -
NP-complete = NP ∩ NP-hard
Worked example
Problem. To show that problem X is NP-complete, which direction should the reduction go, given that SAT is NP-complete?
Solution. SAT ≤p X (reduce the known hard problem to X), plus X ∈ NP. Reducing X to SAT only shows X is no harder than SAT.
Classic NP-complete problems (recognition level)
- SAT (satisfiability of a Boolean formula) and 3-SAT (CNF with 3 literals per clause) are NP-complete. 2-SAT is in P (implication graph + SCC). SAT of formulas in DNF is in P.
- Clique (is there a complete subgraph of size k?), Independent Set (k mutually non-adjacent vertices), and Vertex Cover (k vertices touching every edge) are NP-complete. A set S is an independent set iff V \ S is a vertex cover, so IS of size k ⟺ VC of size n − k.
- A graph has a clique of size k iff its complement graph has an independent set of size k (this gives Clique ↔ Independent Set reductions).
- Hamiltonian cycle/path and the decision version of the Travelling Salesman Problem are NP-complete. Graph k-colouring is NP-complete for k ≥ 3 (2-colouring is in P).
- Subset sum, partition and 0/1 knapsack (decision) are NP-complete in the weak sense: DP solves them in pseudo-polynomial time Θ(nW), which is not polynomial in the bit-length of W.
- Set cover, bin packing and longest simple path are also NP-hard (their decision versions are NP-complete); they appear rarely in GATE, so recognition is enough.
- Certificates showing membership in NP: SAT — a satisfying assignment (check every clause); Clique — the k vertices (check all pairs are adjacent, O(k²)); Vertex Cover — the k vertices (check every edge has an endpoint in the set, O(E)); Hamiltonian cycle — the vertex order (O(V)); TSP — the tour (add up the lengths).
- 3-SAT ≤p Clique idea: make one vertex per literal occurrence, grouped by clause; join literals from DIFFERENT clauses unless they contradict (x and ¬x). A clique of size m (m = number of clauses) picks one consistent true literal per clause.
- The parameter matters: clique of fixed size k is O(n^k), polynomial for constant k, but NP-complete when k is part of the input. Similarly 2-colouring and 2-SAT are in P while 3-colouring and 3-SAT are NP-complete.
- Special-status problems: graph isomorphism and integer factoring are in NP but neither known to be in P nor known to be NP-complete (factoring is also in co-NP). Primality testing is in P (AKS algorithm).
| Hard (NP-complete) | Easy variant (in P) |
|---|---|
| 3-SAT | 2-SAT |
| Hamiltonian cycle | Euler circuit |
| Longest simple path | Shortest path (non-negative weights) |
| 3-colouring | 2-colouring (bipartiteness) |
| Travelling salesman (decision) | Minimum spanning tree |
| 0/1 knapsack (decision) | Fractional knapsack |
| Clique of size k (k in input) | Clique of fixed size k = 3 (O(n³)) |
Worked example
Problem. A graph with 10 vertices has a maximum independent set of size 4. What is the size of a minimum vertex cover?
Solution. A minimum vertex cover is the complement of a maximum independent set: 10 − 4 = 6.
Class relationships and consequences
- Believed picture (assuming P ≠ NP): P and NP-complete are disjoint subsets of NP; NP-hard extends beyond NP (e.g., the halting problem, TSP optimisation).
- If P = NP then every non-trivial problem in P or NP is NP-complete, and all of NP collapses to P. If P ≠ NP then no NP-complete problem is in P.
- Halting problem: undecidable, NP-hard but not in NP (it is not even decidable). Undecidable problems are outside every complexity class discussed here.
- Hierarchy of complexity classes: P ⊆ NP ⊆ PSPACE ⊆ EXPTIME. Only P ⊊ EXPTIME is known to be strict.
- Optimisation versions (find the minimum vertex cover) are NP-hard; their decision versions are NP-complete.
- Approximation and heuristics are practical responses to NP-hardness; GATE asks only the classification.
- Reading the Venn diagram (assuming P ≠ NP): P lies inside NP and is disjoint from NP-complete; NP-complete is exactly the overlap of NP and NP-hard; the part of NP-hard outside NP contains problems like the halting problem and TSP optimisation.
- If P = NP the picture collapses: P = NP = NP-complete (apart from the trivial languages ∅ and Σ*), while NP-hard still extends beyond NP because undecidable problems remain.
- co-NP contains the complements of NP problems (tautology, unsatisfiability). P ⊆ NP ∩ co-NP; if any NP-complete problem were in co-NP then NP = co-NP.
- Decidability link: every problem in NP is decidable by brute force in exponential time (NP ⊆ EXPTIME). The halting problem is undecidable, hence outside NP and not NP-complete.
- Space classes: P ⊆ NP ⊆ PSPACE (Savitch gives NPSPACE = PSPACE). Quantified Boolean formulas are PSPACE-complete; these appear only as recognition questions.
| Statement | True? |
|---|---|
| P ⊆ NP | Yes (proved) |
| NP-complete ⊆ NP | Yes (by definition) |
| P = NP | Open (believed false) |
| Any NP-complete problem in P ⇒ P = NP | Yes |
| NP-hard ⊆ NP | No (halting problem is NP-hard but not in NP) |
Worked example
Problem. If a polynomial-time algorithm is found for 3-SAT, what follows?
Solution. 3-SAT is NP-complete, so every problem in NP reduces to it in polynomial time: P = NP.
Worked trace — proving Vertex Cover NP-complete
- Claim: Vertex Cover (is there a set of at most k vertices touching every edge?) is NP-complete.
- Step 1 — in NP: the certificate is the set S with |S| ≤ k; the verifier scans all edges and checks that at least one endpoint lies in S, in O(E) time.
- Step 2 — choose the source problem: Independent Set (known NP-complete). We show IS ≤p VC.
- Step 3 — the map: f(G, k) = (G, n − k) where n = |V|. It copies the graph and subtracts two numbers, so it is clearly polynomial.
- Step 4 — correctness: S is an independent set iff V \ S is a vertex cover, because every edge has at most one endpoint in S, hence at least one outside S. So G has an independent set of size ≥ k iff G has a vertex cover of size ≤ n − k.
- Conclusion: IS ≤p VC and VC ∈ NP, so VC is NP-complete. The same transformation gives VC ≤p IS, so the two problems are equivalent.
Worked example
Problem. A graph with 8 vertices has a minimum vertex cover of size 3. What is its maximum independent set?
Solution. Maximum independent set = n − minimum vertex cover = 8 − 3 = 5.
GATE exam tricks — class questions
- Check the reduction direction: to prove X hard, reduce a known hard problem TO X. If instead you reduce X to a known easy problem, you prove X easy.
- NP-complete needs BOTH "in NP" and "NP-hard". A problem that is NP-hard but undecidable (halting) is not NP-complete.
- NP stands for NON-DETERMINISTIC polynomial, not "not polynomial". All of P is inside NP.
- Easy/hard twin pairs are favourites: 2-SAT vs 3-SAT, Euler vs Hamilton, shortest vs longest path, 2- vs 3-colouring, MST vs TSP, fractional vs 0/1 knapsack.
- Knapsack DP in Θ(nW) does NOT put knapsack in P because W is exponential in its bit length.
- Assume P ≠ NP when asked "which of the following is NOT known/likely to be NP-complete"; the answer is usually a polynomial-time solvable variant.
- Conversions: IS of size k ⟺ VC of size n − k; clique in G ⟺ independent set in complement of G.
- A polynomial-time verifier with a short certificate shows membership in NP, never in P.
- Worked example 2 (true/false drill): "A ≤p B and B ∈ NP ⇒ A ∈ NP" is TRUE; "A ≤p B and A ∈ NP ⇒ B ∈ NP" is FALSE; "A ≤p B, A NP-complete and B ∈ NP ⇒ B NP-complete" is TRUE.
- Worked example 3 (unary versus binary): subset sum with n numbers and target at most n² is solvable by DP in polynomial time, because the target is then polynomial in n. The problem is NP-complete only when numbers are encoded in binary and can be exponentially large.
- Language trick: "NP-hard" alone says nothing about membership in NP; "NP-complete" always implies membership in NP. If an option says "X is NP-hard, hence X is in NP", it is false.
Worked example
Problem. Which is NP-complete: shortest path with non-negative weights, MST, Hamiltonian cycle, or topological sort?
Solution. Only Hamiltonian cycle; the other three have polynomial-time algorithms.
Exam tips
- Memorise: NP-complete = NP ∩ NP-hard and P ⊆ NP.
- Always determine the direction of the reduction before choosing an option.
- Use the easy/hard twin pairs to eliminate options quickly.
- Remember pseudo-polynomial algorithms (knapsack, subset sum) do not show membership in P.
- For consequences, convert to: any NP-complete in P ⇒ P = NP.
10 GATE-style MCQs for Complexity Classes with full explanations. Attempt each question before revealing the answer.
If a polynomial-time algorithm is discovered for any NP-complete problem, then:
Show answer & explanation
Answer: A
Every NP problem reduces in polynomial time to an NP-complete problem, so a polynomial algorithm for one gives one for all of NP.
A problem is NP-complete if it is:
Show answer & explanation
Answer: C
By definition NP-complete = NP ∩ NP-hard.
To prove that a problem X in NP is NP-complete using reduction, one must:
Show answer & explanation
Answer: B
Reducing a known NP-hard problem Y to X (Y ≤p X) shows X is at least as hard as Y, hence NP-hard; together with X ∈ NP this gives NP-completeness.
Assuming P ≠ NP, which of the following is NOT NP-complete?
Show answer & explanation
Answer: C
2-SAT is solvable in polynomial time, so it is in P and is not NP-complete if P ≠ NP.
Which of the following problems is NP-complete?
Show answer & explanation
Answer: C
Hamiltonian cycle is a classic NP-complete problem; the others have polynomial-time algorithms.
A graph with n vertices has an independent set of size k. It must then have a vertex cover of size:
Show answer & explanation
Answer: B
If S is an independent set, every edge has at least one endpoint outside S, so V \ S is a vertex cover of size n − k.
Which of the following statements is certainly TRUE?
Show answer & explanation
Answer: A
A polynomial-time algorithm can verify certificates by ignoring them, so P ⊆ NP. The others are false or unknown.
The halting problem is:
Show answer & explanation
Answer: C
Every NP problem reduces to the halting problem, so it is NP-hard, but it is undecidable and therefore not in NP.
The class NP consists of decision problems for which:
Show answer & explanation
Answer: B
NP = problems with polynomial-time verifiable certificates (equivalently solvable by a non-deterministic polynomial-time machine).
The 0/1 knapsack decision problem is NP-complete even though DP solves it in Θ(nW). This is because:
Show answer & explanation
Answer: B
Polynomial time must be polynomial in the input length; W is encoded in log W bits, so nW can be exponential in that length.
Extra page-specific practice beyond the GATE samples. Attempt all 10, then reveal answers to check your reasoning.
If A ≤p B and B ≤p C, then:
Show answer & explanation
Answer: B) A ≤p C
Polynomial-time reductions are transitive: composing two polynomial-time functions is polynomial.
If A ≤p B and B ∈ P, then:
Show answer & explanation
Answer: A) A ∈ P
Convert A instances to B instances in polynomial time and then solve B in polynomial time.
Which of the following problems is solvable in polynomial time?
Show answer & explanation
Answer: C) 2-colourability
2-colourability is bipartiteness, tested by BFS in Θ(V + E).
The decision version of the travelling salesman problem is:
Show answer & explanation
Answer: B) NP-complete
A tour of length ≤ k is verifiable in polynomial time and Hamiltonian cycle reduces to it.
Given a sequence of vertices, verifying whether it is a Hamiltonian cycle takes:
Show answer & explanation
Answer: B) Polynomial (linear) time
Check that each vertex appears once and consecutive vertices (including last to first) are adjacent: O(V) with an adjacency matrix.
The Cook-Levin theorem states that:
Show answer & explanation
Answer: B) SAT is NP-complete
Cook (1971) and Levin showed every NP problem reduces to SAT.
Which of the following problems is in P?
Show answer & explanation
Answer: B) Existence of an Euler circuit
An Euler circuit exists iff the graph is connected (on non-isolated vertices) and every vertex has even degree, which is checked in polynomial time.
A graph with 10 vertices has a maximum independent set of size 4. The minimum vertex cover has size:
Show answer & explanation
Answer: C) 6
Minimum vertex cover = n − maximum independent set = 10 − 4 = 6.
The fractional knapsack problem is:
Show answer & explanation
Answer: B) In P
Greedy by value-to-weight ratio solves it in Θ(n log n).
Which of the following is true about class P?
Show answer & explanation
Answer: A) It is closed under complement
A deterministic polynomial-time decider for L can swap its YES and NO answers to decide the complement of L.
Topic FAQs
Does NP mean "not polynomial"?
No. NP means non-deterministic polynomial time: problems whose solutions can be verified in polynomial time. P is a subset of NP.
What is the difference between NP-hard and NP-complete?
NP-complete problems are NP-hard AND belong to NP; NP-hard problems may lie outside NP (even be undecidable).
Is 2-SAT NP-complete?
No, 2-SAT is solvable in polynomial (linear) time using the implication graph and strongly connected components, while 3-SAT is NP-complete.
Why is the Θ(nW) knapsack algorithm not polynomial time?
Polynomial time is measured in the input length (bits); W has only log W bits, so nW is exponential in the bit-length of W.
Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.