To describe hard problems precisely, computer scientists divide problems into classes by how efficiently they can be solved and checked.
Decision Problems and Polynomial Time
A decision problem has the answer yes or no, such as "does this graph have a Hamiltonian circuit?". An algorithm runs in polynomial time if its worst-case time is O(nᵏ) for some constant k. Such problems are called tractable (efficiently solvable); problems needing exponential time such as 2ⁿ are intractable. Optimisation problems are linked to decision problems: "find the shortest tour" becomes "is there a tour of length at most L?".
Non-Deterministic Algorithms
A deterministic algorithm always follows one path of execution. A non-deterministic algorithm has a special operation, choice, which can "guess" the correct value among many in a single step, followed by a verification phase that checks the guess; the algorithm succeeds if some sequence of guesses leads to acceptance. Such machines do not exist physically, but the idea defines the class NP. The pattern is: guess a solution, then verify it in polynomial time. For sum of subsets, guess a subset and check in O(n) time that its sum is M.
Definition: Non-deterministic algorithm. An algorithm that may guess a candidate solution through a choice operation and then verifies it, accepting if some guess passes the verification.
The Class P
P is the set of decision problems solvable by a deterministic algorithm in polynomial time. Examples: sorting, searching, shortest path, minimum spanning tree, matrix multiplication, whether a graph is connected, and whether a number is in a list. These are considered the problems we can solve efficiently.
The Class NP
NP (non-deterministic polynomial time) is the set of decision problems whose yes answers can be verified in polynomial time given a certificate (a proposed solution), or equivalently, that can be solved by a non-deterministic algorithm in polynomial time. Examples: Hamiltonian circuit (the certificate is the cycle), subset sum (the subset), vertex cover (the set), satisfiability (the truth assignment), and also every problem in P. Because a problem in P can be solved, hence also verified, in polynomial time, P ⊆ NP.
The P versus NP Question
Is P = NP? That is, if a solution can be checked quickly, can it also be found quickly? Nobody knows. It is the most famous open problem in computer science and one of the seven Clay Millennium Problems with a prize of one million dollars. Most experts believe P ≠ NP, because decades of effort have found no fast algorithm for the hardest NP problems; if P = NP, many cryptographic systems would collapse.
Table 4.5: P and NP
| Class | Meaning | Examples |
|---|---|---|
| P | Solvable in polynomial time | Sorting, shortest path, MST |
| NP | Verifiable in polynomial time | Hamiltonian circuit, subset sum, SAT |
| P ⊆ NP | Every P problem is in NP | Known |
| P = NP? | Open question | Unsolved |
Example 4.5: Verifying a certificate
Problem: For the set {3, 34, 4, 12, 5, 2} and M = 9, verify the certificate {4, 5} in polynomial time, and verify the Hamiltonian certificate 0-1-2-4-3-0.
Solution:
- Subset: check each element belongs to the set and add: 4 + 5 = 9 = M, taking O(n) time.
- Cycle: check that it has 5 distinct vertices, each consecutive pair is an edge (0-1, 1-2, 2-4, 4-3) and the last returns to 0 (3-0): O(n) checks.
- Both verifications are polynomial, although finding the certificate may take exponential time.
Result: Both problems lie in NP, as verification is fast.
S, M = [3, 34, 4, 12, 5, 2], 9
cert = [4, 5]
ok = all(x in S for x in cert) and sum(cert) == M
print("subset certificate valid:", ok)
edges = {(0,1),(0,3),(1,2),(1,3),(1,4),(2,4),(3,4)}
cyc = [0, 1, 2, 4, 3, 0]
good = len(set(cyc[:-1])) == 5 and all(
(a, b) in edges or (b, a) in edges for a, b in zip(cyc, cyc[1:]))
print("Hamiltonian certificate valid:", good)
Output
subset certificate valid: True
Hamiltonian certificate valid: True
Common mistakes
- Thinking NP means "not polynomial"; it means non-deterministic polynomial.
- Confusing "verified in polynomial time" with "solved in polynomial time".
- Saying P ≠ NP has been proved; it is open.
- Forgetting that every problem in P is also in NP.
Key takeaways
- P: solvable in polynomial time; NP: verifiable in polynomial time.
- A non-deterministic algorithm guesses and then verifies.
- P ⊆ NP; whether P = NP is open.
- Hamiltonian circuit, subset sum and SAT are in NP.
Practice questions
- Define the class NP.
- Is P a subset of NP?
- What does NP stand for?
Answers to the practice questions
- Decision problems whose yes answers can be verified in polynomial time, or solved in polynomial time by a non-deterministic algorithm.
- Yes, anything solvable in polynomial time is also verifiable in polynomial time.
- Non-deterministic polynomial time.
Sources
- Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit IV
