This week’s post is about an interesting relation between counting and sampling, motivated from an enlightening paper [MD02] by Prof. Martin Dyer. More specifically, the paper introduces dynamic programming as a technique to aid in approximate counting and uniform sampling using a dart throwing approach, however, this blog post is only about the example presented in the beginning of the paper where Prof. Dyer uses the count of the solutions to the 0-1 knapsack problem to sample from the set of these solutions uniformly at random in expected polynomial time (in the number of variables). You are encouraged to read [WK16] to learn more about the knapsack problem. I hope you like this post 🙂
So, the problem at hand is to be able to produce a solution to a given 0-1 knapsack problem which has been sampled uniformly at random from the set of all such solutions. Why would one require to do so? Well, among the many applications of the ability to sample uniformly at random from combinatorial sets, the one I encounter the most is in computer simulations and producing approximations. It is well known that the general knapsack problem is hard to solve, so counting the number of solutions will only be harder, let alone the problem of sampling one of these solutions. Hence, if we can approximate the count using some efficient technique, (almost) uniform sampling will become readily available. How?
Well, to answer this question, let’s first formalize the problem a little bit. The 0-1 knapsack problem can be written as the inequality for for all . Here, we assume that are all integers. Note that any single linear inequality in 0-1 variables with rational coefficients can be written in this form [MD02, WL75]. Denote by the set of all solutions to this inequation. Then, the problem at hand is to sample uniformly at random from . So, back to the question. What if we don’t know ?
One way of sampling from without any knowledge of its size is to exploit the fact that we can efficiently check if a given assignment of 0’s and 1’s to our variables satisfies the inequality above, i.e. whether a given 0-1 assignment is in or not. This is crucial for obvious reasons, most important of which is that we will rely on this capability to accept only those assignments that lie in and reject others in a technique popularly known as rejection sampling. Start with randomly producing a 0-1 assignment of the variables and accept it if it satisfies the inequality and repeat otherwise. Simple, but not always efficient. Why?
Suppose is very small, say . Then, the probability of acceptance in one iteration when we have variables is , which is . Hence, in expectation, exponentially many rejections will happen before the first assignment is sampled, making the algorithm extremely slow. Can we do better? Well, let’s try Prof. Dyer’s approach.
The main idea behind the technique I am now going to talk about is eliminating the rejections above and directly sampling a solution to the problem. Since each solution must be sampled with probability , it will be good to know . Let us say we do. Now, fix a variable, say . What is the probability that in a satisfying assignment to the problem? Well, since we are sampling uniformly at random from , this probability is equal to the ratio of the number of solutions in which to . Hence, it will be good to know the number of solutions in which after which the problem becomes trivial. So, how to compute this number?
[Enter dynamic programming.]
Let be the number of solutions to inequality , where ‘s and ‘s are the same as above. Clearly, we can write if and 2 otherwise. Also, observe that . To recursively compute , note that if we set , then we have the inequality , whose number of solutions is . However, if we set , then we have the inequality , whose number of solutions is . Thus, we can write and solve the dynamic program recursively in time.
We are not quite done yet. We still do not know the number of solutions in which . At least not directly. However, this is where the genius in [MD02] shines. We sample an assignment from as follows. With probability , set and with probability , set . Once we have this done, use the dynamic program above to recursively set the values of the other variables until we are done. The claim here is that the resulting assignment is a valid solution to the inequality and is sampled uniformly at random from . How? Here is why.
To see why the resulting assignment is in is easy. Once we assign the value to a variable, the dynamic program lets us sample the value of the next variable (or the previous one, whichever way you see it) based on the number of solutions in which the next (previous) variable is assigned a particular value. In other words, say we just assigned the value to . Then, if is not a valid solution, the dynamic program will take care of it by itself.
To see why the resulting assignment is uniformly random, let us compute the probability that a solution is produced for some such that . We can write . Since the assignment to is independent of other variables, we can write this as . Now, we can expand similarly as . Again, the assignment of is independent of all others and hence, . Keep going on like this to obtain that the probability , which is what we wanted to prove.
Hence, we just saw a technique using counting and dynamic programming that allows us to sample exactly from a set of objects which would be difficult to sample from in general. This technique can be extended to many other combinatorial problems, however, the dynamic program to count may not be straight forward. However, it should now be clear that if exact counts are replaced by approximate counts (through some technique), then uniform sampling becomes almost uniform sampling. An important application of this is approximate counting of the number of perfect matchings in a given graph and then sampling (almost) uniformly at random one of these matchings.
The reverse direction is, however, pretty straight forward. If we knew how to sample, we can count easily. However, an exact count may not be possible using this approach, but we can count to a precision that is arbitrarily close to the exact count by using a technique similar to rejection sampling.
I hope this post was helpful in understanding this fascinating connection between sampling and counting of combinatorial objects. It is a huge area of research and lots of interesting applications have been explored. I am trying to learn more in this area and hope to come up with more fascinating examples in the future posts. Until then, ciao!
[MD02] Dyer, Martin. “Approximate counting by dynamic programming.” In Proceedings of the thirty-fifth annual ACM symposium on Theory of computing, pp. 693-699. ACM, 2003.
[WL75] Wolsey, Laurence A. “Faces for a linear inequality in 0–1 variables.” Mathematical Programming 8.1 (1975): 165-178.