CSC 201 Unit 4 Counting
CSC 201 Unit 4 Counting
1
Subunit 4(a): The basics of counting
2
Product rule
3
4
Generalized product rule
5
Example 1
6
Example 2
7
Example 3
8
Example 4
How many different bit strings of length seven
are there?
9
10
Cardinality of Cartesian products
The product rule is often phrased in terms of sets in
this way:
If A1, A2, ... , Am are finite sets, then the number of
elements in the Cartesian product of these sets is the
product of the number of elements in each set.
To relate this to the product rule, note that the task of
choosing an element in the Cartesian product,
AI 𝗑 A2 𝗑 … 𝗑 Am is done by choosing an element in
AI, an element in A2, … , and an element in Am.
11
Cardinality of Cartesian products
12
The number of subsets of a finite set
● Let S be a finite set. List the elements of S in
arbitrary order.
● There is a one-to-one correspondence between
subsets of S and bit strings of length |S|. Namely, a
subset of S is associated with the bit string with a 1
in the i-th position if the i-th element in the list is in
the subset, and a 0 in this position otherwise.
● By the product rule, there are 2|S| bit strings of
length |S|.
● Hence, |P(S)| = 2|S|
13
The number of subsets of a finite set
14
The sum rule
If a task can be done either in one of n1
ways or in one of n2 ways, where none of
the set of n1 ways is the same as any of the
set of n2 ways, then there are n1 + n2 ways
to do the task.
15
Generalized sum rule
16
Example 5
A student can choose a computer project from one of three
lists. The three lists contain 23, 15, and 19 possible
projects, respectively. No project is on more than one list.
How many possible projects are there to choose from?
17
The sum rule expressed in terms of sets
18
Example 6
Each user on a system has a password, which is 6-8 characters long,
where each character is an uppercase letter or a digit. Each password
must contain at least 1 digit. How many possible passwords are
there?
Solution: Let P be the total number of possible passwords, and let
P6, P7, and P8 denote the number of possible passwords of length 6,
7, and 8, respectively.
By the sum rule, P = P6 + P7 + P8.
We will now find P6, P7, and P8.
Finding P6 directly is difficult. To find P6 it is easier to find the
number of strings of uppercase letters and digits that are 6 characters
long, including those with no digits, and subtract from this the
number of strings with no digits.
19
Example 6 Continued...
By the product rule, the number of strings of 6 characters is
366, and the number of strings with no digits is 266.
Hence,
P6 = 366 - 266 = 2,176,782,336 - 308,915,776 = 1,867,866,560.
Similarly, it can be shown that,
P7 = 367 - 267 = 78,364,164,096 - 8,031,810,176 =
70,332,353,920
And,
P8 = 368 - 268 = 2,821,109,907,456 - 208,827,064,576 =
2,612,282,842,880.
Consequently,
P = P6 + P7 + P8 = 2,684,483,063,360.
20
Example 7
In a version of the computer language BASIC, the
name of a variable is a string of one or two
alphanumeric characters, where uppercase and
lowercase letters are not distinguished. (An
alphanumeric character is either one of the 26 English
letters or one of the 10 digits.) Moreover, a variable
name must begin with a letter and must be different
from the five strings of two characters that are
reserved for programming use. How many different
variable names are there in this version of BASIC?
21
Example 7
Solution: Let V equal the number of different variable names in this
version of BASIC. Let V1 be the number of these that are one
character long and V2 be the number of these that are two characters
long.
Then by the sum rule, V = V1 + V2.
Note that V1 = 26, because a one-character variable name must be a
letter.
And by the product rule there are 26*36 strings of length two that
begin with a letter and end with an alphanumeric character.
However, five of these are excluded, so,
V2= 26*36 - 5 = 931.
Hence, there are V = V1 + V2 = 26 + 931 = 957 different names for
variables in this version o f BASIC.
22
Subtraction Rule
• Suppose that a task can be done in n1 or in n2 ways, but that
some of the set of n1 ways to do the task are the same as
some of the n2 other ways to do the task.
• We cannot use sum rule because it will lead to an
overcount, as the ways to do the task in the ways that are
common are counted twice.
• To correctly count the number of ways to do the two tasks,
we add the n1 and n2 ways of doing the task, and then
subtract the number of ways to do the task that are both
among the set of n1 ways and the set of n2 ways.
• This technique is called the principle of
inclusion-exclusion. Sometimes, it is also called the
subtraction principle for counting.
23
Subtraction Rule: Inclusion-Exclusion
Principle for two sets
24
Example 8
25
Example 8 explained…
• We basically need to construct a bit string of length eight
beginning with a 1 or a bit string of length eight that ends
with 00.
• To construct a bit string of length eight that begins with a 1,
the first bit can be chosen in only one way and each of the
other seven bits can be chosen in two ways.
By the product rule, this can be done in 27 = 128 ways.
• Similarly, for a bit string of length eight ending with the 00,
each of the first six bits can be chosen in two ways and the
last two bits can be chosen in only one way.
Following the product rule, there 26 = 64 ways to do this.
26
Example 8 continued…
• Some of the ways to construct a bit string of length
eight starting with a 1 are the same as the ways to
construct a bit string of length eight that ends with
the two bits 00.
• There are 25 = 32 ways to construct such a string.
This follows by the product rule, because the first
bit can be chosen in only one way, each of the
second through the sixth bits can be chosen in two
ways, and the last two bits can be chosen in one
way.
27
Example 8 continued…
Hence, the number of bit
strings of length eight that
begin with a 1 or end with a
00, which equals the number
of ways to construct a bit
string of length eight that
begin with a 1 or that ends
with 00, equals,
128 + 64 - 32 = 160.
28
Subunit 4(b): Permutations and
combinations
29
Permutations
30
31
Example 6
• How many ways are there to select a first-prize
winner, a second-prize winner and a third-prize
winner from 100 different people?
Solution: The required number is
32
Example 7
•
33
Combinations
•
34
35
Some remarks
Let r and n be integers with
• We have
• Given a set S of cardinality n, the number of
subsets of S of cardinality r is
• The number of bit strings of length n
containing exactly r ones is
36
Example 8
• Suppose there are 9 faculty members in the
mathematics department and 11 in the
computer science department. How many ways
are there to select a committee if the
committee is to consist of 3 faculty members
from the mathematics department and 4 from
the computer science department.
Answer:
37