







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








20480135385502964448038 3171004832173501394113017 5763257331083479647409398 8247331000042995311646021 489445991866915676240992 3208234421597368647019265 5800949123548989122628663 8496243997123475922766310 1082662032430379651370981 3437254656355157864869113 6042900801199280218026001 8518399140676002660747477 1178480894769706178994993 3574883393058653923711365 6116171789137737896701405 8543691283470191452333763 1253127351683239693851327 3644909946040480189969149 6144868973001582369723512 8675309258374137092461352 1301505129234077811069011 3790044132737084094417246 6247314593851169234746152 8694321112363996867296665 1311567111143866433882194 3870332127437971355322815 6814428944266874963488274 8772321203608477245851154 1470029452721203587686214 4080505804577801451363100 6870852945543886849147881 8791422161722582546341091 1578271047286257499433886 4167283461025702348124920 6914955508120950093732397 9062628024592126283973285 1638243921852176243192354 4235996831123777788211249 6949632451365987152423541 9137845566925526349897794 1763580219131985963102365 4670939445749439042111220 7128211143613619828415650 9153762966803189291934419 1826227795601842231029694 4815379351865384279613427 7173920083651862307925394 9270880194077636406984249 1843971862675102037201420 4837052948212922604442190 7215654874211755676220587 9324301480722103490379204 2396951193722134526177237 5106389423855018550671530 7256932847164391040233050 9436090832146695147140581 2781394568268599801096354 5142368192004769218069910 7332822657075235431620317 9475308159734538249013238 2796605196713610405408019 5181234096130144084041856 7426441829541573444964139 9492376623917486974923202 2931016394761975263190347 5198267398125617994391348 7632198126531809327186321 9511972558779880288252979 2933458058294405155197296 5317592940316231219758372 7712154432211912882310511 9602413424619187112552264 3075514410490975920315348 5384358126771794128356947 7858918664240262356610010 9631217114906129219461111 3111474985252793452860017 5439211712248901995423441 7898156786763212963178679 9908189853102753335981319 3145621587936120118438701 5610379826092838192760458 8147591017037573337848616 9913237476341764299813987 3148901255628881103198549 5632317555465228677676044 8149436716871371161932035 3157693105325111284321993 5692168374637019617423712 8176063831682536571306791
Two different subsets of the ninety 25digit numbers shown above have the same sum. For example, maybe the sum of the numbers in the first column is equal to the sum of the numbers in the second column. Can you find two such subsets? This is a very difficult computational problem. But we’ll prove that such subsets must exist! This is the sort of weird conclusion one can reach by tricky use of counting, the topic of this chapter.
Counting seems easy enough: 1, 2, 3, 4, etc. This explicit approach works well for counting simple things, like your toes, and for extremely complicated things for which there’s no identifiable structure. However, subtler methods can help you count many things in the vast middle ground, such as:
Counting is useful in computer science for several reasons:
We’re going to present a lot of rules for counting. These rules are actually theorems, but we’re generally not going to prove them. Our objective is to teach you counting as a practical skill, like integration. And most of the rules seem “obvious” anyway.
How do you count the number of people in a crowded room? We could count heads, since for each person there is exactly one head. Alternatively, we could count ears and divide by two. Of course, we might have to adjust the calculation if someone lost an ear in a pirate raid or someone was born with three ears. The point here is that we can often count one thing by counting another, though some fudge factors may be required. This is the central theme of counting, from the easiest problems to the hardest.
In more formal terms, every counting problem comes down to determining the size of some set. The size or cardinality of a set S is the number of elements in S and is denoted |S|. In these terms, we’re claiming that we can often find the size of one set S by finding the size of a related set T. We already have a mathematical tool for relating one set to another: relations. Not surprisingly, a particular kind of relation is at the heart of counting.
Functions like f (x) = x^2 + 1 and g(x) = tan−^1 (x) are surely quite familiar from calculus. We’re going to use functions for counting as well, but in a way that might not be familiar. Instead of using functions that map one real number to another, we’ll use functions that relate elements of finite sets.
In order to count accurately, we need to carefully define the notion of a function. For mally, a function f : X →Y is a relation between two sets, X and Y , that relates every element of X to exactly one element of Y. The set X is called the domain of the function f , and Y is called the codomain. Here is an illustration of a function:
�
�
�
X f^ Y a PP 1 PPP domain b P �Pq^2 codomain PP PPP c (^) PP�^ q^3 PPP d �^ Pq 4 e - 5
In contrast, these relations that are not bijections:
a -^1 a -^1 a -^1 b PPP PPPq c (^) PP PPP d Pq
^3
e
^3
b PPP PPPq c (^) Q Q QQ QQs
d
^3
b c
d e
PP PPP Pq
^3
^3
The first function is not bijective because 3 is related to two elements of the domain. The second function is not bijective because 4 is related to zero elements of the domain. The last relation is not even a function, because b is associated with zero elements of the domain.
Bijective functions are also found in the morefamiliar world of realvalued functions. For example, f (x) = 6x+5 is a bijective function with domain and codomain R. For every real number y in the codomain, f (x) = y for exactly one real number x in the domain. On the other hand, f (x) = x^2 is not a bijective function. The number 4 in the codomain is related to both 2 and 2 in the domain.
If we can pair up all the girls at a dance with all the boys, then there must be an equal number of each. This simple observation generalizes to a powerful counting rule:
Rule 1 (Bijection Rule). If there exists a bijection f : A → B, then |A | = |B |.
In the example, A is the set of boys, B is the set of girls, and the function f defines how they are paired.
The Bijection Rule acts as a magnifier of counting ability; if you figure out the size of one set, then you can immediately determine the sizes of many other sets via bijections.
For example, let’s return to two sets mentioned earlier:
A = all ways to select a dozen doughnuts when five varieties are available B = all 16bit sequences with exactly 4 ones
Let’s consider a particular element of set A:
chocolate lemonfilled sugar glazed plain
We’ve depicted each doughnut with a 0 and left a gap between the different varieties. Thus, the selection above contains two chocolate doughnuts, no lemonfilled, six sugar, two glazed, and two plain. Now let’s put a 1 into each of the four gaps:
chocolate lemonfilled sugar glazed plain
We’ve just formed a 16bit number with exactly 4 ones— an element of B!
This example suggests a bijection from set A to set B: map a dozen doughnuts consist ing of: c chocolate, l lemonfilled, s sugar, g glazed, and p plain
to the sequence:
c l s g p
The resulting sequence always has 16 bits and exactly 4 ones, and thus is an element of B. Moreover, the mapping is a bijection; every such bit sequence is mapped to by exactly one order of a dozen doughnuts. Therefore, |A | = |B |by the Bijection Rule!
This demonstrates the magnifying power of the bijection rule. We managed to prove that two very different sets are actually the same size— even though we don’t know exactly how big either one is. But as soon as we figure out the size of one set, we’ll immediately know the size of the other.
This particular bijection might seem frighteningly ingenious if you’ve not seen it be fore. But you’ll use essentially this same argument over and over and over, and soon you’ll consider it boringly routine.
The Bijection Rule lets us count one thing by counting another. This suggests a general strategy: get really good at counting just a few things and then use bijections to count everything else:
Rule 2 (Sum Rule). If A 1 , A 2 ,... , An are disjoint sets, then:
| A 1 ∪ A 2 ∪... ∪ An | = |A 1 | + |A 2 | +... +|An|
Thus, according to Linus’ budget, Lucy can be outofsorts for:
|C ∪ I ∪ S| = |C | ∪ | | ∪ |I S | = 20 + 40 + 60 = 120 days
Notice that the Sum Rule holds only for a union of disjoint sets. Finding the size of a union of intersecting sets is a more complicated problem that we’ll take up later.
The product rule gives the size of a product of sets. Recall that if P 1 , P 2 ,... , Pn are sets, then P 1 × P 2 ×... × Pn
is the set of all sequences whose first term is drawn from P 1 , second term is drawn from P 2 and so forth.
Rule 3 (Product Rule). If P 1 , P 2 ,... Pn are sets, then:
| P 1 × P 2 ×... × Pn | = |P 1 | · |P 2 | · · · |Pn|
Unlike the sum rule, the product rule does not require the sets P 1 ,... , Pn to be disjoint. For example, suppose a daily diet consists of a breakfast selected from set B, a lunch from set L, and a dinner from set D:
B = {pancakes, bacon and eggs, bagel, Doritos} L = {burger and fries, garden salad, Doritos} D = {macaroni, pizza, frozen burrito, pasta, Doritos}
Then B × L × D is the set of all possible daily diets. Here are some sample elements:
(pancakes, burger and fries, pizza) (bacon and eggs, garden salad, pasta) (Doritos, Doritos, frozen burrito)
The Product Rule tells us how many different daily diets are possible:
|B × L × D| = |B| · | L | · |D| = 4 · 3 · 5 = 60
Few counting problems can be solved with a single rule. More often, a solution is a flury of sums, products, bijections, and other methods. Let’s look at some examples that bring more than one rule into play.
Passwords
The sum and product rules together are useful for solving problems involving passwords, telephone numbers, and license plates. For example, on a certain computer system, a valid password is a sequence of between six and eight symbols. The first symbol must be a letter (which can be lowercase or uppercase), and the remaining symbols must be either letters or digits. How many different passwords are possible?
Let’s define two sets, corresponding to valid symbols in the first and subsequent posi tions in the password.
F = {a, b,... , z, A, B,... , Z} S = {a, b,... , z, A, B,... , Z, 0 , 1 ,... , 9 }
In these terms, the set of all possible passwords is:
(F × S^5 ) ∪ (F × S^6 ) ∪ (F × S^7 )
Thus, the lengthsix passwords are in set F ×S^5 , the lengthseven passwords are in F ×S^6 , and the lengtheight passwords are in F × S^7. Since these sets are disjoint, we can apply the Sum Rule and count the total number of possible passwords as follows:
(F × S^5 ) ∪ (F × S^6 ) ∪ (F × S^7 ) = F × S^5 + F × S^6 + F × S^7 Sum Rule = |F | · | S |^5 + |F | · |S |^6 + |F | · |S |^7 Product Rule = 52 · 625 + 52 · 626 + 52 · 627 ≈ 1. 8 · 1014 different passwords
Subsets of an n element Set
How many different subsets of an n element set X are there? For example, the set X = { x 1 , x 2 , x 3 } has eight different subsets:
{} { x 1 } {x 2 } {x 1 , x 2 } {x 3 } {x 1 , x 3 } {x 2 , x 3 } { x 1 , x 2 , x 3 }
There is a natural bijection from subsets of X to nbit sequences. Let x 1 , x 2 ,... , xn be the elements of X. Then a particular subset of X maps to the sequence (b 1 ,... , bn)
a - 1 a -^1 b PPPP 3 2 3 2 PP
b PPP PPP c (^) q^3 c (^) Q ^ q^3 PPPP (^3) Q (^) PP d ^ q^4 d ^ QQ 4 QQ e ^ s^5
The function on the left is surjective (every element on the right is mapped to at least once), but not injective (element 3 is mapped to twice). The function on the right is injec tive (every element is mapped to at most once), but not surjective (element 4 is mapped to zero times).
Earlier, we observed that two sets are the same size if there is a bijection between them. Similarly, surjections and injections imply certain size relationships between sets.
Rule 4 (Mapping Rule).
Here is an old puzzle:
A drawer in a dark room contains red socks, green socks, and blue socks. How many socks must you withdraw to be sure that you have a matching pair?
For example, picking out three socks is not enough; you might end up with one red, one green, and one blue. The solution relies on the Pigeonhole Principle, which is a friendly name for the contrapositive of part (2) of the Mapping Rule. Let’s write it down:
If |X | > Y| |, then no function f : X →Y is injective.
Now let’s rewrite this a second time to eliminate the word “injective” since, by now, there’s not a ghost of a chance that you remember what that means:
Rule 5 (Pigeonhole Principle). If |X | > |Y |, then for every function f : X Y there exist two different elements of X that are mapped to the same element of Y.
Perhaps the relevance of this abstract mathematical statement to selecting footwear under poor lighting conditions is not obvious. However, let A be the set of socks you pick out, let B be the set of colors available, and let f map each sock to its color. The Pigeonhole Principle says that if |A > B| | |= 3, then at least two elements of A (that is, at least two socks) must be mapped to the same element of B (that is, the same color). For example, one possible mapping of four socks to three colors is shown below.
A f^ B 1st sock -^ red 2nd sock -^ green ^3 3rd sock (^) ^ -^ blue 4th sock^
Therefore, four socks are enough to ensure a matched pair.
Not surprisingly, the pigeonhole principle is often described in terms of pigeons: if more than n pigeons fly into n pigeonholes, then at least two pigeons must fly into some hole. In this case, the pigeons form set A, the pigeonholes are set B, and f describes the assignment of pigeons to pigeonholes.
Mathematicians have come up with many ingenious applications for the pigeonhole principle. If there were a cookbook procedure for generating such arguments, we’d give it to you. Unfortunately, there isn’t one. One helpful tip, though: when you try to solve a problem with the pigeonhole principle, the key is to clearly identify three things:
There are a number of generalizations of the pigeonhole principle. For example:
Rule 6 (Generalized Pigeonhole Principle). If |X | > k · |Y |, then every function f : X Y maps at least k + 1 different elements of X to the same element of Y.
For example, if you pick two people at random, surely they are extremely unlikely to have exactly the same number of hairs on their heads. However, in the remarkable city of Boston, Massachusetts there are actually three people who have exactly the same number of hairs! Of course, there are many bald people in Boston, and they all have zero hairs. But I’m talking about nonbald people.
How can we construct a set of n positive integers such that all its subsets have distinct sums? One way is to use powers of two:
{ 1 , 2 , 4 , 8 , 16 }
This approach is so natural that one suspects all other such sets must involve larger numbers. (For example, we could safely replace 16 by 17, but not by 15.) Remarkably, there are examples involving smaller numbers. Here is one:
{ 6 , 9 , 11 , 12 , 13 }
One of the top mathematicans of the century, Paul Erd os,˝ conjectured in 1931 that there are no such sets involving significantly smaller numbers. More precisely, he conjectured that the largest number must be Ω(2n). He offered $500 to anyone who could prove or disprove his conjecture, but the problem remains unsolved.