# 随机算法 (Spring 2014)/The Monte Carlo Method

## Contents

# Parameter Estimation

Consider the following abstract problem of parameter estimation.

Let be a finite set of known size, and let . We want to estimate the *parameter* , i.e. the size of , or equivalently, the density (or frequency) .

We assume two devices:

- A
**uniform sampler**, which uniformly and independently samples a member of upon each calling. - A
**membership oracle**of , denoted . Given as the input an , indicates whether or not is a member of .

Equipped by and , we can have the following **Monte Carlo method**:

- Choose independent samples from by the uniform sampler , represented by the random variables .
- Let be the indicator random variable defined as , namely, indicates whether .
- Define the estimator random variable for as

- Define the estimator random variable for as

- .

It is easy to see that and .
We might hope that with high probability the values of and are close to their respective expectations. Formally, is called an **-approximation** of if

And is called an **-estimator** for if

The following general theorem bounds the reliance between the accuracy of estimation and the number of samples.

**Theorem (estimator theorem)**- Let . The following holds:
- 1. is an -approximation to with probability at least if
- .

- 2. is an -estimator for if
- .

**Proof.**Recall that indicates whether the -th sample is in . Let . Then we have , and hence the event is equivalent to that . Note that each is a Bernoulli trial that succeeds with probability , thus . Then the rest is due to Chernoff bound.

A counting algorithm for the set has to deal with the following three issues:

- Implement the membership oracle . This is usually straightforward, or assumed by the model.
- Implement the uniform sampler . This can be straightforward or highly nontrivial, depending on the problem.
- Deal with exponentially small . This requires us to cleverly choose the universe . And this part usually uses some beautiful ideas.

# Counting DNFs

A disjunctive normal form (DNF) formular is a disjunction (OR) of clauses, where each clause is a conjunction (AND) of literals. For example:

- .

Note the difference from the conjunctive normal forms (CNF).

Given a DNF formular as the input, the problem is to count the number of satisfying assignments of . This problem is **#P-complete**. So we do not expect to have a polynomial-time algorithm for exact solving the problem. Instead we are seeking for approximation algorithms. Interestingly, even though the computational complexity of the problem is so high, it admits some very nice approximation algorithm which has very good approximation ratio (theoretically speaking). We need to introduce some general concepts for approximation algorithms.

**FPTAS (Fully Polynomial-Time Approximation Scheme)**- Consider a computational problem whose outputs are positive integers.

- A
**FPTAS (fully polynomial-time approximation scheme)**for is an algorithm that takes the following inputs:- an input instance of problem , which we use to denote its length;
- a real number ;

- and in time polynomial in both and , returns an output such that

The adverb "fully" stresses the polynomial reliance on not just the input size but also the approximation parameter . An FPTAS may approximate the true value of the output within any accuracy in polynomial time, though the polynomial becomes larger when the approximation is more accurate. Moreover, the running time grows just polynomially with repeat to the accuracy bounds. Theoretically speaking, FPTAS is the best we can get for computational hard problems, though in practice sometimes the polynomial bound might be too large even for a moderate .

The FPRAS (fully polynomial-time randomized approximation scheme), on the other hand, is the randomized relaxation of FPTAS.

**FPRAS (Fully Polynomial-time Randomized Approximation Scheme)**- Consider a computational problem whose outputs are positive integers.

- A
**FPRAS (fully polynomial-time randomized approximation scheme)**for is a randomized algorithm that takes the following inputs:- an input instance of problem , which we use to denote its length;
- two real numbers ;

- and in time polynomial in , , and , returns an output such that

Note that the reliance on the *confidence error* is polynomial in , instead of . This is because due to the Chernoff bound, the confidence error can be reduced in an exponential rate by independent repetitions. This motivates the following simpler but equivalent definition of FPRAS, which only considers the special confidence error . Apparently, this is a more natural way to relax the FPTAS to randomized algorithms as it mimics the way of generalizing class **P** to **BPP** for decision problems. If you are familiar with the Chernoff bound, it is quite easy to observe the equivalence between the following definition and the above one. And this simple definition is also easier to check (you only need to verify the case for the error ), which makes it more convenient to apply.

**FPRAS (equivalent simple version)**- Consider a computational problem whose outputs are positive integers.

- A
**FPRAS (fully polynomial-time randomized approximation scheme)**for is a randomized algorithm that takes the following inputs:- an input instance of problem , which we use to denote its length;
- a real number ;

- and in time polynomial in and , returns an output such that

The Monte Carlo method for the parameter estimation problem would give us an FPRAS if the sampler and membership oracle are poly-time realizable and the fraction is bounded from below by a .

However, for the DNF counting problem, naively applying the Monte Carlo method (treat the set of all truth assignments as and the set of DNF solutions as ) will not give a good algorithm. The problem is that the fraction can be exponentially small:

- Suppose that there are variables. Let be the set of all truth assignments of the variables. Let be the set of satisfying assignments for . The straightforward use of Monte Carlo method samples assignments from and check how many of them satisfy . This algorithm fails when is exponentially small, namely, when exponentially small fraction of the assignments satisfy the input DNF formula.

So instead of naively considering the space of satisfying solutions and the space of all solutions, we represent the space of satisfying solutions to a DNF formula as a union of sets of satisfying solutions to the individual clauses, and count this union of sets. This gives us an abstract problem called the union of sets problem.

## The union of sets problem

We reformulate the DNF counting problem in a more abstract framework, called the **union of sets** problem.

**The union of sets problem**- Let be a finite universe.
**Input**: subsets of the universe;**Output**: the size of the union set

To make the algorithm work, we make the following assumptions:

- For all , the value of can be computed efficiently.
- It is possible to sample uniformly from each individual .
- For any , it can be determined efficiently whether .

DNF counting can be easily put into this general framework: Suppose that the DNF formula is a disjunction () of clauses on variables, where each clause is a conjunction () of literals. Without loss of generality, we may assume that in each clause, each variable appears at most once. The satisfying solutions to this DNF can be represented as a union of sets as follows:

- is the set of all truth assignments.
- Each is the set of satisfying assignments for the -th clause of the DNF formula.

Then the union of sets gives the set of satisfying assignments for the DNF formula . Its size gives the number of satisfying assignments for the DNF.

Furthermore, the three assumptions above can also be guaranteed in this construction:

- Since each clause is a conjunction of literals, it is easy to see that , which is obviously efficiently computable.
- Sampling from each is very easy: we just fix the assignments of the variables involved in the corresponding clause to satisfy (there is exactly one such choice), and sample uniformly and independently the rest variable assignments.
- For each assignment , it is easy to check whether it satisfies a clause , thus it is easy to determine whether .

## The coverage algorithm

We now introduce the coverage algorithm for the union of sets problem.

Consider the multiset defined by

- ,

where denotes the multiset union. It is more convenient to define as the set

- .

For each , there may be more than one instances of . We may choose the with the minimum , and collect all such to form a set .

Formally, the set is defined as

- .

Every corresponds to a unique where is the smallest among .

The following proposition is obvious

**Proposition**- ;
- .

Therefore, the estimation of the size of the union set is reduced to the estimation of the size of the subset , where is the multiset union of and . Note that now the fraction becomes large enough. As long as we can implement a uniform sampler for and a membership oracle for , we can apply the estimator theorem to get a good approximation algorithm for .

An uniform sample from can be implemented as follows.

**Uniform sampler for**- generate an with probability ;
- uniformly sample an , and return .

It is easy to see that this gives a uniform member of . And this uniform sampler is efficient as long as each is efficiently computable and we have an efficient uniform sample for each , both of which are assumed.

It is also easy to implement the membership oracle for :

**Membership oracle for**upon each query :

- if is the smallest among all that
- return "yes";

- else return "no";

- if is the smallest among all that

By definition of , it is easy to see this membership oracle works, and it is efficient if the membership of each can be efficiently determined, which is also assumed.

We now only need to lower bound the fraction

- .

**Proposition**- .

**Proof.**For every , there are at most different pairs in , thus . And we already know that .

Applying the estimator theorem, this gives us an algorithm which returns an -approximation of with probability at least , by making at most calls to the uniform random sampler for and the membership oracle for , due to the three efficiency assumptions, both of which can be efficiently implemented. The resulting algorithm is called the **coverage algorithm**.

Applying the coverage algorithm to the DNF counting, which as we have discussed above, is a special case for the union of set problem, we have an FPRAS.

**Theorem (Karp-Luby, 1983)**- There exists an FPRAS for the DNF counting problem.

# Count Matchings

Recall that a matching in a graph is a subset of edges such that no vertex in is incident to more than one edges in , i.e. a matching is an "edge-version" of independent set.

Let us consider a more complicated counting problem, counting the number matchings in an input graph. The problem is formally defined as follows.

**Input**: an indirected graph ;**Output**: number of matchings in .

Note that we count all matchings, not just maximum or maximal matchings. For example, an empty set (containing no edge) is always a matching.

This problem is **#P-complete**. In fact, it is among the first few problems known to be **#P-complete**. The studies of the computational complexity and efficient approximation algorithms for this problem pioneer the fields of counting complexity and randomized algorithms.

## A self-reduction procedure

We show a generic self-reduction procedure due to Jerrum-Valiant-Vazirani, which reduces a counting problem to the problems of smaller instances. This technique does not just work for counting matchings, but may also work a wide range of counting version of CSPs (constraint satisfaction problem) who show self-reducibility property.

Fix an input graph . Let be the number of matchings in .

We consider the uniform probability distribution over all matchings in . Let be such a random matching sampled uniformly at random from all matchings in . More formally, for any ,

Consider the special matching . It clearly holds that , thus

Thus we can estimate by estimating the probability .

We denote , where the order of edges is arbitrary.

- The random matching can be seen as a random vector , where each indicates whether appears in the random matching .
Thus the probability is usually called a by the chain rule.
marginal probabilities |

Back to the matching problem, note that the event is equivalent to the event that . Due to the chain rule, we have

For a graph and an edge , we use

to denote the marginal probability of being occupied by a uniformly random matching in .

**Observation (self-reduction)**- Let be the subgraph of obtained from removing the first edges from .
- It holds that

This observation can be easily proved by the following "telescopic product" version of the chain rule. Note that with the notation we have and to be the empty graph.

By definition of conditional probability, we have

On the other hand, it is also obvious that

So the above observation is proved.

## From counting to sampling

With the above observation and recalling that , we have

The number of matchings in graph , , can be efficiently approximated if the marginal probabilities can be efficiently approximated, where the latter can be achieved by doing the statistical experiments by sampling uniform random matchings in graph .

This generic process is described in more details as follows:

- If we can sample a uniform (or
*almost uniform*, whose meaning to be specified later) random matching from any graph in polynomial time, then due to the estimator theorem, we can have an**-estimator**of each . This estimator runs in time polynomial in , , and , and returns an estimation for each such that holds with probability at most . - The above -estimators are efficient approximations of marginal probabilities with small
*additive errors*. To get an FPRAS for , whose error is multiplicative, we still need that each is bounded away from 1. i.e. there is a constant such that it always holds that . For matching, this is always guaranteed. We have the following lemma.

**Lemma**- For any graph and edge , it always holds that .

- This lemma holds for quite obvious reasons: If is an isolated edge (with both endpoints of degree 1), then it is covered by an uniformly random matching with probability exactly 1/2. And more edges sharing vertices with can not increase this probability.

With the above lemma, the task of designing a FPRAS for the number of matchings in an input graph is reduced to the task of uniformly sampling a matching in the input graph in polynomial time. In fact, precisely uniform sampling such complicated structure is quite difficult, but there are ways to *almost uniform sampling* matchings or other complicated structures in polynomial time. This is achieved by **random walks** in a very large but fast converging Markov chain.