{
    "slug": "np_completeness",
    "term": "P vs NP & NP-Completeness",
    "category": "algorithms",
    "difficulty": "advanced",
    "short": "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.",
    "long": "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).",
    "aliases": [
        "P vs NP",
        "NP-hard",
        "NP-complete",
        "computational complexity"
    ],
    "tags": [
        "algorithms",
        "complexity",
        "theory"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "big_o_notation",
        "dynamic_programming",
        "greedy_algorithms",
        "backtracking"
    ],
    "prerequisites": [
        "big_o_notation",
        "algorithms",
        "recursion_patterns"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/P_versus_NP_problem"
    ],
    "bad_code": "// Exact TSP solver — O(n!) time — impractical at scale:\nfunction tsp(array $cities): int {\n    // Try every permutation:\n    $minCost = PHP_INT_MAX;\n    foreach (permutations($cities) as $route) {\n        $minCost = min($minCost, routeCost($route));\n    }\n    return $minCost;\n}\n// 20 cities: 20! = 2.4 quintillion permutations — heat death of universe",
    "good_code": "// Nearest neighbour heuristic — O(n^2), approximation:\nfunction tspApprox(array $cities): array {\n    $route   = [$cities[0]];\n    $unvisited = array_slice($cities, 1);\n    while ($unvisited) {\n        $last    = end($route);\n        $nearest = array_reduce($unvisited,\n            fn($best, $city) => distance($last, $city) < distance($last, $best) ? $city : $best,\n            $unvisited[0]\n        );\n        $route[]   = $nearest;\n        $unvisited = array_diff($unvisited, [$nearest]);\n    }\n    return $route;\n}\n// Not optimal, but ~25% of optimal — runs in milliseconds for 1000 cities",
    "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",
    "severity": "info",
    "effort": "high",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/np_completeness",
        "html_url": "https://codeclaritylab.com/glossary/np_completeness",
        "json_url": "https://codeclaritylab.com/glossary/np_completeness.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[P vs NP & NP-Completeness](https://codeclaritylab.com/glossary/np_completeness) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/np_completeness"
            }
        }
    }
}