Inclusion-Exclusion principle, which will be called from now also the principle, is a famous and very useful technique in combinatorics, probability and counting.
For the purpose of this article, at the beginning the most common application of the principle, which is counting the cardinality of sum of sets, will be considered. Later, more applications will be given.
Cardinality of the sum of sets
For a given set of sets , let be the sum of these sets, more formally,
The principle gives a direct formula for computing . It is important to notice that since any two of given sets can have a non-empty intersection, the cardinality of their sum can be smaller than the sum of their cardinalities. This is the reason why inclusion-exclusion principle is so useful.
The formula:
If for a given set of sets , the cardinality of intersection of sets in any subset of can be computed efficiently, then the formula can be used in order to get the value of .
A very important thing to notice is that the formula can be quite long. Actually, each subset of is considered exactly once in the formula, thus the formula for a set of sets has components. This is very important fact to keep in mind. Inclusion-exclusion principle has may different applications, but in some of them the complexity cost of using it is too big to be practical.
Example:
Let be a set of sets:
The goal is to compute the cardinality of sum of all the above subsets. For the purpose of the example, let denote correspondently the intersection of sets and , the intersection of sets , and the intersection of sets .
Then the formula for the above example looks like this:
Since in the example the cardinality of each above intersection can be computed just by looking at the sets in the intersection, the formula is transformed to:
This example might seem trivial, because computing the cardinality of sum of all sets is as straightforward like computing the cardinality of their intersections. However, in many cases the complexity of computing these two values differ significantly. At the end of this article, there will be an example problem demonstrating such case. But first, a very handy method of implementing the formula will be given.
Implementation:
For the purpose of the article, let
int intersectionCardinality(vector<int> indices)
be a function returning the cardinality of intersection of sets , where i is in the indices vector.
Then the value of the formula can be computed by generating all subsets of , which is the set of sets , and for each such subset, by computing the cardinality of its intersection and adding this value with an appropriate sign to the final result.
int n; // the number of sets in the set A
int result = 0; //final result, the cardinality of sum of all subsets of A
for(int b = 0; b < (1 << n); ++b)
{
vector<int> indices;
for(int k = 0; k < n; ++k)
{
if(b & (1 << k))
{
indices.push_back(k);
}
}
int cardinality = intersectionCardinality(indices);
if(indices.size() % 2 == 1) result += cardinality;
else result -= cardinality;
}
cout << result << endl; //printing the final result
This method of iterating over all subsets of some set using indicator vector is a super handy method and it is important to remember and master it. The idea is pretty simple, since there are subsets of a set of n elements, each integer in a range represents an indicator vector of one such subset when this integer is interpreted as a binary number.
Không có nhận xét nào:
Đăng nhận xét