Skip to content

Instantly share code, notes, and snippets.

@codegraphtheory
Created November 8, 2025 22:03
Show Gist options
  • Select an option

  • Save codegraphtheory/38b94ca760fe54b2e1005c58a6ea6d91 to your computer and use it in GitHub Desktop.

Select an option

Save codegraphtheory/38b94ca760fe54b2e1005c58a6ea6d91 to your computer and use it in GitHub Desktop.
Understanding Polynomial Time
Class Description Key Characteristics Examples
P Problems that can be solved efficiently (in polynomial time) on a deterministic computer. Easy to solve and verify. Includes everyday computational tasks. Sorting a list (e.g., quicksort), finding the shortest path in a graph (e.g., Dijkstra's algorithm).
NP Problems where a proposed solution can be verified efficiently (in polynomial time), but solving them might be hard. Includes all P problems. Solutions are "guessable" and checkable quickly. Checking if a number is composite (easy to verify factors), puzzle solving like Sudoku (verify a filled grid quickly).
NP-complete The hardest problems in NP; if any can be solved in polynomial time, all NP problems can. In NP, and every NP problem reduces to them in polynomial time. Boolean Satisfiability (SAT: Is there an assignment that makes a formula true?), Traveling Salesman Problem (decision version: Is there a tour shorter than k?).
NP-hard Problems at least as hard as NP-complete ones, but not necessarily in NP (verification might not be efficient). May include optimization or undecidable problems; no efficient verification required. Halting Problem (Does a program halt on input?), Traveling Salesman Problem (optimization version: Find the shortest tour).

P Problems

P stands for "Polynomial time" and includes decision problems that can be solved by an algorithm in a time bounded by a polynomial function of the input size. These are considered "efficiently solvable." For instance, sorting a list of n numbers takes O(n log n) time, which is polynomial. Another example is Dijkstra's algorithm for shortest paths in graphs with non-negative weights, running in O((V+E) log V) time. Problems in P are tractable for large inputs on standard computers.

NP Problems

NP stands for "Nondeterministic Polynomial time" and consists of decision problems where, given a potential solution (a "certificate"), you can verify its correctness in polynomial time. All P problems are in NP (since solving implies verifying), but NP may contain harder problems. For example, determining if a number is composite: if given factors, you can multiply them quickly to check. Sudoku is in NP because you can verify a completed grid by checking rows, columns, and boxes in linear time relative to grid size.

NP-Complete Problems

NP-complete problems are a subset of NP that are the most difficult within it. They are in NP (verifiable in polynomial time) and every problem in NP can be reduced to them in polynomial time (polynomial-time reducibility). Solving any NP-complete problem efficiently would solve all NP problems efficiently. Classic examples include the Boolean Satisfiability Problem (SAT), where you check if there's a variable assignment making a logical formula true (verifiable by plugging in values). Another is the decision version of the Traveling Salesman Problem (TSP): given cities and distances, is there a tour visiting each exactly once and returning, with total distance ≤ k? Verification checks the tour's length quickly.

NP-Hard Problems

NP-hard problems are at least as hard as any NP-complete problem, meaning NP-complete problems reduce to them in polynomial time. Unlike NP-complete, they don't need to be in NP—verification might not be polynomial or even possible. This class includes undecidable problems. For example, the Halting Problem: given a program and input, does it halt? It's NP-hard but undecidable (no algorithm solves it for all cases). The optimization version of TSP (find the shortest tour, not just check existence) is NP-hard, as outputting the tour doesn't guarantee efficient verification without additional checks.

The P vs NP Question

This is an open problem in computer science: Does P equal NP? If P = NP, every problem with efficiently verifiable solutions could also be solved efficiently, revolutionizing fields like optimization and cryptography (e.g., breaking RSA easily). Most experts believe P ≠ NP, implying inherent hardness in some problems, forcing approximations or heuristics. It's one of the Millennium Prize Problems, with a $1 million reward for a proof.

For more Graph Theory and related topics in mathematics and computer science check out graphtech.dev.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment