Chapter 6
Chapter 6
disjoint sets.
Solution:
By the sum rule it follows that there are 7 +
3 = 10 choices for a student who wants to take
one optional course.
representative.
Basic Counting Principles: The Sum
Rule
Example: 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:
Each bit (binary digit) is either 0 or 1.
Hence, there are 2 ways to choose each bit. Since
we have to choose four bits therefore,
FREE:different bit
For Complete Playlist: strings of length 8 that begin with
https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
The Product Rule
(ii) begin and end with a 1?
Solution:
If the first and last bit in an 8 bit string is a 1,
then only the intermediate six bits can be filled
in 2 ways, i.e. by a 0 or 1.
Hence there are
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 For
FREE: license plates are
Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books:
1https://fahadhussaincs.blogspot.com/
× 1 × 24 × 10 × 9 × 8 =
Telephone Numbering Plan
telephone number consists of 10 digits,
Example: The North American numbering plan (NANP)
specifies that a
consisting of a three-digit area code, a three-digit office code,
and a four-digit station code. There are some restrictions on the
In the old plan (in use in the 1960s) the format was NYX-NNX-
⚫
⚫
XXXX.
⚫ In the new plan, the format is NXX-NXX-XXXX.
How many different telephone numbers are possible under the
old plan and the new plan?
⚫ There are 8 ∙2 ∙10 = 160 area codes with the format NYX.
Solution: Use the Product Rule.
⚫ There are 8 ∙10 ∙10 = 800 area codes with the format NXX.
⚫ There are 8 ∙8 ∙10 = 640 office codes with the format NNX.
⚫There are 10 ∙10 ∙10 ∙10 = 10,000 station codes with the format
XXXX. Number of old plan telephone numbers: 160 ∙640
∙10,000 = 1,024,000,000. Number of new plan telephone
NUMBER OF ITERATIONS OF A NESTED LOOP
Example: Determine how many
times when
iterate the the following
inner algorithm
loop willbe
is implemented
d and run For i: = 1 to 4
For j : = 1 to 3
[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 four times, and during
each iteration of
the outer loop, there are three iterations of the
inner loop.
Example: 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:
Example: 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.]
n
ext j
next
i
Solu
tion:
The outer loop is iterated 4 times, but during each
iteration of the outer loop, the inner loop iterates
Combining the Sum and Product Rule
Example: Suppose statement labels in a
programming language can be either a single
letter or a letter followed by a digit. Find the
number of possible labels.
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 26×10 = 260
ways.
Combining the Sum and Product Rule
Example: A computer access code word consists
of from one to three letters of English alphabets
with repetitions allowed. How many different
code words are possible.
Solution:
Number of code words of length 1
= 261 Number of code words of
length 2 = 262 Number of code
words of length 3 = 263 Hence,
the total number of code words =
26 1+ 262 +
263= 18,278
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Counting Passwords
⚫ Combining the sum and product rule allows us to solve more complex
problems.
Example: Each user on a computer system has a password,
which is six to eight characters long, where each character is
an uppercase letter or a digit. Each password must contain at
least one digit. How many possible passwords are there?
Solution: Let P be the total number of passwords, and let
P6, P7, and P8
be the passwords of length 6, 7, and 8.
⚫ By the sum rule P = P6 + P7 +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 six
characters long, including those with no digits, and subtract
digits is 266
rule,
FREE: the number
For Complete Playlist: of strings
https://www.youtube.com/c/FahadHussaintutorial/playlists
number For of strings
Code, Slide andwith
Books: no https://fahadhussaincs.blogspot.com/
Counting Passwords(Continued)
⚫ To find each of P6, P7, and P8 , we find the
number of passwords of the specified length
composed of letters and digits and subtract the
number composed only of letters. We find that:
⚫ P6 = 366 − 266 = 2,176,782,336 − 308,915,776
=1,867,866,560.
⚫ P7 = 367 − 267 = 78,364,164,096 − 8,031,810,176 =
70,332,353,920.
⚫ 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.
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Basic Counting Principles: Subtraction Rule
Subtraction Rule: If a task can be done
either in one of n1 ways or in one of n2
1 bit: 27 = 128
length eight that start with a
00: 26 = 64
length eight that end with bits
with bits 00 :
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Counting Functions
Counting Functions: How many functions are
there from a
set with m elements to a set with n elements?
Solution: Since a function represents a choice of
one of the n elements of the codomain for each of
1 objects.
contradicts the statement that we have k +
birthdays. ⌈367/366⌉ = 2
month.
Solution:
The minimum number of students needed to
≤For
FREE: n,Complete
thenPlaylist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Solving Counting Problems by
Counting Permutations
P(7,7)= 7! = 7 ∙ 6 ∙ 5 ∙ 4 ∙ 3 ∙ 2 ∙ 1 = 5040
are ordered arbitrarily. Hence the orders are:
P(6,6)= 6! = 6 ∙ 5 ∙ 4 ∙ 3 ∙ 2 ∙ 1 = 720
Section. 6.4)
notation again in the binomial theorem in
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Combinations
Example:
⚫ Let S be the set {a, b, c, d}. Then {a, c, d} is a 3- combination
from S. It is the same as {d, c, a} since the order listed does not
matter.
⚫ C(4,2) = 6 because the 2-combinations of {a, b, c, d} are the
six subsets {a, b}, {a, c}, {a, d}, {b, c}, {b, d}, and {c, d}.
result. →
This is a special case of a general
and
manipulation. →
This result can be proved without using algebraic
By definition, the number of subsets of S with r elements equals C(n, r). But
each subset A of S is also determined by specifying which elements are not in
A, and so are in A`. Because the complement of a subset of S with r elements
has n − r elements, there are also C(n, n − r) subsets of S with r elements. It
follows that C(n, r) = C(n, n − r).
combinations is
expanding (x + y)3.
⚫ To illustrate this idea, we first look at the process of
⚫ Terms of the form x3, x2y, x y2, y3 arise. The question is what
from each of the three sums.
1662)
(1623-
Pascal’s Identity
≥ k ≥ 0, then
Pascal’s Identity: If n and k are integers with n
1, a ∊T, and
Proof (combinatorial): Let T be a set where |T| = n +
of arek − 1
there
There
⚫ are
subsetsofofS,k elements of T that do not contain a,
⚫ elements
subsets
because there
are
Henc subsets of k elements of S.
e, 19
See Exercise
for an
Pascal’s Triangle
The nth row
in the
triangle
consists of
the binomial
coefficients
, k = 0,1,
….,n.