Skip to main content

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.

~3 hrs ~0.5–1 marks · Moderate 10 samples · 10 practice · 10 cards

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.
ClassInformal meaningExample
PSolvable in polynomial timeShortest path, MST, 2-SAT
NPVerifiable in polynomial timeSAT, clique, Hamiltonian cycle
NP-hardAt least as hard as every NP problemTSP optimisation, halting problem
NP-completeIn NP and NP-hardSAT, 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.

Problem A on the left, problem B on the right and an arrow labelled f from x to f of x showing A reduces to B in polynomial time
A ≤p B: instances of A map to instances of B in polynomial time, so B is at least as hard as A.

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-SAT2-SAT
Hamiltonian cycleEuler circuit
Longest simple pathShortest path (non-negative weights)
3-colouring2-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.

Six boxes for SAT, 3-SAT, Clique, Vertex Cover, Hamiltonian Cycle and TSP decision joined by a reduction chain
Classic NP-complete problems and a typical reduction chain: SAT → 3-SAT → Clique → Vertex Cover → Hamiltonian Cycle → TSP.

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.
StatementTrue?
P ⊆ NPYes (proved)
NP-complete ⊆ NPYes (by definition)
P = NPOpen (believed false)
Any NP-complete problem in P ⇒ P = NPYes
NP-hard ⊆ NPNo (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.

Venn diagram with P inside NP, NP-complete as the overlap of NP and NP-hard, and NP-hard extending outside NP
The standard GATE diagram, assuming P ≠ NP: NP-complete = NP ∩ NP-hard.

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.
Exam pattern · 10 questions

GATE sample questions & answers

10 GATE-style MCQs for Complexity Classes with full explanations. Attempt each question before revealing the answer.

Q1 P = NP consequence

If a polynomial-time algorithm is discovered for any NP-complete problem, then:

  • A) P = NP
  • B) P ≠ NP
  • C) NP = co-NP only
  • D) NP-hard = P
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.

Q2

A problem is NP-complete if it is:

  • A) In NP only
  • B) NP-hard only
  • C) In NP and NP-hard
  • D) In P and NP-hard
Show answer & explanation

Answer: C

By definition NP-complete = NP ∩ NP-hard.

Q3 Direction of reduction

To prove that a problem X in NP is NP-complete using reduction, one must:

  • A) Reduce X to a known NP-complete problem
  • B) Reduce a known NP-complete problem to X
  • C) Reduce X to a problem in P
  • D) Reduce a problem in P to X
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.

Q4

Assuming P ≠ NP, which of the following is NOT NP-complete?

  • A) 3-SAT
  • B) Vertex cover
  • C) 2-SAT
  • D) Hamiltonian cycle
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.

Q5

Which of the following problems is NP-complete?

  • A) Minimum spanning tree
  • B) Single-source shortest path with non-negative weights
  • C) Hamiltonian cycle
  • D) Topological sorting
Show answer & explanation

Answer: C

Hamiltonian cycle is a classic NP-complete problem; the others have polynomial-time algorithms.

Q6

A graph with n vertices has an independent set of size k. It must then have a vertex cover of size:

  • A) k
  • B) n − k
  • C) n + k
  • D) n/k
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.

Q7

Which of the following statements is certainly TRUE?

  • A) P ⊆ NP
  • B) NP ⊆ P
  • C) NP-hard ⊆ NP
  • D) P = NP-complete
Show answer & explanation

Answer: A

A polynomial-time algorithm can verify certificates by ignoring them, so P ⊆ NP. The others are false or unknown.

Q8

The halting problem is:

  • A) In NP
  • B) NP-complete
  • C) NP-hard but not in NP
  • D) In P
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.

Q9

The class NP consists of decision problems for which:

  • A) No polynomial-time algorithm exists
  • B) A proposed YES-solution can be verified in polynomial time
  • C) Solutions cannot be verified efficiently
  • D) Only exponential algorithms are known
Show answer & explanation

Answer: B

NP = problems with polynomial-time verifiable certificates (equivalently solvable by a non-deterministic polynomial-time machine).

Q10

The 0/1 knapsack decision problem is NP-complete even though DP solves it in Θ(nW). This is because:

  • A) DP is incorrect
  • B) W is exponential in the number of bits needed to represent it, so Θ(nW) is pseudo-polynomial
  • C) n is too large
  • D) Knapsack is undecidable
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.

Topic drill · 10 questions

10 practice questions — Complexity Classes

Extra page-specific practice beyond the GATE samples. Attempt all 10, then reveal answers to check your reasoning.

P1

If A ≤p B and B ≤p C, then:

  • A) C ≤p A
  • B) A ≤p C
  • C) A ∈ P
  • D) No relation between A and C
Show answer & explanation

Answer: B) A ≤p C

Polynomial-time reductions are transitive: composing two polynomial-time functions is polynomial.

P2

If A ≤p B and B ∈ P, then:

  • A) A ∈ P
  • B) A is NP-hard
  • C) B is NP-complete
  • D) P = NP
Show answer & explanation

Answer: A) A ∈ P

Convert A instances to B instances in polynomial time and then solve B in polynomial time.

P3

Which of the following problems is solvable in polynomial time?

  • A) 3-colourability
  • B) Hamiltonian cycle
  • C) 2-colourability
  • D) Vertex cover
Show answer & explanation

Answer: C) 2-colourability

2-colourability is bipartiteness, tested by BFS in Θ(V + E).

P4

The decision version of the travelling salesman problem is:

  • A) In P
  • B) NP-complete
  • C) Undecidable
  • D) Not in NP
Show answer & explanation

Answer: B) NP-complete

A tour of length ≤ k is verifiable in polynomial time and Hamiltonian cycle reduces to it.

P5

Given a sequence of vertices, verifying whether it is a Hamiltonian cycle takes:

  • A) Exponential time
  • B) Polynomial (linear) time
  • C) It cannot be verified
  • D) Undecidable time
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.

P6

The Cook-Levin theorem states that:

  • A) P = NP
  • B) SAT is NP-complete
  • C) 2-SAT is NP-complete
  • D) Halting is in NP
Show answer & explanation

Answer: B) SAT is NP-complete

Cook (1971) and Levin showed every NP problem reduces to SAT.

P7

Which of the following problems is in P?

  • A) Hamiltonian cycle
  • B) Existence of an Euler circuit
  • C) Clique of size k
  • D) 3-SAT
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.

P8

A graph with 10 vertices has a maximum independent set of size 4. The minimum vertex cover has size:

  • A) 4
  • B) 5
  • C) 6
  • D) 10
Show answer & explanation

Answer: C) 6

Minimum vertex cover = n − maximum independent set = 10 − 4 = 6.

P9

The fractional knapsack problem is:

  • A) NP-complete
  • B) In P
  • C) Undecidable
  • D) NP-hard but not in P
Show answer & explanation

Answer: B) In P

Greedy by value-to-weight ratio solves it in Θ(n log n).

P10

Which of the following is true about class P?

  • A) It is closed under complement
  • B) It contains the halting problem
  • C) It is a strict superset of NP
  • D) Its problems cannot be verified quickly
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.

Quick revision · 10 cards

10 flashcards — Complexity Classes

Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.

Q1 What is a decision problem and why are optimisation problems converted to decision form?
Q2 What does it mean for a problem to be in class P?
Q3 What does it mean for a problem to be in class NP?
Q4 How are P and NP related, and what is the status of P = NP?
Q5 What does a polynomial-time reduction A ≤p B tell you?
Q6 What is the difference between NP-hard and NP-complete problems?
Q7 What is the Cook-Levin theorem?
Q8 What are the steps to prove a new problem X is NP-complete?
Q9 Which classic problems are NP-complete and which similar-looking versions are in P?
Q10 Why is knapsack NP-complete even though a Θ(nW) dynamic programme exists?