← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

P vs NP & NP-Completeness

algorithms Advanced

Also Known As

P vs NP NP-hard NP-complete computational complexity

TL;DR

P problems are solvable in polynomial time; NP problems have solutions verifiable in polynomial time. NP-complete problems are the hardest in NP — no polynomial-time algorithm is known.

Explanation

P (Polynomial time): problems solvable in O(n^k) — sorting, shortest path, spanning tree. NP (Non-deterministic Polynomial): solutions verifiable in polynomial time — the Travelling Salesman Problem, satisfiability. NP-complete: any NP problem can be reduced to it — the hardest problems in NP. If P = NP (unproven), all NP problems would have polynomial-time solutions. Practical implications: NP-complete problems require heuristics, approximations, or exponential algorithms. Examples relevant to software: Boolean satisfiability (SAT — used in compilers, verification), constraint satisfaction (scheduling, routing), and graph colouring (register allocation).

Common Misconception

NP means non-polynomial (intractable) — NP means Non-deterministic Polynomial — verifiable in polynomial time, not necessarily solvable; the question is whether they are also solvable in polynomial time.

Why It Matters

Recognising NP-complete problems in practice (scheduling, optimisation, bin-packing) prevents wasting time searching for an efficient exact algorithm that almost certainly does not exist.

Common Mistakes

  • Trying to solve NP-complete problems exactly at large scale — use approximation algorithms or heuristics.
  • Confusing NP-hard (at least as hard as NP) with NP-complete (NP-hard and in NP).
  • Not recognising that many real scheduling and optimisation problems are NP-complete.
  • Assuming P != NP is proven — it is the most famous open problem in computer science.

Code Examples

✗ Vulnerable
// Exact TSP solver — O(n!) time — impractical at scale:
function tsp(array $cities): int {
    // Try every permutation:
    $minCost = PHP_INT_MAX;
    foreach (permutations($cities) as $route) {
        $minCost = min($minCost, routeCost($route));
    }
    return $minCost;
}
// 20 cities: 20! = 2.4 quintillion permutations — heat death of universe
✓ Fixed
// Nearest neighbour heuristic — O(n^2), approximation:
function tspApprox(array $cities): array {
    $route   = [$cities[0]];
    $unvisited = array_slice($cities, 1);
    while ($unvisited) {
        $last    = end($route);
        $nearest = array_reduce($unvisited,
            fn($best, $city) => distance($last, $city) < distance($last, $best) ? $city : $best,
            $unvisited[0]
        );
        $route[]   = $nearest;
        $unvisited = array_diff($unvisited, [$nearest]);
    }
    return $route;
}
// Not optimal, but ~25% of optimal — runs in milliseconds for 1000 cities

Added 16 Mar 2026
Edited 22 Mar 2026
Views 36
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
3 pings W 0 pings T 1 ping F 3 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 2 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 12 Perplexity 10 Ahrefs 3 Unknown AI 3 Google 2 Majestic 1
crawler 30 pre-tracking 1
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: High
⚡ Quick Fix
When a problem is NP-complete (TSP, knapsack, graph colouring), don't try to find the optimal solution — use heuristics, approximation algorithms, or dynamic programming for small inputs
📦 Applies To
any web cli
🔗 Prerequisites
🔍 Detection Hints
Brute-force O(n!) or O(2^n) algorithm on potentially large input; exact solution to NP-complete problem without bounding input size
Auto-detectable: ✗ No blackfire
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: High Context: Function

✓ schema.org compliant