The complexity class of #P problems

Posted on March 23, 2016 in learning

Recently, I had an assignment in one of my courses - Beyond NP-Completeness, to present the complexity class of #P (Sharp-P) problems based on L.G. Valiant’s paper on complexity of computing the permanent of a matrix.

This post is an attempt to explain the paper as I found very limited number of resources on the internet where this topic is discussed in detail, one of the sightings being a lecture by Professor Erik Demaine under MIT open courseware which can be found on youtube.

Before we begin, it is strongly recommended to familiarize yourself with the following:

So let’s begin with the permanent, which is defined for an n x n matrix A as:

Before proceeding, let’s understand what this means, and what we want to compute. is a permutation of the numbers {1, 2, 3, … n}. For example, for n = 5, if = {2, 5, 3, 4, 1}, then , , and so on. The summation, therefore, is over all , which means we are doing the summation for all possible permutations, a total of them being (factorial of n). So it is evident, that when taking the product, which is inside the summation term, we must take one element from each row and each column of the matrix, and a total of n elements have to be picked, therefore exactly one element from each row and each column has to be picked.

A closer look at the permanent tells us that if we change all the negative signs in the expression of the determinant of a matrix to positive signs, it will indeed become the Permanent. To show this with an example: Consider the matrix,

The determinant of , while the permanent is: , which is clearly obtainable by changing all negative signs in the determinant’s expression to positive. Surprisingly, though there exist efficient solutions to compute the determinant of a matrix, yet to compute the permanent, no algorithm that takes better than exponential time is known.

Valiant comments on the complexity of the problem of finding the permanent of a (0-1) matrix, for which he defines the class #P. To put it easily, #P problems are the counting problems associated with the decision problems in class NP. It is a class of function problems, and not decision problems (where the answer is a simple yes/no). An NP problem of the form “Does there exist a solution that satisfies X?” usually corresponds to the #P problem “How many solutions exist which satisfy X?”. Here goes the example of a #P problem: #SAT - Given a boolean formula , find the number of assignments that satisfy . This is the counting version of the famous SAT problem which is known to be NP-complete.

Defining #P completeness

To define completeness in this class of problems, we need to bring in Oracle Turing Machines and the class FP. Oracle machines are those which have access to an oracle that can magically solve the decision problem for some language . These machines with oracle access can then make queries of the form “Is ?” in one computational step. We can generalize this to non-boolean functions by saying that a T.M. M has oracle access to a function if it is given access to the language . For every , we denote as the set of languages that can be decided by a polynomial-time DTM (Deterministic Turing Machine) with oracle access to O. As an example, consider the problem, which denotes the language of unsatisfiable boolean formulae. because if we are given an oracle access to the SAT language, then we can solve an instance of the problem in polynomial time, by asking “Is ?” and negating the answer to that. Now we are ready to define #P completeness. A function is said to be #P-complete if and is in (Cook reduction).

Back to the problem

Now let’s get back to the permanent. We take a special case of the permanent problem where we put a constraint that the input matrix is a (0, 1) matrix, that is, all the entries of the given matrix are either 0 or 1. Let us look at this problem of finding the permanent of a binary matrix with a different perspective. Imagine that the given matrix A is an adjacency matrix of a bipartite graph where,

Now if we look at the term inside the summation, that is for , we can try to imagine the value of this term as synonymous to a possible perfect matching in the bipartite graph represented by A. Why? Because we know that this term takes one element from each row and each column, so all vertices are covered in the term, and this term = 0, if any of the elements picked are 0. But in our adjacency matrix, 0 for row i and column j means that there is no edge between and , so this term evaluates to 1 if and only if the selected elements form an edge cover (a perfect matching) for the given bipartite graph. So clearly, the whole term where the summation is over all represents the total number of perfect matchings in the bipartite graph represented by A. As finding the number of perfect matchings in a bipartite graph #P, therefore clearly, finding the permanent of a binary matrix #P.

Now we shall move on to prove the Valiant’s theorem which says that finding permanent for a binary matrix is #P-complete. We already proved it is in #P, so now all remains for us is to prove that it is in #P-Hard, that is all #P problems can be reduced to this problem in the way explained earlier in this post (where completeness is defined for #P problems). For this, we look at the problem of finding the permanent as a different graph problem.

Consider A = adjacency matrix of a weighted and directed graph with n nodes (We are talking about a general matrix now, not a binary matrix). We define two things now:

Now, with a good observation, we can conclude that where is the weight of the cycle cover. We sum the weights of all possible cycle covers of the graph and it turns out to be equal to the permanent of the adjacency matrix. If this is hard to visualize, feel free to work out on an example having 3 or 4 nodes to let that sink in.

Proceeding with the proof, we will attempt to reduce an instance of the 3-SAT problem to an instance of the cycle cover problem. The methodology and examples are directly taken from the original paper. We begin with a boolean formula given to us in 3-CNF form. , a conjunction of m clauses where , a disjunction of 3 literals, where , the set of variable and their negations.

We construct graph G by superposing the following the structures:

  1. A Track for each variable
  2. An Interchange for each clause
  3. For each literal such that or , a Junction at which interchange and track meet.
  4. The interchanges also have internal junctions, which are exactly same as above

Example: Let’s take some formula F where: occurs in and occurs in

For this example, the following are the structures:

Track Interchange
Track T5 Interchange R3

The small shaded regions are the junctions, which are themselves a network of nodes and edges represented by the adjacency matrix,

Why this matrix is valued exactly with these numbers will be clear as we proceed. This is a very crucial part. Let’s note some important things:

Using these properties, we can draw very good insights. Let’s look at routes in the graph. A route is a cycle cover. If we consider all the routes which have the same set of edges outside of the junctions, then we can call a route bad if:

So the only choice we have is to enter at either node 1 or node 4 and leave at the opposite end after covering nodes 2 and 3 if we want to make that route count towards the total number of cycle covers (the value of the permanent). Now if we go by this only choice, the contribution to the cycle will be 4, as Perm() = Perm() = 4.

In any track of any good route, there are two cases as seen in the structure of the track:

These two cases correspond to whether or

Now observe the interchanges. Each interchange has 5 junctions, 3 of which are connected to a corresponding track, which consists of the variable that is present in that particular clause, and 2 are internal junctions, which are of the same structure as a normal junction. A careful observation tells us that the whole of an interchange (all the five junctions) cannot be picked up by a route, in fact, all 3 of the external junctions can never be picked up in a route by the interchange, so at least one of the 3 junctions connected to tracks must be picked up by a track, this constraint being synonymous to the fact that we need at least one literal in the clause to be true, to make the whole clause true.

Now the total number of good routes (cycle covers) exactly corresponds to the total number of satisfying variable assignments for the boolean formula of the SAT instance we had taken. Since #SAT is known to be #P-complete, the Permanent problem is now proved to be #P-Hard.

Let’s get on to finding the permanent of a 0-1 matrix now. It is a great thing to note that though the problem of finding a perfect matching in a bipartite graph P, yet counting the total number of perfect matchings #P-complete. This is one of those examples where it becomes clear that easy decision problems can have very hard counting versions of themselves. Rest of the post is about proving that 0-1 permanent is #P-complete.

To prove this, we will reduce the permanent problem to the 0-1 permanent problem. First, we need to make the weights of all the edges non-negative. Doing this is easy with modular arithmetic. We can compute for all where , as we do not need to consider primes greater than the largest value the expression of the permanent can take. Here is the maximum value in the matrix, so a total of permutations of the entries with each of them being equal to the maximum value. That’s the maximum we will ever have.

Since the number of primes is , the complexity is polynomial in terms of input size for this reduction. We can reconstruct the permanent using CRT.

So now we have a matrix where all the entries are non-negative. We have to reduce this to a form where all entries are either 0 or 1, to prove that the permanent problem is reducible to the 0-1 permanent problem. We can do so in two ways, by transforming the original graph to an equivalent graph.

The first method is mentioned in the paper, so I will be explaining the second perspective here. Images are taken from Wikipedia. First, we transform the graph into one where all edge weights are powers of 2, the graph . To do this, we take an edge with weight and split it into a widget as shown in the image below. We use the fact that any number can be expressed as a sum of powers of 2.

powers of 2

To prove the correspondence, take two cases for a cycle cover C of the graph :

Hence graphs and are equivalent in terms of the permanent problem. Now we are left with a transformation such that all edges become binary, with weights 0 or 1. This is easy in the following way (Refer to the image below).

to binary

This is the transformation from to . Again we have two cases to show the similarity. Let C be a cycle cover in , then:

Thus the problem of finding the permanent of a 0-1 matrix, and equivalently the problem of finding the number of perfect matchings in a bipartite graph #P-complete.

I have talked about more #P-complete problems and their proofs in this post.