#P, Counting, the Permanent, and Toda's Theorem
Anchor (Master): Arora and Barak 2009 Computational Complexity: A Modern Approach (Cambridge) §17.1-17.4 (#P and its structural properties, Valiant's theorem that the permanent is #P-complete, Toda's theorem with the full proof via probabilistic reductions and the pumping lemma, and connections to interactive proofs and hardness of approximation)
Intuition Beginner
In NP problems, the question is "does at least one solution exist?" A different question is "how many solutions exist?" This is a counting problem, and it is often much harder than the existence question.
Think of a jigsaw puzzle. Asking "can this puzzle be solved?" is an existence question. Asking "in how many different ways can this puzzle be solved?" is a counting question. The second question contains the answer to the first (if the count is zero, no solution exists), but it also gives much more information.
The class #P (pronounced "sharp P") captures this counting perspective. A function is in #P if it counts the number of accepting computation paths of a polynomial-time nondeterministic machine. This is the counting analogue of NP: NP asks whether the count is positive, while #P asks for the exact count.
A remarkable theorem by Toda shows that #P is at least as powerful as the entire polynomial hierarchy. Any problem that can be expressed with any number of alternating "there exists" and "for all" quantifiers can be solved with a single query to a #P oracle. This means counting is extraordinarily powerful — far more powerful than mere existence.
The permanent of a matrix, a simple-looking algebraic expression that swaps the signs in the determinant, turns out to be #P-complete. Computing it is as hard as any counting problem.
Visual Beginner
The relationship between NP, #P, and the polynomial hierarchy.
P^#P
/ | \
PH | \
/|\ | \
/ | \ | \
P NP coNP ...Toda's theorem says the entire polynomial hierarchy (PH) sits inside P with access to a #P oracle. The permanent function is #P-complete: it encodes every counting problem.
| concept | NP version | #P version |
|---|---|---|
| SAT | is there a satisfying assignment? | how many satisfying assignments? |
| matching | is there a perfect matching? | how many perfect matchings? |
| paths | is there a path from s to t? | how many paths from s to t? |
Worked example Beginner
Consider a 3-CNF formula with three variables and two clauses: .
The SAT question: is at least one of the 8 possible assignments satisfying? Let us check. Assignment gives both clauses true. Assignment gives first clause true, second clause false. So yes, is satisfiable.
The #SAT question: how many satisfying assignments are there? Checking all 8: fails clause 1. satisfies both. satisfies both. satisfies both. satisfies both. satisfies both. fails clause 2. fails clause 2. Total: 5 satisfying assignments.
The #SAT function maps to 5. This function is in #P: a nondeterministic machine guesses an assignment and verifies it in polynomial time, and the number of accepting paths equals the number of satisfying assignments. The #P-completeness of #SAT means every #P function reduces to counting satisfying assignments.
Check your understanding Beginner
Formal definition Intermediate+
A function is in #P if there exists a polynomial-time nondeterministic Turing machine such that for every input ,
The class is a function class, not a language class. To relate it to decision classes, define as the class of languages decidable by a polynomial-time deterministic Turing machine with oracle access to a function in : the machine may write a query on a special tape, receive as the answer, and use it in subsequent computation. Define (probabilistic polynomial time) as the class of languages for which there exists a polynomial-time probabilistic Turing machine such that .
The permanent of an matrix is
where is the symmetric group on . Unlike the determinant, the permanent omits the sign factor . This seemingly small change has enormous computational consequences: the determinant is computable in polynomial time by Gaussian elimination, but the permanent is #P-complete [Arora-Barak §17.2].
A parsimonious reduction from function to function is a polynomial-time computable function with for all . A polynomial-time Turing reduction from to is a polynomial-time oracle machine computing with access to .
Counterexamples to common slips Intermediate+
"#P is just NP with counting." The distinction is subtler. NP is a class of languages (decision problems); #P is a class of functions. A #P function gives the exact number of witnesses, which contains more information than the mere existence question that NP asks. The decision version "is the count positive?" gives NP, but recovering the exact count from NP queries may require exponentially many queries.
"The permanent is the determinant without signs, so it should be equally easy to compute." The sign in the determinant enables row-reduction (Gaussian elimination) without changing the answer. Without the sign, row operations can change the permanent, so the efficient algorithm breaks down entirely. Valiant's theorem shows the permanent is #P-complete even for 0-1 matrices.
"Toda's theorem means PH collapses." It does not. Toda's theorem places PH inside , but could be much larger than PH. The theorem shows that counting is powerful, not that the hierarchy collapses. A collapse would mean some level equals .
Key theorem with proof Intermediate+
Theorem (Valiant, 1979). The permanent of 0-1 matrices is #P-complete under polynomial-time reductions.
Proof sketch. Membership in #P: the permanent of an matrix equals the number of perfect matchings in the weighted bipartite graph defined by . A nondeterministic machine guesses a permutation and verifies that , accepting if so. The number of accepting paths is .
Hardness: reduce #3SAT to the permanent. Given a 3-CNF formula with variables and clauses, construct an integer matrix such that . The construction uses a graph whose cycle covers encode variable assignments and clause satisfactions. Each variable is represented by a gadget with two possible traversal modes (true or false). Each clause is a gadget that contributes a weight of 1 for each satisfied literal. The matrix entries are 0 or 1, and the permanent counts exactly the number of satisfying assignments, up to a known multiplicative factor that can be divided out. The construction is polynomial in .
Theorem (Toda, 1991). .
Proof sketch. The proof has two stages. Stage 1: show for every , where is the class of languages for which there exists a polynomial-time predicate such that and . Stage 2: show .
Stage 1 uses the Valiant-Vazirani lemma: given a Boolean formula , one can produce a formula (by adding random constraints) such that if is unsatisfiable then is unsatisfiable, and if has at least one satisfying assignment then has exactly one with probability . This "isolation" converts a "there exists" quantifier into a parity constraint, and the probability boost is handled by amplification [Arora-Barak §17.3].
Stage 2: computing reduces to a single #P query, since the probability is a ratio of counts that can be extracted from a #P oracle.
Bridge. This theorem is the foundational reason counting is more powerful than alternating quantification: a single #P query captures the entire polynomial hierarchy, and this is exactly the structural insight that makes the permanent such a potent computational object — it encodes every counting problem into a purely algebraic expression. The isolation lemma of Valiant-Vazirani builds toward the PCP theorem 47.03.05 where similar hashing techniques isolate witness structures, and the counting perspective appears again in the FPRAS algorithms 47.05.04 where approximate counting for #P-complete problems like the permanent becomes feasible when randomisation is allowed. The central insight is that parity (counting modulo 2) plus randomisation already captures existential quantification, and putting these together through the Toda pipeline, the entire quantifier hierarchy is swallowed by a single counting query. The bridge is that the permanent, as the simplest algebraic expression that is #P-complete, connects the discrete world of Boolean satisfiability to the continuous world of linear algebra, and this connection generalises to the entire theory of algebraic complexity.
Exercises Intermediate+
Advanced results Master
The theory of #P extends beyond the foundational theorems into structural complexity, algebraic methods, and the surprising power of approximate counting.
Theorem 1 (Valiant's theorem, detailed). The reduction from #3SAT to the permanent constructs a matrix of size for a formula with variables and clauses. Each variable gadget is a subgraph with two cycle-cover modes: one corresponding to "true" and one to "false." Clause gadgets connect to the variable gadgets through edges that are present only when the corresponding literal satisfies the clause. The permanent of counts cycle covers of the resulting graph, which are in bijection with pairs (variable assignment, clause satisfaction pattern). A carefully designed weighting scheme ensures that only satisfying assignments contribute non-zero weight, and each contributes . Dividing by gives the exact count of satisfying assignments [Arora-Barak §17.2].
Theorem 2 (PP-completeness of MAJ-SAT). The language MAJ-SAT is PP-complete. More generally, captures "threshold" decision problems: does a count exceed a specified fraction? The PP hierarchy collapses (), unlike the polynomial hierarchy which is not known to collapse. This makes PP structurally simpler than PH despite being computationally powerful [Arora-Barak §17.3].
Theorem 3 (Toda's theorem, proof outline). The proof proceeds in three steps. Step 1: the Valiant-Vazirani isolation lemma converts a SAT instance into a formula such that if is unsatisfiable then is unsatisfiable, and if is satisfiable then has exactly one satisfying assignment with probability . Step 2: this converts existential quantification into parity (), since "exactly one satisfying assignment" is detected by parity. Step 3: iterate this conversion across all quantifier levels, using amplification to boost the success probability at each level. The final result is that any predicate reduces to a predicate, and by computing the relevant counts exactly [Arora-Barak §17.3].
Theorem 4 (permanent for matrices with non-negative entries). While the permanent of general 0-1 matrices is #P-complete, the permanent of matrices with non-negative entries can be approximated efficiently. The Jerrum-Sinclair-Vigoda algorithm (2004) gives an FPRAS (fully polynomial-time randomised approximation scheme) for the permanent of matrices with non-negative entries. This is a rare example of a #P-complete problem that admits efficient approximation, connecting to 47.05.04.
Theorem 5 (algebraic characterisation). The permanent can be expressed as a multivariate polynomial: where the matrix entries are variables. This polynomial has degree in variables. The theory of arithmetic circuits studies the complexity of computing such polynomials, and the permanent plays a role analogous to SAT in algebraic complexity. The VP versus VNP question (the algebraic analogue of P versus NP) asks whether the permanent polynomial can be computed by polynomial-size arithmetic circuits [Arora-Barak §17.4].
Synthesis. The foundational reason #P is so powerful is that a single count encodes exponentially many bits of information about the structure of a computation, and this is exactly what Toda's theorem exploits: the entire polynomial hierarchy, built from unbounded quantifier alternation, collapses into a single counting query. The Valiant-Vazirani isolation lemma builds toward the PCP theorem 47.03.05 and appears again in the design of interactive proof systems 47.03.04 where isolating witnesses is a key technique. The central insight is that parity plus randomisation can simulate existential quantification, and putting these together through iteration, any fixed level of the hierarchy is captured. The bridge is that the permanent, as the simplest #P-complete problem, connects combinatorial counting to algebra, and this connection generalises through the theory of arithmetic circuits to the VP versus VNP question, which is dual to the P versus NP question in the algebraic setting. The #P framework also appears in the derandomisation programme 47.05.05, where approximate counting via random walks provides efficient algorithms for special cases.
Full proof set Master
Proposition 1 (#SAT is #P-complete). The function is #P-complete under parsimonious reductions.
Proof. Membership: a nondeterministic polynomial-time machine on input with variables guesses an assignment and evaluates . It accepts iff is true. The number of accepting paths equals the number of satisfying assignments.
Completeness: let via machine with nondeterministic choices. By the Cook-Levin construction 47.02.02, there is a formula such that satisfying assignments of are in bijection with accepting paths of on . The bijection is parsimonious (preserves counts). Hence .
Proposition 2 (permanent is in #P). The permanent of an matrix with non-negative integer entries is in #P.
Proof. A nondeterministic machine guesses a permutation and computes . It accepts with multiplicity equal to this product (achieved by branching). The total number of accepting paths is .
Proposition 3 (PP within P^#P). .
Proof. Let via machine using random bits. The number of accepting random strings is a #P function. Query the #P oracle for and compare to . If , accept; else reject. This uses one #P query and polynomial post-processing.
Proposition 4 (PH within BP . . P). For every , .
Proof sketch. By induction on . Base case : . Given a formula , apply Valiant-Vazirani to obtain with the isolation property. Then iff with probability over the random hash function, has exactly one satisfying assignment (detected by parity). This gives .
Inductive step: . Apply isolation to the outermost existential quantifier, converting it to BP and parity. The inner quantifiers are handled by the induction hypothesis. Amplification boosts the success probability at each level.
Proposition 5 (BP . . P within P^#P). .
Proof. A language has a polynomial-time predicate such that for , , and for the probability is . The quantity is computable from two #P queries: (a) count pairs with , and (b) count pairs with , which lets us extract the parity information. The comparison to the threshold is a polynomial-time post-processing step.
Connections Master
NP-completeness
47.02.02provides the Cook-Levin reduction that underlies #P-completeness. The parsimonious version of Cook-Levin preserves witness counts, making #3SAT #P-complete. The entire #P theory is the counting extension of the NP-completeness framework.Probability spaces and Kolmogorov axioms
37.01.01provide the measure-theoretic foundation for PP and the probabilistic analysis in Toda's theorem. The threshold behaviour of PP ("accept if probability exceeds 1/2") is a measure comparison.Interactive proofs
47.03.04connect to #P through the LFKN protocol and the IP = PSPACE proof. The counting techniques developed for #P (especially the sum-check protocol) are central to the interactive proof machinery.FPRAS for counting
47.05.04studies efficient approximation algorithms for #P-complete problems. The permanent of non-negative matrices admits an FPRAS, showing that approximate counting can be easier than exact counting.
Historical & philosophical context Master
Leslie Valiant proved the #P-completeness of the permanent in 1979, in his paper "The Complexity of Computing the Permanent" published in Theoretical Computer Science. The result was striking because the permanent is a simple algebraic expression — just the determinant without signs — yet it captures the full difficulty of counting. The construction introduced the cycle-cover method for encoding Boolean constraints into matrix entries, a technique that has become standard in algebraic complexity [Arora-Barak §17.2].
Seinosuke Toda proved his theorem in 1991, in "PP is Harder than the Polynomial-Time Hierarchy" (SIAM Journal on Computing). The result was unexpected: it showed that counting is strictly more powerful than any fixed level of quantifier alternation, giving the first tight connection between counting classes and the polynomial hierarchy. The proof introduced the use of the Valiant-Vazirani isolation lemma as a tool for converting quantifiers into parity constraints.
The development of FPRAS algorithms for counting, culminating in the Jerrum-Sinclair-Vigoda algorithm for the permanent (2004), showed that approximate counting is sometimes feasible even when exact counting is #P-complete. This opened the field of approximate counting, connecting complexity theory to practical algorithm design.
Bibliography Master
@article{valiant1979complexity,
author = {Valiant, Leslie G.},
title = {The complexity of computing the permanent},
journal = {Theoretical Computer Science},
volume = {8},
number = {2},
year = {1979},
pages = {189--201}
}
@article{toda1991pp,
author = {Toda, Seinosuke},
title = {{PP} is harder than the polynomial-time hierarchy},
journal = {SIAM Journal on Computing},
volume = {20},
number = {5},
year = {1991},
pages = {865--877}
}
@article{valiant1986np,
author = {Valiant, Leslie G. and Vazirani, Vijay V.},
title = {{NP} is as easy as detecting unique solutions},
journal = {Theoretical Computer Science},
volume = {47},
year = {1986},
pages = {85--93}
}
@article{jerrum2004polynomial,
author = {Jerrum, Mark and Sinclair, Alistair and Vigoda, Eric},
title = {A polynomial-time approximation algorithm for the permanent of a matrix with nonnegative entries},
journal = {Journal of the ACM},
volume = {51},
number = {4},
year = {2004},
pages = {671--697}
}
@book{arora2009computational,
author = {Arora, Sanjeev and Barak, Boaz},
title = {Computational Complexity: A Modern Approach},
publisher = {Cambridge University Press},
year = {2009}
}