Counting
Counting
COMBINATORICS
If one event can occur in n1 ways, a second event can occur in n2 (different)
ways, then the total number of ways in which exactly one of the events (i.e.,
first or second) can occur is n1 + n2.
EXAMPLE
Suppose there are 7 different optional courses in Computer Science and 3
different optional courses in Mathematics. Then there are
7 + 3 = 10
choices for a student who wants to take one optional course.
1
EXERCISE
A student can choose a computer project from one of the three lists. The
three lists contain 23, 15 and 19 possible projects, respectively. How many
possible projects are there to choose from?
SOLUTION
The student can choose a project from the first list in 23 ways, from the
second list in 15 ways, and from the third list in 19 ways. Hence, there are
23 + 15 + 19 = 57
projects to choose from.
2
THE PRODUCT RULE
If one event can occur in n1 ways and if for each of these n1 ways, a second
event can occur in n2 ways, then the total number of ways in which both
events occur is n1 · n2.
EXAMPLE
EXAMPLE
SOLUTION
The procedure of labeling a chair consists of two
events, namely,
(i) Assigning one of the 26 letters: A, B, C, …, Z and
(ii) Assigning one of the 10 digits: 0, 1, 2, …, 9
By product rule, there are
26 10 = 260
different ways that a chair can be labeled by both a letter and a digit.
3
GENERALIZED PRODUCT RULE
If some event can occur in n1 different ways, and if, following this event, a
second event can occur in n2 different ways, and following this second
event, a third event can occur in n3 different ways, …, then the number of
ways all the events can occur in the order indicated is
n 1 · n2 · n3 · …
PRODUCT RULE IN TERMS OF SETS
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.
If n(Ai) denotes the number of elements in set A i, then
n(A1 A2 … Am) = n (A1) · n(A2) ·… ·n (Am)
EXERCISE
4
EXERCISE
There are four bus lines between A and B; and three bus lines between B
and C.
Find the number of ways a person can travel:
(a) By bus from A to C by way of B;
(b) Round trip by bus from A to C by way of B;
(c) Round trip by bus from A to C by way of B, if the person does not
want to use a bus line more than once.
SOLUTION
(a) There are 4 ways to go from A to B and 3 ways to go from B to C;
hence there are
4 3 = 12
ways to go from A to C by way of B.
(c) The person can travel 4 ways from A to B and 3 ways from
B to C, but only 2 ways from C to B and 3 ways from B to A,
since bus line cannot be used more than once. Thus
i.e., 4 A3B 2C B 3 A
6
EXERCISE
Suppose that an automobile license plate has three letters followed by three
digits.
(a) How many different license plates are possible?
SOLUTION
Each of the three letters can be written in 26 different ways, and each of the
three digits can be written in 10 different ways.
letters digits
(b) How many license plates could begin with A and end on 0?
SOLUTION
The first and last place can be filled in one way only,
while each of second and third place can be filled in 26 ways and each of
fourth and fifth place can be filled in 10 ways.
7
digits
letters
A 0
P Q R
8
(d) How many license plates are possible in which all the letters
and digits are distinct?
SOLUTION
The first letter place can be filled in 26 ways. Since, the second letter place
should contain a different letter than the first, so it can be filled in 25 ways.
Similarly, the third letter place can be filled in 10, 9, and 8 ways.
Hence; number of license plates in which all the letters and digits are
distinct are
26 25 24 10 9 8 = 11, 232, 000
(e) How many license plates could begin with AB and have all
three letters and digits distinct.
SOLUTION
digits
letters
A B
The first two letters places are fixed (to be filled with A and B), so there
is only one way to fill them. The third letter place should contain a letter
different from A & B, so there are 24 ways to fill it.
The three digit positions can be filled in 10 and 8 ways to have distinct
digits.
Hence, desired number of license plates are
1 1 24 10 9 8 = 17280 9
EXERCISE
A variable name in a programming language must be either a letter or a
letter followed by a digit.
How many different variable names are possible?
SOLUTION
First consider variable names one character in length. Since such names
consist of a single letter, there are 26 variable names of length 1.
Next, consider variable names two characters in length. Since the first
character is a letter, there are 26 ways to choose it. The second character is
a digit, there are 10 ways to choose it. Hence, to construct variable name of
two characters in length, there are
2610 = 260 ways.
Finally, by sum rule, there are 26 + 260 = 286 possible variable names in
the programming language.
EXERCISE
(a) How many bit strings consist of from one through four digits?
(b) How many bit strings consist of from five through eight
digits?
SOLUTION
(a) Number of bit strings consisting of 1 digit = 2
Number of bit strings consisting of 2 digits = 2·2 = 22
Number of bit strings consisting of 3 digits = 2·2·2 = 23
Number of bit strings consisting of 4 digits = 2·2·2·2 = 24
Hence by sum rule, the total number of bit strings consisting of one through
four digit is
2+22+23+24 = 2 + 4 + 8 + 16 = 30 10
(b) Number of bit strings of 5 digits = 2 5
Number of bit strings of 6 digits = 2 6
Number of bit strings of 7 digits = 2 7
Number of bit strings of 8 digits = 2 8
Hence, by sum rule, the total number of bit strings consisting of five
through eight digit is
25 + 26 + 27 + 28 = 480
12
Determine how many times the inner loop will be iterated when the
following algorithm is implemented and run.
for i = 5 to 50
for j: = 10 to 20
[Statement in body of inner
loop. None contain branching statements that lead
out of the inner loop.] next j
next i
SOLUTION
The outer loop is iterated 50 - 5 + 1 = 46 times and during each iteration of
the outer loop there are 20 - 10 + 1 = 11 iterations of the inner loop. Hence
by product rule, the total number of iterations of the inner loop is 46.11 =
506
EXERCISE
Determine how many times the inner loop will be iterated when the
following algorithm is implemented and run.
for i: = 1 to 4
for j: = 1 to i
[Statements in body of inner loop.
None contain branching statements
that lead outside the loop.]
next j
next i
13
SOLUTION
The outer loop is iterated 4 times, but during each
iteration of the outer loop, the inner loop iterates different number of times.
For first iteration of outer loop, inner loop iterates 1 times.
For second iteration of outer loop, inner loop iterates 2 times.
For third iteration of outer loop, inner loop iterates 3 times.
For fourth iteration of outer loop, inner loop iterates 4 times.
Hence, total number of iterations of inner loop = 1 + 2 + 3 + 4 = 10
14