Data Structure Lect14
Data Structure Lect14
Lecture 14
Topics
Recursive Algorithm for Computing gcd
Counting
Product Rule
Sum Rule
Find the gcd (a,b) = gcd (b mod a, a) when input a=5, b=8.
With this input,
gcd(5, 8) = gcd(8 mod 5, 5) = gcd(3, 5).
gcd(3, 5) = gcd(5 mod 3, 3) = gcd(2, 3),
gcd(2, 3) = gcd(3 mod 2, 2) = gcd(1, 2),
gcd(1, 2) = gcd(2 mod 1, 1) = gcd(0, 1).
Finally, to find gcd(0, 1) it uses the first step with a = 0 to find that gcd(0, 1) = 1.
Consequently, the algorithm finds that gcd(5, 8) = 1.
Recursive Algorithm for Computing gcd(a, b)
if a = 0 then return b
Solution:
The procedure of assigning offices to these two employees consists of assigning an
office to Nasir, which can be done in 12 ways, then assigning an office to Saleem
different from the office assigned to Nasir, which can be done in 11 ways.
By the product rule, there are 12 · 11 = 132 ways to assign offices to these two
employees.
Example 2:
Solution:
The procedure of labeling a chair consists of two tasks, namely, assigning to the
seat one of the 26 uppercase English letters, and then assigning to it one of the
100 possible integers. The product rule shows that there are 26 · 100 = 2600
different ways that a chair can be labeled. Therefore, the largest number of
chairs that can be labeled differently is 2600.
Example 3:
Solution:
The procedure of choosing a port consists of two tasks, first picking a
microcomputer and then picking a port on this microcomputer. Because there are
32 ways to choose the microcomputer and 24 ways to choose the port no matter
which microcomputer has been selected, the product rule shows that there are
32 · 24 = 768 ports.
Example 4:
Solution:
Each of the seven bits can be chosen in two ways, because each bit is either 0 or 1.
Therefore, the product rule shows there are a total of 27 = 128 different bit strings
of length seven.
Example 5
How many different license plates can be made if each plate contains a
sequence of three uppercase English letters followed by three digits (and no
sequences of letters are prohibited, even if they are obscene)?
Solution: There are 26 choices for each of the three uppercase English letters
and ten choices for each of the three digits. Hence, by the product rule there are
a total of
26 · 26 · 26 · 10 · 10 · 10 = 17,576,000
possible license plates.
ABB962
The Sum Rule
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?
Solution: The student can choose a project by selecting a project from the first
list, the second list, or the third list.
Because no project is on more than one list, by the sum rule there are
23 + 15 + 19 = 57 ways to choose a project.
Example 2
Suppose that either a member of the mathematics faculty or a student who is
a mathematics major is chosen as a representative to a university committee.
How many different choices are there for this representative if there are 37
members of the mathematics faculty and 83 mathematics majors and no one
is both a faculty member and a student?
Solution:
There are 37 ways to choose a member of the mathematics faculty and there are
83 ways to choose a student who is a mathematics major. Choosing a member of
the mathematics faculty is never the same as choosing a student who is a
mathematics major because no one is both a faculty member and a student. By
the sum rule it follows that there are
37 + 83 = 120 possible ways to pick this representative.
The Subtraction Rule
If a task can be done in either n1 ways or n2 ways, then the number of ways to
do the task is n1 + n2 minus the number of ways to do the task that are common
to the two different ways.
There are n/d ways to do a task if it can be done using a procedure that can be
carried out in n ways, and for every way w, exactly d of the n ways correspond to
way w.
Example of Division Rule
How many bit strings of length four do not have two consecutive 1s?
Solution:
The binary combination, displays all bit strings of length four without two
consecutive 1s. We see that there are eight bit strings of length four without two
consecutive 1s.
0001, 0010, 0100, 1000,1001,1010, 0101, 0000